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