rename newBuilder() to mutator() to follow principle of least surprise, let newBuilder() create a new and empty URL builder instead

This commit is contained in:
Jörg Prante 2019-08-07 23:27:21 +02:00
parent 53e9469d18
commit 4665353fc7
5 changed files with 24 additions and 10 deletions

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = net name = net
version = 1.3.3 version = 1.3.4
# test # test
jackson.version = 2.8.11 jackson.version = 2.8.11

View file

@ -325,10 +325,14 @@ public class URL implements Comparable<URL> {
} }
} }
public Builder newBuilder() { public Builder mutator() {
return builder; return builder;
} }
public Builder newBuilder() {
return new Builder();
}
private String toString(boolean withFragment) { private String toString(boolean withFragment) {
if (internalStringRepresentation != null) { if (internalStringRepresentation != null) {
return internalStringRepresentation; return internalStringRepresentation;

View file

@ -118,7 +118,7 @@ class URLBuilderTest {
@Test @Test
void testAllParts() { void testAllParts() {
assertUrl(URL.https().resolveFromHost("foo.bar.com").port(3333) assertUrl(URL.https().resolveFromHost("foobar.com").port(3333)
.pathSegment("foo") .pathSegment("foo")
.pathSegment("bar") .pathSegment("bar")
.matrixParam("mtx1", "val1") .matrixParam("mtx1", "val1")
@ -127,11 +127,11 @@ class URLBuilderTest {
.queryParam("q2", "v2") .queryParam("q2", "v2")
.fragment("zomg it's a fragment") .fragment("zomg it's a fragment")
.toUrlString(), .toUrlString(),
"https://foo.bar.com:3333/foo/bar;mtx1=val1;mtx2=val2?q1=v1&q2=v2#zomg%20it's%20a%20fragment"); "https://foobar.com:3333/foo/bar;mtx1=val1;mtx2=val2?q1=v1&q2=v2#zomg%20it's%20a%20fragment");
} }
@Test @Test
void testSlashInHost() { void testSlashHost() {
URL.http().resolveFromHost("/").toUrlString(); URL.http().resolveFromHost("/").toUrlString();
} }
@ -259,12 +259,21 @@ class URLBuilderTest {
} }
@Test @Test
void testNewBuilder() { void testMutator() {
URL.Builder builder = URL.from("http://google.com:8008/foobar").newBuilder(); URL.Builder builder = URL.from("http://google.com:8008/foobar").mutator();
builder.scheme("https"); builder.scheme("https");
assertEquals("https://google.com:8008/foobar", builder.build().toString()); assertEquals("https://google.com:8008/foobar", builder.build().toString());
} }
@Test
void testNewBuilder() {
URL url = URL.from("http://google.com:8008/foobar");
URL.Builder builder = url.newBuilder()
.scheme(url.getScheme())
.schemeSpecificPart(url.getSchemeSpecificPart());
assertEquals("http://google.com:8008/foobar", builder.build().toString());
}
@Test @Test
void testUserInfo(){ void testUserInfo(){
String s = URL.http().userInfo("foo:bar").host("foo.com").toUrlString(); String s = URL.http().userInfo("foo:bar").host("foo.com").toUrlString();

View file

@ -14,6 +14,10 @@ class URLResolverTest {
void testResolveURI() throws Exception { void testResolveURI() throws Exception {
URI base = URI.create("http://example.org/foo"); URI base = URI.create("http://example.org/foo");
assertEquals("http://example.org/", base.resolve("/").toString()); assertEquals("http://example.org/", base.resolve("/").toString());
assertEquals("http://example.org/foo", base.toString());
URI rel = URI.create("http://foobar/rel");
assertEquals("http://foobar/rel", base.resolve(rel).toString());
assertEquals("http://example.org/foo", base.toString());
assertEquals("http://example.org/foobar", base.resolve("/foobar").toString()); assertEquals("http://example.org/foobar", base.resolve("/foobar").toString());
assertEquals("http://example.org/foobar", base.resolve("foobar").toString()); assertEquals("http://example.org/foobar", base.resolve("foobar").toString());
base = URI.create("http://example.org/foo/"); base = URI.create("http://example.org/foo/");

View file

@ -20,14 +20,12 @@ class URLTest {
for (JsonTest test : tests) { for (JsonTest test : tests) {
String base = test.base; String base = test.base;
String input = test.input; String input = test.input;
//System.err.println("testing: " + base + " " + input + " " + test.failure);
if (test.skip) { if (test.skip) {
continue; continue;
} }
if (test.failure) { if (test.failure) {
try { try {
URL url = URL.base(base).resolve(input); URL url = URL.base(base).resolve(input);
//System.err.println("resolved: " + url.toString());
fail(); fail();
} catch (Exception e) { } catch (Exception e) {
// pass // pass
@ -36,7 +34,6 @@ class URLTest {
if (base != null && input != null) { if (base != null && input != null) {
try { try {
URL url = URL.base(base).resolve(input); URL url = URL.base(base).resolve(input);
//System.err.println("resolved: " + url.toString());
if (test.protocol != null) { if (test.protocol != null) {
assertEquals(test.protocol, url.getScheme() + ":"); assertEquals(test.protocol, url.getScheme() + ":");
} }