Author: niallp
Date: Fri Dec 8 16:33:59 2006
New Revision: 484865
URL: http://svn.apache.org/viewvc?view=rev&rev=484865
Log:
VALIDATOR-214 - New Regular Expression validator using JDK 1.4's Regex
Added:
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
(with props)
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
(with props)
Modified:
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
Added:
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java?view=auto&rev=484865
==============================================================================
---
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
(added)
+++
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
Fri Dec 8 16:33:59 2006
@@ -0,0 +1,452 @@
+/*
+ * 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 java.io.Serializable;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+/**
+ * Regular Expression validator (using JDK 1.4+ regex support).
+ * <p>
+ * This validator provides convenient regular expression validation
+ * in one of two ways:
+ *
+ * <h4>1. One Off validation using the static methods<h4>
+ * <ul>
+ * <li>Validate <code>true</code> or <code>false</code>:</li>
+ * <ul>
+ * <li><code>boolean valid = RegexValidator.isValid(value,
regex);</code></li>
+ * <li><code>boolean valid = RegexValidator.isValid(value, regex,
caseSensitive);</code></li>
+ * </ul>
+ * <li>Validate returning an aggregated String of the matched groups:</li>
+ * <ul>
+ * <li><code>String result = RegexValidator.validate(value,
regex);</code></li>
+ * <li><code>String result = RegexValidator.validate(value, regex,
caseSensitive);</code></li>
+ * </ul>
+ * <li>Validate returning the matched groups:</li>
+ * <ul>
+ * <li><code>String[] result = RegexValidator.match(value,
regex);</code></li>
+ * <li><code>String[] result = RegexValidator.match(value, regex,
caseSensitive);</code></li>
+ * </ul>
+ * </ul>
+ *
+ * <h4>2. Re-using cached instances validating against one or more regular
expression<h4>
+ * Construct the validator either for a single regular expression or a set
(array) of
+ * regular expressions. By default validation is <i>case sensitive</i> but
constructors
+ * are provided to allow <i>case in-sensitive</i> validation. For example to
create
+ * a validator which does <i><i>case in-sensitive</i> validation for a set of
regular
+ * expressions:
+ * <pre>
+ * String[] regexs = new String[] {...};
+ * RegexValidator validator = new RegexValidator(regexs, false);
+ * </pre>
+ * <p>
+ * <ul>
+ * <li>Validate <code>true</code> or <code>false</code>:</li>
+ * <ul>
+ * <li><code>boolean valid = validator.isValid(value);</code></li>
+ * </ul>
+ * <li>Validate returning an aggregated String of the matched groups:</li>
+ * <ul>
+ * <li><code>String result = validator.validate(value);</code></li>
+ * </ul>
+ * <li>Validate returning the matched groups:</li>
+ * <ul>
+ * <li><code>String[] result = validator.match(value);</code></li>
+ * </ul>
+ * </ul>
+ * <p>
+ * Cached instances pre-compile and re-use [EMAIL PROTECTED] Pattern}(s) -
which according
+ * to the [EMAIL PROTECTED] Pattern} API are safe to use in a multi-threaded
environment.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class RegexValidator implements Serializable {
+
+ private static final String MISSING_REGEX = "Regular Expression is
missing";
+ private Pattern pattern;
+ private Pattern[] patterns;
+
+ /**
+ * Construct a <i>case sensitive</i> validator for a single
+ * regular expression.
+ *
+ * @param regex The regular expression this validator will
+ * validate against
+ */
+ public RegexValidator(String regex) {
+ this(regex, 0);
+ }
+
+ /**
+ * Construct a validator for a single regular expression
+ * with the specified case sensitivity.
+ *
+ * @param regex The regular expression this validator will
+ * validate against
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ */
+ public RegexValidator(String regex, boolean caseSensitive) {
+ this(regex, (caseSensitive ? 0 : Pattern.CASE_INSENSITIVE));
+ }
+
+ /**
+ * Construct a validator for a single regular expression with
+ * the specified flags.
+ *
+ * @param regex The regular expression this validator will
+ * validate against
+ * @param flags Bit mask of matching flags - see [EMAIL PROTECTED]
Pattern} for values
+ */
+ private RegexValidator(String regex, int flags) {
+ if (regex == null || regex.length() == 0) {
+ throw new IllegalArgumentException(MISSING_REGEX);
+ }
+ pattern = Pattern.compile(regex, flags);
+ }
+
+ /**
+ * Construct a <i>case sensitive</i> validator for a set
+ * of regular expressions.
+ *
+ * @param regexs The set of regular expressions this validator will
+ * validate against
+ */
+ public RegexValidator(String[] regexs) {
+ this(regexs, 0);
+ }
+
+ /**
+ * Construct a validator for a set of regular expressions
+ * with the specified case sensitivity.
+ *
+ * @param regexs The set of regular expressions this validator will
+ * validate against
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ */
+ public RegexValidator(String[] regexs, boolean caseSensitive) {
+ this(regexs, (caseSensitive ? 0: Pattern.CASE_INSENSITIVE));
+ }
+
+ /**
+ * Construct a validator for a set of regular expressions
+ * the specified flags.
+ *
+ * @param regexs The set of regular expressions this validator will
+ * validate against
+ * @param flags Bit mask of matching flags - see [EMAIL PROTECTED]
Pattern} for values
+ */
+ private RegexValidator(String[] regexs, int flags) {
+ if (regexs == null || regexs.length == 0) {
+ throw new IllegalArgumentException("Regular expressions are
missing");
+ }
+ patterns = new Pattern[regexs.length];
+ for (int i = 0; i < regexs.length; i++) {
+ if (regexs[i] == null || regexs[i].length() == 0) {
+ throw new IllegalArgumentException("Regular expression[" + i +
"] is missing");
+ }
+ patterns[i] = Pattern.compile(regexs[i], flags);
+ }
+ }
+
+ /**
+ * Validate a value against the set of regular expressions.
+ *
+ * @param value The value to validate.
+ * @return <code>true</code> if the value is valid
+ * otherwise <code>false</code>.
+ */
+ public boolean isValid(String value) {
+ if (value == null) {
+ return false;
+ } else if (pattern != null) {
+ return pattern.matcher(value).matches();
+ } else {
+ for (int i = 0; i < patterns.length; i++) {
+ if (patterns[i].matcher(value).matches()) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * Validate a value against a regular expression
+ * (<i>case sensitive</i>).
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @return <code>true</code> if the value is valid
+ * otherwise <code>false</code>.
+ */
+ public static boolean isValid(String value, String regex) {
+ return RegexValidator.isValid(value, regex, 0);
+ }
+
+ /**
+ * Validate a value against a regular expression
+ * with the specified case sensitivity.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ * @return <code>true</code> if the value is valid
+ * otherwise <code>false</code>.
+ */
+ public static boolean isValid(String value, String regex, boolean
caseSensitive) {
+ int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
+ return RegexValidator.isValid(value, regex, flags);
+ }
+
+ /**
+ * Validate a value against a regular expression with the
+ * specified flags.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param flags Bit mask of matching flags - see [EMAIL PROTECTED]
Pattern} for values
+ * @return <code>true</code> if the value is valid
+ * otherwise <code>false</code>.
+ */
+ private static boolean isValid(String value, String regex, int flags) {
+ if (regex == null || regex.length() == 0) {
+ throw new IllegalArgumentException(MISSING_REGEX);
+ }
+ if (value == null) {
+ return false;
+ } else {
+ return Pattern.compile(regex, flags).matcher(value).matches();
+ }
+ }
+
+ /**
+ * Validate a value against the set of regular expressions
+ * returning the array of matched groups.
+ *
+ * @param value The value to validate.
+ * @return String array of the <i>groups</i> matched if
+ * valid or <code>null</code> if invalid
+ */
+ public String[] match(String value) {
+ if (value == null) {
+ return null;
+ } else if (pattern != null) {
+ return RegexValidator.match(value, pattern);
+ } else {
+ String[] result = null;
+ for (int i = 0; i < patterns.length; i++) {
+ result = RegexValidator.match(value, patterns[i]);
+ if (result != null) {
+ return result;
+ }
+ }
+ return null;
+ }
+ }
+
+ /**
+ * Validate a value against the specified regular expression
+ * returning the matched groups (<i>case sensitive</i>).
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @return String array of the <i>groups</i> matched if
+ * valid or <code>null</code> if invalid
+ */
+ public static String[] match(String value, String regex) {
+ return RegexValidator.match(value, regex, 0);
+ }
+
+ /**
+ * Validate a value against a regular expression with the
+ * specified <i>case sensitivity</i> returning the matched groups.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ * @return String array of the <i>groups</i> matched if
+ * valid or <code>null</code> if invalid
+ */
+ public static String[] match(String value, String regex, boolean
caseSensitive) {
+ int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
+ return RegexValidator.match(value, regex, flags);
+ }
+
+ /**
+ * Validate a value against a regular expression with the
+ * specified flags returning the matched groups.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ * @return String array of the <i>groups</i> matched if
+ * valid or <code>null</code> if invalid
+ */
+ private static String[] match(String value, String regex, int flags) {
+ if (regex == null || regex.length() == 0) {
+ throw new IllegalArgumentException(MISSING_REGEX);
+ }
+ if (value == null) {
+ return null;
+ }
+ return RegexValidator.match(value, Pattern.compile(regex, flags));
+ }
+
+ /**
+ * Validate a value against the specified pattern
+ * returning the matched groups.
+ *
+ * @param value Value to validate
+ * @param pattern The pattern to match against.
+ * @return String array of the <i>groups</i> matched if
+ * valid or <code>null</code> if invalid
+ */
+ private static String[] match(String value, Pattern pattern) {
+ Matcher matcher = pattern.matcher(value);
+ if (matcher.matches()) {
+ int count = matcher.groupCount();
+ String[] groups = new String[count];
+ for (int i = 0; i < count; i++) {
+ groups[i] = matcher.group(i+1);
+ }
+ return groups;
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Validate a value against the set of regular expressions
+ * returning a String value of the aggregated groups.
+ *
+ * @param value The value to validate.
+ * @return Aggregated String value comprised of the
+ * <i>groups</i> matched if valid or <code>null</code> if invalid
+ */
+ public String validate(String value) {
+ if (value == null) {
+ return null;
+ } else if (pattern != null) {
+ return RegexValidator.validate(value, pattern);
+ } else {
+ String result = null;
+ for (int i = 0; i < patterns.length; i++) {
+ result = RegexValidator.validate(value, patterns[i]);
+ if (result != null) {
+ return result;
+ }
+ }
+ return null;
+ }
+ }
+
+ /**
+ * Validate a value against the specified regular expression
+ * returning a String value of the aggregated groups
+ * (<i>case sensitive</i>).
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @return Aggregated String value comprised of the
+ * <i>groups</i> matched if valid or <code>null</code> if invalid
+ */
+ public static String validate(String value, String regex) {
+ return RegexValidator.validate(value, regex, 0);
+ }
+
+ /**
+ * Validate a value against a regular expression with the
+ * specified <i>case sensitivity</i> returning a String
+ * value of the aggregated groups.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param caseSensitive when <code>true</code> matching is <i>case
+ * sensitive</i>, otherwise matching is <i>case in-sensitive</i>
+ * @return Aggregated String value comprised of the
+ * <i>groups</i> matched if valid or <code>null</code> if invalid
+ */
+ public static String validate(String value, String regex, boolean
caseSensitive) {
+ int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
+ return RegexValidator.validate(value, regex, flags);
+ }
+
+ /**
+ * Validate a value against a regular expression with the
+ * specified flags returning a String value of the aggregated
+ * groups.
+ *
+ * @param value Value to validate
+ * @param regex The regular expression to validate against.
+ * @param flags Bit mask of matching flags - see [EMAIL PROTECTED]
Pattern} for values
+ * @return Aggregated String value comprised of the
+ * <i>groups</i> matched if valid or <code>null</code> if invalid
+ */
+ private static String validate(String value, String regex, int flags) {
+ if (regex == null || regex.length() == 0) {
+ throw new IllegalArgumentException(MISSING_REGEX);
+ }
+ if (value == null) {
+ return null;
+ }
+ return RegexValidator.validate(value, Pattern.compile(regex, flags));
+ }
+
+ /**
+ * Validate a value against the specified pattern
+ * returning a String value of the aggregated groups.
+ *
+ * @param value Value to validate
+ * @param pattern The pattern to validate against.
+ * @return Aggregated String value comprised of the
+ * <i>groups</i> matched if valid or <code>null</code> if invalid
+ */
+ private static String validate(String value, Pattern pattern) {
+ Matcher matcher = pattern.matcher(value);
+ if (matcher.matches()) {
+ int count = matcher.groupCount();
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < count; i++) {
+ buffer.append(matcher.group(i+1));
+ }
+ return buffer.toString();
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Provide a String representation of this validator.
+ * @return A String representation of this validator
+ */
+ public String toString() {
+ if (pattern != null) {
+ return "RegexValidator{" + pattern.toString() + "}";
+ } else {
+ return "RegexValidator[" + patterns.length + "]";
+ }
+ }
+
+}
Propchange:
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/RegexValidator.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java?view=auto&rev=484865
==============================================================================
---
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
(added)
+++
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
Fri Dec 8 16:33:59 2006
@@ -0,0 +1,376 @@
+/*
+ * 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 java.util.regex.PatternSyntaxException;
+
+import junit.framework.TestCase;
+
+/**
+ * Test Case for RegexValidatorTest.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public class RegexValidatorTest extends TestCase {
+
+ private static final String MISSING_REGEX = "Regular Expression is
missing";
+ private static final String REGEX =
"^([abc]*)(?:\\-)([DEF]*)(?:\\-)([123])*$";
+
+ private static final String COMPONENT_1 = "([abc]{3})";
+ private static final String COMPONENT_2 = "([DEF]{3})";
+ private static final String COMPONENT_3 = "([123]{3})";
+ private static final String SEPARATOR_1 = "(?:\\-)";
+ private static final String SEPARATOR_2 = "(?:\\s)";
+ private static final String REGEX_1 = "^" + COMPONENT_1 + SEPARATOR_1 +
COMPONENT_2 + SEPARATOR_1 + COMPONENT_3 + "$";
+ private static final String REGEX_2 = "^" + COMPONENT_1 + SEPARATOR_2 +
COMPONENT_2 + SEPARATOR_2 + COMPONENT_3 + "$";
+ private static final String REGEX_3 = "^" + COMPONENT_1 + COMPONENT_2 +
COMPONENT_3 + "$";
+ private static final String[] MULTIPLE_REGEX = new String[] {REGEX_1,
REGEX_2, REGEX_3};
+
+ /**
+ * Constrct a new test case.
+ * @param name The name of the test
+ */
+ public RegexValidatorTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set Up.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /**
+ * Tear Down.
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Test static methods.
+ */
+ public void testStatic() {
+
+ // isValid()
+ assertEquals("Sensitive isValid() valid", true,
RegexValidator.isValid("ac-DE-1", REGEX));
+ assertEquals("Sensitive isValid() invalid", false,
RegexValidator.isValid("AB-de-1", REGEX));
+ assertEquals("Insensitive isValid() valid", true,
RegexValidator.isValid("AB-de-1", REGEX, false));
+ assertEquals("Insensitive isValid() invalid", false,
RegexValidator.isValid("ABd-de-1", REGEX, false));
+
+ // validate()
+ assertEquals("Sensitive validate() valid", "acDE1",
RegexValidator.validate("ac-DE-1", REGEX));
+ assertEquals("Sensitive validate() invalid", null,
RegexValidator.validate("AB-de-1", REGEX));
+ assertEquals("Insensitive validate() valid", "ABde1",
RegexValidator.validate("AB-de-1", REGEX, false));
+ assertEquals("Insensitive validate() invalid", null,
RegexValidator.validate("ABd-de-1", REGEX, false));
+
+ // match()
+ checkArray("Sensitive match() valid", new String[] {"ac", "DE",
"1"}, RegexValidator.match("ac-DE-1", REGEX));
+ checkArray("Sensitive match() invalid", null,
RegexValidator.match("AB-de-1", REGEX));
+ checkArray("Insensitive match() valid", new String[] {"AB", "de",
"1"}, RegexValidator.match("AB-de-1", REGEX, false));
+ checkArray("Insensitive match() invalid", null,
RegexValidator.match("ABd-de-1", REGEX, false));
+ }
+
+ /**
+ * Test instance methods with single regular expression.
+ */
+ public void testInstanceSingle() {
+ RegexValidator sensitive = new RegexValidator(REGEX);
+ RegexValidator insensitive = new RegexValidator(REGEX, false);
+
+ // isValid()
+ assertEquals("Sensitive isValid() valid", true,
sensitive.isValid("ac-DE-1"));
+ assertEquals("Sensitive isValid() invalid", false,
sensitive.isValid("AB-de-1"));
+ assertEquals("Insensitive isValid() valid", true,
insensitive.isValid("AB-de-1"));
+ assertEquals("Insensitive isValid() invalid", false,
insensitive.isValid("ABd-de-1"));
+
+ // validate()
+ assertEquals("Sensitive validate() valid", "acDE1",
sensitive.validate("ac-DE-1"));
+ assertEquals("Sensitive validate() invalid", null,
sensitive.validate("AB-de-1"));
+ assertEquals("Insensitive validate() valid", "ABde1",
insensitive.validate("AB-de-1"));
+ assertEquals("Insensitive validate() invalid", null,
insensitive.validate("ABd-de-1"));
+
+ // match()
+ checkArray("Sensitive match() valid", new String[] {"ac", "DE",
"1"}, sensitive.match("ac-DE-1"));
+ checkArray("Sensitive match() invalid", null,
sensitive.match("AB-de-1"));
+ checkArray("Insensitive match() valid", new String[] {"AB", "de",
"1"}, insensitive.match("AB-de-1"));
+ checkArray("Insensitive match() invalid", null,
insensitive.match("ABd-de-1"));
+ }
+
+ /**
+ * Test instance methods with multiple regular expressions.
+ */
+ public void testInstanceMultipleSensitive() {
+
+ // ------------ Set up Sensitive Validators
+ RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX);
+ RegexValidator single1 = new RegexValidator(REGEX_1);
+ RegexValidator single2 = new RegexValidator(REGEX_2);
+ RegexValidator single3 = new RegexValidator(REGEX_3);
+
+ // ------------ Set up test values
+ String value = "aac FDE 321";
+ String expect = "aacFDE321";
+ String[] array = new String[] {"aac", "FDE", "321"};
+
+ // isValid()
+ assertEquals("Sensitive isValid() Multiple", true,
multiple.isValid(value));
+ assertEquals("Sensitive isValid() 1st", false,
single1.isValid(value));
+ assertEquals("Sensitive isValid() 2nd", true,
single2.isValid(value));
+ assertEquals("Sensitive isValid() 3rd", false,
single3.isValid(value));
+
+ // validate()
+ assertEquals("Sensitive validate() Multiple", expect,
multiple.validate(value));
+ assertEquals("Sensitive validate() 1st", null,
single1.validate(value));
+ assertEquals("Sensitive validate() 2nd", expect,
single2.validate(value));
+ assertEquals("Sensitive validate() 3rd", null,
single3.validate(value));
+
+ // match()
+ checkArray("Sensitive match() Multiple", array, multiple.match(value));
+ checkArray("Sensitive match() 1st", null, single1.match(value));
+ checkArray("Sensitive match() 2nd", array, single2.match(value));
+ checkArray("Sensitive match() 3rd", null, single3.match(value));
+
+ // All invalid
+ value = "AAC*FDE*321";
+ assertEquals("isValid() Invalid", false, multiple.isValid(value));
+ assertEquals("validate() Invalid", null, multiple.validate(value));
+ assertEquals("match() Multiple", null, multiple.match(value));
+ }
+
+ /**
+ * Test instance methods with multiple regular expressions.
+ */
+ public void testInstanceMultipleInsensitive() {
+
+ // ------------ Set up In-sensitive Validators
+ RegexValidator multiple = new RegexValidator(MULTIPLE_REGEX, false);
+ RegexValidator single1 = new RegexValidator(REGEX_1, false);
+ RegexValidator single2 = new RegexValidator(REGEX_2, false);
+ RegexValidator single3 = new RegexValidator(REGEX_3, false);
+
+ // ------------ Set up test values
+ String value = "AAC FDE 321";
+ String expect = "AACFDE321";
+ String[] array = new String[] {"AAC", "FDE", "321"};
+
+ // isValid()
+ assertEquals("isValid() Multiple", true, multiple.isValid(value));
+ assertEquals("isValid() 1st", false, single1.isValid(value));
+ assertEquals("isValid() 2nd", true, single2.isValid(value));
+ assertEquals("isValid() 3rd", false, single3.isValid(value));
+
+ // validate()
+ assertEquals("validate() Multiple", expect, multiple.validate(value));
+ assertEquals("validate() 1st", null, single1.validate(value));
+ assertEquals("validate() 2nd", expect, single2.validate(value));
+ assertEquals("validate() 3rd", null, single3.validate(value));
+
+ // match()
+ checkArray("match() Multiple", array, multiple.match(value));
+ checkArray("match() 1st", null, single1.match(value));
+ checkArray("match() 2nd", array, single2.match(value));
+ checkArray("match() 3rd", null, single3.match(value));
+
+ // All invalid
+ value = "AAC*FDE*321";
+ assertEquals("isValid() Invalid", false, multiple.isValid(value));
+ assertEquals("validate() Invalid", null, multiple.validate(value));
+ assertEquals("match() Multiple", null, multiple.match(value));
+ }
+
+ /**
+ * Test Null value
+ */
+ public void testNullValue() {
+
+ // Test instance methods
+ RegexValidator validator = new RegexValidator(REGEX);
+ assertEquals("Instance isValid()", false, validator.isValid(null));
+ assertEquals("Instance validate()", null, validator.validate(null));
+ assertEquals("Instance match()", null, validator.match(null));
+
+ // Test static methods
+ assertEquals("Static isValid()", false, RegexValidator.isValid(null,
REGEX));
+ assertEquals("Static validate()", null, RegexValidator.validate(null,
REGEX));
+ assertEquals("Static match()", null, RegexValidator.match(null,
REGEX));
+ }
+
+ /**
+ * Test exceptions
+ */
+ public void testStaicMissingRegex() {
+
+ // isValid() - Null regular expression
+ try {
+ RegexValidator.isValid("abc", null);
+ fail("isValid Null - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("isValid Null", MISSING_REGEX, e.getMessage());
+ }
+
+ // validate() - Null regular expression
+ try {
+ RegexValidator.validate("abc", null);
+ fail("validate Null - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("validate Null", MISSING_REGEX, e.getMessage());
+ }
+
+ // match() - Null regular expression
+ try {
+ RegexValidator.match("abc", null);
+ fail("match Null - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("match Null", MISSING_REGEX, e.getMessage());
+ }
+
+ // isValid() - Zero Length regular expression
+ try {
+ RegexValidator.isValid("abc", "");
+ fail("isValid Zero Length - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("isValid Zero Length", MISSING_REGEX, e.getMessage());
+ }
+
+ // validate() - Zero Length regular expression
+ try {
+ RegexValidator.validate("abc", "");
+ fail("validate Zero Length - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("validate Zero Length", MISSING_REGEX,
e.getMessage());
+ }
+
+ // match() - Zero Length regular expression
+ try {
+ RegexValidator.match("abc", "");
+ fail("match Zero Length - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("match Zero Length", MISSING_REGEX, e.getMessage());
+ }
+ }
+
+ /**
+ * Test exceptions
+ */
+ public void testInstanceMissingRegex() {
+
+ // Single Regular Expression - null
+ try {
+ new RegexValidator((String)null);
+ fail("Single Null - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Single Null", MISSING_REGEX, e.getMessage());
+ }
+
+ // Single Regular Expression - Zero Length
+ try {
+ new RegexValidator("");
+ fail("Single Zero Length - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Single Zero Length", MISSING_REGEX, e.getMessage());
+ }
+
+ // Multiple Regular Expression - Null array
+ try {
+ new RegexValidator((String[])null);
+ fail("Null Array - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Null Array", "Regular expressions are missing",
e.getMessage());
+ }
+
+ // Multiple Regular Expression - Zero Length array
+ try {
+ new RegexValidator(new String[0]);
+ fail("Zero Length Array - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Zero Length Array", "Regular expressions are
missing", e.getMessage());
+ }
+
+ // Multiple Regular Expression - Array has Null
+ String[] expressions = new String[] {"ABC", null};
+ try {
+ new RegexValidator(expressions);
+ fail("Array has Null - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Array has Null", "Regular expression[1] is missing",
e.getMessage());
+ }
+
+ // Multiple Regular Expression - Array has Zero Length
+ expressions = new String[] {"", "ABC"};
+ try {
+ new RegexValidator(expressions);
+ fail("Array has Zero Length - expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Array has Zero Length", "Regular expression[0] is
missing", e.getMessage());
+ }
+ }
+
+ /**
+ * Test exceptions
+ */
+ public void testExceptions() {
+ String invalidRegex = "^([abCD12]*$";
+ try {
+ RegexValidator.isValid("ab", invalidRegex);
+ } catch (PatternSyntaxException e) {
+ // expected
+ }
+ }
+
+ /**
+ * Test toString() method
+ */
+ public void testToString() {
+ RegexValidator single = new RegexValidator(REGEX);
+ assertEquals("Single", "RegexValidator{" + REGEX + "}",
single.toString());
+
+ RegexValidator multiple = new RegexValidator(new String[] {REGEX,
REGEX});
+ assertEquals("Multiple", "RegexValidator[2]", multiple.toString());
+ }
+
+ /**
+ * Compare two arrays
+ * @param label Label for the test
+ * @param expect Expected array
+ * @param result Actual array
+ */
+ private void checkArray(String label, String[] expect, String[] result) {
+
+ // Handle nulls
+ if (expect == null || result == null) {
+ if (expect == null && result == null) {
+ return; // valid, both null
+ } else {
+ fail(label + " Null expect=" + expect + " result=" + result);
+ }
+ }
+
+ // Check Length
+ if (expect.length != result.length) {
+ fail(label + " Length expect=" + expect.length + " result=" +
result.length);
+ }
+
+ // Check Values
+ for (int i = 0; i < expect.length; i++) {
+ assertEquals(label +" value[" + i + "]", expect[i], result[i]);
+ }
+ }
+
+}
Propchange:
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RegexValidatorTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified:
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java?view=diff&rev=484865&r1=484864&r2=484865
==============================================================================
---
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
(original)
+++
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
Fri Dec 8 16:33:59 2006
@@ -52,6 +52,7 @@
suite.addTestSuite(IntegerValidatorTest.class);
suite.addTestSuite(LongValidatorTest.class);
suite.addTestSuite(PercentValidatorTest.class);
+ suite.addTestSuite(RegexValidatorTest.class);
suite.addTestSuite(ShortValidatorTest.class);
suite.addTestSuite(TimeValidatorTest.class);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]