dwinterfeldt 02/03/16 16:27:35
Modified: validator/src/test/org/apache/commons/validator
RequiredNameTest.java TestValidator.java
ValidatorTest.java
Log:
Switched to use ValidatorResults and tests weren't correct because the required test
method was returning the wrong value.
Revision Changes Path
1.2 +91 -40
jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredNameTest.java
Index: RequiredNameTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/RequiredNameTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequiredNameTest.java 11 Mar 2002 05:12:02 -0000 1.1
+++ RequiredNameTest.java 17 Mar 2002 00:27:35 -0000 1.2
@@ -80,6 +80,12 @@
* rules from the xml file.
*/
protected static String FORM_KEY = "nameForm";
+
+ /**
+ * The key used to retrieve the validator action.
+ */
+ protected static String ACTION = "required";
+
/**
* Commons Logging instance.
@@ -144,7 +150,7 @@
public void testRequired() throws ValidatorException {
// Create bean to run test on.
Name name = new Name();
-
+
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY);
@@ -153,18 +159,26 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
// throws ValidatorException,
// but we aren't catching for testing
// since no validation methods we use
// throw this
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNull("First Name should have passed no validations.",
hResults.get("firstName"));
- assertNull("Last Name should have passed no validations.",
hResults.get("lastName"));
-
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !lastNameResult.isValid(ACTION));
}
/**
@@ -183,14 +197,22 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNull("First Name should have passed no validations.",
hResults.get("firstName"));
- assertNull("Last Name should have passed no validations.",
hResults.get("lastName"));
-
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !lastNameResult.isValid(ACTION));
}
/**
@@ -209,15 +231,22 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNotNull("First Name result should not be null.",
hResults.get("firstName"));
- assertEquals("First Name should have passed one validation.", 1,
((Integer)hResults.get("firstName")).intValue());
- assertNull("Last Name should have passed no validations.",
hResults.get("lastName"));
-
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have passed.", firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !lastNameResult.isValid(ACTION));
}
/**
@@ -236,14 +265,22 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNull("First Name should have passed no validations.",
hResults.get("firstName"));
- assertNull("Last Name should have passed no validations.",
hResults.get("lastName"));
-
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !lastNameResult.isValid(ACTION));
}
/**
@@ -262,14 +299,22 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNull("First Name should have passed no validations.",
hResults.get("firstName"));
- assertNotNull("Last Name result should not be null.",
hResults.get("lastName"));
- assertEquals("Last Name should have passed one validation.", 1,
((Integer)hResults.get("lastName")).intValue());
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have failed.", !firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have passed.", lastNameResult.isValid(ACTION));
}
@@ -290,16 +335,22 @@
validator.addResource(Validator.BEAN_KEY, name);
// Get results of the validation.
- Map hResults = null;
+ ValidatorResults results = null;
- hResults = validator.validate();
+ results = validator.validate();
- assertNotNull("Results are null.", hResults);
- assertNotNull("First Name result should not be null.",
hResults.get("firstName"));
- assertEquals("First Name should have passed one validation.", 1,
((Integer)hResults.get("firstName")).intValue());
- assertNotNull("Last Name result should not be null.",
hResults.get("lastName"));
- assertEquals("Last Name should have passed one validation.", 1,
((Integer)hResults.get("lastName")).intValue());
-
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult firstNameResult = results.getValidatorResult("firstName");
+ ValidatorResult lastNameResult = results.getValidatorResult("lastName");
+
+ assertNotNull("First Name ValidatorResult should not be null.",
firstNameResult);
+ assertTrue("First Name ValidatorResult should contain the '" + ACTION +"'
action.", firstNameResult.containsAction(ACTION));
+ assertTrue("First Name ValidatorResult for the '" + ACTION +"' action should
have passed.", firstNameResult.isValid(ACTION));
+
+ assertNotNull("First Name ValidatorResult should not be null.",
lastNameResult);
+ assertTrue("Last Name ValidatorResult should contain the '" + ACTION +"'
action.", lastNameResult.containsAction(ACTION));
+ assertTrue("Last Name ValidatorResult for the '" + ACTION +"' action should
have passed.", lastNameResult.isValid(ACTION));
}
}
1.2 +1 -1
jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java
Index: TestValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/TestValidator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestValidator.java 11 Mar 2002 05:12:02 -0000 1.1
+++ TestValidator.java 17 Mar 2002 00:27:35 -0000 1.2
@@ -77,7 +77,7 @@
public static boolean validateRequired(Object bean, Field field) {
String value = ValidatorUtil.getValueAsString(bean, field.getProperty());
- return GenericValidator.isBlankOrNull(value);
+ return !GenericValidator.isBlankOrNull(value);
}
}
1.3 +31 -6
jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java
Index: ValidatorTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/validator/src/test/org/apache/commons/validator/ValidatorTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ValidatorTest.java 15 Jan 2002 06:06:24 -0000 1.2
+++ ValidatorTest.java 17 Mar 2002 00:27:35 -0000 1.3
@@ -81,7 +81,7 @@
}
/**
- * Start the tests.
+ * Start the tests.
*
* @param theArgs the arguments. Not used
*/
@@ -109,10 +109,15 @@
* method being tested returns an object (<code>null</code> will be considered
an error).
*/
public void testManualObject() {
+ // property name of the method we are validating
+ String property = "date";
+ // name of ValidatorAction
+ String action = "date";
+
ValidatorResources resources = new ValidatorResources();
ValidatorAction va = new ValidatorAction();
- va.setName("date");
+ va.setName(action);
va.setClassname("org.apache.commons.validator.ValidatorTest");
va.setMethod("formatDate");
va.setMethodParams("java.lang.Object,org.apache.commons.validator.Field");
@@ -121,8 +126,8 @@
Form form = new Form();
form.setName("testForm");
Field field = new Field();
- field.setProperty("date");
- field.setDepends("date");
+ field.setProperty(property);
+ field.setDepends(action);
form.addField(field);
fs.addForm(form);
@@ -139,7 +144,17 @@
validator.addResource(Validator.BEAN_KEY, bean);
try {
- assertEquals("Validation of the date formatting has failed.", 0,
validator.validate().size());
+ ValidatorResults results = validator.validate();
+
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult result = results.getValidatorResult(property);
+
+ assertNotNull("Results are null.", results);
+
+ assertTrue("ValidatorResult does not contain '" + action + "' validator
result.", result.containsAction(action));
+
+ assertTrue("Validation of the date formatting has failed.",
result.isValid(action));
} catch (Exception e) {
fail("An exception was thrown while calling Validator.validate()");
}
@@ -147,7 +162,17 @@
bean.setDate("2/30/1999");
try {
- assertEquals("Validation of the date formatting has failed.", 1,
validator.validate().size());
+ ValidatorResults results = validator.validate();
+
+ assertNotNull("Results are null.", results);
+
+ ValidatorResult result = results.getValidatorResult(property);
+
+ assertNotNull("Results are null.", results);
+
+ assertTrue("ValidatorResult does not contain '" + action + "' validator
result.", result.containsAction(action));
+
+ assertTrue("Validation of the date formatting has passed when it should
have failed.", !result.isValid(action));
} catch (Exception e) {
fail("An exception was thrown while calling Validator.validate()");
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>