~ubuntu-branches/ubuntu/trusty/libcommons-validator-java/trusty

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/validator/UrlTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 13:35:53 UTC
  • Revision ID: james.westby@ubuntu.com-20041119133553-qtl7whjkjv9q1rbd
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.9 $
 
4
 * $Date: 2004/02/21 17:10:30 $
 
5
 *
 
6
 * ====================================================================
 
7
 * Copyright 2003-2004 The Apache Software Foundation
 
8
 *
 
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
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
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.
 
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;//print index that indicates current scheme,host,port,path, query test were using.
 
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
        System.out.println("\nTesting options ....");
 
56
        setUp();
 
57
        int options =
 
58
                UrlValidator.ALLOW_2_SLASHES
 
59
                        + UrlValidator.ALLOW_ALL_SCHEMES
 
60
                        + UrlValidator.NO_FRAGMENTS;
 
61
    
 
62
        testIsValid(testUrlPartsOptions, options);
 
63
   }
 
64
 
 
65
   public void testIsValidScheme() {
 
66
      if (printStatus) {
 
67
         System.out.print("\n testIsValidScheme() ");
 
68
      }
 
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);
 
76
         if (printStatus) {
 
77
            if (result == testPair.valid) {
 
78
               System.out.print('.');
 
79
            } else {
 
80
               System.out.print('X');
 
81
            }
 
82
         }
 
83
      }
 
84
      if (printStatus) {
 
85
         System.out.println();
 
86
      }
 
87
 
 
88
   }
 
89
 
 
90
   /**
 
91
    * Create set of tests by taking the testUrlXXX arrays and
 
92
    * running through all possible permutations of their combinations.
 
93
    *
 
94
    * @param testObjects Used to create a url.
 
95
    */
 
96
   public void testIsValid(Object[] testObjects, int options) {
 
97
      UrlValidator urlVal = new UrlValidator(null, options);
 
98
      int statusPerLine = 60;
 
99
      int printed = 0;
 
100
      if (printIndex)  {
 
101
         statusPerLine = 6;
 
102
      }
 
103
      do {
 
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;
 
111
         }
 
112
         String url = testBuffer.toString();
 
113
         boolean result = urlVal.isValid(url);
 
114
         assertEquals(url, expected, result);
 
115
         if (printStatus) {
 
116
            if (printIndex) {
 
117
               System.out.print(testPartsIndextoString());
 
118
            } else {
 
119
               if (result == expected) {
 
120
                  System.out.print('.');
 
121
               } else {
 
122
                  System.out.print('X');
 
123
               }
 
124
            }
 
125
            printed++;
 
126
            if (printed == statusPerLine) {
 
127
               System.out.println();
 
128
               printed = 0;
 
129
            }
 
130
         }
 
131
      } while (incrementTestPartsIndex(testPartsIndex, testObjects));
 
132
      if (printStatus) {
 
133
         System.out.println();
 
134
      }
 
135
   }
 
136
 
 
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];
 
143
         if (carry) {
 
144
            if (index < part.length - 1) {
 
145
               index++;
 
146
               testPartsIndex[testPartsIndexIndex] = index;
 
147
               carry = false;
 
148
            } else {
 
149
               testPartsIndex[testPartsIndexIndex] = 0;
 
150
               carry = true;
 
151
            }
 
152
         }
 
153
         maxIndex &= (index == (part.length - 1));
 
154
      }
 
155
 
 
156
 
 
157
      return (!maxIndex);
 
158
   }
 
159
 
 
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(',');
 
166
         } else {
 
167
            carryMsg.append('}');
 
168
         }
 
169
      }
 
170
      return carryMsg.toString();
 
171
 
 
172
   }
 
173
 
 
174
   public void testValidateUrl() {
 
175
      assertTrue(true);
 
176
   }
 
177
 
 
178
   /**
 
179
    * Only used to debug the unit tests.
 
180
    * @param argv
 
181
    */
 
182
   public static void main(String[] argv) {
 
183
 
 
184
      UrlTest fct = new UrlTest("url test");
 
185
      fct.setUp();
 
186
      fct.testIsValid();
 
187
      fct.testIsValidScheme();
 
188
   }
 
189
   //-------------------- Test data for creating a composite URL
 
190
   /**
 
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
 
196
    * valid.
 
197
    */
 
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)};
 
207
 
 
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)
 
226
   };
 
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)
 
234
   };
 
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)
 
245
   };
 
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)
 
262
   };
 
263
 
 
264
   TestPair[] testUrlQuery = {new TestPair("?action=view", true),
 
265
                              new TestPair("?action=edit&mode=up", true),
 
266
                              new TestPair("", true)
 
267
   };
 
268
 
 
269
   Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
 
270
   Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
 
271
   int[] testPartsIndex = {0, 0, 0, 0, 0};
 
272
 
 
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)};
 
278
 
 
279
 
 
280
}
 
 
b'\\ No newline at end of file'