1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.apache.commons.httpclient;
33
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38 import org.apache.commons.httpclient.util.URIUtil;
39
40 /***
41 *
42 * Unit tests for {@link URIUtil}. These tests care currently quite limited
43 * and should be expanded to test more functionality.
44 *
45 * @author Marc A. Saegesser
46 * @version $Id: TestURIUtil.java 608014 2008-01-02 05:48:53Z rolandw $
47 */
48 public class TestURIUtil extends TestCase {
49
50 URITestCase pathTests[] = {new URITestCase("http://www.server.com/path1/path2", "/path1/path2"),
51 new URITestCase("http://www.server.com/path1/path2/", "/path1/path2/"),
52 new URITestCase("http://www.server.com/path1/path2?query=string", "/path1/path2"),
53 new URITestCase("http://www.server.com/path1/path2/?query=string", "/path1/path2/"),
54 new URITestCase("www.noscheme.com/path1/path2", "/path1/path2"),
55 new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "/path1/path2"),
56 new URITestCase("/noscheme/nohost/path", "/noscheme/nohost/path"),
57 new URITestCase("http://www.server.com", "/"),
58 new URITestCase("https://www.server.com:443/ssl/path", "/ssl/path"),
59 new URITestCase("http://www.server.com:8080/path/with/port", "/path/with/port"),
60 new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "/path1/path2")};
61
62 URITestCase queryTests[] = {new URITestCase("http://www.server.com/path1/path2", null),
63 new URITestCase("http://www.server.com/path1/path2?query=string", "query=string"),
64 new URITestCase("http://www.server.com/path1/path2/?query=string", "query=string"),
65 new URITestCase("www.noscheme.com/path1/path2#anchor?query=string", "query=string"),
66 new URITestCase("/noscheme/nohost/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
67 new URITestCase("https://www.server.com:443/ssl/path?query1=string1&query2=string2", "query1=string1&query2=string2"),
68 new URITestCase("http://www.server.com:8080/path/with/port?query1=string1&query2=string2", "query1=string1&query2=string2"),
69 new URITestCase("http://www.server.com/path1/path2?query1=string?1&query2=string2", "query1=string?1&query2=string2")};
70
71
72
73
74 public TestURIUtil(String testName) {
75 super(testName);
76 }
77
78
79 public static void main(String args[]) {
80 String[] testCaseName = { TestURIUtil.class.getName() };
81 junit.textui.TestRunner.main(testCaseName);
82 }
83
84
85
86 public static Test suite() {
87 return new TestSuite(TestURIUtil.class);
88 }
89
90
91
92 public void testGetPath()
93 {
94 String testValue = "";
95 String expectedResult = "";
96
97 for(int i=0;i<pathTests.length;i++){
98 testValue = pathTests[i].getTestValue();
99 expectedResult = pathTests[i].getExpectedResult();
100 assertEquals("Path test", expectedResult, URIUtil.getPath(testValue));
101 }
102 }
103
104 public void testGetQueryString()
105 {
106 String testValue = "";
107 String expectedResult = "";
108
109 for(int i=0;i<queryTests.length;i++){
110 testValue = queryTests[i].getTestValue();
111 expectedResult = queryTests[i].getExpectedResult();
112 assertEquals("Path test", expectedResult, URIUtil.getQuery(testValue));
113 }
114 }
115
116 private class URITestCase{
117 private String testValue;
118 private String expectedResult;
119
120 public URITestCase(String testValue, String expectedResult){
121 this.testValue = testValue;
122 this.expectedResult = expectedResult;
123 }
124
125 public String getTestValue(){
126 return testValue;
127 }
128
129 public String getExpectedResult(){
130 return expectedResult;
131 }
132 }
133 }