This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push: new 4b4d6fa 100% code coverage for org.apache.commons.text.lookup. 4b4d6fa is described below commit 4b4d6faf80aa51814673de0f1b8b5b5d6472d4e3 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jul 17 17:33:09 2020 -0400 100% code coverage for org.apache.commons.text.lookup. --- .../text/lookup/UrlDecoderStringLookup.java | 10 ++++++--- .../text/lookup/UrlEncoderStringLookup.java | 10 ++++++--- .../text/lookup/UrlDecoderStringLookupTest.java | 25 +++++++++++++++++++--- .../text/lookup/UrlEncoderStringLookupTest.java | 21 +++++++++++++++++- 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java index 78ffa8f..d2b99d4 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlDecoderStringLookup.java @@ -36,12 +36,16 @@ final class UrlDecoderStringLookup extends AbstractStringLookup { static final UrlDecoderStringLookup INSTANCE = new UrlDecoderStringLookup(); /** - * No need to build instances for now. + * This ctor is not private to allow Mockito spying. */ - private UrlDecoderStringLookup() { + UrlDecoderStringLookup() { // empty } + String decode(final String key, final String enc) throws UnsupportedEncodingException { + return URLDecoder.decode(key, enc); + } + @Override public String lookup(final String key) { if (key == null) { @@ -49,7 +53,7 @@ final class UrlDecoderStringLookup extends AbstractStringLookup { } final String enc = StandardCharsets.UTF_8.name(); try { - return URLDecoder.decode(key, enc); + return decode(key, enc); } catch (final UnsupportedEncodingException e) { // Can't happen since UTF-8 is required by the Java specification. throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc); diff --git a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java index dd6d5ab..62513aa 100644 --- a/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java +++ b/src/main/java/org/apache/commons/text/lookup/UrlEncoderStringLookup.java @@ -35,12 +35,16 @@ final class UrlEncoderStringLookup extends AbstractStringLookup { static final UrlEncoderStringLookup INSTANCE = new UrlEncoderStringLookup(); /** - * No need to build instances for now. + * This ctor is not private to allow Mockito spying. */ - private UrlEncoderStringLookup() { + UrlEncoderStringLookup() { // empty } + String encode(final String key, final String enc) throws UnsupportedEncodingException { + return URLEncoder.encode(key, enc); + } + @Override public String lookup(final String key) { if (key == null) { @@ -48,7 +52,7 @@ final class UrlEncoderStringLookup extends AbstractStringLookup { } final String enc = StandardCharsets.UTF_8.name(); try { - return URLEncoder.encode(key, enc); + return encode(key, enc); } catch (final UnsupportedEncodingException e) { // Can't happen since UTF-8 is required by the Java specification. throw IllegalArgumentExceptions.format(e, "%s: source=%s, encoding=%s", e, key, enc); diff --git a/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java index 48518b7..34d8b47 100644 --- a/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/UrlDecoderStringLookupTest.java @@ -17,6 +17,13 @@ package org.apache.commons.text.lookup; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,14 +32,26 @@ import org.junit.jupiter.api.Test; */ public class UrlDecoderStringLookupTest { + private static final String DATA = "Hello World!"; + @Test public void testAllPercent() { - Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World%21")); + Assertions.assertEquals(DATA, UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World%21")); + } + + @Test + public void testExceptionGettingString() throws UnsupportedEncodingException { + final UrlDecoderStringLookup mockLookup = spy(UrlDecoderStringLookup.class); + when(mockLookup.decode(DATA, StandardCharsets.UTF_8.displayName())) + .thenThrow(UnsupportedEncodingException.class); + assertThrows(IllegalArgumentException.class, () -> { + mockLookup.lookup(DATA); + }); } @Test public void testExclamation() { - Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World!")); + Assertions.assertEquals(DATA, UrlDecoderStringLookup.INSTANCE.lookup("Hello%20World!")); } @Test @@ -42,7 +61,7 @@ public class UrlDecoderStringLookupTest { @Test public void testPlus() { - Assertions.assertEquals("Hello World!", UrlDecoderStringLookup.INSTANCE.lookup("Hello+World!")); + Assertions.assertEquals(DATA, UrlDecoderStringLookup.INSTANCE.lookup("Hello+World!")); } @Test diff --git a/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java index 75fbb62..d0e66d1 100644 --- a/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/UrlEncoderStringLookupTest.java @@ -17,6 +17,13 @@ package org.apache.commons.text.lookup; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,9 +32,21 @@ import org.junit.jupiter.api.Test; */ public class UrlEncoderStringLookupTest { + private static final String DATA = "Hello+World%21"; + @Test public void test() { - Assertions.assertEquals("Hello+World%21", UrlEncoderStringLookup.INSTANCE.lookup("Hello World!")); + Assertions.assertEquals(DATA, UrlEncoderStringLookup.INSTANCE.lookup("Hello World!")); + } + + @Test + public void testExceptionGettingString() throws UnsupportedEncodingException { + final UrlEncoderStringLookup mockLookup = spy(UrlEncoderStringLookup.class); + when(mockLookup.encode(DATA, StandardCharsets.UTF_8.displayName())) + .thenThrow(UnsupportedEncodingException.class); + assertThrows(IllegalArgumentException.class, () -> { + mockLookup.lookup(DATA); + }); } @Test