diff --git a/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java b/core/src/main/java/com/tngtech/valueprovider/ValueProvider.java index e7d3f46..7614be4 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) { @@ -931,12 +932,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("domain"); } /** @@ -945,15 +941,15 @@ public URL url() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.url("foo"); // -> "http://foo.com"
+     *         vp.url("foo"); // -> "http://fooeFg.com"
      * 
*

* * @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 +966,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("domain"); } /** @@ -984,15 +975,15 @@ public URL httpUrl() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.httpUrl("mydomain"); // -> "http://www.mydomain.com"
+     *         vp.httpUrl("mydomain"); // -> "http://www.mydomainStu.com"
      * 
*

* * @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 +1000,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("domain"); } /** @@ -1023,24 +1009,29 @@ public URL httpsUrl() { * Example: *
      *         ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
-     *         vp.httpsUrl("somedomain"); // -> "https://somedomain.co.uk"
+     *         vp.httpsUrl("somedomain"); // -> "https://somedomainUVw.co.uk"
      * 
*

* * @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 decoratedDomain = fixedDecoratedString(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(format("%s://%s%s.%s", scheme, optionalWWWPrefix, decoratedDomain, countrySuffix)); + } catch (MalformedURLException e) { + throw new IllegalArgumentException(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()); }