Skip to content

Commit

Permalink
Merge pull request #14 from TNG/issue-13-remove-checked-exceptions
Browse files Browse the repository at this point in the history
Issue 13 remove checked exceptions
  • Loading branch information
stefanhechtltng authored Oct 1, 2021
2 parents c132560 + 77f267d commit eb518ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
55 changes: 23 additions & 32 deletions core/src/main/java/com/tngtech/valueprovider/ValueProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -199,7 +200,7 @@ public List<String> fixedDecoratedStrings(int number) {
public List<String> fixedDecoratedStrings(int number, String base) {
List<String> 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;
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -902,7 +903,7 @@ private List<String> createIPv6AddressBlocksEndingWithIPv4() {
}

private String createIPv6Block() {
return String.format("%04x", intNumber(0, 0xFFFF));
return format("%04x", intNumber(0, 0xFFFF));
}

private List<String> truncateLeadingZerosFromBlocks(List<String> blocks) {
Expand Down Expand Up @@ -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");
}

/**
Expand All @@ -945,15 +941,15 @@ public URL url() {
* Example:
* <pre>
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.url("foo"); // -> "http://foo.com"
* vp.url("foo"); // -> "http://fooeFg.com"
* </pre>
* </p>
*
* @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);
}

Expand All @@ -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");
}

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

Expand All @@ -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");
}

/**
Expand All @@ -1023,24 +1009,29 @@ public URL httpsUrl() {
* Example:
* <pre>
* ValueProvider vp = ValueProviderFactory.createRandomValueProvider();
* vp.httpsUrl("somedomain"); // -> "https://somedomain.co.uk"
* vp.httpsUrl("somedomain"); // -> "https://somedomainUVw.co.uk"
* </pre>
* </p>
*
* @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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, URL> urlCreator) {
String illegalDomain = ":";
assertThatThrownBy(() -> urlCreator.apply(illegalDomain))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(illegalDomain);
}

private static ValueProvider withRandomValues() {
return new ValueProvider(createRandomInitialization());
}
Expand Down

0 comments on commit eb518ca

Please sign in to comment.