Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 67427 invoked from network); 14 Nov 2007 16:35:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 14 Nov 2007 16:35:20 -0000 Received: (qmail 4957 invoked by uid 500); 14 Nov 2007 16:35:07 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 4605 invoked by uid 500); 14 Nov 2007 16:35:06 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 4596 invoked by uid 99); 14 Nov 2007 16:35:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Nov 2007 08:35:05 -0800 X-ASF-Spam-Status: No, hits=-96.8 required=10.0 tests=ALL_TRUSTED,LONGWORDS X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 14 Nov 2007 16:35:05 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 91DB91A9844; Wed, 14 Nov 2007 08:34:45 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r594937 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/ test/java/org/apache/commons/validator/routines/ Date: Wed, 14 Nov 2007 16:34:44 -0000 To: commits@commons.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20071114163445.91DB91A9844@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: niallp Date: Wed Nov 14 08:34:43 2007 New Revision: 594937 URL: http://svn.apache.org/viewvc?rev=594937&view=rev Log: Remove UrlValidator dependency on util/Flags and switch to using "long" for flags for URL and CreditCard options Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java?rev=594937&r1=594936&r2=594937&view=diff ============================================================================== --- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java (original) +++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java Wed Nov 14 08:34:43 2007 @@ -54,32 +54,32 @@ * v.isValid(aCardNumber); * */ - public static final int NONE = 0; + public static final long NONE = 0; /** * Option specifying that American Express cards are allowed. */ - public static final int AMEX = 1 << 0; + public static final long AMEX = 1 << 0; /** * Option specifying that Visa cards are allowed. */ - public static final int VISA = 1 << 1; + public static final long VISA = 1 << 1; /** * Option specifying that Mastercard cards are allowed. */ - public static final int MASTERCARD = 1 << 2; + public static final long MASTERCARD = 1 << 2; /** * Option specifying that Discover cards are allowed. */ - public static final int DISCOVER = 1 << 3; + public static final long DISCOVER = 1 << 3; /** * Option specifying that Diners cards are allowed. */ - public static final int DINERS = 1 << 4; + public static final long DINERS = 1 << 4; /** * The CreditCardTypes that are allowed to pass validation. @@ -122,7 +122,7 @@ * CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that * those are the only valid card types. */ - public CreditCardValidator(int options) { + public CreditCardValidator(long options) { super(); if (isOn(options, VISA)) { @@ -207,7 +207,7 @@ * * @return whether the specified flag value is on. */ - private boolean isOn(int options, int flag) { + private boolean isOn(long options, long flag) { return (options & flag) > 0; } Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java?rev=594937&r1=594936&r2=594937&view=diff ============================================================================== --- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java (original) +++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/UrlValidator.java Wed Nov 14 08:34:43 2007 @@ -16,8 +16,6 @@ */ package org.apache.commons.validator.routines; -import org.apache.commons.validator.util.Flags; - import java.io.Serializable; import java.util.Arrays; import java.util.HashSet; @@ -81,23 +79,23 @@ * Allows all validly formatted schemes to pass validation instead of * supplying a set of valid schemes. */ - public static final int ALLOW_ALL_SCHEMES = 1 << 0; + public static final long ALLOW_ALL_SCHEMES = 1 << 0; /** * Allow two slashes in the path component of the URL. */ - public static final int ALLOW_2_SLASHES = 1 << 1; + public static final long ALLOW_2_SLASHES = 1 << 1; /** * Enabling this options disallows any URL fragments. */ - public static final int NO_FRAGMENTS = 1 << 2; + public static final long NO_FRAGMENTS = 1 << 2; /** * If enabled, consults a provided list of RegexValidators when validating * the URL authority. This allows names like "localhost", "test-machine", etc. */ - public static final int MANUAL_AUTHORITY_VALIDATION = 1 << 3; + public static final long MANUAL_AUTHORITY_VALIDATION = 1 << 3; // Drop numeric, and "+-." for now private static final String AUTHORITY_CHARS_REGEX = "\\p{Alnum}\\-\\."; @@ -161,7 +159,7 @@ /** * Holds the set of current validation options. */ - private Flags options = null; + private final long options; /** * The set of schemes that are allowed to be in a URL. @@ -208,7 +206,7 @@ * ignore the contents of schemes. */ public UrlValidator(String[] schemes) { - this(schemes, 0); + this(schemes, (long)0); } /** @@ -217,7 +215,7 @@ * this class. To set multiple options you simply add them together. For example, * ALLOW_2_SLASHES + NO_FRAGMENTS enables both of those options. */ - public UrlValidator(int options) { + public UrlValidator(long options) { this(null, null, options); } @@ -228,7 +226,7 @@ * this class. To set multiple options you simply add them together. For example, * ALLOW_2_SLASHES + NO_FRAGMENTS enables both of those options. */ - public UrlValidator(String[] schemes, int options) { + public UrlValidator(String[] schemes, long options) { this(schemes, null, options); } @@ -241,7 +239,7 @@ *

ALLOW_2_SLASHES + NO_FRAGMENTS

* enables both of those options. */ - public UrlValidator(RegexValidator[] authorityValidators, int options) { + public UrlValidator(RegexValidator[] authorityValidators, long options) { this(null, authorityValidators, options); } @@ -254,10 +252,10 @@ *

ALLOW_2_SLASHES + NO_FRAGMENTS

* enables both of those options. */ - public UrlValidator(String[] schemes, RegexValidator[] authorityValidators, int options) { - this.options = new Flags(options); + public UrlValidator(String[] schemes, RegexValidator[] authorityValidators, long options) { + this.options = options; - if (this.options.isOn(ALLOW_ALL_SCHEMES)) { + if (isOn(ALLOW_ALL_SCHEMES)) { return; } @@ -265,7 +263,7 @@ schemes = this.defaultSchemes; } - if (this.options.isOn(MANUAL_AUTHORITY_VALIDATION)) { + if (isOn(MANUAL_AUTHORITY_VALIDATION)) { this.authorityValidators = authorityValidators; } @@ -334,7 +332,7 @@ return false; } - if (this.options.isOff(ALLOW_ALL_SCHEMES)) { + if (isOff(ALLOW_ALL_SCHEMES)) { if (!this.allowedSchemes.contains(scheme)) { return false; @@ -356,7 +354,7 @@ } // check manual authority validation if specified - if (this.options.isOn(MANUAL_AUTHORITY_VALIDATION)) { + if (isOn(MANUAL_AUTHORITY_VALIDATION)) { if (authorityValidators == null) { throw new IllegalStateException( "manual authority validation enabled, but no authority validators specified"); @@ -396,7 +394,7 @@ } String extra = authorityMatcher.group(PARSE_AUTHORITY_EXTRA); - if (extra== null) || extra.trim().length() == 0){ + if (extra != null && extra.trim().length() > 0){ return false; } @@ -418,7 +416,7 @@ } int slash2Count = countToken("//", path); - if (this.options.isOff(ALLOW_2_SLASHES) && (slash2Count > 0)) { + if (isOff(ALLOW_2_SLASHES) && (slash2Count > 0)) { return false; } @@ -456,7 +454,7 @@ return true; } - return this.options.isOff(NO_FRAGMENTS); + return isOff(NO_FRAGMENTS); } /** @@ -476,5 +474,29 @@ } } return count; + } + + /** + * Tests whether the given flag is on. If the flag is not a power of 2 + * (ie. 3) this tests whether the combination of flags is on. + * + * @param flag Flag value to check. + * + * @return whether the specified flag value is on. + */ + private boolean isOn(long flag) { + return (this.options & flag) > 0; + } + + /** + * Tests whether the given flag is off. If the flag is not a power of 2 + * (ie. 3) this tests whether the combination of flags is off. + * + * @param flag Flag value to check. + * + * @return whether the specified flag value is off. + */ + private boolean isOff(long flag) { + return (this.options & flag) == 0; } } Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java?rev=594937&r1=594936&r2=594937&view=diff ============================================================================== --- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java (original) +++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java Wed Nov 14 08:34:43 2007 @@ -1,321 +1,321 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.validator.routines; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.apache.commons.validator.ResultPair; - -/** - * Performs Validation Test for url validations. - * - * @version $Revision: 493905 $ $Date: 2007-01-07 18:11:38 -0800 (Sun, 07 Jan 2007) $ - */ -public class UrlValidatorTest extends TestCase { - - private boolean printStatus = false; - private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using. - - public UrlValidatorTest(String testName) { - super(testName); - } - - public static Test suite() { - return new TestSuite(UrlValidatorTest.class); - } - - protected void setUp() { - for (int index = 0; index < testPartsIndex.length - 1; index++) { - testPartsIndex[index] = 0; - } - } - - protected void tearDown() { - } - - public void testIsValid() { - testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES); - setUp(); - int options = - UrlValidator.ALLOW_2_SLASHES - + UrlValidator.ALLOW_ALL_SCHEMES - + UrlValidator.NO_FRAGMENTS; - - testIsValid(testUrlPartsOptions, options); - } - - public void testIsValidScheme() { - if (printStatus) { - System.out.print("\n testIsValidScheme() "); - } - String[] schemes = {"http", "gopher"}; - //UrlValidator urlVal = new UrlValidator(schemes,false,false,false); - UrlValidator urlVal = new UrlValidator(schemes, 0); - for (int sIndex = 0; sIndex < testScheme.length; sIndex++) { - ResultPair testPair = testScheme[sIndex]; - boolean result = urlVal.isValidScheme(testPair.item); - assertEquals(testPair.item, testPair.valid, result); - if (printStatus) { - if (result == testPair.valid) { - System.out.print('.'); - } else { - System.out.print('X'); - } - } - } - if (printStatus) { - System.out.println(); - } - - } - - /** - * Create set of tests by taking the testUrlXXX arrays and - * running through all possible permutations of their combinations. - * - * @param testObjects Used to create a url. - */ - public void testIsValid(Object[] testObjects, int options) { - UrlValidator urlVal = new UrlValidator(null, null, options); - assertTrue(urlVal.isValid("http://www.google.com")); - assertTrue(urlVal.isValid("http://www.google.com/")); - int statusPerLine = 60; - int printed = 0; - if (printIndex) { - statusPerLine = 6; - } - do { - StringBuffer testBuffer = new StringBuffer(); - boolean expected = true; - for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { - int index = testPartsIndex[testPartsIndexIndex]; - ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex]; - testBuffer.append(part[index].item); - expected &= part[index].valid; - } - String url = testBuffer.toString(); - boolean result = urlVal.isValid(url); - assertEquals(url, expected, result); - if (printStatus) { - if (printIndex) { - System.out.print(testPartsIndextoString()); - } else { - if (result == expected) { - System.out.print('.'); - } else { - System.out.print('X'); - } - } - printed++; - if (printed == statusPerLine) { - System.out.println(); - printed = 0; - } - } - } while (incrementTestPartsIndex(testPartsIndex, testObjects)); - if (printStatus) { - System.out.println(); - } - } - - public void testValidator202() { - String[] schemes = {"http","https"}; - UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS); - 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")); - } - - public void testValidator204() { - String[] schemes = {"http","https"}; - UrlValidator urlValidator = new UrlValidator(schemes); - assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5")); - } - - public void testValidator218() { - UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES); - assertTrue("parentheses should be valid in URLs", - validator.isValid("http://somewhere.com/pathxyz/file(1).html")); - } - - public void testValidator248() { - RegexValidator[] regex = new RegexValidator[] { - new RegexValidator("localhost"), - new RegexValidator(".*\\.my-testing") - }; - UrlValidator validator = new UrlValidator(regex, UrlValidator.MANUAL_AUTHORITY_VALIDATION); - - assertTrue("localhost URL should validate", - validator.isValid("http://localhost/test/index.html")); - assertTrue("first.my-testing should validate", - validator.isValid("http://first.my-testing/test/index.html")); - assertTrue("sup3r.my-testing should validate", - validator.isValid("http://sup3r.my-testing/test/index.html")); - - assertFalse("broke.my-test should not validate", - validator.isValid("http://broke.my-test/test/index.html")); - - assertTrue("www.apache.org should still validate", - validator.isValid("http://www.apache.org/test/index.html")); - - } - - static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) { - boolean carry = true; //add 1 to lowest order part. - boolean maxIndex = true; - for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) { - int index = testPartsIndex[testPartsIndexIndex]; - ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex]; - if (carry) { - if (index < part.length - 1) { - index++; - testPartsIndex[testPartsIndexIndex] = index; - carry = false; - } else { - testPartsIndex[testPartsIndexIndex] = 0; - carry = true; - } - } - maxIndex &= (index == (part.length - 1)); - } - - - return (!maxIndex); - } - - private String testPartsIndextoString() { - StringBuffer carryMsg = new StringBuffer("{"); - for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { - carryMsg.append(testPartsIndex[testPartsIndexIndex]); - if (testPartsIndexIndex < testPartsIndex.length - 1) { - carryMsg.append(','); - } else { - carryMsg.append('}'); - } - } - return carryMsg.toString(); - - } - - public void testValidateUrl() { - assertTrue(true); - } - - /** - * Only used to debug the unit tests. - * @param argv - */ - public static void main(String[] argv) { - - UrlValidatorTest fct = new UrlValidatorTest("url test"); - fct.setUp(); - fct.testIsValid(); - fct.testIsValidScheme(); - } - //-------------------- Test data for creating a composite URL - /** - * The data given below approximates the 4 parts of a URL - * ://? except that the port number - * is broken out of authority to increase the number of permutations. - * A complete URL is composed of a scheme+authority+port+path+query, - * all of which must be individually valid for the entire URL to be considered - * valid. - */ - ResultPair[] testUrlScheme = {new ResultPair("http://", true), - new ResultPair("ftp://", true), - new ResultPair("h3t://", true), - new ResultPair("3ht://", false), - new ResultPair("http:/", false), - new ResultPair("http:", false), - new ResultPair("http/", false), - new ResultPair("://", false), - new ResultPair("", true)}; - - ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true), - new ResultPair("go.com", true), - new ResultPair("go.au", true), - new ResultPair("0.0.0.0", true), - new ResultPair("255.255.255.255", true), - new ResultPair("256.256.256.256", false), - new ResultPair("255.com", true), - new ResultPair("1.2.3.4.5", false), - new ResultPair("1.2.3.4.", false), - new ResultPair("1.2.3", false), - new ResultPair(".1.2.3.4", false), - new ResultPair("go.a", false), - new ResultPair("go.a1a", false), - new ResultPair("go.cc", true), - new ResultPair("go.1aa", false), - new ResultPair("aaa.", false), - new ResultPair(".aaa", false), - new ResultPair("aaa", false), - new ResultPair("", false) - }; - ResultPair[] testUrlPort = {new ResultPair(":80", true), - new ResultPair(":65535", true), - new ResultPair(":0", true), - new ResultPair("", true), - new ResultPair(":-1", false), - new ResultPair(":65636", true), - new ResultPair(":65a", false) - }; - ResultPair[] testPath = {new ResultPair("/test1", true), - new ResultPair("/t123", true), - new ResultPair("/$23", true), - new ResultPair("/..", false), - new ResultPair("/../", false), - new ResultPair("/test1/", true), - new ResultPair("", true), - new ResultPair("/test1/file", true), - new ResultPair("/..//file", false), - new ResultPair("/test1//file", false) - }; - //Test allow2slash, noFragment - ResultPair[] testUrlPathOptions = {new ResultPair("/test1", true), - new ResultPair("/t123", true), - new ResultPair("/$23", true), - new ResultPair("/..", false), - new ResultPair("/../", false), - new ResultPair("/test1/", true), - new ResultPair("/#", false), - new ResultPair("", true), - new ResultPair("/test1/file", true), - new ResultPair("/t123/file", true), - new ResultPair("/$23/file", true), - new ResultPair("/../file", false), - new ResultPair("/..//file", false), - new ResultPair("/test1//file", true), - new ResultPair("/#/file", false) - }; - - ResultPair[] testUrlQuery = {new ResultPair("?action=view", true), - new ResultPair("?action=edit&mode=up", true), - new ResultPair("", true) - }; - - Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery}; - Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery}; - int[] testPartsIndex = {0, 0, 0, 0, 0}; - - //---------------- Test data for individual url parts ---------------- - ResultPair[] testScheme = {new ResultPair("http", true), - new ResultPair("ftp", false), - new ResultPair("httpd", false), - new ResultPair("telnet", false)}; - - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.validator.routines; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.validator.ResultPair; + +/** + * Performs Validation Test for url validations. + * + * @version $Revision: 493905 $ $Date: 2007-01-07 18:11:38 -0800 (Sun, 07 Jan 2007) $ + */ +public class UrlValidatorTest extends TestCase { + + private boolean printStatus = false; + private boolean printIndex = false;//print index that indicates current scheme,host,port,path, query test were using. + + public UrlValidatorTest(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(UrlValidatorTest.class); + } + + protected void setUp() { + for (int index = 0; index < testPartsIndex.length - 1; index++) { + testPartsIndex[index] = 0; + } + } + + protected void tearDown() { + } + + public void testIsValid() { + testIsValid(testUrlParts, UrlValidator.ALLOW_ALL_SCHEMES); + setUp(); + long options = + UrlValidator.ALLOW_2_SLASHES + + UrlValidator.ALLOW_ALL_SCHEMES + + UrlValidator.NO_FRAGMENTS; + + testIsValid(testUrlPartsOptions, options); + } + + public void testIsValidScheme() { + if (printStatus) { + System.out.print("\n testIsValidScheme() "); + } + String[] schemes = {"http", "gopher"}; + //UrlValidator urlVal = new UrlValidator(schemes,false,false,false); + UrlValidator urlVal = new UrlValidator(schemes, 0); + for (int sIndex = 0; sIndex < testScheme.length; sIndex++) { + ResultPair testPair = testScheme[sIndex]; + boolean result = urlVal.isValidScheme(testPair.item); + assertEquals(testPair.item, testPair.valid, result); + if (printStatus) { + if (result == testPair.valid) { + System.out.print('.'); + } else { + System.out.print('X'); + } + } + } + if (printStatus) { + System.out.println(); + } + + } + + /** + * Create set of tests by taking the testUrlXXX arrays and + * running through all possible permutations of their combinations. + * + * @param testObjects Used to create a url. + */ + public void testIsValid(Object[] testObjects, long options) { + UrlValidator urlVal = new UrlValidator(null, null, options); + assertTrue(urlVal.isValid("http://www.google.com")); + assertTrue(urlVal.isValid("http://www.google.com/")); + int statusPerLine = 60; + int printed = 0; + if (printIndex) { + statusPerLine = 6; + } + do { + StringBuffer testBuffer = new StringBuffer(); + boolean expected = true; + for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { + int index = testPartsIndex[testPartsIndexIndex]; + ResultPair[] part = (ResultPair[]) testObjects[testPartsIndexIndex]; + testBuffer.append(part[index].item); + expected &= part[index].valid; + } + String url = testBuffer.toString(); + boolean result = urlVal.isValid(url); + assertEquals(url, expected, result); + if (printStatus) { + if (printIndex) { + System.out.print(testPartsIndextoString()); + } else { + if (result == expected) { + System.out.print('.'); + } else { + System.out.print('X'); + } + } + printed++; + if (printed == statusPerLine) { + System.out.println(); + printed = 0; + } + } + } while (incrementTestPartsIndex(testPartsIndex, testObjects)); + if (printStatus) { + System.out.println(); + } + } + + public void testValidator202() { + String[] schemes = {"http","https"}; + UrlValidator urlValidator = new UrlValidator(schemes, UrlValidator.NO_FRAGMENTS); + 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")); + } + + public void testValidator204() { + String[] schemes = {"http","https"}; + UrlValidator urlValidator = new UrlValidator(schemes); + assertTrue(urlValidator.isValid("http://tech.yahoo.com/rc/desktops/102;_ylt=Ao8yevQHlZ4On0O3ZJGXLEQFLZA5")); + } + + public void testValidator218() { + UrlValidator validator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES); + assertTrue("parentheses should be valid in URLs", + validator.isValid("http://somewhere.com/pathxyz/file(1).html")); + } + + public void testValidator248() { + RegexValidator[] regex = new RegexValidator[] { + new RegexValidator("localhost"), + new RegexValidator(".*\\.my-testing") + }; + UrlValidator validator = new UrlValidator(regex, UrlValidator.MANUAL_AUTHORITY_VALIDATION); + + assertTrue("localhost URL should validate", + validator.isValid("http://localhost/test/index.html")); + assertTrue("first.my-testing should validate", + validator.isValid("http://first.my-testing/test/index.html")); + assertTrue("sup3r.my-testing should validate", + validator.isValid("http://sup3r.my-testing/test/index.html")); + + assertFalse("broke.my-test should not validate", + validator.isValid("http://broke.my-test/test/index.html")); + + assertTrue("www.apache.org should still validate", + validator.isValid("http://www.apache.org/test/index.html")); + + } + + static boolean incrementTestPartsIndex(int[] testPartsIndex, Object[] testParts) { + boolean carry = true; //add 1 to lowest order part. + boolean maxIndex = true; + for (int testPartsIndexIndex = testPartsIndex.length - 1; testPartsIndexIndex >= 0; --testPartsIndexIndex) { + int index = testPartsIndex[testPartsIndexIndex]; + ResultPair[] part = (ResultPair[]) testParts[testPartsIndexIndex]; + if (carry) { + if (index < part.length - 1) { + index++; + testPartsIndex[testPartsIndexIndex] = index; + carry = false; + } else { + testPartsIndex[testPartsIndexIndex] = 0; + carry = true; + } + } + maxIndex &= (index == (part.length - 1)); + } + + + return (!maxIndex); + } + + private String testPartsIndextoString() { + StringBuffer carryMsg = new StringBuffer("{"); + for (int testPartsIndexIndex = 0; testPartsIndexIndex < testPartsIndex.length; ++testPartsIndexIndex) { + carryMsg.append(testPartsIndex[testPartsIndexIndex]); + if (testPartsIndexIndex < testPartsIndex.length - 1) { + carryMsg.append(','); + } else { + carryMsg.append('}'); + } + } + return carryMsg.toString(); + + } + + public void testValidateUrl() { + assertTrue(true); + } + + /** + * Only used to debug the unit tests. + * @param argv + */ + public static void main(String[] argv) { + + UrlValidatorTest fct = new UrlValidatorTest("url test"); + fct.setUp(); + fct.testIsValid(); + fct.testIsValidScheme(); + } + //-------------------- Test data for creating a composite URL + /** + * The data given below approximates the 4 parts of a URL + * ://? except that the port number + * is broken out of authority to increase the number of permutations. + * A complete URL is composed of a scheme+authority+port+path+query, + * all of which must be individually valid for the entire URL to be considered + * valid. + */ + ResultPair[] testUrlScheme = {new ResultPair("http://", true), + new ResultPair("ftp://", true), + new ResultPair("h3t://", true), + new ResultPair("3ht://", false), + new ResultPair("http:/", false), + new ResultPair("http:", false), + new ResultPair("http/", false), + new ResultPair("://", false), + new ResultPair("", true)}; + + ResultPair[] testUrlAuthority = {new ResultPair("www.google.com", true), + new ResultPair("go.com", true), + new ResultPair("go.au", true), + new ResultPair("0.0.0.0", true), + new ResultPair("255.255.255.255", true), + new ResultPair("256.256.256.256", false), + new ResultPair("255.com", true), + new ResultPair("1.2.3.4.5", false), + new ResultPair("1.2.3.4.", false), + new ResultPair("1.2.3", false), + new ResultPair(".1.2.3.4", false), + new ResultPair("go.a", false), + new ResultPair("go.a1a", false), + new ResultPair("go.cc", true), + new ResultPair("go.1aa", false), + new ResultPair("aaa.", false), + new ResultPair(".aaa", false), + new ResultPair("aaa", false), + new ResultPair("", false) + }; + ResultPair[] testUrlPort = {new ResultPair(":80", true), + new ResultPair(":65535", true), + new ResultPair(":0", true), + new ResultPair("", true), + new ResultPair(":-1", false), + new ResultPair(":65636", true), + new ResultPair(":65a", false) + }; + ResultPair[] testPath = {new ResultPair("/test1", true), + new ResultPair("/t123", true), + new ResultPair("/$23", true), + new ResultPair("/..", false), + new ResultPair("/../", false), + new ResultPair("/test1/", true), + new ResultPair("", true), + new ResultPair("/test1/file", true), + new ResultPair("/..//file", false), + new ResultPair("/test1//file", false) + }; + //Test allow2slash, noFragment + ResultPair[] testUrlPathOptions = {new ResultPair("/test1", true), + new ResultPair("/t123", true), + new ResultPair("/$23", true), + new ResultPair("/..", false), + new ResultPair("/../", false), + new ResultPair("/test1/", true), + new ResultPair("/#", false), + new ResultPair("", true), + new ResultPair("/test1/file", true), + new ResultPair("/t123/file", true), + new ResultPair("/$23/file", true), + new ResultPair("/../file", false), + new ResultPair("/..//file", false), + new ResultPair("/test1//file", true), + new ResultPair("/#/file", false) + }; + + ResultPair[] testUrlQuery = {new ResultPair("?action=view", true), + new ResultPair("?action=edit&mode=up", true), + new ResultPair("", true) + }; + + Object[] testUrlParts = {testUrlScheme, testUrlAuthority, testUrlPort, testPath, testUrlQuery}; + Object[] testUrlPartsOptions = {testUrlScheme, testUrlAuthority, testUrlPort, testUrlPathOptions, testUrlQuery}; + int[] testPartsIndex = {0, 0, 0, 0, 0}; + + //---------------- Test data for individual url parts ---------------- + ResultPair[] testScheme = {new ResultPair("http", true), + new ResultPair("ftp", false), + new ResultPair("httpd", false), + new ResultPair("telnet", false)}; + + +}