PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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