PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-cxf-2.4.8-src/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java

#
Java | 1222 lines | 1016 code | 181 blank | 25 comment | 4 complexity | efabc9573513c445f06749f544e57b45 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.cxf.jaxrs.impl;
  20. import java.lang.reflect.Method;
  21. import java.net.URI;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import javax.ws.rs.core.MultivaluedMap;
  26. import javax.ws.rs.core.UriBuilder;
  27. import org.apache.cxf.jaxrs.resources.Book;
  28. import org.apache.cxf.jaxrs.resources.BookStore;
  29. import org.apache.cxf.jaxrs.resources.UriBuilderWrongAnnotations;
  30. import org.apache.cxf.jaxrs.utils.JAXRSUtils;
  31. import org.junit.Assert;
  32. import org.junit.Ignore;
  33. import org.junit.Test;
  34. public class UriBuilderImplTest extends Assert {
  35. @Test
  36. public void testQueryParamWithTemplateValues() {
  37. URI uri;
  38. uri = UriBuilder.fromPath("/index.jsp").queryParam("a", "{a}").queryParam("b", "{b}")
  39. .build("valueA", "valueB");
  40. assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());
  41. }
  42. @Test(expected = IllegalArgumentException.class)
  43. public void testQueryParamWithMissingTemplateValues() {
  44. UriBuilder.fromPath("/index.jsp").queryParam("a", "{a}").queryParam("b", "{b}")
  45. .build("valueA");
  46. }
  47. @Test
  48. public void testPathAndQueryParamWithTemplateValues() {
  49. URI uri;
  50. uri = UriBuilder.fromPath("/index{ind}.jsp").queryParam("a", "{a}").queryParam("b", "{b}")
  51. .build("1", "valueA", "valueB");
  52. assertEquals("/index1.jsp?a=valueA&b=valueB", uri.toString());
  53. }
  54. @Test
  55. public void testReplaceQueryStringWithTemplateValues() {
  56. URI uri;
  57. uri = UriBuilder.fromUri("/index.jsp").replaceQuery("a={a}&b={b}")
  58. .build("valueA", "valueB");
  59. assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());
  60. }
  61. @Test
  62. public void testQueryParamUsingMapWithTemplateValues() {
  63. Map<String, String> values = new HashMap<String, String>();
  64. values.put("a", "valueA");
  65. values.put("b", "valueB");
  66. URI uri;
  67. uri = UriBuilder.fromPath("/index.jsp")
  68. .queryParam("a", "{a}")
  69. .queryParam("b", "{b}")
  70. .buildFromMap(values);
  71. assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());
  72. }
  73. @Test
  74. public void testPathAndQueryParamUsingMapWithTemplateValues() {
  75. Map<String, String> values = new HashMap<String, String>();
  76. values.put("a", "valueA");
  77. values.put("b", "valueB");
  78. values.put("ind", "1");
  79. URI uri;
  80. uri = UriBuilder.fromPath("/index{ind}.jsp")
  81. .queryParam("a", "{a}")
  82. .queryParam("b", "{b}")
  83. .buildFromMap(values);
  84. assertEquals("/index1.jsp?a=valueA&b=valueB", uri.toString());
  85. }
  86. @Test(expected = IllegalArgumentException.class)
  87. public void testCtorNull() throws Exception {
  88. new UriBuilderImpl(null);
  89. }
  90. @Test(expected = IllegalArgumentException.class)
  91. public void testPathStringNull() throws Exception {
  92. new UriBuilderImpl().path((String)null);
  93. }
  94. @Test
  95. public void testCtorAndBuild() throws Exception {
  96. URI uri = new URI("http://foo/bar/baz?query=1#fragment");
  97. URI newUri = new UriBuilderImpl(uri).build();
  98. assertEquals("URI is not built correctly", uri, newUri);
  99. }
  100. @Test
  101. public void testTrailingSlash() throws Exception {
  102. URI uri = new URI("http://bar/");
  103. URI newUri = new UriBuilderImpl(uri).build();
  104. assertEquals("URI is not built correctly", "http://bar/", newUri.toString());
  105. }
  106. @Test
  107. public void testPathTrailingSlash() throws Exception {
  108. URI uri = new URI("http://bar");
  109. URI newUri = new UriBuilderImpl(uri).path("/").build();
  110. assertEquals("URI is not built correctly", "http://bar/", newUri.toString());
  111. }
  112. @Test(expected = IllegalArgumentException.class)
  113. public void testNullPathWithBuildEncoded() throws Exception {
  114. URI uri = new URI("http://bar");
  115. new UriBuilderImpl(uri).path("{bar}").buildFromEncoded((Object[])null);
  116. }
  117. @Test(expected = IllegalArgumentException.class)
  118. public void testNullPathWithBuildEncoded2() throws Exception {
  119. URI uri = new URI("http://bar");
  120. new UriBuilderImpl(uri).path("{bar}").buildFromEncoded(new Object[] {null});
  121. }
  122. @Test
  123. public void testPathTrailingSlash2() throws Exception {
  124. URI uri = new URI("http://bar");
  125. URI newUri = new UriBuilderImpl(uri).path("/").path("/").build();
  126. assertEquals("URI is not built correctly", "http://bar/", newUri.toString());
  127. }
  128. @Test
  129. public void testClone() throws Exception {
  130. URI uri = new URI("http://bar");
  131. URI newUri = new UriBuilderImpl(uri).clone().build();
  132. assertEquals("URI is not built correctly", "http://bar", newUri.toString());
  133. }
  134. @Test
  135. public void testCloneWithoutLeadingSlash() throws Exception {
  136. URI uri = new URI("bar/foo");
  137. URI newUri = new UriBuilderImpl(uri).clone().build();
  138. assertEquals("URI is not built correctly", "bar/foo", newUri.toString());
  139. }
  140. @Test
  141. public void testCloneWithLeadingSlash() throws Exception {
  142. URI uri = new URI("/bar/foo");
  143. URI newUri = new UriBuilderImpl(uri).clone().build();
  144. assertEquals("URI is not built correctly", "/bar/foo", newUri.toString());
  145. }
  146. @Test
  147. public void testBuildWithLeadingSlash() throws Exception {
  148. URI uri = new URI("/bar/foo");
  149. URI newUri = UriBuilder.fromUri(uri).build();
  150. assertEquals("URI is not built correctly", "/bar/foo", newUri.toString());
  151. }
  152. @Test
  153. public void testClonePctEncodedFromUri() throws Exception {
  154. URI uri = new URI("http://bar/foo%20");
  155. URI newUri = new UriBuilderImpl(uri).clone().buildFromEncoded();
  156. assertEquals("URI is not built correctly", "http://bar/foo%20", newUri.toString());
  157. }
  158. @Test
  159. public void testClonePctEncoded() throws Exception {
  160. URI uri = new URI("http://bar");
  161. URI newUri = new UriBuilderImpl(uri)
  162. .path("{a}").path("{b}")
  163. .matrixParam("m", "m1 ", "m2+%20")
  164. .queryParam("q", "q1 ", "q2+q3%20").clone().buildFromEncoded("a+ ", "b%2B%20 ");
  165. assertEquals("URI is not built correctly",
  166. "http://bar/a+%20/b%2B%20%20;m=m1%20;m=m2+%20?q=q1+&q=q2%2Bq3%20",
  167. newUri.toString());
  168. }
  169. @Test
  170. public void testEncodedPathQueryFromExistingURI() throws Exception {
  171. URI uri = new URI("http://bar/foo+%20%2B?q=a+b%20%2B");
  172. URI newUri = new UriBuilderImpl(uri).buildFromEncoded();
  173. assertEquals("URI is not built correctly",
  174. "http://bar/foo+%20%2B?q=a%2Bb%20%2B", newUri.toString());
  175. }
  176. @Test
  177. public void testEncodedPathWithAsteriscs() throws Exception {
  178. URI uri = new URI("http://bar/foo/");
  179. URI newUri = new UriBuilderImpl(uri).path("*").buildFromEncoded();
  180. assertEquals("URI is not built correctly",
  181. "http://bar/foo/*", newUri.toString());
  182. }
  183. @Test
  184. public void testPathWithAsteriscs() throws Exception {
  185. URI uri = new URI("http://bar/foo/");
  186. URI newUri = new UriBuilderImpl(uri).path("*").build();
  187. assertEquals("URI is not built correctly",
  188. "http://bar/foo/*", newUri.toString());
  189. }
  190. @Test
  191. public void testEncodedPathWithTwoAsteriscs() throws Exception {
  192. URI uri = new URI("http://bar/foo/");
  193. URI newUri = new UriBuilderImpl(uri).path("**").buildFromEncoded();
  194. assertEquals("URI is not built correctly",
  195. "http://bar/foo/**", newUri.toString());
  196. }
  197. @Test
  198. public void testPathWithTwoAsteriscs() throws Exception {
  199. URI uri = new URI("http://bar/foo/");
  200. URI newUri = new UriBuilderImpl(uri).path("**").build();
  201. assertEquals("URI is not built correctly",
  202. "http://bar/foo/**", newUri.toString());
  203. }
  204. @Test
  205. public void testEncodedAddedQuery() throws Exception {
  206. URI uri = new URI("http://bar");
  207. URI newUri = new UriBuilderImpl(uri).queryParam("q", "a+b%20%2B").buildFromEncoded();
  208. assertEquals("URI is not built correctly", "http://bar?q=a%2Bb%20%2B", newUri.toString());
  209. }
  210. @Test
  211. public void testQueryWithNoValue() throws Exception {
  212. URI uri = new URI("http://bar");
  213. URI newUri = new UriBuilderImpl(uri).queryParam("q").build();
  214. assertEquals("URI is not built correctly", "http://bar?q", newUri.toString());
  215. }
  216. @Test
  217. public void testMatrixWithNoValue() throws Exception {
  218. URI uri = new URI("http://bar/foo");
  219. URI newUri = new UriBuilderImpl(uri).matrixParam("q").build();
  220. assertEquals("URI is not built correctly", "http://bar/foo;q", newUri.toString());
  221. }
  222. @Test
  223. public void testSchemeSpecificPart() throws Exception {
  224. URI uri = new URI("http://bar");
  225. URI newUri = new UriBuilderImpl(uri).scheme("https").schemeSpecificPart("//localhost:8080/foo/bar")
  226. .build();
  227. assertEquals("URI is not built correctly", "https://localhost:8080/foo/bar", newUri.toString());
  228. }
  229. @Test
  230. public void testOpaqueSchemeSpecificPart() throws Exception {
  231. URI expectedUri = new URI("mailto:javanet@java.net.com");
  232. URI newUri = new UriBuilderImpl().scheme("mailto")
  233. .schemeSpecificPart("javanet@java.net.com").build();
  234. assertEquals("URI is not built correctly", expectedUri, newUri);
  235. }
  236. @Test
  237. public void testReplacePath() throws Exception {
  238. URI uri = new URI("http://foo/bar/baz;m1=m1value");
  239. URI newUri = new UriBuilderImpl(uri).replacePath("/newpath").build();
  240. assertEquals("URI is not built correctly", "http://foo/newpath", newUri.toString());
  241. }
  242. @Test
  243. public void testReplaceNullPath() throws Exception {
  244. URI uri = new URI("http://foo/bar/baz;m1=m1value");
  245. URI newUri = new UriBuilderImpl(uri).replacePath(null).build();
  246. assertEquals("URI is not built correctly", "http://foo", newUri.toString());
  247. }
  248. @Test(expected = IllegalArgumentException.class)
  249. public void testUriNull() throws Exception {
  250. new UriBuilderImpl().uri(null);
  251. }
  252. @Test
  253. public void testUri() throws Exception {
  254. URI uri = new URI("http://foo/bar/baz?query=1#fragment");
  255. URI newUri = new UriBuilderImpl().uri(uri).build();
  256. assertEquals("URI is not built correctly", uri, newUri);
  257. }
  258. @Test
  259. public void testBuildValues() throws Exception {
  260. URI uri = new URI("http://zzz");
  261. URI newUri = new UriBuilderImpl(uri).path("/{b}/{a}/{b}").build("foo", "bar", "baz");
  262. assertEquals("URI is not built correctly", new URI("http://zzz/foo/bar/foo"), newUri);
  263. }
  264. @Test(expected = IllegalArgumentException.class)
  265. public void testBuildMissingValues() throws Exception {
  266. URI uri = new URI("http://zzz");
  267. new UriBuilderImpl(uri).path("/{b}/{a}/{b}").build("foo");
  268. }
  269. @Test(expected = IllegalArgumentException.class)
  270. public void testBuildMissingValues2() throws Exception {
  271. URI uri = new URI("http://zzz");
  272. new UriBuilderImpl(uri).path("/{b}").build();
  273. }
  274. @Test
  275. public void testBuildValueWithBrackets() throws Exception {
  276. URI uri = new URI("http://zzz");
  277. URI newUri = new UriBuilderImpl(uri).path("/{a}").build("{foo}");
  278. assertEquals("URI is not built correctly", new URI("http://zzz/%7Bfoo%7D"), newUri);
  279. }
  280. @Test
  281. public void testBuildValuesPct() throws Exception {
  282. URI uri = new URI("http://zzz");
  283. URI newUri = new UriBuilderImpl(uri).path("/{a}").build("foo%25/bar%");
  284. assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525/bar%25"), newUri);
  285. }
  286. @Test
  287. public void testBuildValuesPctEncoded() throws Exception {
  288. URI uri = new URI("http://zzz");
  289. URI newUri = new UriBuilderImpl(uri).path("/{a}/{b}/{c}")
  290. .buildFromEncoded("foo%25", "bar%", "baz%20");
  291. assertEquals("URI is not built correctly", new URI("http://zzz/foo%25/bar%25/baz%20"), newUri);
  292. }
  293. @Test
  294. public void testBuildFromMapValues() throws Exception {
  295. URI uri = new URI("http://zzz");
  296. Map<String, String> map = new HashMap<String, String>();
  297. map.put("b", "foo");
  298. map.put("a", "bar");
  299. Map<String, String> immutable = Collections.unmodifiableMap(map);
  300. URI newUri = new UriBuilderImpl(uri).path("/{b}/{a}/{b}").buildFromMap(immutable);
  301. assertEquals("URI is not built correctly", new URI("http://zzz/foo/bar/foo"), newUri);
  302. }
  303. @Test(expected = IllegalArgumentException.class)
  304. public void testBuildFromMapMissingValues() throws Exception {
  305. URI uri = new URI("http://zzz");
  306. Map<String, String> map = new HashMap<String, String>();
  307. map.put("b", "foo");
  308. Map<String, String> immutable = Collections.unmodifiableMap(map);
  309. new UriBuilderImpl(uri).path("/{b}/{a}/{b}").buildFromMap(immutable);
  310. }
  311. @Test
  312. public void testBuildFromMapValueWithBrackets() throws Exception {
  313. URI uri = new URI("http://zzz");
  314. Map<String, String> map = new HashMap<String, String>();
  315. map.put("a", "{foo}");
  316. Map<String, String> immutable = Collections.unmodifiableMap(map);
  317. URI newUri = new UriBuilderImpl(uri).path("/{a}").buildFromMap(immutable);
  318. assertEquals("URI is not built correctly", new URI("http://zzz/%7Bfoo%7D"), newUri);
  319. }
  320. @Test
  321. public void testBuildFromMapValuesPct() throws Exception {
  322. URI uri = new URI("http://zzz");
  323. Map<String, String> map = new HashMap<String, String>();
  324. map.put("a", "foo%25/bar%");
  325. Map<String, String> immutable = Collections.unmodifiableMap(map);
  326. URI newUri = new UriBuilderImpl(uri).path("/{a}").buildFromMap(immutable);
  327. assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525/bar%25"), newUri);
  328. }
  329. @Test
  330. public void testBuildFromMapValuesPctEncoded() throws Exception {
  331. URI uri = new URI("http://zzz");
  332. Map<String, String> map = new HashMap<String, String>();
  333. map.put("a", "foo%25");
  334. map.put("b", "bar%");
  335. Map<String, String> immutable = Collections.unmodifiableMap(map);
  336. URI newUri = new UriBuilderImpl(uri).path("/{a}/{b}").buildFromEncodedMap(immutable);
  337. assertEquals("URI is not built correctly", new URI("http://zzz/foo%25/bar%25"), newUri);
  338. }
  339. @Test
  340. public void testBuildFromEncodedMapComplex() throws Exception {
  341. Map<String, Object> maps = new HashMap<String, Object>();
  342. maps.put("x", "x%20yz");
  343. maps.put("y", "/path-absolute/%test1");
  344. maps.put("z", "fred@example.com");
  345. maps.put("w", "path-rootless/test2");
  346. String expectedPath =
  347. "path-rootless/test2/x%20yz//path-absolute/%25test1/fred@example.com/x%20yz";
  348. URI uri = UriBuilder.fromPath("").path("{w}/{x}/{y}/{z}/{x}")
  349. .buildFromEncodedMap(maps);
  350. String rawPath = uri.getRawPath();
  351. assertEquals(expectedPath, rawPath);
  352. }
  353. @Test
  354. public void testBuildFromEncodedMapComplex2() throws Exception {
  355. Map<String, Object> maps = new HashMap<String, Object>();
  356. maps.put("x", "x%yz");
  357. maps.put("y", "/path-absolute/test1");
  358. maps.put("z", "fred@example.com");
  359. maps.put("w", "path-rootless/test2");
  360. maps.put("u", "extra");
  361. String expectedPath =
  362. "path-rootless/test2/x%25yz//path-absolute/test1/fred@example.com/x%25yz";
  363. URI uri = UriBuilder.fromPath("").path("{w}/{x}/{y}/{z}/{x}")
  364. .buildFromEncodedMap(maps);
  365. String rawPath = uri.getRawPath();
  366. assertEquals(expectedPath, rawPath);
  367. }
  368. @Test
  369. public void testBuildFromEncodedMapMultipleTimes() throws Exception {
  370. Map<String, Object> maps = new HashMap<String, Object>();
  371. maps.put("x", "x%yz");
  372. maps.put("y", "/path-absolute/test1");
  373. maps.put("z", "fred@example.com");
  374. maps.put("w", "path-rootless/test2");
  375. Map<String, Object> maps1 = new HashMap<String, Object>();
  376. maps1.put("x", "x%20yz");
  377. maps1.put("y", "/path-absolute/test1");
  378. maps1.put("z", "fred@example.com");
  379. maps1.put("w", "path-rootless/test2");
  380. Map<String, Object> maps2 = new HashMap<String, Object>();
  381. maps2.put("x", "x%yz");
  382. maps2.put("y", "/path-absolute/test1");
  383. maps2.put("z", "fred@example.com");
  384. maps2.put("w", "path-rootless/test2");
  385. maps2.put("v", "xyz");
  386. String expectedPath =
  387. "path-rootless/test2/x%25yz//path-absolute/test1/fred@example.com/x%25yz";
  388. String expectedPath1 =
  389. "path-rootless/test2/x%20yz//path-absolute/test1/fred@example.com/x%20yz";
  390. String expectedPath2 =
  391. "path-rootless/test2/x%25yz//path-absolute/test1/fred@example.com/x%25yz";
  392. UriBuilder ub = UriBuilder.fromPath("").path("{w}/{x}/{y}/{z}/{x}");
  393. URI uri = ub.buildFromEncodedMap(maps);
  394. assertEquals(expectedPath, uri.getRawPath());
  395. uri = ub.buildFromEncodedMap(maps1);
  396. assertEquals(expectedPath1, uri.getRawPath());
  397. uri = ub.buildFromEncodedMap(maps2);
  398. assertEquals(expectedPath2, uri.getRawPath());
  399. }
  400. @Test(expected = IllegalArgumentException.class)
  401. public void testBuildFromEncodedMapWithNullValue() throws Exception {
  402. Map<String, Object> maps = new HashMap<String, Object>();
  403. maps.put("x", null);
  404. maps.put("y", "bar");
  405. UriBuilder.fromPath("").path("{x}/{y}").buildFromEncodedMap(maps);
  406. }
  407. @Test
  408. public void testAddPath() throws Exception {
  409. URI uri = new URI("http://foo/bar");
  410. URI newUri = new UriBuilderImpl().uri(uri).path("baz").build();
  411. assertEquals("URI is not built correctly", new URI("http://foo/bar/baz"), newUri);
  412. newUri = new UriBuilderImpl().uri(uri).path("baz").path("1").path("2").build();
  413. assertEquals("URI is not built correctly", new URI("http://foo/bar/baz/1/2"), newUri);
  414. }
  415. @Test
  416. public void testAddPathSlashes() throws Exception {
  417. URI uri = new URI("http://foo/");
  418. URI newUri = new UriBuilderImpl().uri(uri).path("/bar").path("baz/").path("/blah/").build();
  419. assertEquals("URI is not built correctly", new URI("http://foo/bar/baz/blah/"), newUri);
  420. }
  421. @Test
  422. public void testAddPathSlashes2() throws Exception {
  423. URI uri = new URI("http://foo/");
  424. URI newUri = new UriBuilderImpl().uri(uri).path("/bar///baz").path("blah//").build();
  425. assertEquals("URI is not built correctly", new URI("http://foo/bar/baz/blah/"), newUri);
  426. }
  427. @Test
  428. public void testAddPathSlashes3() throws Exception {
  429. URI uri = new URI("http://foo/");
  430. URI newUri = new UriBuilderImpl().uri(uri).path("/bar/").path("").path("baz").build();
  431. assertEquals("URI is not built correctly", new URI("http://foo/bar/baz"), newUri);
  432. }
  433. @Test
  434. public void testAddPathClass() throws Exception {
  435. URI uri = new URI("http://foo/");
  436. URI newUri = new UriBuilderImpl().uri(uri).path(BookStore.class).path("/").build();
  437. assertEquals("URI is not built correctly", new URI("http://foo/bookstore/"), newUri);
  438. }
  439. @Test(expected = IllegalArgumentException.class)
  440. public void testAddPathClassNull() throws Exception {
  441. new UriBuilderImpl().path((Class)null).build();
  442. }
  443. @Test(expected = IllegalArgumentException.class)
  444. public void testAddPathClassNoAnnotation() throws Exception {
  445. new UriBuilderImpl().path(this.getClass()).build();
  446. }
  447. @Test
  448. public void testAddPathClassMethod() throws Exception {
  449. URI uri = new URI("http://foo/");
  450. URI newUri = new UriBuilderImpl().uri(uri).path(BookStore.class, "updateBook").path("bar").build();
  451. assertEquals("URI is not built correctly", new URI("http://foo/books/bar"), newUri);
  452. }
  453. @Test(expected = IllegalArgumentException.class)
  454. public void testAddPathClassMethodNull1() throws Exception {
  455. new UriBuilderImpl().path(null, "methName").build();
  456. }
  457. @Test(expected = IllegalArgumentException.class)
  458. public void testAddPathClassMethodNull2() throws Exception {
  459. new UriBuilderImpl().path(BookStore.class, null).build();
  460. }
  461. @Test(expected = IllegalArgumentException.class)
  462. public void testAddPathClassMethodTooMany() throws Exception {
  463. new UriBuilderImpl().path(UriBuilderWrongAnnotations.class, "overloaded").build();
  464. }
  465. @Test(expected = IllegalArgumentException.class)
  466. public void testAddPathClassMethodTooLess() throws Exception {
  467. new UriBuilderImpl().path(BookStore.class, "nonexistingMethod").build();
  468. }
  469. @Test
  470. public void testAddPathMethod() throws Exception {
  471. Method meth = BookStore.class.getMethod("updateBook", Book.class);
  472. URI uri = new URI("http://foo/");
  473. URI newUri = new UriBuilderImpl().uri(uri).path(meth).path("bar").build();
  474. assertEquals("URI is not built correctly", new URI("http://foo/books/bar"), newUri);
  475. }
  476. @Test(expected = IllegalArgumentException.class)
  477. public void testAddPathMethodNull() throws Exception {
  478. new UriBuilderImpl().path((Method)null).build();
  479. }
  480. @Test(expected = IllegalArgumentException.class)
  481. public void testAddPathMethodNoAnnotation() throws Exception {
  482. Method noAnnot = BookStore.class.getMethod("getBook", String.class);
  483. new UriBuilderImpl().path(noAnnot).build();
  484. }
  485. @Test
  486. public void testSchemeHostPortQueryFragment() throws Exception {
  487. URI uri = new URI("http://foo:1234/bar?n1=v1&n2=v2#fragment");
  488. URI newUri = new UriBuilderImpl().scheme("http").host("foo").port(1234).path("bar").queryParam("n1",
  489. "v1")
  490. .queryParam("n2", "v2").fragment("fragment").build();
  491. compareURIs(uri, newUri);
  492. }
  493. @Test
  494. public void testReplaceQueryNull() throws Exception {
  495. URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
  496. URI newUri = new UriBuilderImpl(uri).replaceQuery(null).build();
  497. assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
  498. }
  499. @Test
  500. public void testReplaceQueryWithNull2() {
  501. String expected = "http://localhost:8080";
  502. URI uri = UriBuilder.fromPath("http://localhost:8080")
  503. .queryParam("name", "x=", "y?", "x y", "&").replaceQuery(null).build();
  504. assertEquals(expected, uri.toString());
  505. }
  506. @Test
  507. public void testReplaceQueryEmpty() throws Exception {
  508. URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
  509. URI newUri = new UriBuilderImpl(uri).replaceQuery("").build();
  510. assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
  511. }
  512. @Test
  513. public void testReplaceQuery() throws Exception {
  514. URI uri = new URI("http://foo/bar?p1=v1");
  515. URI newUri = new UriBuilderImpl(uri).replaceQuery("p1=nv1").build();
  516. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
  517. }
  518. @Test
  519. public void testReplaceQuery2() throws Exception {
  520. URI uri = new URI("http://foo/bar");
  521. URI newUri = new UriBuilderImpl(uri).replaceQuery("p1=nv1").build();
  522. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
  523. }
  524. @Test
  525. public void testReplaceQuery3() {
  526. String expected = "http://localhost:8080?name1=xyz";
  527. URI uri = UriBuilder.fromPath("http://localhost:8080")
  528. .queryParam("name", "x=", "y?", "x y", "&").replaceQuery("name1=xyz").build();
  529. assertEquals(expected, uri.toString());
  530. }
  531. @Test(expected = IllegalArgumentException.class)
  532. public void testQueryParamNameNull() throws Exception {
  533. new UriBuilderImpl().queryParam(null, "baz");
  534. }
  535. @Test(expected = IllegalArgumentException.class)
  536. public void testQueryParamNullVal() throws Exception {
  537. new UriBuilderImpl().queryParam("foo", "bar", null, "baz");
  538. }
  539. @Test
  540. public void testNullQueryParamValues() {
  541. try {
  542. UriBuilder.fromPath("http://localhost:8080").queryParam("hello", (Object[])null);
  543. fail("Should be IllegalArgumentException");
  544. } catch (IllegalArgumentException ex) {
  545. //expected
  546. }
  547. }
  548. @Test
  549. public void testQueryParamSameNameAndVal() throws Exception {
  550. URI uri = new URI("http://foo/bar?p1=v1");
  551. URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v1").build();
  552. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v1"), newUri);
  553. }
  554. @Test
  555. public void testQueryParamVal() throws Exception {
  556. URI uri = new URI("http://foo/bar?p1=v1");
  557. URI newUri = new UriBuilderImpl(uri).queryParam("p2", "v2").build();
  558. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p2=v2"), newUri);
  559. }
  560. @Test
  561. public void testQueryParamSameNameDiffVal() throws Exception {
  562. URI uri = new URI("http://foo/bar?p1=v1");
  563. URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v2").build();
  564. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v2"), newUri);
  565. }
  566. @Test
  567. public void testQueryParamMultiVal() throws Exception {
  568. URI uri = new URI("http://foo/bar?p1=v1");
  569. URI newUri = new UriBuilderImpl(uri).queryParam("p1", "v2", "v3").build();
  570. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=v1&p1=v2&p1=v3"), newUri);
  571. }
  572. @Test(expected = IllegalArgumentException.class)
  573. public void testReplaceQueryParamNameNull() throws Exception {
  574. new UriBuilderImpl().replaceQueryParam(null, "baz");
  575. }
  576. @Test
  577. public void testReplaceQueryParamValNull() throws Exception {
  578. URI uri = new URI("http://foo/bar?p1=v1&p2=v2&p1=v3");
  579. URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", (Object)null).build();
  580. assertEquals("URI is not built correctly", new URI("http://foo/bar?p2=v2"), newUri);
  581. }
  582. @Test
  583. public void testReplaceQueryParamValEmpty() throws Exception {
  584. URI uri = new URI("http://foo/bar?p1=v1&p2=v2&p1=v3");
  585. URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1").build();
  586. assertEquals("URI is not built correctly", new URI("http://foo/bar?p2=v2"), newUri);
  587. }
  588. @Test
  589. public void testReplaceQueryParamExisting() throws Exception {
  590. URI uri = new URI("http://foo/bar?p1=v1");
  591. URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", "nv1").build();
  592. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1"), newUri);
  593. }
  594. @Test
  595. public void testReplaceQueryParamExistingMulti() throws Exception {
  596. URI uri = new URI("http://foo/bar?p1=v1&p2=v2");
  597. URI newUri = new UriBuilderImpl(uri).replaceQueryParam("p1", "nv1", "nv2").build();
  598. assertEquals("URI is not built correctly", new URI("http://foo/bar?p1=nv1&p1=nv2&p2=v2"), newUri);
  599. }
  600. @Test
  601. public void testReplaceMatrixNull() throws Exception {
  602. URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
  603. URI newUri = new UriBuilderImpl(uri).replaceMatrix(null).build();
  604. assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
  605. }
  606. @Test
  607. public void testReplaceMatrixEmpty() throws Exception {
  608. URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
  609. URI newUri = new UriBuilderImpl(uri).replaceMatrix("").build();
  610. assertEquals("URI is not built correctly", new URI("http://foo/bar"), newUri);
  611. }
  612. @Test
  613. public void testReplaceMatrix() throws Exception {
  614. URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
  615. URI newUri = new UriBuilderImpl(uri).replaceMatrix("p1=nv1").build();
  616. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1"), newUri);
  617. }
  618. @Test
  619. public void testReplaceMatrix2() throws Exception {
  620. URI uri = new URI("http://foo/bar/");
  621. URI newUri = new UriBuilderImpl(uri).replaceMatrix("p1=nv1").build();
  622. assertEquals("URI is not built correctly", new URI("http://foo/bar/;p1=nv1"), newUri);
  623. }
  624. @Test(expected = IllegalArgumentException.class)
  625. public void testMatrixParamNameNull() throws Exception {
  626. new UriBuilderImpl().matrixParam(null, "baz");
  627. }
  628. @Test(expected = IllegalArgumentException.class)
  629. public void testMatrixParamNullVal() throws Exception {
  630. new UriBuilderImpl().matrixParam("foo", "bar", null, "baz");
  631. }
  632. @Test
  633. public void testMatrixParamSameNameAndVal() throws Exception {
  634. URI uri = new URI("http://foo/bar;p1=v1");
  635. URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v1").build();
  636. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v1"), newUri);
  637. }
  638. @Test
  639. public void testMatrixParamNewNameAndVal() throws Exception {
  640. URI uri = new URI("http://foo/bar;p1=v1");
  641. URI newUri = new UriBuilderImpl(uri).matrixParam("p2", "v2").build();
  642. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p2=v2"), newUri);
  643. }
  644. @Test
  645. public void testMatrixParamSameNameDiffVal() throws Exception {
  646. URI uri = new URI("http://foo/bar;p1=v1");
  647. URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v2").build();
  648. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v2"), newUri);
  649. }
  650. @Test
  651. public void testMatrixParamMultiSameNameNewVals() throws Exception {
  652. URI uri = new URI("http://foo/bar;p1=v1");
  653. URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v2", "v3").build();
  654. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v2;p1=v3"), newUri);
  655. }
  656. @Test
  657. public void testPctEncodedMatrixParam() throws Exception {
  658. URI uri = new URI("http://foo/bar");
  659. URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v1%20").buildFromEncoded();
  660. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1%20"), newUri);
  661. }
  662. @Test(expected = IllegalArgumentException.class)
  663. public void testReplaceMatrixParamNameNull() throws Exception {
  664. new UriBuilderImpl().replaceMatrixParam(null, "baz");
  665. }
  666. @Test
  667. public void testReplaceMatrixParamValNull() throws Exception {
  668. URI uri = new URI("http://foo/bar;p1=v1;p2=v2;p1=v3?noise=bazzz");
  669. URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", (Object)null).build();
  670. assertEquals("URI is not built correctly", new URI("http://foo/bar;p2=v2?noise=bazzz"), newUri);
  671. }
  672. @Test
  673. public void testReplaceMatrixParamValEmpty() throws Exception {
  674. URI uri = new URI("http://foo/bar;p1=v1;p2=v2;p1=v3?noise=bazzz");
  675. URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1").build();
  676. assertEquals("URI is not built correctly", new URI("http://foo/bar;p2=v2?noise=bazzz"), newUri);
  677. }
  678. @Test
  679. public void testReplaceMatrixParamExisting() throws Exception {
  680. URI uri = new URI("http://foo/bar;p1=v1");
  681. URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", "nv1").build();
  682. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1"), newUri);
  683. }
  684. @Test
  685. public void testReplaceMatrixParamExistingMulti() throws Exception {
  686. URI uri = new URI("http://foo/bar;p1=v1;p2=v2");
  687. URI newUri = new UriBuilderImpl(uri).replaceMatrixParam("p1", "nv1", "nv2").build();
  688. assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=nv1;p1=nv2;p2=v2"), newUri);
  689. }
  690. @Test
  691. public void testMatrixNonFinalPathSegment() throws Exception {
  692. URI uri = new URI("http://blah/foo;p1=v1/bar");
  693. URI newUri = new UriBuilderImpl(uri).build();
  694. assertEquals("URI is not built correctly", new URI("http://blah/foo;p1=v1/bar"), newUri);
  695. }
  696. @Test
  697. public void testMatrixFinalPathSegment() throws Exception {
  698. URI uri = new URI("http://blah/foo;p1=v1/bar;p2=v2");
  699. URI newUri = new UriBuilderImpl(uri).build();
  700. assertEquals("URI is not built correctly", new URI("http://blah/foo;p1=v1/bar;p2=v2"), newUri);
  701. }
  702. @Test
  703. public void testAddPathWithMatrix() throws Exception {
  704. URI uri = new URI("http://blah/foo/bar;p1=v1");
  705. URI newUri = new UriBuilderImpl(uri).path("baz;p2=v2").build();
  706. assertEquals("URI is not built correctly", new URI("http://blah/foo/bar;p1=v1/baz;p2=v2"), newUri);
  707. }
  708. @Test
  709. public void testNonHttpSchemes() {
  710. String[] uris = {"ftp://ftp.is.co.za/rfc/rfc1808.txt",
  711. "mailto:java-net@java.sun.com",
  712. "news:comp.lang.java",
  713. "urn:isbn:096139212y",
  714. "ldap://[2001:db8::7]/c=GB?objectClass?one",
  715. "telnet://194.1.2.17:81/",
  716. "tel:+1-816-555-1212",
  717. "foo://bar.com:8042/there/here?name=baz#brr"};
  718. int expectedCount = 0;
  719. for (int i = 0; i < uris.length; i++) {
  720. URI uri = UriBuilder.fromUri(uris[i]).build();
  721. assertEquals("Strange", uri.toString(), uris[i]);
  722. expectedCount++;
  723. }
  724. assertEquals(8, expectedCount);
  725. }
  726. private void compareURIs(URI uri1, URI uri2) {
  727. assertEquals("Unexpected scheme", uri1.getScheme(), uri2.getScheme());
  728. assertEquals("Unexpected host", uri1.getHost(), uri2.getHost());
  729. assertEquals("Unexpected port", uri1.getPort(), uri2.getPort());
  730. assertEquals("Unexpected path", uri1.getPath(), uri2.getPath());
  731. assertEquals("Unexpected fragment", uri1.getFragment(), uri2.getFragment());
  732. MultivaluedMap<String, String> queries1 =
  733. JAXRSUtils.getStructuredParams(uri1.getRawQuery(), "&", false, false);
  734. MultivaluedMap<String, String> queries2 =
  735. JAXRSUtils.getStructuredParams(uri2.getRawQuery(), "&", false, false);
  736. assertEquals("Unexpected queries", queries1, queries2);
  737. }
  738. @Test
  739. public void testTck1() {
  740. String value = "test1#test2";
  741. String expected = "test1%23test2";
  742. String path = "{arg1}";
  743. URI uri = UriBuilder.fromPath(path).build(value);
  744. assertEquals(expected, uri.toString());
  745. }
  746. @Test
  747. public void testNullPathValue() {
  748. String value = null;
  749. String path = "{arg1}";
  750. try {
  751. UriBuilder.fromPath(path).build(value);
  752. fail("Should be IllegalArgumentException");
  753. } catch (IllegalArgumentException ex) {
  754. //expected
  755. }
  756. }
  757. @Test
  758. public void testFragment() {
  759. String expected = "test#abc";
  760. String path = "test";
  761. URI uri = UriBuilder.fromPath(path).fragment("abc").build();
  762. assertEquals(expected, uri.toString());
  763. }
  764. @Test
  765. public void testFragmentTemplate() {
  766. String expected = "abc#xyz";
  767. URI uri = UriBuilder
  768. .fromPath("{arg1}")
  769. .fragment("{arg2}")
  770. .build("abc", "xyz");
  771. assertEquals(expected, uri.toString());
  772. }
  773. @Test
  774. public void testSegments() {
  775. String path1 = "ab";
  776. String[] path2 = {"a1", "x/y", "3b "};
  777. String expected = "ab/a1/x%2Fy/3b%20";
  778. URI uri = UriBuilder.fromPath(path1).segment(path2).build();
  779. assertEquals(expected, uri.toString());
  780. }
  781. @Test
  782. public void testSegments2() {
  783. String path1 = "";
  784. String[] path2 = {"a1", "/", "3b "};
  785. String expected = "a1/%2F/3b%20";
  786. URI uri = UriBuilder.fromPath(path1).segment(path2).build();
  787. assertEquals(expected, uri.toString());
  788. }
  789. @Test
  790. public void testNullSegment() {
  791. try {
  792. UriBuilder.fromPath("/").segment((String)null).build();
  793. fail("Should be IllegalArgumentException");
  794. } catch (IllegalArgumentException ex) {
  795. //expected
  796. }
  797. }
  798. @Test
  799. public void testInvalidPort() {
  800. try {
  801. UriBuilder.fromUri("http://localhost:8080/some/path?name=foo").port(-10).build();
  802. fail("Should be IllegalArgumentException");
  803. } catch (IllegalArgumentException ex) {
  804. //expected
  805. }
  806. }
  807. @Test
  808. public void testResetPort() {
  809. URI uri = UriBuilder.fromUri("http://localhost:8080/some/path").port(-1).build();
  810. assertEquals("http://localhost/some/path", uri.toString());
  811. }
  812. @Test
  813. public void testInvalidHost() {
  814. try {
  815. UriBuilder.fromUri("http://localhost:8080/some/path?name=foo").host("").build();
  816. fail("Should be IllegalArgumentException");
  817. } catch (IllegalArgumentException ex) {
  818. //expected
  819. }
  820. }
  821. @Test
  822. public void testFromEncodedDuplicateVar2() {
  823. String expected = "http://localhost:8080/xy/%20/%25/xy";
  824. URI uri = UriBuilder.fromPath("http://localhost:8080")
  825. .path("/{x}/{y}/{z}/{x}")
  826. .buildFromEncoded("xy", " ", "%");
  827. assertEquals(expected, uri.toString());
  828. }
  829. @Test
  830. public void testFromEncodedDuplicateVarReplacePath() {
  831. String expected = "http://localhost:8080/1/2/3/1";
  832. URI uri = UriBuilder.fromPath("")
  833. .replacePath("http://localhost:8080")
  834. .path("/{a}/{b}/{c}/{a}")
  835. .buildFromEncoded("1", "2", "3");
  836. assertEquals(expected, uri.toString());
  837. }
  838. @Test
  839. public void testNullScheme() {
  840. String expected = "//localhost:8080";
  841. URI uri = UriBuilder.fromUri("http://localhost:8080")
  842. .scheme(null)
  843. .build();
  844. assertEquals(expected, uri.toString());
  845. }
  846. @Test
  847. public void testNullMapValue() {
  848. try {
  849. Map<String, String> maps = new HashMap<String, String>();
  850. maps.put("x", null);
  851. maps.put("y", "/path-absolute/test1");
  852. maps.put("z", "fred@example.com");
  853. maps.put("w", "path-rootless/test2");
  854. maps.put("u", "extra");
  855. URI uri = UriBuilder.fromPath("")
  856. .path("{w}/{x}/{y}/{z}/{x}")
  857. .buildFromMap(maps);
  858. fail("Should be IllegalArgumentException. Not return " + uri.toString());
  859. } catch (IllegalArgumentException ex) {
  860. //expected
  861. }
  862. }
  863. @Test
  864. public void testMissingMapValue() {
  865. try {
  866. Map<String, String> maps = new HashMap<String, String>();
  867. maps.put("x", null);
  868. maps.put("y", "/path-absolute/test1");
  869. maps.put("z", "fred@example.com");
  870. maps.put("w", "path-rootless/test2");
  871. maps.put("u", "extra");
  872. URI uri = UriBuilder.fromPath("")
  873. .path("{w}/{v}/{x}/{y}/{z}/{x}")
  874. .buildFromMap(maps);
  875. fail("Should be IllegalArgumentException. Not return " + uri.toString());
  876. } catch (IllegalArgumentException ex) {
  877. //expected
  878. }
  879. }
  880. @Test
  881. public void testFromEncodedDuplicateVar() {
  882. String expected = "http://localhost:8080/a/%25/=/%25G0/%25/=";
  883. URI uri = UriBuilder.fromPath("http://localhost:8080")
  884. .path("/{v}/{w}/{x}/{y}/{z}/{x}")
  885. .buildFromEncoded("a", "%25", "=", "%G0", "%", "23");
  886. assertEquals(expected, uri.toString());
  887. }
  888. @Test
  889. public void testMultipleUriSchemes() throws Exception {
  890. URI uri;
  891. String[] urisOriginal = {
  892. "ftp://ftp.is.co.za/rfc/rfc1808.txt",
  893. "ftp://ftp.is.co.za/rfc/rfc1808.txt",
  894. "mailto:java-net@java.sun.com",
  895. "mailto:java-net@java.sun.com",
  896. "news:comp.lang.java",
  897. "news:comp.lang.java",
  898. "urn:isbn:096139210x",
  899. "http://www.ietf.org/rfc/rfc2396.txt",
  900. "http://www.ietf.org/rfc/rfc2396.txt",
  901. "ldap://[2001:db8::7]/c=GB?objectClass?one",
  902. "ldap://[2001:db8::7]/c=GB?objectClass?one",
  903. "tel:+1-816-555-1212",
  904. "tel:+1-816-555-1212",
  905. "telnet://192.0.2.16:80/",
  906. "telnet://192.0.2.16:80/",
  907. "foo://example.com:8042/over/there?name=ferret#nose",
  908. "foo://example.com:8042/over/there?name=ferret#nose"
  909. };
  910. URI urisReplace[] = new URI[urisOriginal.length];
  911. urisReplace[0] = new URI("http", "//ftp.is.co.za/rfc/rfc1808.txt",
  912. null);
  913. urisReplace[1] = new URI(null, "ftp.is.co.za",
  914. "/test/rfc1808.txt", null, null);
  915. urisReplace[2] = new URI("mailto", "java-net@java.sun.com", null);
  916. urisReplace[3] = new URI(null, "testuser@sun.com", null);
  917. urisReplace[4] = new URI("http", "//comp.lang.java", null);
  918. urisReplace[5] = new URI(null, "news.lang.java", null);
  919. urisReplace[6] = new URI("urn:isbn:096139210x");
  920. urisReplace[7] = new URI(null, "//www.ietf.org/rfc/rfc2396.txt",
  921. null);
  922. urisReplace[8] = new URI(null, "www.ietf.org", "/rfc/rfc2396.txt", null,
  923. null);
  924. urisReplace[9] =
  925. new URI("ldap", "//[2001:db8::7]/c=GB?objectClass?one", null);
  926. urisReplace[10] =
  927. new URI(null, "//[2001:db8::7]/c=GB?objectClass?one", null);
  928. urisReplace[11] = new URI("tel", "+1-816-555-1212", null);
  929. urisReplace[12] = new URI(null, "+1-866-555-1212", null);
  930. urisReplace[13] = new URI("telnet", "//192.0.2.16:80/", null);
  931. urisReplace[14] = new URI(null, "//192.0.2.16:81/", null);
  932. urisReplace[15] =
  933. new URI("http", "//example.com:8042/over/there?name=ferret",
  934. null);
  935. urisReplace[16] =
  936. new URI(null, "//example.com:8042/over/there?name=ferret",
  937. "mouth");
  938. String[] urisExpected = {
  939. "http://ftp.is.co.za/rfc/rfc1808.txt",
  940. "ftp://ftp.is.co.za/test/rfc1808.txt",
  941. "mailto:java-net@java.sun.com",
  942. "mailto:testuser@sun.com",
  943. "http://comp.lang.java",
  944. "news:news.lang.java",
  945. "urn:isbn:096139210x",
  946. "http://www.ietf.org/rfc/rfc2396.txt",
  947. "http://www.ietf.org/rfc/rfc2396.txt",
  948. "ldap://[2001:db8::7]/c=GB?objectClass?one",
  949. "ldap://[2001:db8::7]/c=GB?objectClass?one",
  950. "tel:+1-816-555-1212",
  951. "tel:+1-866-555-1212",
  952. "telnet://192.0.2.16:80/",
  953. "telnet://192.0.2.16:81/",
  954. "http://example.com:8042/over/there?name=ferret#nose",
  955. "foo://example.com:8042/over/there?name=ferret#mouth"
  956. };
  957. for (int i = 0; i < urisOriginal.length; i++) {
  958. uri = UriBuilder.fromUri(new URI(urisOriginal[i])).uri(urisReplace[i]).
  959. build();
  960. if (uri.toString().trim().compareToIgnoreCase(urisExpected[i]) != 0) {
  961. fail("Problem replacing " + urisOriginal[i] + " with " + urisReplace[i]
  962. + ", index " + i);
  963. }
  964. }
  965. }
  966. @Test
  967. public void testEncodingQueryParamFromBuild() throws Exception {
  968. String expectedValue =
  969. "http://localhost:8080?name=x%3D&name=y?&name=x+y&name=%26";
  970. URI uri = UriBuilder.fromPath("http://localhost:8080").queryParam("name",
  971. "x=", "y?", "x y", "&").build();
  972. assertEquals(expectedValue, uri.toString());
  973. }
  974. @Test
  975. public void testReplaceParamAndEncodeQueryParamFromBuild() throws Exception {
  976. String expectedValue =
  977. "http://localhost:8080?name=x&name=y&name=y+x&name=x%25y&name=%20";
  978. URI uri = UriBuilder.fromPath("http://localhost:8080").queryParam("name",
  979. "x=", "y?", "x y", "&").replaceQueryParam("name", "x", "y",
  980. "y x", "x%y", "%20").build();
  981. assertEquals(expectedValue, uri.toString());
  982. }
  983. @Test
  984. public void testReplaceStringAndEncodeQueryParamFromBuild() {
  985. String expected = "http://localhost:8080?name1=x&name2=%20&name3=x+y&name4=23&name5=x%20y";
  986. URI uri = UriBuilder.fromPath("http://localhost:8080")
  987. .queryParam("name", "x=", "y?", "x y", "&")
  988. .replaceQuery("name1=x&name2=%20&name3=x+y&name4=23&name5=x y").build();
  989. assertEquals(expected, uri.toString());
  990. }
  991. @Ignore
  992. @Test
  993. public void testPathParamSpaceBuild() {
  994. String expected = "http://localhost:8080/name/%20";
  995. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name/%20").build();
  996. assertEquals(expected, uri.toString());
  997. }
  998. @Test
  999. public void testPathParamSpaceBuild2() {
  1000. String expected = "http://localhost:8080/name/%2520";
  1001. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name/{value}").build("%20");
  1002. assertEquals(expected, uri.toString());
  1003. }
  1004. @Test
  1005. public void testPathParamSpaceBuild3() {
  1006. String expected = "http://localhost:8080/name%20space";
  1007. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name space").build();
  1008. assertEquals(expected, uri.toString());
  1009. }
  1010. @Test
  1011. public void testPathParamSpaceBuild4() {
  1012. String expected = "http://localhost:8080/name%20space";
  1013. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name space").buildFromEncoded();
  1014. assertEquals(expected, uri.toString());
  1015. }
  1016. @Test
  1017. public void testPathParamSpaceBuildEncoded() {
  1018. String expected = "http://localhost:8080/name/%20";
  1019. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name/%20").buildFromEncoded();
  1020. assertEquals(expected, uri.toString());
  1021. }
  1022. @Test
  1023. public void testPathParamSpaceBuildEncoded2() {
  1024. String expected = "http://localhost:8080/name/%20";
  1025. URI uri = UriBuilder.fromUri("http://localhost:8080").path("name/{value}").buildFromEncoded("%20");
  1026. assertEquals(expected, uri.toString());
  1027. }
  1028. @Test
  1029. public void testQueryParamSpaceBuild() {
  1030. String expected = "http://localhost:8080?name=%20";
  1031. URI uri = UriBuilder.fromUri("http://localhost:8080").queryParam("name", "%20").build();
  1032. assertEquals(expected, uri.toString());
  1033. }
  1034. @Ignore
  1035. @Test
  1036. public void testQueryParamSpaceBuild2() {
  1037. String expected = "http://localhost:8080?name=%2520";
  1038. URI uri = UriBuilder.fromUri("http://localhost:8080").queryParam("name", "{value}").build("%20");
  1039. assertEquals(expected, uri.toString());
  1040. }
  1041. }