/xwork2.1.6/core/src/test/java/com/opensymphony/xwork2/validator/SimpleActionValidationTest.java

http://szjc.googlecode.com/ · Java · 226 lines · 161 code · 48 blank · 17 comment · 0 complexity · 00b52ba71653751712be98ba9b46e301 MD5 · raw file

  1. /*
  2. * Copyright (c) 2002-2003 by OpenSymphony
  3. * All rights reserved.
  4. */
  5. package com.opensymphony.xwork2.validator;
  6. import com.opensymphony.xwork2.*;
  7. import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
  8. import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
  9. import com.opensymphony.xwork2.util.ValueStack;
  10. import com.opensymphony.xwork2.validator.validators.ValidatorSupport;
  11. import java.util.*;
  12. /**
  13. * SimpleActionValidationTest
  14. * <p/>
  15. * Created : Jan 20, 2003 11:04:25 PM
  16. *
  17. * @author Jason Carreira
  18. */
  19. public class SimpleActionValidationTest extends XWorkTestCase {
  20. private Locale origLocale;
  21. public void testAliasValidation() {
  22. HashMap<String, Object> params = new HashMap<String, Object>();
  23. params.put("baz", "10");
  24. //valid values
  25. params.put("bar", "7");
  26. params.put("date", "12/23/2002");
  27. params.put("percentage", "1.23456789");
  28. HashMap<String, Object> extraContext = new HashMap<String, Object>();
  29. extraContext.put(ActionContext.PARAMETERS, params);
  30. try {
  31. ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
  32. proxy.execute();
  33. ValidationAware validationAware = (ValidationAware) proxy.getAction();
  34. assertFalse(validationAware.hasFieldErrors());
  35. // put in an out-of-range value to see if the old validators still work
  36. ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
  37. params.put("bar", "42");
  38. proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, extraContext);
  39. proxy.execute();
  40. validationAware = (ValidationAware) proxy.getAction();
  41. assertTrue(validationAware.hasFieldErrors());
  42. Map<String, List<String>> errors = validationAware.getFieldErrors();
  43. assertTrue(errors.containsKey("baz"));
  44. List<String> bazErrors = errors.get("baz");
  45. assertEquals(1, bazErrors.size());
  46. String message = bazErrors.get(0);
  47. assertEquals("baz out of range.", message);
  48. assertTrue(errors.containsKey("bar"));
  49. List<String> barErrors = errors.get("bar");
  50. assertEquals(1, barErrors.size());
  51. message = barErrors.get(0);
  52. assertEquals("bar must be between 6 and 10, current value is 42.", message);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. fail();
  56. }
  57. }
  58. public void testLookingUpFieldNameAsTextKey() {
  59. HashMap<String, Object> params = new HashMap<String, Object>();
  60. // should cause a message
  61. params.put("baz", "-1");
  62. //valid values
  63. params.put("bar", "7");
  64. HashMap<String, Object> extraContext = new HashMap<String, Object>();
  65. extraContext.put(ActionContext.PARAMETERS, params);
  66. try {
  67. ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
  68. proxy.execute();
  69. assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
  70. Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
  71. List<String> bazErrors = errors.get("baz");
  72. assertEquals(1, bazErrors.size());
  73. String errorMessage = bazErrors.get(0);
  74. assertNotNull(errorMessage);
  75. assertEquals("Baz Field must be greater than 0", errorMessage);
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. fail();
  79. }
  80. }
  81. public void testMessageKey() {
  82. HashMap<String, Object> params = new HashMap<String, Object>();
  83. params.put("foo", "200");
  84. HashMap<String, Object> extraContext = new HashMap<String, Object>();
  85. extraContext.put(ActionContext.PARAMETERS, params);
  86. try {
  87. ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
  88. ValueStack stack = ActionContext.getContext().getValueStack();
  89. ActionContext.setContext(new ActionContext(stack.getContext()));
  90. ActionContext.getContext().setLocale(Locale.US);
  91. proxy.execute();
  92. assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
  93. Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
  94. List<String> fooErrors = errors.get("foo");
  95. assertEquals(1, fooErrors.size());
  96. String errorMessage = fooErrors.get(0);
  97. assertNotNull(errorMessage);
  98. assertEquals("Foo Range Message", errorMessage);
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. fail();
  102. }
  103. }
  104. public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
  105. Validator validator = new ValidatorSupport() {
  106. public void validate(Object object) throws ValidationException {
  107. addActionError(object);
  108. }
  109. };
  110. validator.setValueStack(ActionContext.getContext().getValueStack());
  111. String messageKey = "does.not.exist";
  112. validator.setMessageKey(messageKey);
  113. ValidatorContext validatorContext = new DelegatingValidatorContext(new SimpleAction());
  114. validator.setValidatorContext(validatorContext);
  115. validator.validate(this);
  116. assertTrue(validatorContext.hasActionErrors());
  117. Collection<String> errors = validatorContext.getActionErrors();
  118. assertEquals(1, errors.size());
  119. assertEquals(messageKey, errors.toArray()[0]);
  120. }
  121. public void testParamterizedMessage() {
  122. HashMap<String, Object> params = new HashMap<String, Object>();
  123. params.put("bar", "42");
  124. HashMap<String, Object> extraContext = new HashMap<String, Object>();
  125. extraContext.put(ActionContext.PARAMETERS, params);
  126. try {
  127. ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
  128. proxy.execute();
  129. assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
  130. Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
  131. List<String> barErrors = errors.get("bar");
  132. assertEquals(1, barErrors.size());
  133. String errorMessage = barErrors.get(0);
  134. assertNotNull(errorMessage);
  135. assertEquals("bar must be between 6 and 10, current value is 42.", errorMessage);
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. fail();
  139. }
  140. }
  141. public void testSubPropertiesAreValidated() {
  142. HashMap<String, Object> params = new HashMap<String, Object>();
  143. params.put("baz", "10");
  144. //valid values
  145. params.put("foo", "8");
  146. params.put("bar", "7");
  147. params.put("date", "12/23/2002");
  148. params.put("bean.name", "Name should be valid");
  149. // this should cause a message
  150. params.put("bean.count", "100");
  151. HashMap<String, Object> extraContext = new HashMap<String, Object>();
  152. extraContext.put(ActionContext.PARAMETERS, params);
  153. try {
  154. ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, extraContext);
  155. proxy.execute();
  156. assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
  157. Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
  158. List<String> beanCountErrors = errors.get("bean.count");
  159. assertEquals(1, beanCountErrors.size());
  160. String errorMessage = beanCountErrors.get(0);
  161. assertNotNull(errorMessage);
  162. assertEquals("bean.count out of range.", errorMessage);
  163. } catch (Exception e) {
  164. e.printStackTrace();
  165. fail();
  166. }
  167. }
  168. @Override
  169. protected void setUp() throws Exception {
  170. origLocale = Locale.getDefault();
  171. Locale.setDefault(Locale.US);
  172. loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-beans.xml"), new MockConfigurationProvider());
  173. }
  174. @Override
  175. protected void tearDown() throws Exception {
  176. Locale.setDefault(origLocale);
  177. }
  178. }