From 9acca6177ad070e6821dcf263998e2dc06c4a401 Mon Sep 17 00:00:00 2001 From: jonashoef Date: Wed, 15 Sep 2021 21:17:32 +0200 Subject: [PATCH 1/3] Replace potential (checked) MalformedURLException with (runtime) IllegalArgumentException to ease using URL-related methods. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #13 Signed-off-by: Jonas Höf --- .../tngtech/valueprovider/ValueProvider.java | 41 +++++++------------ .../valueprovider/ValueProviderTest.java | 17 ++++++++ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java index e7d3f46..3b963c1 100644 --- a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java +++ b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java @@ -931,12 +931,7 @@ private String mergeZeroGroups(String address) { * @return the generated URL. */ public URL url() { - try { - return url(fixedDecoratedString("domain")); - } catch (MalformedURLException e) { - // won't happen. 'domain' yields valid URL. - } - return null; // won't happen. 'domain' yields valid URL. + return url(fixedDecoratedString("domain")); } /** @@ -951,9 +946,9 @@ public URL url() { * * @param domain the domain of the {@link URL}. * @return the generated URL. - * @throws MalformedURLException if {@code domain} yields an invalid {@link URL}. + * @throws IllegalArgumentException if {@code domain} yields an invalid {@link URL}. */ - public URL url(String domain) throws MalformedURLException { + public URL url(String domain) { return booleanValue() ? httpUrl(domain) : httpsUrl(domain); } @@ -970,12 +965,7 @@ public URL url(String domain) throws MalformedURLException { * @return the generated URL. */ public URL httpUrl() { - try { - return httpUrl(fixedDecoratedString("domain")); - } catch (MalformedURLException e) { - // won't happen. 'domain' yields valid URL. - } - return null; // won't happen. 'domain' yields valid URL. + return httpUrl(fixedDecoratedString("domain")); } /** @@ -990,9 +980,9 @@ public URL httpUrl() { * * @param domain the domain of the {@link URL}. * @return the generated URL. - * @throws MalformedURLException if {@code domain} yields an invalid {@link URL}. + * @throws IllegalArgumentException if {@code domain} yields an invalid {@link URL}. */ - public URL httpUrl(String domain) throws MalformedURLException { + public URL httpUrl(String domain) { return createUrl("http", domain); } @@ -1009,12 +999,7 @@ public URL httpUrl(String domain) throws MalformedURLException { * @return the generated URL. */ public URL httpsUrl() { - try { - return httpsUrl(fixedDecoratedString("domain")); - } catch (MalformedURLException e) { - // won't happen. 'domain' yields valid URL. - } - return null; // won't happen. 'domain' yields valid URL. + return httpsUrl(fixedDecoratedString("domain")); } /** @@ -1028,19 +1013,23 @@ public URL httpsUrl() { *

* * @return the generated URL. - * @throws MalformedURLException if {@code domain} yields an invalid {@link URL}. + * @throws IllegalArgumentException if {@code domain} yields an invalid {@link URL}. */ - public URL httpsUrl(String domain) throws MalformedURLException { + public URL httpsUrl(String domain) { return createUrl("https", domain); } - private URL createUrl(String scheme, String domain) throws MalformedURLException { + private URL createUrl(String scheme, String domain) { String optionalWWWPrefix = ""; if (!domain.startsWith("www")) { optionalWWWPrefix = booleanValue() ? "www." : ""; } String countrySuffix = oneOf("de", "com", "net", "co.uk"); - return new URL(String.format("%s://%s%s.%s", scheme, optionalWWWPrefix, domain, countrySuffix)); + try { + return new URL(String.format("%s://%s%s.%s", scheme, optionalWWWPrefix, domain, countrySuffix)); + } catch (MalformedURLException e) { + throw new IllegalArgumentException(String.format("Illegal domain %s", domain)); + } } String getPrefix() { diff --git a/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java b/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java index 032d9b6..e45ac7b 100644 --- a/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java +++ b/core/src/test/java/com/tngtech/valueprovider/ValueProviderTest.java @@ -5,6 +5,7 @@ import java.math.BigInteger; import java.net.Inet6Address; import java.net.InetAddress; +import java.net.URL; import java.net.UnknownHostException; import java.time.LocalDateTime; import java.util.Arrays; @@ -581,6 +582,22 @@ void ipV6Address_should_return_valid_IPv6_address() throws UnknownHostException assertThat(address).isInstanceOf(Inet6Address.class); } + @Test + void url_related_methods_should_fail_on_malformed_urls() { + ValueProvider random = withRandomValues(); + + assertFailsOnMalformedUrl(random::url); + assertFailsOnMalformedUrl(random::httpUrl); + assertFailsOnMalformedUrl(random::httpsUrl); + } + + private void assertFailsOnMalformedUrl(Function urlCreator) { + String illegalDomain = ":"; + assertThatThrownBy(() -> urlCreator.apply(illegalDomain)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining(illegalDomain); + } + private static ValueProvider withRandomValues() { return new ValueProvider(createRandomInitialization()); } From d3576f05379ccd882795d7fe557cd3d4b0652653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6f?= Date: Wed, 15 Sep 2021 21:17:59 +0200 Subject: [PATCH 2/3] Use static import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #13 Signed-off-by: Jonas Höf --- .../java/com/tngtech/valueprovider/ValueProvider.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java index 3b963c1..43a992f 100644 --- a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java +++ b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java @@ -29,6 +29,7 @@ import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.newArrayListWithCapacity; import static com.google.common.collect.Sets.newHashSet; +import static java.lang.String.format; import static java.time.LocalDateTime.now; import static java.time.temporal.ChronoUnit.DAYS; import static java.time.temporal.ChronoUnit.SECONDS; @@ -199,7 +200,7 @@ public List fixedDecoratedStrings(int number) { public List fixedDecoratedStrings(int number, String base) { List strings = newArrayListWithCapacity(number); for (int i = 0; i < number; i++) { - strings.add(String.format("%s %d", fixedDecoratedString(base), i)); + strings.add(format("%s %d", fixedDecoratedString(base), i)); } return strings; } @@ -252,7 +253,7 @@ public String numericString(int length, int min, int max) { length, min, minLength); int number = intNumber(min, max); - return String.format("%0" + length + "d", number); + return format("%0" + length + "d", number); } /** @@ -902,7 +903,7 @@ private List createIPv6AddressBlocksEndingWithIPv4() { } private String createIPv6Block() { - return String.format("%04x", intNumber(0, 0xFFFF)); + return format("%04x", intNumber(0, 0xFFFF)); } private List truncateLeadingZerosFromBlocks(List blocks) { @@ -1026,9 +1027,9 @@ private URL createUrl(String scheme, String domain) { } String countrySuffix = oneOf("de", "com", "net", "co.uk"); try { - return new URL(String.format("%s://%s%s.%s", scheme, optionalWWWPrefix, domain, countrySuffix)); + return new URL(format("%s://%s%s.%s", scheme, optionalWWWPrefix, domain, countrySuffix)); } catch (MalformedURLException e) { - throw new IllegalArgumentException(String.format("Illegal domain %s", domain)); + throw new IllegalArgumentException(format("Illegal domain %s", domain)); } } From 77f267dfd1778a6d5a30fae68f98424346d263ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6f?= Date: Thu, 16 Sep 2021 08:44:04 +0200 Subject: [PATCH 3/3] ensure string decoration for passed domain in URL-related methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #13 Signed-off-by: Jonas Höf --- .../com/tngtech/valueprovider/ValueProvider.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java index 43a992f..7614be4 100644 --- a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java +++ b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java @@ -932,7 +932,7 @@ private String mergeZeroGroups(String address) { * @return the generated URL. */ public URL url() { - return url(fixedDecoratedString("domain")); + return url("domain"); } /** @@ -941,7 +941,7 @@ public URL url() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.url("foo"); // -> "http://foo.com"
+     *         vp.url("foo"); // -> "http://fooeFg.com"
      * 
*

* @@ -966,7 +966,7 @@ public URL url(String domain) { * @return the generated URL. */ public URL httpUrl() { - return httpUrl(fixedDecoratedString("domain")); + return httpUrl("domain"); } /** @@ -975,7 +975,7 @@ public URL httpUrl() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.httpUrl("mydomain"); // -> "http://www.mydomain.com"
+     *         vp.httpUrl("mydomain"); // -> "http://www.mydomainStu.com"
      * 
*

* @@ -1000,7 +1000,7 @@ public URL httpUrl(String domain) { * @return the generated URL. */ public URL httpsUrl() { - return httpsUrl(fixedDecoratedString("domain")); + return httpsUrl("domain"); } /** @@ -1009,7 +1009,7 @@ public URL httpsUrl() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.httpsUrl("somedomain"); // -> "https://somedomain.co.uk"
+     *         vp.httpsUrl("somedomain"); // -> "https://somedomainUVw.co.uk"
      * 
*

* @@ -1021,13 +1021,14 @@ public URL httpsUrl(String domain) { } private URL createUrl(String scheme, String domain) { + String decoratedDomain = fixedDecoratedString(domain); String optionalWWWPrefix = ""; if (!domain.startsWith("www")) { optionalWWWPrefix = booleanValue() ? "www." : ""; } String countrySuffix = oneOf("de", "com", "net", "co.uk"); try { - return new URL(format("%s://%s%s.%s", scheme, optionalWWWPrefix, domain, countrySuffix)); + return new URL(format("%s://%s%s.%s", scheme, optionalWWWPrefix, decoratedDomain, countrySuffix)); } catch (MalformedURLException e) { throw new IllegalArgumentException(format("Illegal domain %s", domain)); }