📄 validationtestcase.java
字号:
package net.sf.irunninglog.validation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import net.sf.irunninglog.junit.BaseTestCase;
import net.sf.irunninglog.util.ConstantValues;
import net.sf.irunninglog.util.DTO;
public abstract class ValidationTestCase extends BaseTestCase {
private static final String [] validRequiredFieldValues = new String [] {STRING_FOO};
private static final String [] invalidRequiredFieldValues = new String [] {null, ConstantValues.STRING_BLANK, " "};
private static final String [] validMaxLengthValues = new String [] {null, ConstantValues.STRING_BLANK, STRING_FOO, "1111111111"};
private static final String [] invalidMaxLengthValues = new String [] {"11111111112"};
private static final String [] validMinLengthValues = new String [] {STRING_FOO, "1111"};
private static final String [] invalidMinLengthValues = new String [] {null, ConstantValues.STRING_BLANK, " "};
private static final String [] validMaxValueValues = new String [] {"-0.1", "0.00", "100.25", "123.45"};
private static final String [] invalidMaxValueValues = new String [] {null, ConstantValues.STRING_BLANK, " ", STRING_FOO, "123.46", "5000"};
private static final String [] validMinValueValues = new String [] {"100", "0.00", "-10", "-12.345"};
private static final String [] invalidMinValueValues = new String [] {null, ConstantValues.STRING_BLANK, " ", STRING_FOO, "-12.346", "-100"};
private static final String [] validValidValueValues = new String [] {null, ConstantValues.STRING_BLANK, " ", STRING_FOO, STRING_FOO.toUpperCase()};
private static final String [] invalidValidValueValues = new String [] {"fOO", "foooo", "111"};
private static final String [] validDateValues = new String [] {null, ConstantValues.STRING_BLANK, " ", "1/1/2005", "12/31/05", "1/2/3"};
private static final String [] invalidDateValues = new String [] {"1/2", "12.31.2005", "/2/3/2", STRING_FOO};
private static final String [] validTimeValues = new String [] {null, ConstantValues.STRING_BLANK, " ", "1:00:2.345", "01:03:45", "7:45.345", "6:23", "23", "23:59:59.999"};
private static final String [] invalidTimeValues = new String [] {STRING_FOO, "1:2:3:4", "1:2:4.342.4", "1:-2:3.4", "1:3:w.2d23d", "24:00:00.000"};
private static final String [] validEmailValues = new String [] {null, ConstantValues.STRING_BLANK, " ", "a@b.com", "allan@irunning.com", "test.email@test.irunning.com"};
private static final String [] invalidEmailValues = new String [] {"a@", "allan@irunning", "allan@irunning.", STRING_FOO};
private static final String [] validDecimalValues = new String [] {null, ConstantValues.STRING_BLANK, " ", "1", ConstantValues.STRING_ZERO, "123.456", "-2345.232"};
private static final String [] invalidDecimalValues = new String [] {STRING_FOO, "1.2.3", "-12.32-"};
private static final String [] validBooleanValues = new String [] {null, ConstantValues.STRING_BLANK, " ", "1", ConstantValues.STRING_FALSE, ConstantValues.STRING_TRUE, "FaLse", "trUE", "yes", "YeS", "no", "NO"};
private static final String [] invalidBooleanValues = new String [] {STRING_FOO};
protected static final Class [] VALIDATION_UTILS_ARGS = new Class [] {DTO.class, String.class, List.class};
protected static final Class [] VALIDATIONS_ARGS = new Class [] {DTO.class, ValidatorAction.class, Field.class, List.class};
protected static final String INVALID_CLASS_ID = STRING_FOO;
protected static final String VALID_CLASS_ID = STRING_FOO.toUpperCase();
protected static final String INVALID_FIELD_NAME = STRING_FOO;
protected static final String VALID_FIELD_NAME = STRING_FOO;
protected static final String FOO_PROPERTY_NAME = "fooProperty";
private Class clazz;
private Class [] args;
public ValidationTestCase(String name) {
super(name);
}
protected void setUp() {
try {
super.setUp();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
clazz = getClassForTesting();
if (clazz.equals(ValidationUtilities.class)) {
args = VALIDATION_UTILS_ARGS;
} else {
args = VALIDATIONS_ARGS;
}
}
public final void testValidMethodCalls() throws Exception {
Class clazz = getClassForTesting();
for (int i=0; i<getAllMethodNames().length; i++) {
Method method = clazz.getMethod(getAllMethodNames()[i], args);
for (int j=0;j<getValidMethodParameters().length;j++) {
method.invoke(clazz, getValidMethodParameters()[j]);
}
}
}
public final void testInvalidMethodCalls() throws Exception {
Class clazz = getClassForTesting();
for (int i=0; i<getAllMethodNames().length; i++) {
Method method = clazz.getMethod(getAllMethodNames()[i], args);
// The following all throw Illegal Arg due to invalid arguments
for (int j=0; j<getIllegalArgumentMethodParameters().length;j++) {
try {
method.invoke(clazz, getIllegalArgumentMethodParameters()[j]);
failTest();
} catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof IllegalArgumentException);
}
}
// The following all throw Missing Resource exceptions
for (int j=0; j<getMissingResourceMethodParameters().length;j++) {
try {
method.invoke(clazz, getMissingResourceMethodParameters()[j]);
failTest();
} catch (InvocationTargetException ex) {
assertTrue(ex.getCause() instanceof MissingResourceException);
}
}
}
}
public final void testRequiredFieldValidations() throws Exception {
doValidationTest(validRequiredFieldValues,
invalidRequiredFieldValues,
getRequiredFieldValidationMethodName());
}
public final void testMinLengthValidations() throws Exception {
doValidationTest(validMinLengthValues,
invalidMinLengthValues,
getMinLengthValidationMethodName());
}
public final void testMaxLengthValidations() throws Exception {
doValidationTest(validMaxLengthValues,
invalidMaxLengthValues,
getMaxLengthValidationMethodName());
}
public final void testMinValueValidations() throws Exception {
doValidationTest(validMinValueValues,
invalidMinValueValues,
getMinValueValidationMethodName());
}
public final void testMaxValueValidations() throws Exception {
doValidationTest(validMaxValueValues,
invalidMaxValueValues,
getMaxValueValidationMethodName());
}
public final void testValidValueValidations() throws Exception {
doValidationTest(validValidValueValues,
invalidValidValueValues,
getValidValueValidationMethodName());
}
public final void testDateValidations() throws Exception {
doValidationTest(validDateValues,
invalidDateValues,
getDateValidationMethodName());
}
public final void testTimeValidations() throws Exception {
doValidationTest(validTimeValues,
invalidTimeValues,
getTimeValidationMethodName());
}
public final void testEmailValidations() throws Exception {
doValidationTest(validEmailValues,
invalidEmailValues,
getEmailValidationMethodName());
}
public final void testDecimalValidations() throws Exception {
doValidationTest(validDecimalValues,
invalidDecimalValues,
getDecimalValidationMethodName());
}
public final void testBooleanValidations() throws Exception {
doValidationTest(validBooleanValues,
invalidBooleanValues,
getBooleanValidationMethodName());
}
protected abstract Object [][] getValidMethodParameters();
protected abstract Object [][] getIllegalArgumentMethodParameters();
protected abstract Object [][] getMissingResourceMethodParameters();
protected abstract Class getClassForTesting();
protected abstract String [] getAllMethodNames();
protected abstract String getMaxLengthValidationMethodName();
protected abstract String getRequiredFieldValidationMethodName();
protected abstract String getMinLengthValidationMethodName();
protected abstract String getMaxValueValidationMethodName();
protected abstract String getMinValueValidationMethodName();
protected abstract String getDateValidationMethodName();
protected abstract String getTimeValidationMethodName();
protected abstract String getEmailValidationMethodName();
protected abstract String getDecimalValidationMethodName();
protected abstract String getBooleanValidationMethodName();
protected abstract String getValidValueValidationMethodName();
protected void doValidationTest(String [] validValues, String [] invalidValues, String methodName) throws Exception {
Method method = clazz.getMethod(methodName, args);
List errors = null;
DTO dto = null;
Object [] params = null;
boolean result;
for (int i=0; i < validValues.length; i++) {
errors = new ArrayList();
dto = new DTO(VALID_CLASS_ID);
dto.setValue(VALID_FIELD_NAME, validValues[i]);
if (clazz.equals(ValidationUtilities.class)) {
params = new Object [] {dto, VALID_FIELD_NAME, errors};
} else {
params = new Object [] {dto,
ValidationUtilities.getNewValidatorAction(null),
ValidationUtilities.getNewValidationField(VALID_FIELD_NAME),
errors};
}
result = ((Boolean) method.invoke(clazz, params)).booleanValue();
assertTrue(result);
assertEquals(0, errors.size());
}
for (int i=0; i < invalidValues.length; i++) {
errors = new ArrayList();
dto = new DTO(VALID_CLASS_ID);
dto.setValue(VALID_FIELD_NAME, invalidValues[i]);
if (clazz.equals(ValidationUtilities.class)) {
params = new Object [] {dto, VALID_FIELD_NAME, errors};
} else {
params = new Object [] {dto,
ValidationUtilities.getNewValidatorAction(null),
ValidationUtilities.getNewValidationField(VALID_FIELD_NAME),
errors};
}
result = ((Boolean) method.invoke(clazz, params)).booleanValue();
assertFalse(result);
assertEquals(1, errors.size());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -