From ae1759868ac4b588a55697d2d17246b912c5b521 Mon Sep 17 00:00:00 2001 From: jpdahlke Date: Thu, 29 Aug 2024 12:58:05 -0400 Subject: [PATCH] errorprone :: update UnusedException messages (#895) --- .../sentinel/protocols/actions/Recover.java | 2 +- .../util/magic/MagicNumberFactory.java | 2 +- .../emissary/util/search/ByteTokenizer.java | 4 +-- .../emissary/util/magic/MagicMathTest.java | 22 ++++++++++++++++ .../util/search/ByteTokenizerTest.java | 25 +++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/test/java/emissary/util/magic/MagicMathTest.java create mode 100644 src/test/java/emissary/util/search/ByteTokenizerTest.java diff --git a/src/main/java/emissary/core/sentinel/protocols/actions/Recover.java b/src/main/java/emissary/core/sentinel/protocols/actions/Recover.java index ac58e07728..27dddd074e 100644 --- a/src/main/java/emissary/core/sentinel/protocols/actions/Recover.java +++ b/src/main/java/emissary/core/sentinel/protocols/actions/Recover.java @@ -27,7 +27,7 @@ public void trigger(Map tracker) { logger.warn("Sentinel attempting recovery for {}", agentName); mobileAgent.interrupt(); } catch (Exception e) { - throw new IllegalStateException("Recovery unavailable ", e); + throw new IllegalStateException("Recovery unavailable", e); } } } diff --git a/src/main/java/emissary/util/magic/MagicNumberFactory.java b/src/main/java/emissary/util/magic/MagicNumberFactory.java index 334d83938e..843b0fb834 100644 --- a/src/main/java/emissary/util/magic/MagicNumberFactory.java +++ b/src/main/java/emissary/util/magic/MagicNumberFactory.java @@ -331,7 +331,7 @@ private static int resolveOffset(String[] columns, MagicNumber item) throws Pars try { return MagicMath.stringToInt(entry); } catch (NumberFormatException e) { - throw new ParseException(e + ": Malformatted offset value: " + entry); + throw new ParseException(e + ": Malformed offset value"); } } diff --git a/src/main/java/emissary/util/search/ByteTokenizer.java b/src/main/java/emissary/util/search/ByteTokenizer.java index 2ca1e082a1..dd69740543 100755 --- a/src/main/java/emissary/util/search/ByteTokenizer.java +++ b/src/main/java/emissary/util/search/ByteTokenizer.java @@ -97,8 +97,8 @@ public ByteTokenizer(byte[] bytes, int start, int len, String delim, boolean ret try { Charset c = Charset.forName(encoding); logger.debug("Loaded charset {}", c); - } catch (Exception ex) { - throw new UnsupportedEncodingException(ex + ": No support for " + encoding); + } catch (IllegalArgumentException ex) { + throw new UnsupportedEncodingException(ex.toString()); } this.encoding = encoding; } diff --git a/src/test/java/emissary/util/magic/MagicMathTest.java b/src/test/java/emissary/util/magic/MagicMathTest.java new file mode 100644 index 0000000000..b1e4a96cb7 --- /dev/null +++ b/src/test/java/emissary/util/magic/MagicMathTest.java @@ -0,0 +1,22 @@ +package emissary.util.magic; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class MagicMathTest { + + @Test + void testStringToInt() { + int x = MagicMath.stringToInt("13"); + assertEquals(13, x); + + // existing behavior + assertThrows(NullPointerException.class, () -> MagicMath.stringToInt(null)); + + String entry = "blah"; + Exception exception = assertThrows(NumberFormatException.class, () -> MagicMath.stringToInt(entry)); + assertEquals("java.lang.NumberFormatException: For input string: \"blah\"", exception.toString()); + } +} diff --git a/src/test/java/emissary/util/search/ByteTokenizerTest.java b/src/test/java/emissary/util/search/ByteTokenizerTest.java new file mode 100644 index 0000000000..d1899a660a --- /dev/null +++ b/src/test/java/emissary/util/search/ByteTokenizerTest.java @@ -0,0 +1,25 @@ +package emissary.util.search; + +import org.junit.jupiter.api.Test; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class ByteTokenizerTest { + + @Test + void testEncodingConstructor() throws Exception { + + byte[] testBytes = "these are test bytes".getBytes(StandardCharsets.UTF_8); + ByteTokenizer byteTokenizer = new ByteTokenizer(testBytes, 0, 12, " ", StandardCharsets.UTF_8.name()); + assertEquals(3, byteTokenizer.countTokens()); + + assertThrows(UnsupportedEncodingException.class, () -> new ByteTokenizer(testBytes, 0, 12, " ", null)); + + Exception exception = assertThrows(UnsupportedEncodingException.class, () -> new ByteTokenizer(testBytes, 0, 12, " ", "fake-encoding")); + assertEquals("java.io.UnsupportedEncodingException: java.nio.charset.UnsupportedCharsetException: fake-encoding", exception.toString()); + } +}