From af02d2c6baeb7817e249a048884bad0652b104b4 Mon Sep 17 00:00:00 2001 From: vvijayaraman Date: Tue, 17 Oct 2023 19:48:30 -0400 Subject: [PATCH 1/4] Print stack trace when exception occurs --- README.md | 4 ++-- .../java/com/ebay/ejmask/core/EJMask.java | 2 +- .../ebay/ejmask/core/util/CommonUtils.java | 22 +++++++++++++++++++ .../ebay/ejmask/core/util/ExecutorUtil.java | 6 ++--- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0ebf97c..5e7d6f5 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ public class JsonPatternBuilder implements IPatternBuilder { } ``` #### Extensions -For many standard use cases you can make use of patten builders defined in `ejmask-extensions` module. +For many standard use cases you can make use of pattern builders defined in `ejmask-extensions` module. - HeaderFieldPatternBuilder - XmlFieldPattenBuilder @@ -91,7 +91,7 @@ public class Sample implements IFilter { ### ContentProcessor `ContentProcessor`(s) configured with the data masker will be invoked to process the data before and after actual masking operations getting invoked. -A few usecase we can use is to decode and encode the sting before masking or to reduce the size of a large string before performing the masking operation to improve performance. +A few use case we can use is to decode and encode the string before masking and/or to reduce the size of a large string before performing the masking operation to improve performance. #### Extensions - ContentSlicerPreProcessor diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java index e0f17b3..2cd0100 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java @@ -158,7 +158,7 @@ public static String mask(final String content, boolean preProcessingRequired, b } return contentInProgress; } catch (Exception ex) { - return "masking sensitive content failed due to " + ex.getMessage(); + return "masking sensitive content failed due to " + CommonUtils.getStackTrace(ex); } } diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java index 836b5a2..022b8ef 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/CommonUtils.java @@ -17,6 +17,8 @@ */ +import java.io.PrintWriter; +import java.io.StringWriter; import java.lang.reflect.Array; import java.util.Collection; import java.util.Collections; @@ -123,4 +125,24 @@ public static boolean isBlank(final CharSequence cs) { } return true; } + + + /** + *

Gets the stack trace from a Throwable as a String.

+ * + *

The result of this method vary by JDK version as this method + * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}. + * On JDK1.3 and earlier, the cause exception will not be shown + * unless the specified throwable alters printStackTrace.

+ * + * @param throwable the Throwable to be examined + * @return the stack trace as generated by the exception's + * printStackTrace(PrintWriter) method + */ + public static String getStackTrace(Throwable throwable) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw, true); + throwable.printStackTrace(pw); + return sw.getBuffer().toString(); + } } diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java index 7c4a27a..4684cc9 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/util/ExecutorUtil.java @@ -56,11 +56,11 @@ public static R execute(Callable task, long timeout, TimeUnit unit) { try { return future.get(timeout, unit); } catch (TimeoutException ex) { - LoggerUtil.error("executor-util", "TimeoutException", ex.getMessage()); + LoggerUtil.error("executor-util", "TimeoutException", CommonUtils.getStackTrace(ex)); } catch (InterruptedException ex) { - LoggerUtil.error("executor-util", "InterruptedException", ex.getMessage()); + LoggerUtil.error("executor-util", "InterruptedException", CommonUtils.getStackTrace(ex)); } catch (ExecutionException ex) { - LoggerUtil.error("executor-util", "ExecutionException", ex.getMessage()); + LoggerUtil.error("executor-util", "ExecutionException", CommonUtils.getStackTrace(ex)); } finally { if (!(future.isDone() || future.isCancelled())) { future.cancel(true); From 9fc9a769588e1f4a7752b3d1d5d01eb32792cdc1 Mon Sep 17 00:00:00 2001 From: vvijayaraman Date: Fri, 27 Oct 2023 12:07:52 -0400 Subject: [PATCH 2/4] Unit tests --- .../ejmask/core/util/CommonUtilsTest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java diff --git a/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java b/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java new file mode 100644 index 0000000..f1a2f17 --- /dev/null +++ b/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java @@ -0,0 +1,84 @@ +package com.ebay.ejmask.core.util; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +class CommonUtilsTest { + + @Test + void isEmpty_whenCollectionNullOrEmpty_returnTrue() { + boolean result = CommonUtils.isEmpty(null); + Assertions.assertTrue(result); + + boolean result2 = CommonUtils.isEmpty(new ArrayList<>()); + Assertions.assertTrue(result2); + + } + + @Test + void isNotEmpty_whenCollectionIsNotEmpty_returnTrue() { + List list = new ArrayList<>(); + list.add("content"); + + boolean result = CommonUtils.isNotEmpty(list); + Assertions.assertTrue(result); + + } + + @Test + void emptyIfNull_ifNullCollection_returnEmptyCollection() { + Collection collection = CommonUtils.emptyIfNull(null); + Assertions.assertNotNull(collection); + Assertions.assertEquals(0, collection.size()); + + } + + @Test + void isNotAnEmptyArray_ifEmptyArray_returnsFalse() { + boolean result = CommonUtils.isNotAnEmptyArray(new String[0]); + Assertions.assertFalse(result); + + } + + @Test + void isAnEmptyArray_ifEmptyArray_returnsFalse() { + boolean result = CommonUtils.isAnEmptyArray(new String[0]); + Assertions.assertTrue(result); + + } + + @Test + void isNotBlank_ifBlankString_returnsFalse() { + boolean result = CommonUtils.isNotBlank(" "); + Assertions.assertFalse(result); + + } + + @Test + void isBlank_ifBlankString_returnsTrue() { + boolean result = CommonUtils.isBlank(" "); + Assertions.assertTrue(result); + } + + @Test + void getStackTrace_whenExceptionThrown_shouldPrintStackTrace() { + Exception e = new Exception("some exception"); + String result = CommonUtils.getStackTrace(e); + Assertions.assertNotNull(result); + Assertions.assertEquals("java.lang.Exception: some exception", result.split("\n")[0]); + } + + + + + + + + + + +} \ No newline at end of file From fc1b13024e69e5a99e3f34ffd42fbabd620b5502 Mon Sep 17 00:00:00 2001 From: vvijayaraman Date: Fri, 27 Oct 2023 12:25:41 -0400 Subject: [PATCH 3/4] Unit tests --- .../src/test/java/com/ebay/ejmask/core/EJMaskTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ejmask-core/src/test/java/com/ebay/ejmask/core/EJMaskTest.java b/ejmask-core/src/test/java/com/ebay/ejmask/core/EJMaskTest.java index c48d500..ac75798 100644 --- a/ejmask-core/src/test/java/com/ebay/ejmask/core/EJMaskTest.java +++ b/ejmask-core/src/test/java/com/ebay/ejmask/core/EJMaskTest.java @@ -92,7 +92,11 @@ void testMask_fail_safe() { EJMask.addFilter(50, "(auth)=([^\\\"]{1,10})[^&]*", "$1xxxx"); String content = "{\"firstName\":\"sensitive data\",\"lastName\":\"sensitive data\",\"nonSensitiveData\":\"firstName\"}"; String result = EJMask.mask(content); - Assertions.assertEquals("masking sensitive content failed due to mock exception please ignore", result); + Assertions.assertTrue(result.contains("masking sensitive content failed due to java.lang.RuntimeException: mock exception please ignore\n" + + "\tat com.ebay.ejmask.api.IContentProcessor.preProcess(IContentProcessor.java:44)\n" + + "\tat com.ebay.ejmask.core.EJMask.process(EJMask.java:174)\n" + + "\tat com.ebay.ejmask.core.EJMask.mask(EJMask.java:153)\n" + + "\tat com.ebay.ejmask.core.EJMask.mask(EJMask.java:100)")); } /** From e78be95f488718bf27ae9e59032f95704b1ab892 Mon Sep 17 00:00:00 2001 From: vvijayaraman Date: Fri, 27 Oct 2023 12:40:47 -0400 Subject: [PATCH 4/4] Unit tests --- .../com/ebay/ejmask/core/util/CommonUtilsTest.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java b/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java index f1a2f17..cec8121 100644 --- a/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java +++ b/ejmask-core/src/test/java/com/ebay/ejmask/core/util/CommonUtilsTest.java @@ -71,14 +71,4 @@ void getStackTrace_whenExceptionThrown_shouldPrintStackTrace() { Assertions.assertNotNull(result); Assertions.assertEquals("java.lang.Exception: some exception", result.split("\n")[0]); } - - - - - - - - - - } \ No newline at end of file