You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by dw...@apache.org on 2002/03/30 05:21:03 UTC
cvs commit: jakarta-commons/validator/src/test/org/apache/commons/validator IntegerTest.java LongTest.java NameBean.java ShortTest.java TestTypeValidator.java TypeBean.java TypeTest.java validator-numeric.xml validator-type.xml
dwinterfeldt 02/03/29 20:21:03
Added: validator/src/test/org/apache/commons/validator
IntegerTest.java LongTest.java NameBean.java
ShortTest.java TestTypeValidator.java TypeBean.java
TypeTest.java validator-numeric.xml
validator-type.xml
Log:
Adding new unit tests.
Revision Changes Path
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/IntegerTest.java
Index: IntegerTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/IntegerTest.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogSource;
/**
* <p>Performs Validation Test for <code>int</code> validations.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class IntegerTest extends TestCase {
/**
* The key used to retrieve the set of validation
* rules from the xml file.
*/
protected static String FORM_KEY = "intForm";
/**
* The key used to retrieve the validator action.
*/
protected static String ACTION = "int";
/**
* Commons Logging instance.
*/
private Log log = LogSource.getInstance(this.getClass().getName());
/**
* Resources used for validation tests.
*/
private ValidatorResources resources = null;
public IntegerTest(String name) {
super(name);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] {IntegerTest.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(IntegerTest.class);
}
/**
* Load <code>ValidatorResources</code> from
* validator-name-required.xml.
*/
protected void setUp() throws IOException {
// Load resources
InputStream in = null;
resources = new ValidatorResources();
try {
in = this.getClass().getResourceAsStream("validator-numeric.xml");
ValidatorResourcesInitializer.initialize(resources, in);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}
protected void tearDown() {
}
/**
* Tests the int validation.
*/
public void testInt() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue("0");
valueTest(info, true);
}
/**
* Tests the int validation.
*/
public void testIntMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Integer(Integer.MIN_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the int validation.
*/
public void testIntegerMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Integer(Integer.MAX_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the int validation failure.
*/
public void testIntFailure() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
valueTest(info, false);
}
/**
* Tests the int validation failure.
*/
public void testIntBeyondMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Integer.MIN_VALUE + "1");
valueTest(info, false);
}
/**
* Tests the int validation failure.
*/
public void testIntBeyondMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Integer.MAX_VALUE + "1");
valueTest(info, false);
}
/**
* Utlity class to run a test on a value.
*
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
*/
private void valueTest(Object info, boolean passed) throws ValidatorException {
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY);
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.addResource(Validator.BEAN_KEY, info);
// Get results of the validation.
ValidatorResults results = null;
// throws ValidatorException,
// but we aren't catching for testing
// since no validation methods we use
// throw this
results = validator.validate();
assertNotNull("Results are null.", results);
ValidatorResult result = results.getValidatorResult("value");
assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/LongTest.java
Index: LongTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/LongTest.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogSource;
/**
* <p>Performs Validation Test for <code>long</code> validations.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class LongTest extends TestCase {
/**
* The key used to retrieve the set of validation
* rules from the xml file.
*/
protected static String FORM_KEY = "longForm";
/**
* The key used to retrieve the validator action.
*/
protected static String ACTION = "long";
/**
* Commons Logging instance.
*/
private Log log = LogSource.getInstance(this.getClass().getName());
/**
* Resources used for validation tests.
*/
private ValidatorResources resources = null;
public LongTest(String name) {
super(name);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] {LongTest.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(LongTest.class);
}
/**
* Load <code>ValidatorResources</code> from
* validator-name-required.xml.
*/
protected void setUp() throws IOException {
// Load resources
InputStream in = null;
resources = new ValidatorResources();
try {
in = this.getClass().getResourceAsStream("validator-numeric.xml");
ValidatorResourcesInitializer.initialize(resources, in);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}
protected void tearDown() {
}
/**
* Tests the long validation.
*/
public void testLong() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue("0");
valueTest(info, true);
}
/**
* Tests the long validation.
*/
public void testLongMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Long(Long.MIN_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the long validation.
*/
public void testLongMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Long(Long.MAX_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the long validation failure.
*/
public void testLongFailure() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
valueTest(info, false);
}
/**
* Tests the long validation failure.
*/
public void testLongBeyondMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Long.MIN_VALUE + "1");
valueTest(info, false);
}
/**
* Tests the long validation failure.
*/
public void testLongBeyondMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Long.MAX_VALUE + "1");
valueTest(info, false);
}
/**
* Utlity class to run a test on a value.
*
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
*/
private void valueTest(Object info, boolean passed) throws ValidatorException {
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY);
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.addResource(Validator.BEAN_KEY, info);
// Get results of the validation.
ValidatorResults results = null;
// throws ValidatorException,
// but we aren't catching for testing
// since no validation methods we use
// throw this
results = validator.validate();
assertNotNull("Results are null.", results);
ValidatorResult result = results.getValidatorResult("value");
assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/NameBean.java
Index: NameBean.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/NameBean.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
/**
* <p>Value object that contains a
* first name and last name.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class NameBean {
protected String firstName = null;
protected String lastName = null;
/**
* Gets the first name.
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the first name.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the last name.
*/
public String getLastName() {
return lastName;
}
/**
* Sets the last name.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/ShortTest.java
Index: ShortTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ShortTest.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogSource;
/**
* <p>Performs Validation Test for <code>short</code> validations.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class ShortTest extends TestCase {
/**
* The key used to retrieve the set of validation
* rules from the xml file.
*/
protected static String FORM_KEY = "shortForm";
/**
* The key used to retrieve the validator action.
*/
protected static String ACTION = "short";
/**
* Commons Logging instance.
*/
private Log log = LogSource.getInstance(this.getClass().getName());
/**
* Resources used for validation tests.
*/
private ValidatorResources resources = null;
public ShortTest(String name) {
super(name);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] {ShortTest.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(ShortTest.class);
}
/**
* Load <code>ValidatorResources</code> from
* validator-name-required.xml.
*/
protected void setUp() throws IOException {
// Load resources
InputStream in = null;
resources = new ValidatorResources();
try {
in = this.getClass().getResourceAsStream("validator-numeric.xml");
ValidatorResourcesInitializer.initialize(resources, in);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}
protected void tearDown() {
}
/**
* Tests the short validation.
*/
public void testShort() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue("0");
valueTest(info, true);
}
/**
* Tests the short validation.
*/
public void testShortMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Short(Short.MIN_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the short validation.
*/
public void testShortMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(new Short(Short.MAX_VALUE).toString());
valueTest(info, true);
}
/**
* Tests the short validation failure.
*/
public void testShortFailure() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
valueTest(info, false);
}
/**
* Tests the short validation failure.
*/
public void testShortBeyondMin() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Short.MIN_VALUE + "1");
valueTest(info, false);
}
/**
* Tests the short validation failure.
*/
public void testShortBeyondMax() throws ValidatorException {
// Create bean to run test on.
ValueBean info = new ValueBean();
info.setValue(Short.MAX_VALUE + "1");
valueTest(info, false);
}
/**
* Utlity class to run a test on a value.
*
* @param info Value to run test on.
* @param passed Whether or not the test is expected to pass.
*/
private void valueTest(Object info, boolean passed) throws ValidatorException {
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY);
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.addResource(Validator.BEAN_KEY, info);
// Get results of the validation.
ValidatorResults results = null;
// throws ValidatorException,
// but we aren't catching for testing
// since no validation methods we use
// throw this
results = validator.validate();
assertNotNull("Results are null.", results);
ValidatorResult result = results.getValidatorResult("value");
assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/TestTypeValidator.java
Index: TestTypeValidator.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestTypeValidator.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
/**
* <p>Contains validation methods for different
* unit tests.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class TestTypeValidator {
/**
* Checks if the field can be successfully converted to a <code>byte</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>byte</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Byte validateByte(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatByte(value);
}
/**
* Checks if the field can be successfully converted to a <code>short</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>short</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Short validateShort(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatShort(value);
}
/**
* Checks if the field can be successfully converted to a <code>int</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>int</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Integer validateInt(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatInt(value);
}
/**
* Checks if the field can be successfully converted to a <code>long</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>long</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Long validateLong(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatLong(value);
}
/**
* Checks if the field can be successfully converted to a <code>float</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>float</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Float validateFloat(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatFloat(value);
}
/**
* Checks if the field can be successfully converted to a <code>double</code>.
*
* @param value The value validation is being performed on.
* @return boolean If the field can be successfully converted
* to a <code>double</code> <code>true</code> is returned.
* Otherwise <code>false</code>.
*/
public static Double validateDouble(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
return GenericTypeValidator.formatDouble(value);
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/TypeBean.java
Index: TypeBean.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TypeBean.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
/**
* <p>Value object that contains
* different fields to test
* type conversion validation.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class TypeBean {
private String sByte = null;
private String sShort = null;
private String sInteger = null;
private String sLong = null;
private String sFloat = null;
private String sDouble = null;
private String sDate = null;
private String sCreditCard = null;
public String getByte() {
return sByte;
}
public void setByte(String sByte) {
this.sByte = sByte;
}
public String getShort() {
return sShort;
}
public void setShort(String sShort) {
this.sShort = sShort;
}
public String getInteger() {
return sInteger;
}
public void setInteger(String sInteger) {
this.sInteger = sInteger;
}
public String getLong() {
return sLong;
}
public void setLong(String sLong) {
this.sLong = sLong;
}
public String getFloat() {
return sFloat;
}
public void setFloat(String sFloat) {
this.sFloat = sFloat;
}
public String getDouble() {
return sDouble;
}
public void setDouble(String sDouble) {
this.sDouble = sDouble;
}
public String getDate() {
return sDate;
}
public void setDate(String sDate) {
this.sDate = sDate;
}
public String getCreditCard() {
return sCreditCard;
}
public void setCreditCard(String sCreditCard) {
this.sCreditCard = sCreditCard;
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/TypeTest.java
Index: TypeTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TypeTest.java,v 1.1 2002/03/30 04:21:03 dwinterfeldt Exp $
* $Revision: 1.1 $
* $Date: 2002/03/30 04:21:03 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.AssertionFailedError;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogSource;
/**
* <p>Performs Validation Test for type validations.</p>
*
* @author David Winterfeldt
* @version $Revision: 1.1 $ $Date: 2002/03/30 04:21:03 $
*/
public class TypeTest extends TestCase {
/**
* The key used to retrieve the set of validation
* rules from the xml file.
*/
protected static String FORM_KEY = "typeForm";
/**
* The key used to retrieve the validator action.
*/
protected static String ACTION = "byte";
/**
* Commons Logging instance.
*/
private Log log = LogSource.getInstance(this.getClass().getName());
/**
* Resources used for validation tests.
*/
private ValidatorResources resources = null;
public TypeTest(String name) {
super(name);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] {TypeTest.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TypeTest.class);
}
/**
* Load <code>ValidatorResources</code> from
* validator-name-required.xml.
*/
protected void setUp() throws IOException {
// Load resources
InputStream in = null;
resources = new ValidatorResources();
try {
in = this.getClass().getResourceAsStream("validator-type.xml");
ValidatorResourcesInitializer.initialize(resources, in);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (in != null) {
try { in.close(); } catch (Exception e) {}
}
}
}
protected void tearDown() {
}
/**
* Tests the byte validation.
*/
public void testType() throws ValidatorException {
// Create bean to run test on.
TypeBean info = new TypeBean();
info.setByte("12");
info.setShort("129");
info.setInteger("-144");
info.setLong("88000");
info.setFloat("12.1555f");
info.setDouble("129.1551511111d");
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY);
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.addResource(Validator.BEAN_KEY, info);
// Get results of the validation.
ValidatorResults results = null;
// throws ValidatorException,
// but we aren't catching for testing
// since no validation methods we use
// throw this
results = validator.validate();
assertNotNull("Results are null.", results);
Map hResultValues = results.getResultValueMap();
for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) {
String key = (String)i.next();
Object value = hResultValues.get(key);
assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value);
}
//ValidatorResult result = results.getValidatorResult("value");
//assertNotNull(ACTION + " value ValidatorResult should not be null.", result);
//assertTrue(ACTION + " value ValidatorResult should contain the '" + ACTION +"' action.", result.containsAction(ACTION));
//assertTrue(ACTION + " value ValidatorResult for the '" + ACTION +"' action should have " + (passed ? "passed" : "failed") + ".", (passed ? result.isValid(ACTION) : !result.isValid(ACTION)));
}
}
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/validator-numeric.xml
Index: validator-numeric.xml
===================================================================
<form-validation>
<global>
<validator name="byte"
classname="org.apache.commons.validator.TestValidator"
method="validateByte"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="short"
classname="org.apache.commons.validator.TestValidator"
method="validateShort"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="int"
classname="org.apache.commons.validator.TestValidator"
method="validateInt"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="long"
classname="org.apache.commons.validator.TestValidator"
method="validateLong"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="float"
classname="org.apache.commons.validator.TestValidator"
method="validateFloat"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="double"
classname="org.apache.commons.validator.TestValidator"
method="validateDouble"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
</global>
<formset>
<form name="byteForm">
<field property="value"
depends="byte">
</field>
</form>
<form name="shortForm">
<field property="value"
depends="short">
</field>
</form>
<form name="intForm">
<field property="value"
depends="int">
</field>
</form>
<form name="longForm">
<field property="value"
depends="long">
</field>
</form>
<form name="floatForm">
<field property="value"
depends="float">
</field>
</form>
<form name="doubleForm">
<field property="value"
depends="double">
</field>
</form>
</formset>
</form-validation>
1.1 jakarta-commons/validator/src/test/org/apache/commons/validator/validator-type.xml
Index: validator-type.xml
===================================================================
<form-validation>
<global>
<validator name="byte"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateByte"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="short"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateShort"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="int"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateInt"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="long"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateLong"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="float"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateFloat"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
<validator name="double"
classname="org.apache.commons.validator.TestTypeValidator"
method="validateDouble"
methodParams="java.lang.Object,org.apache.commons.validator.Field"/>
</global>
<formset>
<form name="typeForm">
<field property="byte"
depends="byte">
</field>
<field property="short"
depends="short">
</field>
<field property="int"
depends="int">
</field>
<field property="long"
depends="long">
</field>
<field property="float"
depends="float">
</field>
<field property="double"
depends="double">
</field>
</form>
</formset>
</form-validation>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>