From ee4fac2ecd334038e9ba23c09bf64dc8b8448096 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Tue, 2 Apr 2019 18:00:39 +0200 Subject: [PATCH 1/6] new module: uri-builder --- pom.xml | 2 + web/pom.xml | 55 +++++ web/uri-builder/pom.xml | 45 ++++ .../utils/web/uribuilder/UriBuilder.java | 216 ++++++++++++++++++ .../utils/web/uribuilder/UriBuilderTest.java | 29 +++ 5 files changed, 347 insertions(+) create mode 100644 web/pom.xml create mode 100644 web/uri-builder/pom.xml create mode 100644 web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java create mode 100644 web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java diff --git a/pom.xml b/pom.xml index cd4d4a3..40a8e69 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,8 @@ utils lang-de + web + test diff --git a/web/pom.xml b/web/pom.xml new file mode 100644 index 0000000..949f3a8 --- /dev/null +++ b/web/pom.xml @@ -0,0 +1,55 @@ + + + + + 4.0.0 + + + io.redlink.utils + redlink-utils + 2.0.0-SNAPSHOT + + + web + pom + Redlink Web Utilities + + + uri-builder + + + + + + maven-install-plugin + + true + + + + maven-deploy-plugin + + true + + + + + + + \ No newline at end of file diff --git a/web/uri-builder/pom.xml b/web/uri-builder/pom.xml new file mode 100644 index 0000000..a128326 --- /dev/null +++ b/web/uri-builder/pom.xml @@ -0,0 +1,45 @@ + + + + + 4.0.0 + + + io.redlink.utils + redlink-utils + 2.0.0-SNAPSHOT + ../../ + + + uri-builder + URI Builder + + + + junit + junit + test + + + org.hamcrest + hamcrest-library + test + + + \ No newline at end of file diff --git a/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java b/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java new file mode 100644 index 0000000..6243fad --- /dev/null +++ b/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2019 Redlink GmbH. + */ +package io.redlink.utils.web.uribuilder; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * Simple, light-weight UriBuilder. + */ +public class UriBuilder { + + private String scheme; + + private String userInfo; + + private String host; + + private int port = -1; + + private List pathSegments = new LinkedList<>(); + + private Map> queryParams = new HashMap<>(); + + private String fragment; + + public UriBuilder scheme(String scheme) { + this.scheme = scheme; + return this; + } + + public UriBuilder userInfo(String user, String password) { + return userInfo(user + ":" + password); + } + + public UriBuilder userInfo(String userInfo) { + this.userInfo = userInfo; + return this; + } + + public UriBuilder host(String host) { + this.host = host; + return this; + } + + public UriBuilder port(int port) { + this.port = port; + return this; + } + + public UriBuilder defaultPort() { + return port(-1); + } + + public UriBuilder removeFragment() { + return fragment(null); + } + + public UriBuilder fragment(String fragment) { + this.fragment = fragment; + return this; + } + + public UriBuilder path() { + return path(null); + } + + public UriBuilder path(String path) { + this.pathSegments = splitPath(path); + return this; + } + + public UriBuilder pathSegment(String pathSegment) { + this.pathSegments.addAll(splitPath(pathSegment)); + return this; + } + + public UriBuilder query(String name, String value) { + return query(name, Collections.singleton(value)); + } + + public UriBuilder query(String name, Collection values) { + this.queryParams.put(name, new LinkedList<>(values)); + return this; + } + + public UriBuilder query(String name, String... values) { + return query(name, Arrays.asList(values)); + } + + public UriBuilder removeQuery() { + this.queryParams.clear(); + return this; + } + + public UriBuilder removeQuery(String name) { + this.queryParams.remove(name); + return this; + } + + public UriBuilder addQuery(String name, String value) { + return addQuery(name, Collections.singleton(value)); + } + + public UriBuilder addQuery(String name, String... values) { + return addQuery(name, Arrays.asList(values)); + } + + public UriBuilder addQuery(String name, Collection values) { + this.queryParams.computeIfAbsent(name, n -> new LinkedList<>()).addAll(values); + return this; + } + + + + public URI build() throws URISyntaxException { + return new URI(scheme, userInfo, host, port, joinPath(pathSegments), toQueryString(queryParams), fragment); + } + + + + public static UriBuilder copy(UriBuilder builder) { + final UriBuilder copy = new UriBuilder(); + + copy.scheme = builder.scheme; + copy.userInfo = builder.userInfo; + copy.host = builder.host; + copy.port = builder.port; + + copy.pathSegments.addAll(builder.pathSegments); + builder.queryParams.forEach((k, v) -> copy.queryParams.put(k, new LinkedList<>(v))); + + return copy; + } + + public static UriBuilder fromString(String uri) { + return fromUri(URI.create(uri)); + } + + public static UriBuilder fromUri(URI uri) { + final UriBuilder uriBuilder = new UriBuilder(); + + uriBuilder.scheme = uri.getScheme(); + uriBuilder.userInfo = uri.getUserInfo(); + uriBuilder.host = uri.getHost(); + uriBuilder.port = uri.getPort(); + + uriBuilder.pathSegments = splitPath(uri.getPath()); + uriBuilder.queryParams = fromQueryString(uri.getQuery()); + + return uriBuilder; + } + + private static String joinPath(List elements) { + return String.join("/", elements); + } + + private static List splitPath(String path) { + List segments = new LinkedList<>(); + if (path != null) { + segments.addAll(Arrays.asList(path.split("/"))); + } + return segments; + } + + private static String toQueryString(Map> elements) { + return elements.entrySet().stream() + .flatMap(e -> e.getValue().stream().map(v -> encode(e.getKey()) + "=" + encode(v))) + .collect(Collectors.joining("&")); + + } + + private static Map> fromQueryString(String queryString) { + final HashMap> query = new HashMap<>(); + if (queryString != null) { + // FIXME: this does not yet work... + Arrays.stream(queryString.split("&")) + .flatMap(e -> Arrays.stream(e.split("=", 2))) + .map(Arrays::asList) + .map(v -> v.stream().map(UriBuilder::decode).collect(Collectors.toList())) + .forEach(v -> query.computeIfAbsent(v.get(0), k -> new LinkedList<>()).add(v.get(1))); + + + } + return query; + } + + private static String decode(String encoded) { + try { + return URLDecoder.decode(encoded, "utf-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e); + } + } + + private static String encode(String raw) { + try { + return URLEncoder.encode(raw, "utf-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalStateException(e); + } + } +} + diff --git a/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java b/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java new file mode 100644 index 0000000..42f0ef4 --- /dev/null +++ b/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2019 redlink GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.redlink.utils.web.uribuilder; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class UriBuilderTest { + + @Test + public void testFromString() { + assertNotNull(UriBuilder.fromString("http://www.example.com/foo/bar.xml?foo=bar#h1")); + } +} From 61b8936429f85543ddb5918e3002b0ee05241bed Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Tue, 16 Apr 2019 19:06:39 +0200 Subject: [PATCH 2/6] fixed uri-builder, added tests and a new module: hamcrest-matchers! --- pom.xml | 6 ++ test/matchers/pom.xml | 41 ++++++++++ .../redlink/utils/hamcrest/UriMatchers.java | 76 +++++++++++++++++++ test/pom.xml | 1 + web/uri-builder/pom.xml | 6 ++ .../utils/web/uribuilder/UriBuilder.java | 12 ++- .../utils/web/uribuilder/UriBuilderTest.java | 29 ------- .../src/test/java/test/UriBuilderTest.java | 59 ++++++++++++++ 8 files changed, 198 insertions(+), 32 deletions(-) create mode 100644 test/matchers/pom.xml create mode 100644 test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java delete mode 100644 web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java create mode 100644 web/uri-builder/src/test/java/test/UriBuilderTest.java diff --git a/pom.xml b/pom.xml index 40a8e69..c4700f3 100644 --- a/pom.xml +++ b/pom.xml @@ -115,6 +115,12 @@ 4.12 test + + org.hamcrest + hamcrest-core + 1.3 + test + org.hamcrest hamcrest-library diff --git a/test/matchers/pom.xml b/test/matchers/pom.xml new file mode 100644 index 0000000..fe74070 --- /dev/null +++ b/test/matchers/pom.xml @@ -0,0 +1,41 @@ + + + + + 4.0.0 + + + io.redlink.utils + redlink-utils + 2.0.0-SNAPSHOT + ../../ + + + hamcrest-matchers + Redlink Hamcrest Matchers + + + + + org.hamcrest + hamcrest-core + compile + + + \ No newline at end of file diff --git a/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java b/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java new file mode 100644 index 0000000..b5c6185 --- /dev/null +++ b/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 Redlink GmbH. + */ +package io.redlink.utils.hamcrest; + +import java.net.URI; +import java.util.function.Function; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +public final class UriMatchers { + + private UriMatchers() {} + + public static TypeSafeDiagnosingMatcher isURI(String uri) { + return isURI(URI.create(uri)); + } + + public static TypeSafeDiagnosingMatcher isURI(URI uri) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(URI item, Description mismatchDescription) { + mismatchDescription.appendText("URI ").appendValue(item); + return uri == null ? item == null : uri.toASCIIString().equals(item.toASCIIString()); + } + + @Override + public void describeTo(Description description) { + description.appendText("URI ").appendValue(uri); + } + }; + } + + + + public static TypeSafeDiagnosingMatcher hasScheme(String scheme) { + return create("scheme", scheme, URI::getScheme); + } + + public static TypeSafeDiagnosingMatcher hasHost(String host) { + return create("host", host, URI::getHost); + } + + public static TypeSafeDiagnosingMatcher hasPort(int port) { + return create("port", port, URI::getPort); + } + + public static TypeSafeDiagnosingMatcher hasPath(String path) { + return create("path", path, URI::getPath); + } + + private static TypeSafeDiagnosingMatcher create(String uriPart, T part, Function fkt) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(URI item, Description mismatchDescription) { + if (item == null) { + mismatchDescription.appendText("URI ").appendValue(null); + return false; + } + describe(fkt.apply(item), mismatchDescription); + return part.equals(fkt.apply(item)); + } + + @Override + public void describeTo(Description description) { + describe(part, description); + } + + private void describe(T part, Description description) { + description.appendText("URI with ").appendText(uriPart).appendText(" ").appendValue(part); + } + }; + } + +} diff --git a/test/pom.xml b/test/pom.xml index 99d97f4..568e0a0 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -32,6 +32,7 @@ testcontainers + matchers diff --git a/web/uri-builder/pom.xml b/web/uri-builder/pom.xml index a128326..8a13583 100644 --- a/web/uri-builder/pom.xml +++ b/web/uri-builder/pom.xml @@ -41,5 +41,11 @@ hamcrest-library test + + io.redlink.utils + hamcrest-matchers + ${project.version} + test + \ No newline at end of file diff --git a/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java b/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java index 6243fad..92f2aad 100644 --- a/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java +++ b/web/uri-builder/src/main/java/io/redlink/utils/web/uribuilder/UriBuilder.java @@ -12,6 +12,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -32,7 +33,7 @@ public class UriBuilder { private List pathSegments = new LinkedList<>(); - private Map> queryParams = new HashMap<>(); + private Map> queryParams = new LinkedHashMap<>(); private String fragment; @@ -145,6 +146,12 @@ public static UriBuilder copy(UriBuilder builder) { return copy; } + public static UriBuilder create(String scheme, String host) { + return new UriBuilder() + .scheme(scheme) + .host(host); + } + public static UriBuilder fromString(String uri) { return fromUri(URI.create(uri)); } @@ -185,9 +192,8 @@ private static String toQueryString(Map> elements) { private static Map> fromQueryString(String queryString) { final HashMap> query = new HashMap<>(); if (queryString != null) { - // FIXME: this does not yet work... Arrays.stream(queryString.split("&")) - .flatMap(e -> Arrays.stream(e.split("=", 2))) + .map(e -> e.split("=", 2)) .map(Arrays::asList) .map(v -> v.stream().map(UriBuilder::decode).collect(Collectors.toList())) .forEach(v -> query.computeIfAbsent(v.get(0), k -> new LinkedList<>()).add(v.get(1))); diff --git a/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java b/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java deleted file mode 100644 index 42f0ef4..0000000 --- a/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2019 redlink GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.redlink.utils.web.uribuilder; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class UriBuilderTest { - - @Test - public void testFromString() { - assertNotNull(UriBuilder.fromString("http://www.example.com/foo/bar.xml?foo=bar#h1")); - } -} diff --git a/web/uri-builder/src/test/java/test/UriBuilderTest.java b/web/uri-builder/src/test/java/test/UriBuilderTest.java new file mode 100644 index 0000000..8f5202e --- /dev/null +++ b/web/uri-builder/src/test/java/test/UriBuilderTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2019 redlink GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package test; + +import io.redlink.utils.hamcrest.UriMatchers; +import io.redlink.utils.web.uribuilder.UriBuilder; +import java.net.URI; +import java.net.URISyntaxException; +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class UriBuilderTest { + + @Test + public void testFromString() throws URISyntaxException { + final UriBuilder uriBuilder = UriBuilder.fromString("http://www.example.com/foo/bar.xml?foo=bar#h1"); + + assertNotNull(uriBuilder); + + final URI uri = uriBuilder.scheme("https") + .port(123) + .query("bar", "foo1", "foo2") + .fragment("h2") + .build(); + + assertThat(uri.toString(), Matchers.is("https://www.example.com:123/foo/bar.xml?bar=foo1&bar=foo2&foo=bar#h2")); + + } + + @Test + public void testFromURI() { + assertNotNull(UriBuilder.fromUri(URI.create("http://www.example.com/foo/bar.xml?foo=bar#h1"))); + } + + @Test + public void testBuild() throws URISyntaxException { + final UriBuilder builder = UriBuilder.create("http", "localhost"); + + assertThat(builder.build(), UriMatchers.hasScheme("http")); + assertThat(builder.build(), UriMatchers.hasHost("localhost")); + + } + +} From 8bd4e3e9b7a407a47184de351955efca7f0e2f95 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Wed, 17 Apr 2019 09:45:09 +0200 Subject: [PATCH 3/6] improved hamcrest-matchers, fixed sonarcloud config --- pom.xml | 13 ++++- test/matchers/pom.xml | 6 +-- .../redlink/utils/hamcrest/UriMatchers.java | 51 ++++++++++++++----- test/pom.xml | 20 +------- test/testcontainers/pom.xml | 6 +-- web/uri-builder/pom.xml | 2 +- .../src/test/java/test/UriBuilderTest.java | 1 + 7 files changed, 59 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index c4700f3..605450b 100644 --- a/pom.xml +++ b/pom.xml @@ -261,13 +261,14 @@ https://sonarcloud.io redlink ${project.build.directory}/dependency-check-report.xml + ${project.build.directory}/jacoco org.jacoco jacoco-maven-plugin - 0.8.2 + 0.8.3 prepare-coverage-agent @@ -276,6 +277,16 @@ prepare-agent + + generate-report + verify + + report-aggregate + + + ${project.build.directory}/jacoco + + diff --git a/test/matchers/pom.xml b/test/matchers/pom.xml index fe74070..4f932e5 100644 --- a/test/matchers/pom.xml +++ b/test/matchers/pom.xml @@ -21,10 +21,10 @@ 4.0.0 - io.redlink.utils - redlink-utils + io.redlink.utils.test + test-utils 2.0.0-SNAPSHOT - ../../ + ../ hamcrest-matchers diff --git a/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java b/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java index b5c6185..f65daa7 100644 --- a/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java +++ b/test/matchers/src/main/java/io/redlink/utils/hamcrest/UriMatchers.java @@ -5,6 +5,7 @@ import java.net.URI; import java.util.function.Function; +import org.hamcrest.CoreMatchers; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; @@ -32,25 +33,47 @@ public void describeTo(Description description) { }; } - - public static TypeSafeDiagnosingMatcher hasScheme(String scheme) { - return create("scheme", scheme, URI::getScheme); + return hasScheme(CoreMatchers.is(scheme)); + } + + public static TypeSafeDiagnosingMatcher hasScheme(Matcher schemeMatcher) { + return create("scheme", schemeMatcher, URI::getScheme); } public static TypeSafeDiagnosingMatcher hasHost(String host) { - return create("host", host, URI::getHost); + return hasHost(CoreMatchers.is(host)); + } + + public static TypeSafeDiagnosingMatcher hasHost(Matcher hostMatcher) { + return create("host", hostMatcher, URI::getHost); } public static TypeSafeDiagnosingMatcher hasPort(int port) { - return create("port", port, URI::getPort); + return hasPort(CoreMatchers.is(port)); + } + + public static TypeSafeDiagnosingMatcher hasPort(Matcher portMatcher) { + return create("port", portMatcher, URI::getPort); } public static TypeSafeDiagnosingMatcher hasPath(String path) { - return create("path", path, URI::getPath); + return hasPath(CoreMatchers.is(path)); + } + + public static TypeSafeDiagnosingMatcher hasPath(Matcher pathMatcher) { + return create("path", pathMatcher, URI::getPath); + } + + public static TypeSafeDiagnosingMatcher hasFragment(String fragment) { + return hasFragment(CoreMatchers.is(fragment)); + } + + public static TypeSafeDiagnosingMatcher hasFragment(Matcher fragmentMatcher) { + return create("fragment", fragmentMatcher, URI::getFragment); } - private static TypeSafeDiagnosingMatcher create(String uriPart, T part, Function fkt) { + private static TypeSafeDiagnosingMatcher create(String uriPart, Matcher partMatcher, Function fkt) { return new TypeSafeDiagnosingMatcher() { @Override protected boolean matchesSafely(URI item, Description mismatchDescription) { @@ -58,17 +81,17 @@ protected boolean matchesSafely(URI item, Description mismatchDescription) { mismatchDescription.appendText("URI ").appendValue(null); return false; } - describe(fkt.apply(item), mismatchDescription); - return part.equals(fkt.apply(item)); + + mismatchDescription.appendText("URI with ").appendText(uriPart).appendText(" "); + partMatcher.describeMismatch(fkt.apply(item), mismatchDescription); + + return partMatcher.matches(fkt.apply(item)); } @Override public void describeTo(Description description) { - describe(part, description); - } - - private void describe(T part, Description description) { - description.appendText("URI with ").appendText(uriPart).appendText(" ").appendValue(part); + description.appendText("URI with ").appendText(uriPart).appendText(" ") + .appendDescriptionOf(partMatcher); } }; } diff --git a/test/pom.xml b/test/pom.xml index 568e0a0..25521dc 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -26,7 +26,8 @@ 2.0.0-SNAPSHOT - test + io.redlink.utils.test + test-utils pom Redlink Test Utilities @@ -35,21 +36,4 @@ matchers - - - - maven-install-plugin - - true - - - - maven-deploy-plugin - - true - - - - - \ No newline at end of file diff --git a/test/testcontainers/pom.xml b/test/testcontainers/pom.xml index 5d15ec3..b1c8639 100644 --- a/test/testcontainers/pom.xml +++ b/test/testcontainers/pom.xml @@ -21,10 +21,10 @@ 4.0.0 - io.redlink.utils - redlink-utils + io.redlink.utils.test + test-utils 2.0.0-SNAPSHOT - ../../ + ../ testcontainers diff --git a/web/uri-builder/pom.xml b/web/uri-builder/pom.xml index 8a13583..f87cdec 100644 --- a/web/uri-builder/pom.xml +++ b/web/uri-builder/pom.xml @@ -42,7 +42,7 @@ test - io.redlink.utils + io.redlink.utils.test hamcrest-matchers ${project.version} test diff --git a/web/uri-builder/src/test/java/test/UriBuilderTest.java b/web/uri-builder/src/test/java/test/UriBuilderTest.java index 8f5202e..0eb0353 100644 --- a/web/uri-builder/src/test/java/test/UriBuilderTest.java +++ b/web/uri-builder/src/test/java/test/UriBuilderTest.java @@ -19,6 +19,7 @@ import io.redlink.utils.web.uribuilder.UriBuilder; import java.net.URI; import java.net.URISyntaxException; +import org.hamcrest.CoreMatchers; import org.hamcrest.Matchers; import org.junit.Test; From 4065c8bdac2d07814d3b4b7489783c9f69ad09ef Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Wed, 17 Apr 2019 10:12:46 +0200 Subject: [PATCH 4/6] fix test-coverage collection for sonarcloud --- pom.xml | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/pom.xml b/pom.xml index 605450b..533ebea 100644 --- a/pom.xml +++ b/pom.xml @@ -261,7 +261,6 @@ https://sonarcloud.io redlink ${project.build.directory}/dependency-check-report.xml - ${project.build.directory}/jacoco @@ -277,16 +276,6 @@ prepare-agent - - generate-report - verify - - report-aggregate - - - ${project.build.directory}/jacoco - - From 186ffe346a98ec9a29cc2e0ca7fc709d7757c415 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Fri, 29 Nov 2019 23:41:45 +0100 Subject: [PATCH 5/6] UriBuilder: some tests with special chars --- .../src/test/java/test/UriBuilderTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/web/uri-builder/src/test/java/test/UriBuilderTest.java b/web/uri-builder/src/test/java/test/UriBuilderTest.java index 0eb0353..e5ab634 100644 --- a/web/uri-builder/src/test/java/test/UriBuilderTest.java +++ b/web/uri-builder/src/test/java/test/UriBuilderTest.java @@ -57,4 +57,26 @@ public void testBuild() throws URISyntaxException { } + @Test + public void testSpecialChars() throws URISyntaxException { + final UriBuilder builder = UriBuilder.create("https", "example.com"); + + builder.pathSegment("some/path/with space"); + builder.query("another space", "key and value"); + builder.query("reserved", "foo&bar"); + builder.query("encoded", "100%25"); + builder.query("non-ascii", "ยข"); + + builder.query("fragment", "#tag"); + + assertThat(builder.build().toString(), Matchers.allOf( + Matchers.containsString("/some/path/with%20space"), + Matchers.containsString("?another%20space=key%20and%20value&"), + Matchers.containsString("&reserved=foo%26bar&"), + Matchers.containsString("&encoded=100%2525&"), + Matchers.containsString("&non-ascii=%C2%A2&"), + Matchers.containsString("&fragment=%23tag&") + )); + + } } From 8620771e05a08af92a74c39be50edec7abd59d82 Mon Sep 17 00:00:00 2001 From: Jakob Frank Date: Mon, 16 Dec 2019 08:26:13 +0100 Subject: [PATCH 6/6] partially fixed uri-builder tests --- .../utils/web/uribuilder}/UriBuilderTest.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) rename web/uri-builder/src/test/java/{test => io/redlink/utils/web/uribuilder}/UriBuilderTest.java (85%) diff --git a/web/uri-builder/src/test/java/test/UriBuilderTest.java b/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java similarity index 85% rename from web/uri-builder/src/test/java/test/UriBuilderTest.java rename to web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java index e5ab634..e333c9b 100644 --- a/web/uri-builder/src/test/java/test/UriBuilderTest.java +++ b/web/uri-builder/src/test/java/io/redlink/utils/web/uribuilder/UriBuilderTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package test; +package io.redlink.utils.web.uribuilder; import io.redlink.utils.hamcrest.UriMatchers; import io.redlink.utils.web.uribuilder.UriBuilder; @@ -61,7 +61,8 @@ public void testBuild() throws URISyntaxException { public void testSpecialChars() throws URISyntaxException { final UriBuilder builder = UriBuilder.create("https", "example.com"); - builder.pathSegment("some/path/with space"); + builder.pathSegment("/some/path/with space"); + builder.pathSegment("folder"); builder.query("another space", "key and value"); builder.query("reserved", "foo&bar"); builder.query("encoded", "100%25"); @@ -69,9 +70,12 @@ public void testSpecialChars() throws URISyntaxException { builder.query("fragment", "#tag"); - assertThat(builder.build().toString(), Matchers.allOf( - Matchers.containsString("/some/path/with%20space"), - Matchers.containsString("?another%20space=key%20and%20value&"), + assertThat(builder.build().toASCIIString(), Matchers.allOf( + Matchers.containsString("/some/path/with%20space/folder"), + Matchers.anyOf( + Matchers.containsString("?another%20space=key%20and%20value&"), + Matchers.containsString("?another+space=key+and+value&") + ), Matchers.containsString("&reserved=foo%26bar&"), Matchers.containsString("&encoded=100%2525&"), Matchers.containsString("&non-ascii=%C2%A2&"),