PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/struts-2.2.1/src/xwork-core/src/test/java/com/opensymphony/xwork2/validator/AnnotationActionValidatorManagerTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 395 lines | 254 code | 86 blank | 55 comment | 1 complexity | e9ca1f218ed695e2a2bcf20ba951b078 MD5 | raw file
  1. /*
  2. * Copyright 2002-2006,2009 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.opensymphony.xwork2.validator;
  17. import com.opensymphony.xwork2.*;
  18. import com.opensymphony.xwork2.config.entities.ActionConfig;
  19. import com.opensymphony.xwork2.test.AnnotationDataAware2;
  20. import com.opensymphony.xwork2.test.AnnotationUser;
  21. import com.opensymphony.xwork2.test.SimpleAnnotationAction2;
  22. import com.opensymphony.xwork2.test.SimpleAnnotationAction3;
  23. import com.opensymphony.xwork2.util.FileManager;
  24. import com.opensymphony.xwork2.validator.validators.*;
  25. import java.util.List;
  26. import org.easymock.EasyMock;
  27. /**
  28. * AnnotationActionValidatorManagerTest
  29. *
  30. * @author Rainer Hermanns
  31. * @author Jason Carreira
  32. * @author tm_jee ( tm_jee (at) yahoo.co.uk )
  33. * Created Jun 9, 2003 11:03:01 AM
  34. */
  35. public class AnnotationActionValidatorManagerTest extends XWorkTestCase {
  36. protected final String alias = "annotationValidationAlias";
  37. AnnotationActionValidatorManager annotationActionValidatorManager;
  38. @Override protected void setUp() throws Exception {
  39. super.setUp();
  40. annotationActionValidatorManager = (AnnotationActionValidatorManager) container.getInstance(ActionValidatorManager.class);
  41. ActionConfig config = new ActionConfig.Builder("", "name", "").build();
  42. ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
  43. ActionProxy proxy = EasyMock.createNiceMock(ActionProxy.class);
  44. EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes();
  45. EasyMock.expect(invocation.getAction()).andReturn(null).anyTimes();
  46. EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes();
  47. EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes();
  48. EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
  49. EasyMock.replay(invocation);
  50. EasyMock.replay(proxy);
  51. ActionContext.getContext().setActionInvocation(invocation);
  52. }
  53. @Override protected void tearDown() throws Exception {
  54. annotationActionValidatorManager = null;
  55. super.tearDown();
  56. }
  57. public void testBuildValidatorKey() {
  58. String validatorKey = AnnotationActionValidatorManager.buildValidatorKey(SimpleAnnotationAction.class);
  59. assertEquals(SimpleAnnotationAction.class.getName() + "/name|execute", validatorKey);
  60. }
  61. public void testBuildsValidatorsForAlias() {
  62. List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias);
  63. // 17 in the class level + 0 in the alias
  64. // TODO: add alias tests
  65. assertEquals(17, validatorList.size());
  66. }
  67. public void testGetValidatorsForGivenMethodNameWithoutReloading() throws ValidationException {
  68. List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
  69. //disable configuration reload/devmode
  70. FileManager.setReloadingConfigs(false);
  71. //17 in the class level + 0 in the alias
  72. assertEquals(12, validatorList.size());
  73. validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias, "execute");
  74. //expect same number of validators
  75. assertEquals(12, validatorList.size());
  76. }
  77. public void testDefaultMessageInterpolation() {
  78. // get validators
  79. List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
  80. assertEquals(3, validatorList.size());
  81. try {
  82. AnnotatedTestBean bean = new AnnotatedTestBean();
  83. bean.setName("foo");
  84. bean.setCount(99);
  85. ValidatorContext context = new GenericValidatorContext(bean);
  86. annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
  87. assertTrue(context.hasErrors());
  88. assertTrue(context.hasFieldErrors());
  89. List<String> l = context.getFieldErrors().get("count");
  90. assertNotNull(l);
  91. assertEquals(1, l.size());
  92. assertEquals("Smaller Invalid Count: 99", l.get(0));
  93. } catch (ValidationException ex) {
  94. ex.printStackTrace();
  95. fail("Validation error: " + ex.getMessage());
  96. }
  97. }
  98. public void testGetValidatorsForInterface() {
  99. List validatorList = annotationActionValidatorManager.getValidators(AnnotationDataAware2.class, alias);
  100. // 1 in interface hierarchy, 2 from parent interface (1 default + 1 context)
  101. assertEquals(3, validatorList.size());
  102. final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(0);
  103. assertEquals("data", dataValidator1.getFieldName());
  104. assertTrue(dataValidator1 instanceof RequiredFieldValidator);
  105. final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(1);
  106. assertEquals("data", dataValidator2.getFieldName());
  107. assertTrue(dataValidator2 instanceof RequiredStringValidator);
  108. final FieldValidator blingValidator = (FieldValidator) validatorList.get(2);
  109. assertEquals("bling", blingValidator.getFieldName());
  110. assertTrue(blingValidator instanceof RequiredStringValidator);
  111. }
  112. public void no_testGetValidatorsFromInterface() {
  113. List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction3.class, alias);
  114. // 17 in the class hierarchy + 1 in the interface + 1 in interface alias
  115. assertEquals(19, validatorList.size());
  116. final FieldValidator v = (FieldValidator) validatorList.get(0);
  117. assertEquals("bar", v.getFieldName());
  118. assertTrue(v instanceof RequiredFieldValidator);
  119. final FieldValidator v1 = (FieldValidator) validatorList.get(1);
  120. assertEquals("bar", v1.getFieldName());
  121. assertTrue(v1 instanceof IntRangeFieldValidator);
  122. final FieldValidator vdouble = (FieldValidator) validatorList.get(2);
  123. assertEquals("percentage", vdouble.getFieldName());
  124. assertTrue(vdouble instanceof DoubleRangeFieldValidator);
  125. final FieldValidator v2 = (FieldValidator) validatorList.get(3);
  126. assertEquals("baz", v2.getFieldName());
  127. assertTrue(v2 instanceof IntRangeFieldValidator);
  128. final FieldValidator v3 = (FieldValidator) validatorList.get(4);
  129. assertEquals("date", v3.getFieldName());
  130. assertTrue(v3 instanceof DateRangeFieldValidator);
  131. // action-level validator comes first
  132. final Validator v4 = (Validator) validatorList.get(5);
  133. assertTrue(v4 instanceof ExpressionValidator);
  134. // action-level validator comes first
  135. final Validator v5 = (Validator) validatorList.get(6);
  136. assertTrue(v5 instanceof ExpressionValidator);
  137. // action-level validator comes first
  138. final Validator v6 = (Validator) validatorList.get(7);
  139. assertTrue(v6 instanceof ExpressionValidator);
  140. // action-level validator comes first
  141. final Validator v7 = (Validator) validatorList.get(8);
  142. assertTrue(v7 instanceof ExpressionValidator);
  143. // action-level validator comes first
  144. final Validator v8 = (Validator) validatorList.get(9);
  145. assertTrue(v8 instanceof ExpressionValidator);
  146. final FieldValidator v9 = (FieldValidator) validatorList.get(10);
  147. assertEquals("datefield", v9.getFieldName());
  148. assertTrue(v9 instanceof DateRangeFieldValidator);
  149. final FieldValidator v10 = (FieldValidator) validatorList.get(11);
  150. assertEquals("emailaddress", v10.getFieldName());
  151. assertTrue(v10 instanceof EmailValidator);
  152. final FieldValidator v11 = (FieldValidator) validatorList.get(12);
  153. assertEquals("intfield", v11.getFieldName());
  154. assertTrue(v11 instanceof IntRangeFieldValidator);
  155. final FieldValidator v12 = (FieldValidator) validatorList.get(13);
  156. assertEquals("customfield", v12.getFieldName());
  157. assertTrue(v12 instanceof RequiredFieldValidator);
  158. final FieldValidator v13 = (FieldValidator) validatorList.get(14);
  159. assertEquals("stringisrequired", v13.getFieldName());
  160. assertTrue(v13 instanceof RequiredStringValidator);
  161. final FieldValidator v14 = (FieldValidator) validatorList.get(15);
  162. assertEquals("needstringlength", v14.getFieldName());
  163. assertTrue(v14 instanceof StringLengthFieldValidator);
  164. final FieldValidator v15 = (FieldValidator) validatorList.get(16);
  165. assertEquals("hreflocation", v15.getFieldName());
  166. assertTrue(v15 instanceof URLValidator);
  167. final FieldValidator v16 = (FieldValidator) validatorList.get(17);
  168. assertEquals("data", v16.getFieldName());
  169. assertTrue(v16 instanceof RequiredFieldValidator);
  170. final FieldValidator v17 = (FieldValidator) validatorList.get(18);
  171. assertEquals("data", v17.getFieldName());
  172. assertTrue(v17 instanceof RequiredStringValidator);
  173. }
  174. public void testMessageInterpolation() {
  175. // get validators
  176. List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
  177. assertEquals(3, validatorList.size());
  178. try {
  179. AnnotatedTestBean bean = new AnnotatedTestBean();
  180. bean.setName("foo");
  181. bean.setCount(150);
  182. ValidatorContext context = new GenericValidatorContext(bean);
  183. annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
  184. assertTrue(context.hasErrors());
  185. assertTrue(context.hasFieldErrors());
  186. List<String> l = context.getFieldErrors().get("count");
  187. assertNotNull(l);
  188. assertEquals(1, l.size());
  189. assertEquals("Count must be between 1 and 100, current value is 150.", l.get(0));
  190. } catch (ValidationException ex) {
  191. ex.printStackTrace();
  192. fail("Validation error: " + ex.getMessage());
  193. }
  194. }
  195. public void testSameAliasWithDifferentClass() {
  196. List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction.class, alias);
  197. List validatorList2 = annotationActionValidatorManager.getValidators(SimpleAnnotationAction2.class, alias);
  198. assertFalse(validatorList.size() == validatorList2.size());
  199. }
  200. public void testSameAliasWithAliasWithSlashes() {
  201. List validatorList = annotationActionValidatorManager.getValidators(SimpleAction.class, "some/alias");
  202. assertNotNull(validatorList);
  203. assertEquals(11, validatorList.size());
  204. }
  205. public void testSkipUserMarkerActionLevelShortCircuit() {
  206. // get validators
  207. List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
  208. assertEquals(10, validatorList.size());
  209. try {
  210. AnnotationUser user = new AnnotationUser();
  211. user.setName("Mark");
  212. user.setEmail("bad_email");
  213. user.setEmail2("bad_email");
  214. ValidatorContext context = new GenericValidatorContext(user);
  215. annotationActionValidatorManager.validate(user, null, context);
  216. assertTrue(context.hasFieldErrors());
  217. // check field errors
  218. List<String> l = context.getFieldErrors().get("email");
  219. assertNotNull(l);
  220. assertEquals(1, l.size());
  221. assertEquals("Not a valid e-mail.", l.get(0));
  222. l = context.getFieldErrors().get("email2");
  223. assertNotNull(l);
  224. assertEquals(2, l.size());
  225. assertEquals("Not a valid e-mail2.", l.get(0));
  226. assertEquals("Email2 not from the right company.", l.get(1));
  227. // check action errors
  228. assertTrue(context.hasActionErrors());
  229. l = (List<String>) context.getActionErrors();
  230. assertNotNull(l);
  231. assertEquals(2, l.size()); // both expression test failed see AnnotationUser-validation.xml
  232. assertEquals("Email does not start with mark", l.get(0));
  233. } catch (ValidationException ex) {
  234. ex.printStackTrace();
  235. fail("Validation error: " + ex.getMessage());
  236. }
  237. }
  238. public void testSkipAllActionLevelShortCircuit2() {
  239. // get validators
  240. List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
  241. assertEquals(10, validatorList.size());
  242. try {
  243. AnnotationUser user = new AnnotationUser();
  244. user.setName("Mark");
  245. // * mark both email to starts with mark to get pass the action-level validator,
  246. // so we could concentrate on testing the field-level validators (AnnotationUser-validation.xml)
  247. // * make both email the same to pass the action-level validator at
  248. // AnnotationUserMarker-validation.xml
  249. user.setEmail("mark_bad_email_for_field_val@foo.com");
  250. user.setEmail2("mark_bad_email_for_field_val@foo.com");
  251. ValidatorContext context = new GenericValidatorContext(user);
  252. annotationActionValidatorManager.validate(user, null, context);
  253. assertTrue(context.hasFieldErrors());
  254. // check field errors
  255. // we have an error in this field level, email does not ends with mycompany.com
  256. List l = (List) context.getFieldErrors().get("email");
  257. assertNotNull(l);
  258. assertEquals(1, l.size()); // because email-field-val is short-circuit
  259. assertEquals("Email not from the right company.", l.get(0));
  260. // check action errors
  261. l = (List) context.getActionErrors();
  262. assertFalse(context.hasActionErrors());
  263. assertEquals(0, l.size());
  264. } catch (ValidationException ex) {
  265. ex.printStackTrace();
  266. fail("Validation error: " + ex.getMessage());
  267. }
  268. }
  269. public void testActionLevelShortCircuit() throws Exception {
  270. List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
  271. assertEquals(10, validatorList.size());
  272. AnnotationUser user = new AnnotationUser();
  273. // all fields will trigger error, but sc of action-level, cause it to not appear
  274. user.setName(null);
  275. user.setEmail("rainerh(at)example.com");
  276. user.setEmail("rainer_h(at)example.com");
  277. ValidatorContext context = new GenericValidatorContext(user);
  278. annotationActionValidatorManager.validate(user, null, context);
  279. // check field level errors
  280. // shouldn't have any because action error prevents validation of anything else
  281. List l = (List) context.getFieldErrors().get("email2");
  282. assertNull(l);
  283. // check action errors
  284. assertTrue(context.hasActionErrors());
  285. l = (List) context.getActionErrors();
  286. assertNotNull(l);
  287. // we only get one, because AnnotationUserMarker-validation.xml action-level validator
  288. // already sc it :-)
  289. assertEquals(1, l.size());
  290. assertEquals("Email not the same as email2", l.get(0));
  291. }
  292. public void testShortCircuitNoErrors() {
  293. // get validators
  294. List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
  295. assertEquals(10, validatorList.size());
  296. try {
  297. AnnotationUser user = new AnnotationUser();
  298. user.setName("Mark");
  299. user.setEmail("mark@mycompany.com");
  300. user.setEmail2("mark@mycompany.com");
  301. ValidatorContext context = new GenericValidatorContext(user);
  302. annotationActionValidatorManager.validate(user, null, context);
  303. assertFalse(context.hasErrors());
  304. } catch (ValidationException ex) {
  305. ex.printStackTrace();
  306. fail("Validation error: " + ex.getMessage());
  307. }
  308. }
  309. }