1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.commons.validator;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 /***
29 * Performs Validation Test for url validations.
30 */
31 public class UrlTest extends TestCase {
32
33 private boolean printStatus = false;
34 private boolean printIndex = false;
35
36 public UrlTest(String testName) {
37 super(testName);
38 }
39
40 public static Test suite() {
41 return new TestSuite(UrlTest.class);
42 }
43
44 protected void setUp() {
45 for (int index = 0; index < testPartsIndex.length - 1; index++) {
46 testPartsIndex[index] = 0;
47 }
48 }
49
50 protected void tearDown() {
51 }
52
53 public void testIsValid() {
54 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
55 setUp();
56 int options =
57 UrlValidator.ALLOW_2_SLASHES
58 + UrlValidator.ALLOW_ALL_SCHEMES
59 + UrlValidator.NO_FRAGMENTS;
60
61 testIsValid(testUrlPartsOptions, options);
62 }
63
64 public void testIsValidScheme() {
65 if (printStatus) {
66 System.out.print("\n testIsValidScheme() ");
67 }
68 String[] schemes = {"http", "gopher"};
69
70 UrlValidator urlVal = new UrlValidator(schemes, 0);
71 for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
72 TestPair testPair = testScheme[sIndex];
73 boolean result = urlVal.isValidScheme(testPair.item);
74 assertEquals(testPair.item, testPair.valid, result);
75 if (printStatus) {
76 if (result == testPair.valid) {
77 System.out.print('.');
78 } else {
79 System.out.print('X');
80 }
81 }
82 }
83 if (printStatus) {
84 System.out.println();
85 }
86
87 }
88
89 /***
90 * Create set of tests by taking the testUrlXXX arrays and
91 * running through all possible permutations of their combinations.
92 *
93 * @param testObjects Used to create a url.
94 */
95 public void testIsValid(Object[] testObjects, int options) {
96 UrlValidator urlVal = new UrlValidator(null, options);
97 assertTrue(urlVal.isValid("http://www.google.com"));
98 assertTrue(urlVal.isValid("http://www.google.com/"));
99 int statusPerLine = 60;
100 int printed = 0;
101 if (printIndex) {
102 statusPerLine = 6;
103 }
104 do {
105 StringBuffer testBuffer = new StringBuffer();
106 boolean expected = true;
107 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
108 int index = testPartsIndex[testPartsIndexIndex];
109 TestPair[] part = (TestPair[]) testObjects[testPartsIndexIndex];
110 testBuffer.append(part[index].item);
111 expected &= part[index].valid;
112 }
113 String url = testBuffer.toString();
114 boolean result = urlVal.isValid(url);
115 assertEquals(url, expected, result);
116 if (printStatus) {
117 if (printIndex) {
118 System.out.print(testPartsIndextoString());
119 } else {
120 if (result == expected) {
121 System.out.print('.');
122 } else {
123 System.out.print('X');
124 }
125 }
126 printed++;
127 if (printed == statusPerLine) {
128 System.out.println();
129 printed = 0;
130 }
131 }
132 } while (incrementTestPartsIndex(testPartsIndex, testObjects));
133 if (printStatus) {
134 System.out.println();
135 }
136 }
137
138 static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
139 boolean carry = true;
140 boolean maxIndex = true;
141 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
142 int index = testPartsIndex[testPartsIndexIndex];
143 TestPair[] part = (TestPair[]) testParts[testPartsIndexIndex];
144 if (carry) {
145 if (index < part.length - 1) {
146 index++;
147 testPartsIndex[testPartsIndexIndex] = index;
148 carry = false;
149 } else {
150 testPartsIndex[testPartsIndexIndex] = 0;
151 carry = true;
152 }
153 }
154 maxIndex &= (index == (part.length - 1));
155 }
156
157
158 return (!maxIndex);
159 }
160
161 private String testPartsIndextoString() {
162 StringBuffer carryMsg = new StringBuffer("{");
163 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
164 carryMsg.append(testPartsIndex[testPartsIndexIndex]);
165 if (testPartsIndexIndex < testPartsIndex.length - 1) {
166 carryMsg.append(',');
167 } else {
168 carryMsg.append('}');
169 }
170 }
171 return carryMsg.toString();
172
173 }
174
175 public void testValidateUrl() {
176 assertTrue(true);
177 }
178
179 /***
180 * Only used to debug the unit tests.
181 * @param argv
182 */
183 public static void main(String[] argv) {
184
185 UrlTest fct = new UrlTest("url test");
186 fct.setUp();
187 fct.testIsValid();
188 fct.testIsValidScheme();
189 }
190
191 /***
192 * The data given below approximates the 4 parts of a URL
193 * <scheme>://<authority><path>?<query> except that the port number
194 * is broken out of authority to increase the number of permutations.
195 * A complete URL is composed of a scheme+authority+port+path+query,
196 * all of which must be individually valid for the entire URL to be considered
197 * valid.
198 */
199 TestPair[] testUrlScheme = {new TestPair("http://", true),
200 new TestPair("ftp://", true),
201 new TestPair("h3t://", true),
202 new TestPair("3ht://", false),
203 new TestPair("http:/", false),
204 new TestPair("http:", false),
205 new TestPair("http/", false),
206 new TestPair("://", false),
207 new TestPair("", true)};
208
209 TestPair[] testUrlAuthority = {new TestPair("www.google.com", true),
210 new TestPair("go.com", true),
211 new TestPair("go.au", true),
212 new TestPair("0.0.0.0", true),
213 new TestPair("255.255.255.255", true),
214 new TestPair("256.256.256.256", false),
215 new TestPair("255.com", true),
216 new TestPair("1.2.3.4.5", false),
217 new TestPair("1.2.3.4.", false),
218 new TestPair("1.2.3", false),
219 new TestPair(".1.2.3.4", false),
220 new TestPair("go.a", false),
221 new TestPair("go.a1a", true),
222 new TestPair("go.1aa", false),
223 new TestPair("aaa.", false),
224 new TestPair(".aaa", false),
225 new TestPair("aaa", false),
226 new TestPair("", false)
227 };
228 TestPair[] testUrlPort = {new TestPair(":80", true),
229 new TestPair(":65535", true),
230 new TestPair(":0", true),
231 new TestPair("", true),
232 new TestPair(":-1", false),
233 new TestPair(":65636", true),
234 new TestPair(":65a", false)
235 };
236 TestPair[] testPath = {new TestPair("/test1", true),
237 new TestPair("/t123", true),
238 new TestPair("/$23", true),
239 new TestPair("/..", false),
240 new TestPair("/../", false),
241 new TestPair("/test1/", true),
242 new TestPair("", true),
243 new TestPair("/test1/file", true),
244 new TestPair("/..//file", false),
245 new TestPair("/test1//file", false)
246 };
247
248 TestPair[] testUrlPathOptions = {new TestPair("/test1", true),
249 new TestPair("/t123", true),
250 new TestPair("/$23", true),
251 new TestPair("/..", false),
252 new TestPair("/../", false),
253 new TestPair("/test1/", true),
254 new TestPair("/#", false),
255 new TestPair("", true),
256 new TestPair("/test1/file", true),
257 new TestPair("/t123/file", true),
258 new TestPair("/$23/file", true),
259 new TestPair("/../file", false),
260 new TestPair("/..//file", false),
261 new TestPair("/test1//file", true),
262 new TestPair("/#/file", false)
263 };
264
265 TestPair[] testUrlQuery = {new TestPair("?action=view", true),
266 new TestPair("?action=edit&mode=up", true),
267 new TestPair("", true)
268 };
269
270 Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
271 Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
272 int[] testPartsIndex = {0, 0, 0, 0, 0};
273
274
275 TestPair[] testScheme = {new TestPair("http", true),
276 new TestPair("ftp", false),
277 new TestPair("httpd", false),
278 new TestPair("telnet", false)};
279
280
281 }