📄 extensiontest.java
字号:
// for the validations to be performed on.
validator.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = null;
results = validator.validate();
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));
}
/**
* Tests the required validation for last name if it is blank.
*/
public void testRequiredLastNameBlank() throws ValidatorException {
// Create bean to run test on.
NameBean name = new NameBean();
name.setLastName("");
// 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.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = null;
results = validator.validate();
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));
}
/**
* Tests the required validation for last name.
*/
public void testRequiredLastName() throws ValidatorException {
// Create bean to run test on.
NameBean name = new NameBean();
name.setLastName("Smith");
// 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.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = null;
results = validator.validate();
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));
}
/**
* Tests the required validation for first and last name.
*/
public void testRequiredName() throws ValidatorException {
// Create bean to run test on.
NameBean name = new NameBean();
name.setFirstName("Joe");
name.setLastName("Smith");
// 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.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = null;
results = validator.validate();
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("Last 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));
}
/**
* Tests if we can override a rule. We "can" override a rule if the message shown
* when the firstName required test fails and the lastName test is null.
*/
public void testOverrideRule() throws ValidatorException {
// Create bean to run test on.
NameBean name = new NameBean();
name.setLastName("Smith");
// Construct validator based on the loaded resources
// and the form key
Validator validator = new Validator(resources, FORM_KEY2);
// add the name bean to the validator as a resource
// for the validations to be performed on.
validator.setParameter(Validator.BEAN_PARAM, name);
// Get results of the validation.
ValidatorResults results = null;
results = validator.validate();
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 for the '" + ACTION +"' action should have '" + CHECK_MSG_KEY + " as a key.", firstNameResult.field.getArg(0).getKey().equals(CHECK_MSG_KEY));
assertNull("Last Name ValidatorResult should be null.", lastNameResult);
}
/**
* Tests if the order is mantained when extending a form. Parent form fields should
* preceed self form fields, except if we override the rules.
*/
public void testOrder() {
Form form = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY);
Form form2 = resources.getForm(ValidatorResources.defaultLocale, FORM_KEY2);
assertNotNull(FORM_KEY + " is null.", form);
assertTrue("There should only be 2 fields in " + FORM_KEY, form.getFields().size() == 2);
assertNotNull(FORM_KEY2 + " is null.", form2);
assertTrue("There should only be 2 fields in " + FORM_KEY2, form2.getFields().size() == 2);
//get the first field
Field fieldFirstName = (Field)form.getFields().get(0);
//get the second field
Field fieldLastName = (Field)form.getFields().get(1);
assertTrue("firstName in " + FORM_KEY + " should be the first in the list", fieldFirstName.getKey().equals("firstName"));
assertTrue("lastName in " + FORM_KEY + " should be the first in the list", fieldLastName.getKey().equals("lastName"));
// get the second field
fieldLastName = (Field)form2.getFields().get(0);
//get the first field
fieldFirstName = (Field)form2.getFields().get(1);
assertTrue("firstName in " + FORM_KEY2 + " should be the first in the list", fieldFirstName.getKey().equals("firstName"));
assertTrue("lastName in " + FORM_KEY2 + " should be the first in the list", fieldLastName.getKey().equals("lastName"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -