use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class IntRangeValidatorTest method testRangeValidation.
public void testRangeValidation() {
HashMap<String, String> params = new HashMap<>();
params.put("bar", "5");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> errorMessages = errors.get("bar");
assertEquals(1, errorMessages.size());
String errorMessage = errorMessages.get(0);
assertNotNull(errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class VisitorFieldValidatorTest method testVisitorChildConversionValidation.
public void testVisitorChildConversionValidation() throws Exception {
// add conversion error
Map<String, ConversionData> conversionErrors = new HashMap<>();
conversionErrors.put("bean.child.count", new ConversionData("bar", Integer.class));
ActionContext.getContext().withConversionErrors(conversionErrors);
validate("visitorChildValidation");
assertTrue(action.hasFieldErrors());
Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(6, fieldErrors.size());
assertFalse(fieldErrors.containsKey("bean.count"));
assertTrue(fieldErrors.containsKey("bean.name"));
assertTrue(fieldErrors.containsKey("bean.birth"));
assertTrue(fieldErrors.containsKey("bean.child.name"));
assertTrue(fieldErrors.containsKey("bean.child.birth"));
// the error from the action should be there too
assertTrue(fieldErrors.containsKey("context"));
// nested visitor conversion error
assertTrue(fieldErrors.containsKey("bean.child.count"));
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class SimpleActionValidationTest method testSubPropertiesAreValidated.
public void testSubPropertiesAreValidated() {
HashMap<String, Object> params = new HashMap<>();
params.put("baz", "10");
// valid values
params.put("foo", "8");
params.put("bar", "7");
params.put("date", "12/23/2002");
params.put("bean.name", "Name should be valid");
// this should cause a message
params.put("bean.count", "100");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> beanCountErrors = errors.get("bean.count");
assertEquals(1, beanCountErrors.size());
String errorMessage = beanCountErrors.get(0);
assertNotNull(errorMessage);
assertEquals("bean.count out of range.", errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class SimpleActionValidationTest method testAliasValidation.
public void testAliasValidation() {
Map<String, Object> params = new HashMap<>();
params.put("baz", "10");
// valid values
params.put("bar", "7");
params.put("date", "12/23/2002");
params.put("percentage", "1.23456789");
Map<String, Object> extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
proxy.execute();
ValidationAware validationAware = (ValidationAware) proxy.getAction();
assertFalse(validationAware.hasFieldErrors());
params.put("bar", "42");
extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, null, extraContext);
proxy.execute();
validationAware = (ValidationAware) proxy.getAction();
assertTrue(validationAware.hasFieldErrors());
Map<String, List<String>> errors = validationAware.getFieldErrors();
assertTrue(errors.containsKey("baz"));
List<String> bazErrors = errors.get("baz");
assertEquals(1, bazErrors.size());
String message = bazErrors.get(0);
assertEquals("baz out of range.", message);
assertTrue(errors.containsKey("bar"));
List<String> barErrors = errors.get("bar");
assertEquals(1, barErrors.size());
message = barErrors.get(0);
assertEquals("bar must be between 6 and 10, current value is 42.", message);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class SimpleActionValidationTest method testLookingUpFieldNameAsTextKey.
public void testLookingUpFieldNameAsTextKey() {
HashMap<String, Object> params = new HashMap<>();
// should cause a message
params.put("baz", "-1");
// valid values
params.put("bar", "7");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> bazErrors = errors.get("baz");
assertEquals(1, bazErrors.size());
String errorMessage = bazErrors.get(0);
assertNotNull(errorMessage);
assertEquals("Baz Field must be greater than 0", errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
Aggregations