PageRenderTime 145ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java

https://bitbucket.org/opensymphony/xwork
Java | 319 lines | 232 code | 76 blank | 11 comment | 0 complexity | 201f1cfbc907138d42c5c112ff9418b1 MD5 | raw file
  1. /*
  2. * Copyright (c) 2002-2006 by OpenSymphony
  3. * All rights reserved.
  4. */
  5. package com.opensymphony.xwork2;
  6. import com.opensymphony.xwork2.util.ValueStack;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.ResourceBundle;
  12. /**
  13. * Unit test for {@link ActionSupport}.
  14. *
  15. * @author Claus Ibsen
  16. */
  17. public class ActionSupportTest extends XWorkTestCase {
  18. private ActionSupport as;
  19. @Override
  20. protected void setUp() throws Exception {
  21. super.setUp();
  22. as = new ActionSupport();
  23. }
  24. @Override
  25. protected void tearDown() throws Exception {
  26. super.tearDown();
  27. as = null;
  28. }
  29. public void testNothingDoneOnActionSupport() throws Exception {
  30. assertEquals(false, as.hasErrors());
  31. assertNotNull(as.getActionErrors());
  32. assertEquals(0, as.getActionErrors().size());
  33. assertEquals(false, as.hasActionErrors());
  34. assertNotNull(as.getActionMessages());
  35. assertEquals(0, as.getActionMessages().size());
  36. assertEquals(false, as.hasActionMessages());
  37. assertNotNull(as.getFieldErrors());
  38. assertEquals(0, as.getFieldErrors().size());
  39. assertEquals(false, as.hasFieldErrors());
  40. assertNull(as.getText(null));
  41. try {
  42. as.pause(null);
  43. } catch (Exception e) {
  44. fail("Should not fail");
  45. }
  46. assertEquals(Action.INPUT, as.input());
  47. assertEquals(Action.SUCCESS, as.doDefault());
  48. assertEquals(Action.SUCCESS, as.execute());
  49. try {
  50. as.clone();
  51. fail("Failure expected for clone()");
  52. } catch (CloneNotSupportedException e) {
  53. // success!
  54. }
  55. assertNull(as.getText(null, (List) null));
  56. assertNull(as.getText(null, (String) null));
  57. assertNull(as.getText(null, (String[]) null));
  58. assertNull(as.getText(null, (String) null, (List) null));
  59. assertNull(as.getText(null, (String) null, (String) null));
  60. assertNull(as.getText(null, (String) null, (String[]) null));
  61. assertNull(as.getText(null, (String) null, (List) null, (ValueStack) null));
  62. assertNull(as.getText(null, (String) null, (String[]) null, (ValueStack) null));
  63. assertNotNull(as.getLocale());
  64. assertEquals(ActionContext.getContext().getLocale(), as.getLocale());
  65. assertNull(as.getTexts()); // can not find a bundle
  66. assertEquals("not.in.bundle", as.getText("not.in.bundle"));
  67. }
  68. public void testActionErrors() {
  69. assertEquals(false, as.hasActionErrors());
  70. assertEquals(0, as.getActionErrors().size());
  71. as.addActionError("Damm");
  72. assertEquals(1, as.getActionErrors().size());
  73. assertEquals("Damm", as.getActionErrors().iterator().next());
  74. assertEquals(true, as.hasActionErrors());
  75. assertEquals(true, as.hasErrors());
  76. as.clearErrorsAndMessages();
  77. assertEquals(false, as.hasActionErrors());
  78. assertEquals(false, as.hasErrors());
  79. }
  80. public void testActionMessages() {
  81. assertEquals(false, as.hasActionMessages());
  82. assertEquals(0, as.getActionMessages().size());
  83. as.addActionMessage("Killroy was here");
  84. assertEquals(1, as.getActionMessages().size());
  85. assertEquals("Killroy was here", as.getActionMessages().iterator().next());
  86. assertEquals(true, as.hasActionMessages());
  87. assertEquals(false, as.hasActionErrors()); // does not count as a error
  88. assertEquals(false, as.hasErrors()); // does not count as a error
  89. as.clearErrorsAndMessages();
  90. assertEquals(false, as.hasActionMessages());
  91. assertEquals(false, as.hasErrors());
  92. }
  93. public void testFieldErrors() {
  94. assertEquals(false, as.hasFieldErrors());
  95. assertEquals(0, as.getFieldErrors().size());
  96. as.addFieldError("username", "Admin is not allowed as username");
  97. List<String> errors = as.getFieldErrors().get("username");
  98. assertEquals(1, errors.size());
  99. assertEquals("Admin is not allowed as username", errors.get(0));
  100. assertEquals(true, as.hasFieldErrors());
  101. assertEquals(true, as.hasErrors());
  102. as.clearErrorsAndMessages();
  103. assertEquals(false, as.hasFieldErrors());
  104. assertEquals(false, as.hasErrors());
  105. }
  106. public void testDeprecated() throws Exception {
  107. assertNotNull(as.getErrorMessages());
  108. assertEquals(0, as.getErrorMessages().size());
  109. assertNotNull(as.getErrors());
  110. assertEquals(0, as.getErrors().size());
  111. }
  112. public void testLocale() {
  113. Locale defLocale = Locale.getDefault();
  114. ActionContext.getContext().setLocale(null);
  115. // will never return null, if no locale is set then default is returned
  116. assertNotNull(as.getLocale());
  117. assertEquals(defLocale, as.getLocale());
  118. ActionContext.getContext().setLocale(Locale.ITALY);
  119. assertEquals(Locale.ITALY, as.getLocale());
  120. ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
  121. assertEquals(defLocale, as.getLocale()); // ActionContext will create a new context, when it was set to null before
  122. }
  123. public void testMyActionSupport() throws Exception {
  124. ActionContext.getContext().setLocale(new Locale("da"));
  125. MyActionSupport mas = new MyActionSupport();
  126. assertEquals("santa", mas.doDefault());
  127. assertNotNull(mas.getTexts());
  128. assertEquals(false, mas.hasActionMessages());
  129. mas.validate();
  130. assertEquals(true, mas.hasActionMessages());
  131. }
  132. public void testSimpleGetTexts() throws Exception {
  133. ActionContext.getContext().setLocale(new Locale("da"));
  134. MyActionSupport mas = new MyActionSupport();
  135. checkGetTexts(mas);
  136. }
  137. public void testSimpleGetTextsWithInjectedTextProvider() throws Exception {
  138. ActionContext.getContext().setLocale(new Locale("da"));
  139. MyActionSupport mas = new MyActionSupport();
  140. TextProvider textProvider = container.getInstance(TextProvider.class, "system");
  141. assertNotNull(textProvider);
  142. container.inject(mas);
  143. checkGetTexts(mas);
  144. }
  145. private void checkGetTexts(MyActionSupport mas) {
  146. assertEquals("Hello World", mas.getText("hello"));
  147. assertEquals("not.in.bundle", mas.getText("not.in.bundle"));
  148. assertEquals("Hello World", mas.getText("hello", "this is default"));
  149. assertEquals("this is default", mas.getText("not.in.bundle", "this is default"));
  150. List nullList = null;
  151. assertEquals("Hello World", mas.getText("hello", nullList));
  152. String[] nullStrings = null;
  153. assertEquals("Hello World", mas.getText("hello", nullStrings));
  154. }
  155. public void testGetTextsWithArgs() throws Exception {
  156. ActionContext.getContext().setLocale(new Locale("da"));
  157. MyActionSupport mas = new MyActionSupport();
  158. assertEquals("Hello World", mas.getText("hello", "this is default", "from me")); // no args in bundle
  159. assertEquals("Hello World from me", mas.getText("hello.0", "this is default", "from me"));
  160. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", "from me"));
  161. assertEquals("this is default from me", mas.getText("not.in.bundle", "this is default {0}", "from me"));
  162. assertEquals("not.in.bundle", mas.getText("not.in.bundle"));
  163. }
  164. public void testGetTextsWithListArgs() throws Exception {
  165. ActionContext.getContext().setLocale(new Locale("da"));
  166. MyActionSupport mas = new MyActionSupport();
  167. List<Object> args = new ArrayList<Object>();
  168. args.add("Santa");
  169. args.add("loud");
  170. assertEquals("Hello World", mas.getText("hello", "this is default", args)); // no args in bundle
  171. assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args)); // only 1 arg in bundle
  172. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args));
  173. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", args));
  174. assertEquals("this is default Santa", mas.getText("not.in.bundle", "this is default {0}", args));
  175. assertEquals("this is default Santa speaking loud", mas.getText("not.in.bundle", "this is default {0} speaking {1}", args));
  176. assertEquals("Hello World", mas.getText("hello", args)); // no args in bundle
  177. assertEquals("Hello World Santa", mas.getText("hello.0", args)); // only 1 arg in bundle
  178. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", args));
  179. assertEquals("not.in.bundle", mas.getText("not.in.bundle", args));
  180. assertEquals("Hello World", mas.getText("hello", "this is default", (List) null));
  181. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", (List) null));
  182. }
  183. public void testGetTextsWithArrayArgs() throws Exception {
  184. ActionContext.getContext().setLocale(new Locale("da"));
  185. MyActionSupport mas = new MyActionSupport();
  186. String[] args = {"Santa", "loud"};
  187. assertEquals("Hello World", mas.getText("hello", "this is default", args)); // no args in bundle
  188. assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args)); // only 1 arg in bundle
  189. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args));
  190. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", args));
  191. assertEquals("this is default Santa", mas.getText("not.in.bundle", "this is default {0}", args));
  192. assertEquals("this is default Santa speaking loud", mas.getText("not.in.bundle", "this is default {0} speaking {1}", args));
  193. assertEquals("Hello World", mas.getText("hello", args)); // no args in bundle
  194. assertEquals("Hello World Santa", mas.getText("hello.0", args)); // only 1 arg in bundle
  195. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", args));
  196. assertEquals("not.in.bundle", mas.getText("not.in.bundle", args));
  197. assertEquals("Hello World", mas.getText("hello", "this is default", (String[]) null));
  198. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", (String[]) null));
  199. }
  200. public void testGetTextsWithListAndStack() throws Exception {
  201. ActionContext.getContext().setLocale(new Locale("da"));
  202. MyActionSupport mas = new MyActionSupport();
  203. ValueStack stack = ActionContext.getContext().getValueStack();
  204. List<Object> args = new ArrayList<Object>();
  205. args.add("Santa");
  206. args.add("loud");
  207. assertEquals("Hello World", mas.getText("hello", "this is default", args, stack)); // no args in bundle
  208. assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args, stack)); // only 1 arg in bundle
  209. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args, stack));
  210. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", args, stack));
  211. assertEquals("this is default Santa", mas.getText("not.in.bundle", "this is default {0}", args, stack));
  212. assertEquals("this is default Santa speaking loud", mas.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
  213. }
  214. public void testGetTextsWithArrayAndStack() throws Exception {
  215. ActionContext.getContext().setLocale(new Locale("da"));
  216. MyActionSupport mas = new MyActionSupport();
  217. ValueStack stack = ActionContext.getContext().getValueStack();
  218. String[] args = {"Santa", "loud"};
  219. assertEquals("Hello World", mas.getText("hello", "this is default", args, stack)); // no args in bundle
  220. assertEquals("Hello World Santa", mas.getText("hello.0", "this is default", args, stack)); // only 1 arg in bundle
  221. assertEquals("Hello World. This is Santa speaking loud", mas.getText("hello.1", "this is default", args, stack));
  222. assertEquals("this is default", mas.getText("not.in.bundle", "this is default", args, stack));
  223. assertEquals("this is default Santa", mas.getText("not.in.bundle", "this is default {0}", args, stack));
  224. assertEquals("this is default Santa speaking loud", mas.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
  225. }
  226. public void testGetBundle() throws Exception {
  227. ActionContext.getContext().setLocale(new Locale("da"));
  228. MyActionSupport mas = new MyActionSupport();
  229. ResourceBundle rb = ResourceBundle.getBundle(MyActionSupport.class.getName(), new Locale("da"));
  230. assertEquals(rb, mas.getTexts(MyActionSupport.class.getName()));
  231. }
  232. private class MyActionSupport extends ActionSupport {
  233. @Override
  234. public String doDefault() throws Exception {
  235. return "santa";
  236. }
  237. @Override
  238. public void validate() {
  239. super.validate(); // to have code coverage
  240. addActionMessage("validation was called");
  241. }
  242. }
  243. }