UriUtils { public static String appendSegmentToPath(String path, String segment) { if (path == null) { path = ""; } if (segment == null) { segment = ""; } if (path.isEmpty()) { return segment; } else if (segment.isEmpty()) { return path; } else if (path.charAt(path.length() - 1) == '/' && segment.startsWith("/")) { return path.substring(0, path.length() - 1) + segment; } else if (path.charAt(path.length() - 1) == '/' || segment.startsWith("/")) { return path + segment; } else { return path + "/" + segment; } } private  UriUtils(); static List<String> parseSegments(String uri); static List<InetSocketAddress> parseInetSocketAddresses(String uri); static List<InetSocketAddress> parseInetSocketAddresses(String uri, int defaultPort); static String parseScheme(String uri); static String parseAuthority(String uri); static String parseUserInfo(String uri); static String parsePath(String uri); static String parseQuery(String uri); static String appendSegmentToPath(String path, String segment);  }
@Test public void test_appendSegmentToPath() { assertThat(UriUtils.appendSegmentToPath(null, null), equalTo("")); assertThat(UriUtils.appendSegmentToPath(null, ""), equalTo("")); assertThat(UriUtils.appendSegmentToPath(null, "/"), equalTo("/")); assertThat(UriUtils.appendSegmentToPath(null, "bar"), equalTo("bar")); assertThat(UriUtils.appendSegmentToPath(null, "/bar"), equalTo("/bar")); assertThat(UriUtils.appendSegmentToPath(null, "bar/"), equalTo("bar/")); assertThat(UriUtils.appendSegmentToPath("", null), equalTo("")); assertThat(UriUtils.appendSegmentToPath("", ""), equalTo("")); assertThat(UriUtils.appendSegmentToPath("", "/"), equalTo("/")); assertThat(UriUtils.appendSegmentToPath("", "bar"), equalTo("bar")); assertThat(UriUtils.appendSegmentToPath("", "/bar"), equalTo("/bar")); assertThat(UriUtils.appendSegmentToPath("", "bar/"), equalTo("bar/")); assertThat(UriUtils.appendSegmentToPath("/", null), equalTo("/")); assertThat(UriUtils.appendSegmentToPath("/", ""), equalTo("/")); assertThat(UriUtils.appendSegmentToPath("/", "/"), equalTo("/")); assertThat(UriUtils.appendSegmentToPath("/", "bar"), equalTo("/bar")); assertThat(UriUtils.appendSegmentToPath("/", "/bar"), equalTo("/bar")); assertThat(UriUtils.appendSegmentToPath("/", "bar/"), equalTo("/bar/")); assertThat(UriUtils.appendSegmentToPath("foo", null), equalTo("foo")); assertThat(UriUtils.appendSegmentToPath("foo", ""), equalTo("foo")); assertThat(UriUtils.appendSegmentToPath("foo", "/"), equalTo("foo/")); assertThat(UriUtils.appendSegmentToPath("foo", "bar"), equalTo("foo/bar")); assertThat(UriUtils.appendSegmentToPath("foo", "/bar"), equalTo("foo/bar")); assertThat(UriUtils.appendSegmentToPath("foo", "bar/"), equalTo("foo/bar/")); assertThat(UriUtils.appendSegmentToPath("foo/", null), equalTo("foo/")); assertThat(UriUtils.appendSegmentToPath("foo/", ""), equalTo("foo/")); assertThat(UriUtils.appendSegmentToPath("foo/", "/"), equalTo("foo/")); assertThat(UriUtils.appendSegmentToPath("foo/", "bar"), equalTo("foo/bar")); assertThat(UriUtils.appendSegmentToPath("foo/", "/bar"), equalTo("foo/bar")); assertThat(UriUtils.appendSegmentToPath("foo/", "bar/"), equalTo("foo/bar/")); }