View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines;
18  
19  import static org.junit.Assert.*;
20  
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.validator.ResultPair;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  /**
31   * Performs Validation Test for url validations.
32   *
33   * @version $Revision$
34   */
35  public class UrlValidatorTest {
36  
37     private final boolean printStatus = false;
38     private final boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
39  
40     @Before
41     public void setUp() {
42        for (int index = 0; index < testPartsIndex.length - 1; index++) {
43           testPartsIndex[index] = 0;
44        }
45     }
46  
47     @Test
48     public void testIsValid() {
49          testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
50          setUp();
51          long options =
52              UrlValidator.ALLOW_2_SLASHES
53                  + UrlValidator.ALLOW_ALL_SCHEMES
54                  + UrlValidator.NO_FRAGMENTS;
55  
56          testIsValid(testUrlPartsOptions, options);
57     }
58  
59     @Test
60     public void testIsValidScheme() {
61        if (printStatus) {
62           System.out.print("\n testIsValidScheme() ");
63        }
64        //UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
65        UrlValidator urlVal = new UrlValidator(schemes, 0);
66        for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
67           ResultPair testPair = testScheme[sIndex];
68           boolean result = urlVal.isValidScheme(testPair.item);
69           assertEquals(testPair.item, testPair.valid, result);
70           if (printStatus) {
71              if (result == testPair.valid) {
72                 System.out.print('.');
73              } else {
74                 System.out.print('X');
75              }
76           }
77        }
78        if (printStatus) {
79           System.out.println();
80        }
81  
82     }
83  
84     /**
85      * Create set of tests by taking the testUrlXXX arrays and
86      * running through all possible permutations of their combinations.
87      *
88      * @param testObjects Used to create a url.
89      */
90     public void testIsValid(Object[] testObjects, long options) {
91        UrlValidator urlVal = new UrlValidator(null, null, options);
92        assertTrue(urlVal.isValid("http://www.google.com"));
93        assertTrue(urlVal.isValid("http://www.google.com/"));
94        int statusPerLine = 60;
95        int printed = 0;
96        if (printIndex)  {
97           statusPerLine = 6;
98        }
99        do {
100           StringBuilder testBuffer = new StringBuilder();
101          boolean expected = true;
102          for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
103             int index = testPartsIndex[testPartsIndexIndex];
104             ResultPair./../../../org/apache/commons/validator/ResultPair.html#ResultPair">ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
105             testBuffer.append(part[index].item);
106             expected &= part[index].valid;
107          }
108          String url = testBuffer.toString();
109          boolean result = urlVal.isValid(url);
110          assertEquals(url, expected, result);
111          if (printStatus) {
112             if (printIndex) {
113                System.out.print(testPartsIndextoString());
114             } else {
115                if (result == expected) {
116                   System.out.print('.');
117                } else {
118                   System.out.print('X');
119                }
120             }
121             printed++;
122             if (printed == statusPerLine) {
123                System.out.println();
124                printed = 0;
125             }
126          }
127       } while (incrementTestPartsIndex(testPartsIndex, testObjects));
128       if (printStatus) {
129          System.out.println();
130       }
131    }
132 
133    @Test
134    public void testValidator202() {
135        String[] schemes = {"http","https"};
136        UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
137        assertTrue(urlValidator.isValid("http://l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.l.org"));
138    }
139 
140    @Test
141    public void testValidator204() {
142        String[] schemes = {"http","https"};
143        UrlValidator urlValidator = new UrlValidator(schemes);
144        assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5"));
145    }
146 
147    @Test
148    public void testValidator218() {
149        UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
150        assertTrue("parentheses should be valid in URLs",
151                validator.isValid("http://somewhere.com/pathxyz/file(1).html"));
152    }
153 
154    @Test
155    public void testValidator235() {
156        String version = System.getProperty("java.version");
157        if (version.compareTo("1.6") < 0) {
158            System.out.println("Cannot run Unicode IDN tests");
159            return; // Cannot run the test
160        }
161        UrlValidator validator = new UrlValidator();
162        assertTrue("xn--d1abbgf6aiiy.xn--p1ai should validate", validator.isValid("http://xn--d1abbgf6aiiy.xn--p1ai"));
163        assertTrue("президент.рф should validate", validator.isValid("http://президент.рф"));
164        assertTrue("www.b\u00fccher.ch should validate", validator.isValid("http://www.b\u00fccher.ch"));
165        assertFalse("www.\uFFFD.ch FFFD should fail", validator.isValid("http://www.\uFFFD.ch"));
166        assertTrue("www.b\u00fccher.ch should validate", validator.isValid("ftp://www.b\u00fccher.ch"));
167        assertFalse("www.\uFFFD.ch FFFD should fail", validator.isValid("ftp://www.\uFFFD.ch"));
168    }
169 
170     @Test
171     public void testValidator248() {
172         RegexValidator regex = new RegexValidator(new String[] {"localhost", ".*\\.my-testing"});
173         UrlValidator validator = new UrlValidator(regex, 0);
174 
175         assertTrue("localhost URL should validate",
176                 validator.isValid("http://localhost/test/index.html"));
177         assertTrue("first.my-testing should validate",
178                 validator.isValid("http://first.my-testing/test/index.html"));
179         assertTrue("sup3r.my-testing should validate",
180                 validator.isValid("http://sup3r.my-testing/test/index.html"));
181 
182         assertFalse("broke.my-test should not validate",
183                 validator.isValid("http://broke.my-test/test/index.html"));
184 
185         assertTrue("www.apache.org should still validate",
186                 validator.isValid("http://www.apache.org/test/index.html"));
187 
188         // Now check using options
189         validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
190 
191         assertTrue("localhost URL should validate",
192               validator.isValid("http://localhost/test/index.html"));
193 
194         assertTrue("machinename URL should validate",
195               validator.isValid("http://machinename/test/index.html"));
196 
197         assertTrue("www.apache.org should still validate",
198               validator.isValid("http://www.apache.org/test/index.html"));
199     }
200 
201     @Test
202     public void testValidator288() {
203         UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);
204 
205         assertTrue("hostname should validate",
206                 validator.isValid("http://hostname"));
207 
208         assertTrue("hostname with path should validate",
209                 validator.isValid("http://hostname/test/index.html"));
210 
211         assertTrue("localhost URL should validate",
212                 validator.isValid("http://localhost/test/index.html"));
213 
214         assertFalse("first.my-testing should not validate",
215                 validator.isValid("http://first.my-testing/test/index.html"));
216 
217         assertFalse("broke.hostname should not validate",
218                 validator.isValid("http://broke.hostname/test/index.html"));
219 
220         assertTrue("www.apache.org should still validate",
221                 validator.isValid("http://www.apache.org/test/index.html"));
222 
223         // Turn it off, and check
224         validator = new UrlValidator(0);
225 
226         assertFalse("hostname should no longer validate",
227                 validator.isValid("http://hostname"));
228 
229         assertFalse("localhost URL should no longer validate",
230                 validator.isValid("http://localhost/test/index.html"));
231 
232         assertTrue("www.apache.org should still validate",
233                 validator.isValid("http://www.apache.org/test/index.html"));
234     }
235 
236     @Test
237     public void testValidator276() {
238         // file:// isn't allowed by default
239         UrlValidator validator = new UrlValidator();
240 
241         assertTrue("http://apache.org/ should be allowed by default",
242                  validator.isValid("http://www.apache.org/test/index.html"));
243 
244         assertFalse("file:///c:/ shouldn't be allowed by default",
245                  validator.isValid("file:///C:/some.file"));
246 
247         assertFalse("file:///c:\\ shouldn't be allowed by default",
248               validator.isValid("file:///C:\\some.file"));
249 
250         assertFalse("file:///etc/ shouldn't be allowed by default",
251               validator.isValid("file:///etc/hosts"));
252 
253         assertFalse("file://localhost/etc/ shouldn't be allowed by default",
254               validator.isValid("file://localhost/etc/hosts"));
255 
256         assertFalse("file://localhost/c:/ shouldn't be allowed by default",
257               validator.isValid("file://localhost/c:/some.file"));
258 
259         // Turn it on, and check
260         // Note - we need to enable local urls when working with file:
261         validator = new UrlValidator(new String[] {"http","file"}, UrlValidator.ALLOW_LOCAL_URLS);
262 
263         assertTrue("http://apache.org/ should be allowed by default",
264                  validator.isValid("http://www.apache.org/test/index.html"));
265 
266         assertTrue("file:///c:/ should now be allowed",
267                  validator.isValid("file:///C:/some.file"));
268 
269         assertFalse("file:///c:\\ should not be allowed", // Only allow forward slashes
270               validator.isValid("file:///C:\\some.file"));
271 
272         assertTrue("file:///etc/ should now be allowed",
273               validator.isValid("file:///etc/hosts"));
274 
275         assertTrue("file://localhost/etc/ should now be allowed",
276               validator.isValid("file://localhost/etc/hosts"));
277 
278         assertTrue("file://localhost/c:/ should now be allowed",
279               validator.isValid("file://localhost/c:/some.file"));
280 
281         // These are never valid
282         assertFalse("file://c:/ shouldn't ever be allowed, needs file:///c:/",
283               validator.isValid("file://C:/some.file"));
284 
285         assertFalse("file://c:\\ shouldn't ever be allowed, needs file:///c:/",
286               validator.isValid("file://C:\\some.file"));
287     }
288 
289     @Test
290     public void testValidator391OK() {
291         String[] schemes = {"file"};
292         UrlValidator urlValidator = new UrlValidator(schemes);
293         assertTrue(urlValidator.isValid("file:///C:/path/to/dir/"));
294     }
295 
296     @Test
297     public void testValidator391FAILS() {
298         String[] schemes = {"file"};
299         UrlValidator urlValidator = new UrlValidator(schemes);
300         assertTrue(urlValidator.isValid("file:/C:/path/to/dir/"));
301     }
302 
303     @Test
304     public void testValidator309() {
305         UrlValidator urlValidator = new UrlValidator();
306         assertTrue(urlValidator.isValid("http://sample.ondemand.com/"));
307         assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/"));
308         assertTrue(urlValidator.isValid("httpS://SAMPLE.ONEMAND.COM/"));
309         urlValidator = new UrlValidator(new String[] {"HTTP","HTTPS"});
310         assertTrue(urlValidator.isValid("http://sample.ondemand.com/"));
311         assertTrue(urlValidator.isValid("hTtP://sample.ondemand.CoM/"));
312         assertTrue(urlValidator.isValid("httpS://SAMPLE.ONEMAND.COM/"));
313     }
314 
315     @Test
316     public void testValidator339(){
317         UrlValidator urlValidator = new UrlValidator();
318         assertTrue(urlValidator.isValid("http://www.cnn.com/WORLD/?hpt=sitenav")); // without
319         assertTrue(urlValidator.isValid("http://www.cnn.com./WORLD/?hpt=sitenav")); // with
320         assertFalse(urlValidator.isValid("http://www.cnn.com../")); // doubly dotty
321         assertFalse(urlValidator.isValid("http://www.cnn.invalid/"));
322         assertFalse(urlValidator.isValid("http://www.cnn.invalid./")); // check . does not affect invalid domains
323     }
324 
325     @Test
326     public void testValidator339IDN(){
327         UrlValidator urlValidator = new UrlValidator();
328         assertTrue(urlValidator.isValid("http://президент.рф/WORLD/?hpt=sitenav")); // without
329         assertTrue(urlValidator.isValid("http://президент.рф./WORLD/?hpt=sitenav")); // with
330         assertFalse(urlValidator.isValid("http://президент.рф..../")); // very dotty
331         assertFalse(urlValidator.isValid("http://президент.рф.../")); // triply dotty
332         assertFalse(urlValidator.isValid("http://президент.рф../")); // doubly dotty
333     }
334 
335     @Test
336     public void testValidator342(){
337         UrlValidator urlValidator = new UrlValidator();
338         assertTrue(urlValidator.isValid("http://example.rocks/"));
339         assertTrue(urlValidator.isValid("http://example.rocks"));
340     }
341 
342     @Test
343     public void testValidator411(){
344         UrlValidator urlValidator = new UrlValidator();
345         assertTrue(urlValidator.isValid("http://example.rocks:/"));
346         assertTrue(urlValidator.isValid("http://example.rocks:0/"));
347         assertTrue(urlValidator.isValid("http://example.rocks:65535/"));
348         assertFalse(urlValidator.isValid("http://example.rocks:65536/"));
349         assertFalse(urlValidator.isValid("http://example.rocks:100000/"));
350     }
351 
352     @Test
353     public void testValidator464() {
354         String[] schemes = {"file"};
355         UrlValidator urlValidator = new UrlValidator(schemes);
356         String fileNAK = "file://bad ^ domain.com/label/test";
357         assertFalse(fileNAK, urlValidator.isValid(fileNAK));
358     }
359 
360     @Test
361     public void testValidator452(){
362       UrlValidator urlValidator = new UrlValidator();
363       assertTrue(urlValidator.isValid("http://[::FFFF:129.144.52.38]:80/index.html"));
364     }
365 
366     @Test(expected = IllegalArgumentException.class)
367     public void testValidator473_1() { // reject null DomainValidator
368         new UrlValidator(new String[]{}, null, 0L, null);
369     }
370 
371     @Test(expected = IllegalArgumentException.class)
372     public void testValidator473_2() { // reject null DomainValidator with mismatched allowLocal
373         List<DomainValidator.Item> items = new ArrayList<>();
374         new UrlValidator(new String[]{}, null, 0L, DomainValidator.getInstance(true, items));
375     }
376 
377     @Test(expected = IllegalArgumentException.class)
378     public void testValidator473_3() { // reject null DomainValidator with mismatched allowLocal
379         List<DomainValidator.Item> items = new ArrayList<>();
380         new UrlValidator(new String[]{}, null, UrlValidator.ALLOW_LOCAL_URLS, DomainValidator.getInstance(false, items));
381     }
382 
383     static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
384       boolean carry = true;  //add 1 to lowest order part.
385       boolean maxIndex = true;
386       for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
387          int index = testPartsIndex[testPartsIndexIndex];
388          ResultPair./../../../org/apache/commons/validator/ResultPair.html#ResultPair">ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex];
389          maxIndex &= (index == (part.length - 1));
390          if (carry) {
391             if (index < part.length - 1) {
392                index++;
393                testPartsIndex[testPartsIndexIndex] = index;
394                carry = false;
395             } else {
396                testPartsIndex[testPartsIndexIndex] = 0;
397                carry = true;
398             }
399          }
400       }
401 
402 
403       return (!maxIndex);
404    }
405 
406    private String testPartsIndextoString() {
407        StringBuilder carryMsg = new StringBuilder("{");
408       for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
409          carryMsg.append(testPartsIndex[testPartsIndexIndex]);
410          if (testPartsIndexIndex < testPartsIndex.length - 1) {
411             carryMsg.append(',');
412          } else {
413             carryMsg.append('}');
414          }
415       }
416       return carryMsg.toString();
417 
418    }
419 
420    @Test
421    public void testValidateUrl() {
422       assertTrue(true);
423    }
424 
425    @Test
426    public void testValidator290() {
427         UrlValidator validator = new UrlValidator();
428         assertTrue(validator.isValid("http://xn--h1acbxfam.idn.icann.org/"));
429 //        assertTrue(validator.isValid("http://xn--e1afmkfd.xn--80akhbyknj4f"));
430         // Internationalized country code top-level domains
431         assertTrue(validator.isValid("http://test.xn--lgbbat1ad8j")); //Algeria
432         assertTrue(validator.isValid("http://test.xn--fiqs8s")); // China
433         assertTrue(validator.isValid("http://test.xn--fiqz9s")); // China
434         assertTrue(validator.isValid("http://test.xn--wgbh1c")); // Egypt
435         assertTrue(validator.isValid("http://test.xn--j6w193g")); // Hong Kong
436         assertTrue(validator.isValid("http://test.xn--h2brj9c")); // India
437         assertTrue(validator.isValid("http://test.xn--mgbbh1a71e")); // India
438         assertTrue(validator.isValid("http://test.xn--fpcrj9c3d")); // India
439         assertTrue(validator.isValid("http://test.xn--gecrj9c")); // India
440         assertTrue(validator.isValid("http://test.xn--s9brj9c")); // India
441         assertTrue(validator.isValid("http://test.xn--xkc2dl3a5ee0h")); // India
442         assertTrue(validator.isValid("http://test.xn--45brj9c")); // India
443         assertTrue(validator.isValid("http://test.xn--mgba3a4f16a")); // Iran
444         assertTrue(validator.isValid("http://test.xn--mgbayh7gpa")); // Jordan
445         assertTrue(validator.isValid("http://test.xn--mgbc0a9azcg")); // Morocco
446         assertTrue(validator.isValid("http://test.xn--ygbi2ammx")); // Palestinian Territory
447         assertTrue(validator.isValid("http://test.xn--wgbl6a")); // Qatar
448         assertTrue(validator.isValid("http://test.xn--p1ai")); // Russia
449         assertTrue(validator.isValid("http://test.xn--mgberp4a5d4ar")); //  Saudi Arabia
450         assertTrue(validator.isValid("http://test.xn--90a3ac")); // Serbia
451         assertTrue(validator.isValid("http://test.xn--yfro4i67o")); // Singapore
452         assertTrue(validator.isValid("http://test.xn--clchc0ea0b2g2a9gcd")); // Singapore
453         assertTrue(validator.isValid("http://test.xn--3e0b707e")); // South Korea
454         assertTrue(validator.isValid("http://test.xn--fzc2c9e2c")); // Sri Lanka
455         assertTrue(validator.isValid("http://test.xn--xkc2al3hye2a")); // Sri Lanka
456         assertTrue(validator.isValid("http://test.xn--ogbpf8fl")); // Syria
457         assertTrue(validator.isValid("http://test.xn--kprw13d")); // Taiwan
458         assertTrue(validator.isValid("http://test.xn--kpry57d")); // Taiwan
459         assertTrue(validator.isValid("http://test.xn--o3cw4h")); // Thailand
460         assertTrue(validator.isValid("http://test.xn--pgbs0dh")); // Tunisia
461         assertTrue(validator.isValid("http://test.xn--mgbaam7a8h")); // United Arab Emirates
462         // Proposed internationalized ccTLDs
463 //        assertTrue(validator.isValid("http://test.xn--54b7fta0cc")); // Bangladesh
464 //        assertTrue(validator.isValid("http://test.xn--90ae")); // Bulgaria
465 //        assertTrue(validator.isValid("http://test.xn--node")); // Georgia
466 //        assertTrue(validator.isValid("http://test.xn--4dbrk0ce")); // Israel
467 //        assertTrue(validator.isValid("http://test.xn--mgb9awbf")); // Oman
468 //        assertTrue(validator.isValid("http://test.xn--j1amh")); // Ukraine
469 //        assertTrue(validator.isValid("http://test.xn--mgb2ddes")); // Yemen
470         // Test TLDs
471 //        assertTrue(validator.isValid("http://test.xn--kgbechtv")); // Arabic
472 //        assertTrue(validator.isValid("http://test.xn--hgbk6aj7f53bba")); // Persian
473 //        assertTrue(validator.isValid("http://test.xn--0zwm56d")); // Chinese
474 //        assertTrue(validator.isValid("http://test.xn--g6w251d")); // Chinese
475 //        assertTrue(validator.isValid("http://test.xn--80akhbyknj4f")); // Russian
476 //        assertTrue(validator.isValid("http://test.xn--11b5bs3a9aj6g")); // Hindi
477 //        assertTrue(validator.isValid("http://test.xn--jxalpdlp")); // Greek
478 //        assertTrue(validator.isValid("http://test.xn--9t4b11yi5a")); // Korean
479 //        assertTrue(validator.isValid("http://test.xn--deba0ad")); // Yiddish
480 //        assertTrue(validator.isValid("http://test.xn--zckzah")); // Japanese
481 //        assertTrue(validator.isValid("http://test.xn--hlcj6aya9esc7a")); // Tamil
482     }
483 
484    @Test
485    public void testValidator361() {
486        UrlValidator validator = new UrlValidator();
487        assertTrue(validator.isValid("http://hello.tokyo/"));
488     }
489 
490    @Test
491    public void testValidator363(){
492         UrlValidator urlValidator = new UrlValidator();
493         assertTrue(urlValidator.isValid("http://www.example.org/a/b/hello..world"));
494         assertTrue(urlValidator.isValid("http://www.example.org/a/hello..world"));
495         assertTrue(urlValidator.isValid("http://www.example.org/hello.world/"));
496         assertTrue(urlValidator.isValid("http://www.example.org/hello..world/"));
497         assertTrue(urlValidator.isValid("http://www.example.org/hello.world"));
498         assertTrue(urlValidator.isValid("http://www.example.org/hello..world"));
499         assertTrue(urlValidator.isValid("http://www.example.org/..world"));
500         assertTrue(urlValidator.isValid("http://www.example.org/.../world"));
501         assertFalse(urlValidator.isValid("http://www.example.org/../world"));
502         assertFalse(urlValidator.isValid("http://www.example.org/.."));
503         assertFalse(urlValidator.isValid("http://www.example.org/../"));
504         assertFalse(urlValidator.isValid("http://www.example.org/./.."));
505         assertFalse(urlValidator.isValid("http://www.example.org/././.."));
506         assertTrue(urlValidator.isValid("http://www.example.org/..."));
507         assertTrue(urlValidator.isValid("http://www.example.org/.../"));
508         assertTrue(urlValidator.isValid("http://www.example.org/.../.."));
509     }
510 
511    @Test
512    public void testValidator375() {
513        UrlValidator validator = new UrlValidator();
514        String url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html";
515        assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url));
516        url = "http://[::1]:80/index.html";
517        assertTrue("IPv6 address URL should validate: " + url, validator.isValid(url));
518        url = "http://FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:80/index.html";
519        assertFalse("IPv6 address without [] should not validate: " + url, validator.isValid(url));
520     }
521 
522 
523    @Test
524    public void testValidator353() { // userinfo
525        UrlValidator validator = new UrlValidator();
526        assertTrue(validator.isValid("http://www.apache.org:80/path"));
527        assertTrue(validator.isValid("http://user:pass@www.apache.org:80/path"));
528        assertTrue(validator.isValid("http://user:@www.apache.org:80/path"));
529        assertTrue(validator.isValid("http://user@www.apache.org:80/path"));
530        assertTrue(validator.isValid("http://us%00er:-._~!$&'()*+,;=@www.apache.org:80/path"));
531        assertFalse(validator.isValid("http://:pass@www.apache.org:80/path"));
532        assertFalse(validator.isValid("http://:@www.apache.org:80/path"));
533        assertFalse(validator.isValid("http://user:pa:ss@www.apache.org/path"));
534        assertFalse(validator.isValid("http://user:pa@ss@www.apache.org/path"));
535    }
536 
537    @Test
538    public void testValidator382() {
539        UrlValidator validator = new UrlValidator();
540        assertTrue(validator.isValid("ftp://username:password@example.com:8042/over/there/index.dtb?type=animal&name=narwhal#nose"));
541    }
542 
543    @Test
544    public void testValidator380() {
545        UrlValidator validator = new UrlValidator();
546        assertTrue(validator.isValid("http://www.apache.org:80/path"));
547        assertTrue(validator.isValid("http://www.apache.org:8/path"));
548        assertTrue(validator.isValid("http://www.apache.org:/path"));
549    }
550 
551    @Test
552    public void testValidator420() {
553        UrlValidator validator = new UrlValidator();
554        assertFalse(validator.isValid("http://example.com/serach?address=Main Avenue"));
555        assertTrue(validator.isValid("http://example.com/serach?address=Main%20Avenue"));
556        assertTrue(validator.isValid("http://example.com/serach?address=Main+Avenue"));
557    }
558 
559    @Test
560    public void testValidator467() {
561       UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
562       assertTrue(validator.isValid("https://example.com/some_path/path/"));
563       assertTrue(validator.isValid("https://example.com//somepath/path/"));
564       assertTrue(validator.isValid("https://example.com//some_path/path/"));
565       assertTrue(validator.isValid("http://example.com//_test")); // VALIDATOR-429
566   }
567 
568   @Test
569   public void testValidator283() {
570    UrlValidator validator = new UrlValidator();
571    assertFalse(validator.isValid("http://finance.yahoo.com/news/Owners-54B-NY-housing-apf-2493139299.html?x=0&ap=%fr"));
572    assertTrue(validator.isValid("http://finance.yahoo.com/news/Owners-54B-NY-housing-apf-2493139299.html?x=0&ap=%22"));
573   }
574 
575   @Test
576   public void testFragments() {
577    String[] schemes = {"http","https"};
578    UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
579    assertFalse(urlValidator.isValid("http://apache.org/a/b/c#frag"));
580    urlValidator = new UrlValidator(schemes);
581    assertTrue(urlValidator.isValid("http://apache.org/a/b/c#frag"));
582 }
583 
584   //-------------------- Test data for creating a composite URL
585    /**
586     * The data given below approximates the 4 parts of a URL
587     * <scheme>://<authority><path>?<query> except that the port number
588     * is broken out of authority to increase the number of permutations.
589     * A complete URL is composed of a scheme+authority+port+path+query,
590     * all of which must be individually valid for the entire URL to be considered
591     * valid.
592     */
593    ResultPair.html#ResultPair">ResultPair[] testUrlScheme = {new ResultPair("http://", true),
594                                new ResultPair("ftp://", true),
595                                new ResultPair("h3t://", true),
596                                new ResultPair("3ht://", false),
597                                new ResultPair("http:/", false),
598                                new ResultPair("http:", false),
599                                new ResultPair("http/", false),
600                                new ResultPair("://", false)};
601 
602    ResultPairml#ResultPair">ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true),
603                                   new ResultPair("www.google.com.", true),
604                                   new ResultPair("go.com", true),
605                                   new ResultPair("go.au", true),
606                                   new ResultPair("0.0.0.0", true),
607                                   new ResultPair("255.255.255.255", true),
608                                   new ResultPair("256.256.256.256", false),
609                                   new ResultPair("255.com", true),
610                                   new ResultPair("1.2.3.4.5", false),
611                                   new ResultPair("1.2.3.4.", false),
612                                   new ResultPair("1.2.3", false),
613                                   new ResultPair(".1.2.3.4", false),
614                                   new ResultPair("go.a", false),
615                                   new ResultPair("go.a1a", false),
616                                   new ResultPair("go.cc", true),
617                                   new ResultPair("go.1aa", false),
618                                   new ResultPair("aaa.", false),
619                                   new ResultPair(".aaa", false),
620                                   new ResultPair("aaa", false),
621                                   new ResultPair("", false)
622    };
623    ResultPairir.html#ResultPair">ResultPair[] testUrlPort = {new ResultPair(":80", true),
624                              new ResultPair(":65535", true), // max possible
625                              new ResultPair(":65536", false), // max possible +1
626                              new ResultPair(":0", true),
627                              new ResultPair("", true),
628                              new ResultPair(":-1", false),
629                              new ResultPair(":65636", false),
630                              new ResultPair(":999999999999999999", false),
631                              new ResultPair(":65a", false)
632    };
633    ResultPairtPair.html#ResultPair">ResultPair[] testPath = {new ResultPair("/test1", true),
634                           new ResultPair("/t123", true),
635                           new ResultPair("/$23", true),
636                           new ResultPair("/..", false),
637                           new ResultPair("/../", false),
638                           new ResultPair("/test1/", true),
639                           new ResultPair("", true),
640                           new ResultPair("/test1/file", true),
641                           new ResultPair("/..//file", false),
642                           new ResultPair("/test1//file", false)
643    };
644    //Test allow2slash, noFragment
645    ResultPair#ResultPair">ResultPair[] testUrlPathOptions = {new ResultPair("/test1", true),
646                                     new ResultPair("/t123", true),
647                                     new ResultPair("/$23", true),
648                                     new ResultPair("/..", false),
649                                     new ResultPair("/../", false),
650                                     new ResultPair("/test1/", true),
651                                     new ResultPair("/#", false),
652                                     new ResultPair("", true),
653                                     new ResultPair("/test1/file", true),
654                                     new ResultPair("/t123/file", true),
655                                     new ResultPair("/$23/file", true),
656                                     new ResultPair("/../file", false),
657                                     new ResultPair("/..//file", false),
658                                     new ResultPair("/test1//file", true),
659                                     new ResultPair("/#/file", false)
660    };
661 
662    ResultPairr.html#ResultPair">ResultPair[] testUrlQuery = {new ResultPair("?action=view", true),
663                               new ResultPair("?action=edit&mode=up", true),
664                               new ResultPair("", true)
665    };
666 
667    Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
668    Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
669    int[] testPartsIndex = {0, 0, 0, 0, 0};
670 
671    //---------------- Test data for individual url parts ----------------
672    private final String[] schemes = {"http", "gopher", "g0-To+.",
673                                       "not_valid" // TODO this will need to be dropped if the ctor validates schemes
674                                     };
675 
676    ResultPairair.html#ResultPair">ResultPair[] testScheme = {new ResultPair("http", true),
677                             new ResultPair("ftp", false),
678                             new ResultPair("httpd", false),
679                             new ResultPair("gopher", true),
680                             new ResultPair("g0-to+.", true),
681                             new ResultPair("not_valid", false), // underscore not allowed
682                             new ResultPair("HtTp", true),
683                             new ResultPair("telnet", false)};
684 
685 
686     /**
687      * Validator for checking URL parsing
688      * @param args - URLs to validate
689      */
690     public static void main(String[] args) {
691       UrlValidator uv = new UrlValidator();
692       for(String arg: args) {
693           try {
694               URI uri = new URI(arg);
695               uri = uri.normalize();
696               System.out.println(uri.toString());
697               System.out.printf("URI scheme: %s%n", uri.getScheme());
698               System.out.printf("URI scheme specific part: %s%n", uri.getSchemeSpecificPart());
699               System.out.printf("URI raw scheme specific part: %s%n", uri.getRawSchemeSpecificPart());
700               System.out.printf("URI auth: %s%n", uri.getAuthority());
701               System.out.printf("URI raw auth: %s%n", uri.getRawAuthority());
702               System.out.printf("URI userInfo: %s%n", uri.getUserInfo());
703               System.out.printf("URI raw userInfo: %s%n", uri.getRawUserInfo());
704               System.out.printf("URI host: %s%n", uri.getHost());
705               System.out.printf("URI port: %s%n", uri.getPort());
706               System.out.printf("URI path: %s%n", uri.getPath());
707               System.out.printf("URI raw path: %s%n", uri.getRawPath());
708               System.out.printf("URI query: %s%n", uri.getQuery());
709               System.out.printf("URI raw query: %s%n", uri.getRawQuery());
710               System.out.printf("URI fragment: %s%n", uri.getFragment());
711               System.out.printf("URI raw fragment: %s%n", uri.getRawFragment());
712           } catch (URISyntaxException e) {
713               System.out.println(e.getMessage());
714           }
715           System.out.printf("isValid: %s%n",uv.isValid(arg));
716       }
717    }   
718 
719 }