001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022:
023: /**
024: * Performs Validation Test for url validations.
025: *
026: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
027: */
028: public class UrlTest extends TestCase {
029:
030: private boolean printStatus = false;
031: private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
032:
033: public UrlTest(String testName) {
034: super (testName);
035: }
036:
037: public static Test suite() {
038: return new TestSuite(UrlTest.class);
039: }
040:
041: protected void setUp() {
042: for (int index = 0; index < testPartsIndex.length - 1; index++) {
043: testPartsIndex[index] = 0;
044: }
045: }
046:
047: protected void tearDown() {
048: }
049:
050: public void testIsValid() {
051: testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
052: setUp();
053: int options = UrlValidator.ALLOW_2_SLASHES
054: + UrlValidator.ALLOW_ALL_SCHEMES
055: + UrlValidator.NO_FRAGMENTS;
056:
057: testIsValid(testUrlPartsOptions, options);
058: }
059:
060: public void testIsValidScheme() {
061: if (printStatus) {
062: System.out.print("\n testIsValidScheme() ");
063: }
064: String[] schemes = { "http", "gopher" };
065: //UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
066: UrlValidator urlVal = new UrlValidator(schemes, 0);
067: for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
068: TestPair testPair = testScheme[sIndex];
069: boolean result = urlVal.isValidScheme(testPair.item);
070: assertEquals(testPair.item, testPair.valid, result);
071: if (printStatus) {
072: if (result == testPair.valid) {
073: System.out.print('.');
074: } else {
075: System.out.print('X');
076: }
077: }
078: }
079: if (printStatus) {
080: System.out.println();
081: }
082:
083: }
084:
085: /**
086: * Create set of tests by taking the testUrlXXX arrays and
087: * running through all possible permutations of their combinations.
088: *
089: * @param testObjects Used to create a url.
090: */
091: public void testIsValid(Object[] testObjects, int options) {
092: UrlValidator urlVal = new UrlValidator(null, options);
093: assertTrue(urlVal.isValid("http://www.google.com"));
094: assertTrue(urlVal.isValid("http://www.google.com/"));
095: int statusPerLine = 60;
096: int printed = 0;
097: if (printIndex) {
098: statusPerLine = 6;
099: }
100: do {
101: StringBuffer testBuffer = new StringBuffer();
102: boolean expected = true;
103: for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
104: int index = testPartsIndex[testPartsIndexIndex];
105: TestPair[] part = (TestPair[]) testObjects[testPartsIndexIndex];
106: testBuffer.append(part[index].item);
107: expected &= part[index].valid;
108: }
109: String url = testBuffer.toString();
110: boolean result = urlVal.isValid(url);
111: assertEquals(url, expected, result);
112: if (printStatus) {
113: if (printIndex) {
114: System.out.print(testPartsIndextoString());
115: } else {
116: if (result == expected) {
117: System.out.print('.');
118: } else {
119: System.out.print('X');
120: }
121: }
122: printed++;
123: if (printed == statusPerLine) {
124: System.out.println();
125: printed = 0;
126: }
127: }
128: } while (incrementTestPartsIndex(testPartsIndex, testObjects));
129: if (printStatus) {
130: System.out.println();
131: }
132: }
133:
134: public void testValidator202() {
135: String[] schemes = { "http", "https" };
136: UrlValidator urlValidator = new UrlValidator(schemes,
137: UrlValidator.NO_FRAGMENTS);
138: urlValidator
139: .isValid("http://www.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.logoworks.comwww.log");
140: }
141:
142: public void testValidator204() {
143: String[] schemes = { "http", "https" };
144: UrlValidator urlValidator = new UrlValidator(schemes);
145: assertTrue(urlValidator
146: .isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
147: }
148:
149: static boolean incrementTestPartsIndex(int[] testPartsIndex,
150: Object[] testParts) {
151: boolean carry = true; //add 1 to lowest order part.
152: boolean maxIndex = true;
153: for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
154: int index = testPartsIndex[testPartsIndexIndex];
155: TestPair[] part = (TestPair[]) testParts[testPartsIndexIndex];
156: if (carry) {
157: if (index < part.length - 1) {
158: index++;
159: testPartsIndex[testPartsIndexIndex] = index;
160: carry = false;
161: } else {
162: testPartsIndex[testPartsIndexIndex] = 0;
163: carry = true;
164: }
165: }
166: maxIndex &= (index == (part.length - 1));
167: }
168:
169: return (!maxIndex);
170: }
171:
172: private String testPartsIndextoString() {
173: StringBuffer carryMsg = new StringBuffer("{");
174: for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
175: carryMsg.append(testPartsIndex[testPartsIndexIndex]);
176: if (testPartsIndexIndex < testPartsIndex.length - 1) {
177: carryMsg.append(',');
178: } else {
179: carryMsg.append('}');
180: }
181: }
182: return carryMsg.toString();
183:
184: }
185:
186: public void testValidateUrl() {
187: assertTrue(true);
188: }
189:
190: /**
191: * Only used to debug the unit tests.
192: * @param argv
193: */
194: public static void main(String[] argv) {
195:
196: UrlTest fct = new UrlTest("url test");
197: fct.setUp();
198: fct.testIsValid();
199: fct.testIsValidScheme();
200: }
201:
202: //-------------------- Test data for creating a composite URL
203: /**
204: * The data given below approximates the 4 parts of a URL
205: * <scheme>://<authority><path>?<query> except that the port number
206: * is broken out of authority to increase the number of permutations.
207: * A complete URL is composed of a scheme+authority+port+path+query,
208: * all of which must be individually valid for the entire URL to be considered
209: * valid.
210: */
211: TestPair[] testUrlScheme = { new TestPair("http://", true),
212: new TestPair("ftp://", true), new TestPair("h3t://", true),
213: new TestPair("3ht://", false),
214: new TestPair("http:/", false),
215: new TestPair("http:", false), new TestPair("http/", false),
216: new TestPair("://", false), new TestPair("", true) };
217:
218: TestPair[] testUrlAuthority = {
219: new TestPair("www.google.com", true),
220: new TestPair("go.com", true), new TestPair("go.au", true),
221: new TestPair("0.0.0.0", true),
222: new TestPair("255.255.255.255", true),
223: new TestPair("256.256.256.256", false),
224: new TestPair("255.com", true),
225: new TestPair("1.2.3.4.5", false),
226: new TestPair("1.2.3.4.", false),
227: new TestPair("1.2.3", false),
228: new TestPair(".1.2.3.4", false),
229: new TestPair("go.a", false), new TestPair("go.a1a", true),
230: new TestPair("go.1aa", false), new TestPair("aaa.", false),
231: new TestPair(".aaa", false), new TestPair("aaa", false),
232: new TestPair("", false) };
233: TestPair[] testUrlPort = { new TestPair(":80", true),
234: new TestPair(":65535", true), new TestPair(":0", true),
235: new TestPair("", true), new TestPair(":-1", false),
236: new TestPair(":65636", true), new TestPair(":65a", false) };
237: TestPair[] testPath = { new TestPair("/test1", true),
238: new TestPair("/t123", true), new TestPair("/$23", true),
239: new TestPair("/..", false), new TestPair("/../", false),
240: new TestPair("/test1/", true), new TestPair("", true),
241: new TestPair("/test1/file", true),
242: new TestPair("/..//file", false),
243: new TestPair("/test1//file", false) };
244: //Test allow2slash, noFragment
245: TestPair[] testUrlPathOptions = { new TestPair("/test1", true),
246: new TestPair("/t123", true), new TestPair("/$23", true),
247: new TestPair("/..", false), new TestPair("/../", false),
248: new TestPair("/test1/", true), new TestPair("/#", false),
249: new TestPair("", true), new TestPair("/test1/file", true),
250: new TestPair("/t123/file", true),
251: new TestPair("/$23/file", true),
252: new TestPair("/../file", false),
253: new TestPair("/..//file", false),
254: new TestPair("/test1//file", true),
255: new TestPair("/#/file", false) };
256:
257: TestPair[] testUrlQuery = { new TestPair("?action=view", true),
258: new TestPair("?action=edit&mode=up", true),
259: new TestPair("", true) };
260:
261: Object[] testUrlParts = { testUrlScheme, testUrlAuthority,
262: testUrlPort, testPath, testUrlQuery };
263: Object[] testUrlPartsOptions = { testUrlScheme, testUrlAuthority,
264: testUrlPort, testUrlPathOptions, testUrlQuery };
265: int[] testPartsIndex = { 0, 0, 0, 0, 0 };
266:
267: //---------------- Test data for individual url parts ----------------
268: TestPair[] testScheme = { new TestPair("http", true),
269: new TestPair("ftp", false), new TestPair("httpd", false),
270: new TestPair("telnet", false) };
271:
272: }
|