PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 349 lines | 115 code | 20 blank | 214 comment | 0 complexity | ad3a5f2e58dc3a3cd9a5d0e185d3c81d MD5 | raw file
  1. /*
  2. * Copyright 2002-2003,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.mockobjects.dynamic.C;
  18. import com.mockobjects.dynamic.Mock;
  19. import com.opensymphony.xwork2.*;
  20. import com.opensymphony.xwork2.config.ConfigurationException;
  21. import com.opensymphony.xwork2.test.DataAware2;
  22. import com.opensymphony.xwork2.test.SimpleAction2;
  23. import com.opensymphony.xwork2.test.SimpleAction3;
  24. import com.opensymphony.xwork2.util.ValueStack;
  25. import junit.framework.TestCase;
  26. import java.util.ArrayList;
  27. import java.util.HashMap;
  28. import java.util.List;
  29. /**
  30. * DefaultActionValidatorManagerTest
  31. *
  32. * @author Jason Carreira
  33. * @author tm_jee
  34. * @version $Date: 2009-12-27 19:18:29 +0100 (Sun, 27 Dec 2009) $ $Id: DefaultActionValidatorManagerTest.java 894090 2009-12-27 18:18:29Z martinc $
  35. */
  36. public class DefaultActionValidatorManagerTest extends TestCase {
  37. protected final String alias = "validationAlias";
  38. DefaultActionValidatorManager actionValidatorManager;
  39. Mock mockValidatorFileParser;
  40. Mock mockValidatorFactory;
  41. ValueStack stubValueStack;
  42. @Override
  43. protected void setUp() throws Exception {
  44. actionValidatorManager = new DefaultActionValidatorManager();
  45. super.setUp();
  46. mockValidatorFileParser = new Mock(ValidatorFileParser.class);
  47. actionValidatorManager.setValidatorFileParser((ValidatorFileParser)mockValidatorFileParser.proxy());
  48. mockValidatorFactory = new Mock(ValidatorFactory.class);
  49. actionValidatorManager.setValidatorFactory((ValidatorFactory)mockValidatorFactory.proxy());
  50. stubValueStack = new StubValueStack();
  51. ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
  52. ActionContext.getContext().setValueStack(stubValueStack);
  53. }
  54. @Override
  55. protected void tearDown() throws Exception {
  56. actionValidatorManager = null;
  57. super.tearDown();
  58. mockValidatorFactory = null;
  59. mockValidatorFileParser = null;
  60. }
  61. public void testBuildValidatorKey() {
  62. String validatorKey = DefaultActionValidatorManager.buildValidatorKey(SimpleAction.class, alias);
  63. assertEquals(SimpleAction.class.getName() + "/" + alias, validatorKey);
  64. }
  65. public void testBuildsValidatorsForAlias() {
  66. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  67. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validation.xml")),
  68. new ArrayList());
  69. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  70. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validationAlias-validation.xml")),
  71. new ArrayList());
  72. actionValidatorManager.getValidators(SimpleAction.class, alias);
  73. mockValidatorFileParser.verify();
  74. }
  75. public void testBuildsValidatorsForAliasError() {
  76. boolean pass = false;
  77. try {
  78. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  79. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/TestBean-validation.xml")),
  80. new ArrayList());
  81. mockValidatorFileParser.expectAndThrow("parseActionValidatorConfigs",
  82. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/TestBean-badtest-validation.xml")),
  83. new ConfigurationException());
  84. List validatorList = actionValidatorManager.getValidators(TestBean.class, "badtest");
  85. } catch (XWorkException ex) {
  86. pass = true;
  87. }
  88. mockValidatorFileParser.verify();
  89. assertTrue("Didn't throw exception on load failure", pass);
  90. }
  91. public void testGetValidatorsForInterface() {
  92. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  93. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/DataAware-validation.xml")),
  94. new ArrayList());
  95. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  96. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/DataAware-validationAlias-validation.xml")),
  97. new ArrayList());
  98. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  99. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/DataAware2-validation.xml")),
  100. new ArrayList());
  101. actionValidatorManager.getValidators(DataAware2.class, alias);
  102. mockValidatorFileParser.verify();
  103. }
  104. public void testGetValidatorsFromInterface() {
  105. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  106. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validation.xml")),
  107. new ArrayList());
  108. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  109. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validationAlias-validation.xml")),
  110. new ArrayList());
  111. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  112. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/DataAware-validation.xml")),
  113. new ArrayList());
  114. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  115. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/DataAware-validationAlias-validation.xml")),
  116. new ArrayList());
  117. actionValidatorManager.getValidators(SimpleAction3.class, alias);
  118. mockValidatorFileParser.verify();
  119. }
  120. public void testSameAliasWithDifferentClass() {
  121. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  122. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validation.xml")),
  123. new ArrayList());
  124. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  125. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/SimpleAction-validationAlias-validation.xml")),
  126. new ArrayList());
  127. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  128. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/SimpleAction2-validation.xml")),
  129. new ArrayList());
  130. mockValidatorFileParser.expectAndReturn("parseActionValidatorConfigs",
  131. C.args(C.IS_NOT_NULL, C.IS_NOT_NULL, C.eq("com/opensymphony/xwork2/test/SimpleAction2-validationAlias-validation.xml")),
  132. new ArrayList());
  133. actionValidatorManager.getValidators(SimpleAction.class, alias);
  134. actionValidatorManager.getValidators(SimpleAction2.class, alias);
  135. mockValidatorFileParser.verify();
  136. }
  137. /*
  138. // TODO: this all need to be converted to real unit tests
  139. public void testSkipUserMarkerActionLevelShortCircuit() {
  140. // get validators
  141. List validatorList = actionValidatorManager.getValidators(User.class, null);
  142. assertEquals(10, validatorList.size());
  143. try {
  144. User user = new User();
  145. user.setName("Mark");
  146. user.setEmail("bad_email");
  147. user.setEmail2("bad_email");
  148. ValidatorContext context = new GenericValidatorContext(user);
  149. actionValidatorManager.validate(user, null, context);
  150. assertTrue(context.hasFieldErrors());
  151. // check field errors
  152. List l = (List) context.getFieldErrors().get("email");
  153. assertNotNull(l);
  154. assertEquals(1, l.size());
  155. assertEquals("Not a valid e-mail.", l.get(0));
  156. l = (List) context.getFieldErrors().get("email2");
  157. assertNotNull(l);
  158. assertEquals(2, l.size());
  159. assertEquals("Not a valid e-mail2.", l.get(0));
  160. assertEquals("Email2 not from the right company.", l.get(1));
  161. // check action errors
  162. assertTrue(context.hasActionErrors());
  163. l = (List) context.getActionErrors();
  164. assertNotNull(l);
  165. assertEquals(2, l.size()); // both expression test failed see User-validation.xml
  166. assertEquals("Email does not start with mark", l.get(0));
  167. } catch (ValidationException ex) {
  168. ex.printStackTrace();
  169. fail("Validation error: " + ex.getMessage());
  170. }
  171. }
  172. public void testSkipAllActionLevelShortCircuit2() {
  173. // get validators
  174. List validatorList = actionValidatorManager.getValidators(User.class, null);
  175. assertEquals(10, validatorList.size());
  176. try {
  177. User user = new User();
  178. user.setName("Mark");
  179. // * mark both email to starts with mark to get pass the action-level validator,
  180. // so we could concentrate on testing the field-level validators (User-validation.xml)
  181. // * make both email the same to pass the action-level validator at
  182. // UserMarker-validation.xml
  183. user.setEmail("mark_bad_email_for_field_val@foo.com");
  184. user.setEmail2("mark_bad_email_for_field_val@foo.com");
  185. ValidatorContext context = new GenericValidatorContext(user);
  186. actionValidatorManager.validate(user, null, context);
  187. assertTrue(context.hasFieldErrors());
  188. // check field errors
  189. // we have an error in this field level, email does not ends with mycompany.com
  190. List l = (List) context.getFieldErrors().get("email");
  191. assertNotNull(l);
  192. assertEquals(1, l.size()); // because email-field-val is short-circuit
  193. assertEquals("Email not from the right company.", l.get(0));
  194. // check action errors
  195. l = (List) context.getActionErrors();
  196. assertFalse(context.hasActionErrors());
  197. assertEquals(0, l.size());
  198. } catch (ValidationException ex) {
  199. ex.printStackTrace();
  200. fail("Validation error: " + ex.getMessage());
  201. }
  202. }
  203. public void testActionLevelShortCircuit() throws Exception {
  204. List validatorList = actionValidatorManager.getValidators(User.class, null);
  205. assertEquals(10, validatorList.size());
  206. User user = new User();
  207. // all fields will trigger error, but sc of action-level, cause it to not appear
  208. user.setName(null);
  209. user.setEmail("tmjee(at)yahoo.co.uk");
  210. user.setEmail("tm_jee(at)yahoo.co.uk");
  211. ValidatorContext context = new GenericValidatorContext(user);
  212. actionValidatorManager.validate(user, null, context);
  213. // check field level errors
  214. // shouldn't have any because action error prevents validation of anything else
  215. List l = (List) context.getFieldErrors().get("email2");
  216. assertNull(l);
  217. // check action errors
  218. assertTrue(context.hasActionErrors());
  219. l = (List) context.getActionErrors();
  220. assertNotNull(l);
  221. // we only get one, because UserMarker-validation.xml action-level validator
  222. // already sc it :-)
  223. assertEquals(1, l.size());
  224. assertEquals("Email not the same as email2", l.get(0));
  225. }
  226. public void testShortCircuitNoErrors() {
  227. // get validators
  228. List validatorList = actionValidatorManager.getValidators(User.class, null);
  229. assertEquals(10, validatorList.size());
  230. try {
  231. User user = new User();
  232. user.setName("Mark");
  233. user.setEmail("mark@mycompany.com");
  234. user.setEmail2("mark@mycompany.com");
  235. ValidatorContext context = new GenericValidatorContext(user);
  236. actionValidatorManager.validate(user, null, context);
  237. assertFalse(context.hasErrors());
  238. } catch (ValidationException ex) {
  239. ex.printStackTrace();
  240. fail("Validation error: " + ex.getMessage());
  241. }
  242. }
  243. public void testFieldErrorsOrder() throws Exception {
  244. ValidationOrderAction action = new ValidationOrderAction();
  245. actionValidatorManager.validate(action, "actionContext");
  246. Map fieldErrors = action.getFieldErrors();
  247. Iterator i = fieldErrors.entrySet().iterator();
  248. assertNotNull(fieldErrors);
  249. assertEquals(fieldErrors.size(), 12);
  250. Map.Entry e = (Map.Entry) i.next();
  251. assertEquals(e.getKey(), "username");
  252. assertEquals(((List)e.getValue()).get(0), "username required");
  253. e = (Map.Entry) i.next();
  254. assertEquals(e.getKey(), "password");
  255. assertEquals(((List)e.getValue()).get(0), "password required");
  256. e = (Map.Entry) i.next();
  257. assertEquals(e.getKey(), "confirmPassword");
  258. assertEquals(((List)e.getValue()).get(0), "confirm password required");
  259. e = (Map.Entry) i.next();
  260. assertEquals(e.getKey(), "firstName");
  261. assertEquals(((List)e.getValue()).get(0), "first name required");
  262. e = (Map.Entry) i.next();
  263. assertEquals(e.getKey(), "lastName");
  264. assertEquals(((List)e.getValue()).get(0), "last name required");
  265. e = (Map.Entry) i.next();
  266. assertEquals(e.getKey(), "city");
  267. assertEquals(((List)e.getValue()).get(0), "city is required");
  268. e = (Map.Entry) i.next();
  269. assertEquals(e.getKey(), "province");
  270. assertEquals(((List)e.getValue()).get(0), "province is required");
  271. e = (Map.Entry) i.next();
  272. assertEquals(e.getKey(), "country");
  273. assertEquals(((List)e.getValue()).get(0), "country is required");
  274. e = (Map.Entry) i.next();
  275. assertEquals(e.getKey(), "postalCode");
  276. assertEquals(((List)e.getValue()).get(0), "postal code is required");
  277. e = (Map.Entry) i.next();
  278. assertEquals(e.getKey(), "email");
  279. assertEquals(((List)e.getValue()).get(0), "email is required");
  280. e = (Map.Entry) i.next();
  281. assertEquals(e.getKey(), "website");
  282. assertEquals(((List)e.getValue()).get(0), "website is required");
  283. e = (Map.Entry) i.next();
  284. assertEquals(e.getKey(), "passwordHint");
  285. assertEquals(((List)e.getValue()).get(0), "password hint is required");
  286. }
  287. */
  288. }