2
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/UrlTest.java,v 1.9 2004/02/21 17:10:30 rleland Exp $
4
* $Date: 2004/02/21 17:10:30 $
6
* ====================================================================
7
* Copyright 2003-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator;
24
import junit.framework.Test;
25
import junit.framework.TestCase;
26
import junit.framework.TestSuite;
29
* Performs Validation Test for url validations.
31
public class UrlTest extends TestCase {
33
private boolean printStatus = false;
34
private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
36
public UrlTest(String testName) {
40
public static Test suite() {
41
return new TestSuite(UrlTest.class);
44
protected void setUp() {
45
for (int index = 0; index < testPartsIndex.length - 1; index++) {
46
testPartsIndex[index] = 0;
50
protected void tearDown() {
53
public void testIsValid() {
54
testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
55
System.out.println("\nTesting options ....");
58
UrlValidator.ALLOW_2_SLASHES
59
+ UrlValidator.ALLOW_ALL_SCHEMES
60
+ UrlValidator.NO_FRAGMENTS;
62
testIsValid(testUrlPartsOptions, options);
65
public void testIsValidScheme() {
67
System.out.print("\n testIsValidScheme() ");
69
String[] schemes = {"http", "gopher"};
70
//UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
71
UrlValidator urlVal = new UrlValidator(schemes, 0);
72
for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
73
TestPair testPair = testScheme[sIndex];
74
boolean result = urlVal.isValidScheme(testPair.item);
75
assertEquals(testPair.item, testPair.valid, result);
77
if (result == testPair.valid) {
78
System.out.print('.');
80
System.out.print('X');
91
* Create set of tests by taking the testUrlXXX arrays and
92
* running through all possible permutations of their combinations.
94
* @param testObjects Used to create a url.
96
public void testIsValid(Object[] testObjects, int options) {
97
UrlValidator urlVal = new UrlValidator(null, options);
98
int statusPerLine = 60;
104
StringBuffer testBuffer = new StringBuffer();
105
boolean expected = true;
106
for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
107
int index = testPartsIndex[testPartsIndexIndex];
108
TestPair[] part = (TestPair[]) testObjects[testPartsIndexIndex];
109
testBuffer.append(part[index].item);
110
expected &= part[index].valid;
112
String url = testBuffer.toString();
113
boolean result = urlVal.isValid(url);
114
assertEquals(url, expected, result);
117
System.out.print(testPartsIndextoString());
119
if (result == expected) {
120
System.out.print('.');
122
System.out.print('X');
126
if (printed == statusPerLine) {
127
System.out.println();
131
} while (incrementTestPartsIndex(testPartsIndex, testObjects));
133
System.out.println();
137
static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
138
boolean carry = true; //add 1 to lowest order part.
139
boolean maxIndex = true;
140
for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
141
int index = testPartsIndex[testPartsIndexIndex];
142
TestPair[] part = (TestPair[]) testParts[testPartsIndexIndex];
144
if (index < part.length - 1) {
146
testPartsIndex[testPartsIndexIndex] = index;
149
testPartsIndex[testPartsIndexIndex] = 0;
153
maxIndex &= (index == (part.length - 1));
160
private String testPartsIndextoString() {
161
StringBuffer carryMsg = new StringBuffer("{");
162
for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
163
carryMsg.append(testPartsIndex[testPartsIndexIndex]);
164
if (testPartsIndexIndex < testPartsIndex.length - 1) {
165
carryMsg.append(',');
167
carryMsg.append('}');
170
return carryMsg.toString();
174
public void testValidateUrl() {
179
* Only used to debug the unit tests.
182
public static void main(String[] argv) {
184
UrlTest fct = new UrlTest("url test");
187
fct.testIsValidScheme();
189
//-------------------- Test data for creating a composite URL
191
* The data given below approximates the 4 parts of a URL
192
* <scheme>://<authority><path>?<query> except that the port number
193
* is broken out of authority to increase the number of permutations.
194
* A complete URL is composed of a scheme+authority+port+path+query,
195
* all of which must be individually valid for the entire URL to be considered
198
TestPair[] testUrlScheme = {new TestPair("http://", true),
199
new TestPair("ftp://", true),
200
new TestPair("h3t://", true),
201
new TestPair("3ht://", false),
202
new TestPair("http:/", false),
203
new TestPair("http:", false),
204
new TestPair("http/", false),
205
new TestPair("://", false),
206
new TestPair("", true)};
208
TestPair[] testUrlAuthority = {new TestPair("www.google.com", true),
209
new TestPair("go.com", true),
210
new TestPair("go.au", true),
211
new TestPair("0.0.0.0", true),
212
new TestPair("255.255.255.255", true),
213
new TestPair("256.256.256.256", false),
214
new TestPair("255.com", true),
215
new TestPair("1.2.3.4.5", false),
216
new TestPair("1.2.3.4.", false),
217
new TestPair("1.2.3", false),
218
new TestPair(".1.2.3.4", false),
219
new TestPair("go.a", false),
220
new TestPair("go.a1a", true),
221
new TestPair("go.1aa", false),
222
new TestPair("aaa.", false),
223
new TestPair(".aaa", false),
224
new TestPair("aaa", false),
225
new TestPair("", false)
227
TestPair[] testUrlPort = {new TestPair(":80", true),
228
new TestPair(":65535", true),
229
new TestPair(":0", true),
230
new TestPair("", true),
231
new TestPair(":-1", false),
232
new TestPair(":65636", true),
233
new TestPair(":65a", false)
235
TestPair[] testPath = {new TestPair("/test1", true),
236
new TestPair("/t123", true),
237
new TestPair("/$23", true),
238
new TestPair("/..", false),
239
new TestPair("/../", false),
240
new TestPair("/test1/", false),
241
new TestPair("", false),
242
new TestPair("/test1/file", true),
243
new TestPair("/..//file", false),
244
new TestPair("/test1//file", false)
246
//Test allow2slash, noFragment
247
TestPair[] testUrlPathOptions = {new TestPair("/test1", true),
248
new TestPair("/t123", true),
249
new TestPair("/$23", true),
250
new TestPair("/..", false),
251
new TestPair("/../", false),
252
new TestPair("/test1/", false),
253
new TestPair("/#", false),
254
new TestPair("", false),
255
new TestPair("/test1/file", true),
256
new TestPair("/t123/file", true),
257
new TestPair("/$23/file", true),
258
new TestPair("/../file", false),
259
new TestPair("/..//file", false),
260
new TestPair("/test1//file", true),
261
new TestPair("/#/file", false)
264
TestPair[] testUrlQuery = {new TestPair("?action=view", true),
265
new TestPair("?action=edit&mode=up", true),
266
new TestPair("", true)
269
Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
270
Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
271
int[] testPartsIndex = {0, 0, 0, 0, 0};
273
//---------------- Test data for individual url parts ----------------
274
TestPair[] testScheme = {new TestPair("http", true),
275
new TestPair("ftp", false),
276
new TestPair("httpd", false),
277
new TestPair("telnet", false)};
b'\\ No newline at end of file'