GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / validation / routines / UrlValidatorTests.java
blob37a717ffe8970061963fc31bca7bc7c437bc5a37
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.codehaus.groovy.grails.validation.routines;
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
23 /**
24 * Performs Validation Test for url validations.
26 * @version $Revision: 595020 $ $Date: 2007-11-14 22:36:45 +0300 (Ср, 14 ноя 2007) $
28 public class UrlValidatorTests extends TestCase {
30 private boolean printStatus = false;
31 private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using.
33 public UrlValidatorTests(String testName) {
34 super(testName);
37 public static Test suite() {
38 return new TestSuite(UrlValidatorTests.class);
41 protected void setUp() {
42 for (int index = 0; index < testPartsIndex.length - 1; index++) {
43 testPartsIndex[index] = 0;
47 protected void tearDown() {
50 public void testIsValid() {
51 testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES);
52 setUp();
53 long options =
54 UrlValidator.ALLOW_2_SLASHES
55 + UrlValidator.ALLOW_ALL_SCHEMES
56 + UrlValidator.NO_FRAGMENTS;
58 testIsValid(testUrlPartsOptions, options);
61 public void testIsValidScheme() {
62 if (printStatus) {
63 System.out.print("\n testIsValidScheme() ");
65 String[] schemes = {"http", "gopher"};
66 //UrlValidator urlVal = new UrlValidator(schemes,false,false,false);
67 UrlValidator urlVal = new UrlValidator(schemes, 0);
68 for (int sIndex = 0; sIndex < testScheme.length; sIndex++) {
69 ResultPair testPair = testScheme[sIndex];
70 boolean result = urlVal.isValidScheme(testPair.item);
71 assertEquals(testPair.item, testPair.valid, result);
72 if (printStatus) {
73 if (result == testPair.valid) {
74 System.out.print('.');
75 } else {
76 System.out.print('X');
80 if (printStatus) {
81 System.out.println();
86 /**
87 * Create set of tests by taking the testUrlXXX arrays and
88 * running through all possible permutations of their combinations.
90 * @param testObjects Used to create a url.
92 public void testIsValid(Object[] testObjects, long options) {
93 UrlValidator urlVal = new UrlValidator(null, null, options);
94 assertTrue(urlVal.isValid("http://www.google.com"));
95 assertTrue(urlVal.isValid("http://www.google.com/"));
96 int statusPerLine = 60;
97 int printed = 0;
98 if (printIndex) {
99 statusPerLine = 6;
101 do {
102 StringBuffer testBuffer = new StringBuffer();
103 boolean expected = true;
104 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
105 int index = testPartsIndex[testPartsIndexIndex];
106 ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex];
107 testBuffer.append(part[index].item);
108 expected &= part[index].valid;
110 String url = testBuffer.toString();
111 boolean result = urlVal.isValid(url);
112 assertEquals(url, expected, result);
113 if (printStatus) {
114 if (printIndex) {
115 System.out.print(testPartsIndextoString());
116 } else {
117 if (result == expected) {
118 System.out.print('.');
119 } else {
120 System.out.print('X');
123 printed++;
124 if (printed == statusPerLine) {
125 System.out.println();
126 printed = 0;
129 } while (incrementTestPartsIndex(testPartsIndex, testObjects));
130 if (printStatus) {
131 System.out.println();
135 public void testValidator202() {
136 String[] schemes = {"http","https"};
137 UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS);
138 assertTrue(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.org"));
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"));
147 public void testValidator218() {
148 UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
149 assertTrue("parentheses should be valid in URLs",
150 validator.isValid("http://somewhere.com/pathxyz/file(1).html"));
153 public void testValidator248() {
154 RegexValidator regex = new RegexValidator(new String[] {"localhost", ".*\\.my-testing"});
155 UrlValidator validator = new UrlValidator(regex, 0);
157 assertTrue("localhost URL should validate",
158 validator.isValid("http://localhost/test/index.html"));
159 assertTrue("first.my-testing should validate",
160 validator.isValid("http://first.my-testing/test/index.html"));
161 assertTrue("sup3r.my-testing should validate",
162 validator.isValid("http://sup3r.my-testing/test/index.html"));
164 assertFalse("broke.my-test should not validate",
165 validator.isValid("http://broke.my-test/test/index.html"));
167 assertTrue("www.apache.org should still validate",
168 validator.isValid("http://www.apache.org/test/index.html"));
172 static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) {
173 boolean carry = true; //add 1 to lowest order part.
174 boolean maxIndex = true;
175 for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) {
176 int index = testPartsIndex[testPartsIndexIndex];
177 ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex];
178 if (carry) {
179 if (index < part.length - 1) {
180 index++;
181 testPartsIndex[testPartsIndexIndex] = index;
182 carry = false;
183 } else {
184 testPartsIndex[testPartsIndexIndex] = 0;
185 carry = true;
188 maxIndex &= (index == (part.length - 1));
192 return (!maxIndex);
195 private String testPartsIndextoString() {
196 StringBuffer carryMsg = new StringBuffer("{");
197 for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) {
198 carryMsg.append(testPartsIndex[testPartsIndexIndex]);
199 if (testPartsIndexIndex < testPartsIndex.length - 1) {
200 carryMsg.append(',');
201 } else {
202 carryMsg.append('}');
205 return carryMsg.toString();
209 public void testValidateUrl() {
210 assertTrue(true);
214 * Only used to debug the unit tests.
215 * @param argv
217 public static void main(String[] argv) {
219 UrlValidatorTests fct = new UrlValidatorTests("url test");
220 fct.setUp();
221 fct.testIsValid();
222 fct.testIsValidScheme();
224 //-------------------- Test data for creating a composite URL
226 * The data given below approximates the 4 parts of a URL
227 * <scheme>://<authority><path>?<query> except that the port number
228 * is broken out of authority to increase the number of permutations.
229 * A complete URL is composed of a scheme+authority+port+path+query,
230 * all of which must be individually valid for the entire URL to be considered
231 * valid.
233 ResultPair[] testUrlScheme = {new ResultPair("http://", true),
234 new ResultPair("ftp://", true),
235 new ResultPair("h3t://", true),
236 new ResultPair("3ht://", false),
237 new ResultPair("http:/", false),
238 new ResultPair("http:", false),
239 new ResultPair("http/", false),
240 new ResultPair("://", false),
241 new ResultPair("", true)};
243 ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true),
244 new ResultPair("go.com", true),
245 new ResultPair("go.au", true),
246 new ResultPair("0.0.0.0", true),
247 new ResultPair("255.255.255.255", true),
248 new ResultPair("256.256.256.256", false),
249 new ResultPair("255.com", true),
250 new ResultPair("1.2.3.4.5", false),
251 new ResultPair("1.2.3.4.", false),
252 new ResultPair("1.2.3", false),
253 new ResultPair(".1.2.3.4", false),
254 new ResultPair("go.a", false),
255 new ResultPair("go.a1a", false),
256 new ResultPair("go.cc", true),
257 new ResultPair("go.1aa", false),
258 new ResultPair("aaa.", false),
259 new ResultPair(".aaa", false),
260 new ResultPair("aaa", false),
261 new ResultPair("", false)
263 ResultPair[] testUrlPort = {new ResultPair(":80", true),
264 new ResultPair(":65535", true),
265 new ResultPair(":0", true),
266 new ResultPair("", true),
267 new ResultPair(":-1", false),
268 new ResultPair(":65636", true),
269 new ResultPair(":65a", false)
271 ResultPair[] testPath = {new ResultPair("/test1", true),
272 new ResultPair("/t123", true),
273 new ResultPair("/$23", true),
274 new ResultPair("/..", false),
275 new ResultPair("/../", false),
276 new ResultPair("/test1/", true),
277 new ResultPair("", true),
278 new ResultPair("/test1/file", true),
279 new ResultPair("/..//file", false),
280 new ResultPair("/test1//file", false)
282 //Test allow2slash, noFragment
283 ResultPair[] testUrlPathOptions = {new ResultPair("/test1", true),
284 new ResultPair("/t123", true),
285 new ResultPair("/$23", true),
286 new ResultPair("/..", false),
287 new ResultPair("/../", false),
288 new ResultPair("/test1/", true),
289 new ResultPair("/#", false),
290 new ResultPair("", true),
291 new ResultPair("/test1/file", true),
292 new ResultPair("/t123/file", true),
293 new ResultPair("/$23/file", true),
294 new ResultPair("/../file", false),
295 new ResultPair("/..//file", false),
296 new ResultPair("/test1//file", true),
297 new ResultPair("/#/file", false)
300 ResultPair[] testUrlQuery = {new ResultPair("?action=view", true),
301 new ResultPair("?action=edit&mode=up", true),
302 new ResultPair("", true)
305 Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery};
306 Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery};
307 int[] testPartsIndex = {0, 0, 0, 0, 0};
309 //---------------- Test data for individual url parts ----------------
310 ResultPair[] testScheme = {new ResultPair("http", true),
311 new ResultPair("ftp", false),
312 new ResultPair("httpd", false),
313 new ResultPair("telnet", false)};