Skip to content

Commit

Permalink
tests for AppendingValidatorContext and nested visitor conversion val…
Browse files Browse the repository at this point in the history
…idation
  • Loading branch information
aleksandr-m committed May 16, 2014
1 parent 2b8a805 commit 46f7f6c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,32 @@ public void testVisitorChildValidation() throws Exception {
//the error from the action should be there too
assertTrue(fieldErrors.containsKey("context"));
}


public void testVisitorChildConversionValidation() throws Exception {
//add conversion error
Map<String, Object> conversionErrors = new HashMap<String, Object>();
conversionErrors.put("bean.child.count", "bar");
ActionContext.getContext().setConversionErrors(conversionErrors);

validate("visitorChildValidation");
assertTrue(action.hasFieldErrors());

Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(6, fieldErrors.size());
assertTrue(!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"));
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.opensymphony.xwork2.validator.validators;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.XWorkTestCase;
import com.opensymphony.xwork2.validator.DelegatingValidatorContext;
import com.opensymphony.xwork2.validator.ValidatorContext;
import com.opensymphony.xwork2.validator.VisitorValidatorTestAction;
import com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext;

public class AppendingValidatorContextTest extends XWorkTestCase {

private static final String FIRST_NAME = "first";
private static final String SECOND_NAME = "second";
private static final String FIELD_NAME = "fieldName";
private static final String FULL_FIELD_NAME = FIRST_NAME + "." + SECOND_NAME + "." + FIELD_NAME;

private VisitorValidatorTestAction action;
private VisitorFieldValidator.AppendingValidatorContext validatorContext;

@Override
protected void setUp() throws Exception {
super.setUp();

action = new VisitorValidatorTestAction();
ValidatorContext vc1 = new DelegatingValidatorContext(action);

VisitorFieldValidator.AppendingValidatorContext vc2 = new AppendingValidatorContext(
vc1, "value", FIRST_NAME, "");
validatorContext = new AppendingValidatorContext(vc2, "value", SECOND_NAME, "");
}

public void testGetFullFieldName() throws Exception {
String fullFieldName = validatorContext.getFullFieldName(FIELD_NAME);
assertEquals(FULL_FIELD_NAME, fullFieldName);
}

public void testAddFieldError() throws Exception {
validatorContext.addFieldError(FIELD_NAME, "fieldError");
assertTrue(action.hasFieldErrors());

Map<String, List<String>> fieldErrors = action.getFieldErrors();
assertEquals(1, fieldErrors.size());
assertTrue(fieldErrors.containsKey(FULL_FIELD_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
<field-validator type="requiredstring" short-circuit="true">
<message>You must enter a name.</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">name == 'test'</param>
<message>Name is invalid</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression">name == 'test'</param>
<message>Name is invalid</message>
</field-validator>
</field>
<field name="birth">
<field-validator type="date">
<param name="min">01/01/1970</param>
<message>You must have been born after 1970.</message>
</field-validator>
<field-validator type="date">
<param name="min">01/01/1970</param>
<message>You must have been born after 1970.</message>
</field-validator>
</field>
<field name="count">
<field-validator type="conversion">
<message>Count must be an integer.</message>
</field-validator>
</field>
</validators>

0 comments on commit 46f7f6c

Please sign in to comment.