1 package com.sri.emo.wizard.expressoimpl;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import com.jcorporate.expresso.core.controller.ControllerException;
6 import com.jcorporate.expresso.core.controller.ExpressoRequest;
7 import com.jcorporate.expresso.core.controller.ExpressoResponse;
8 import com.jcorporate.expresso.core.controller.NonHandleableException;
9 import com.jcorporate.expresso.core.db.DBException;
10 import com.jcorporate.expresso.services.test.ControllerTestFixture;
11 import com.jcorporate.expresso.services.test.TestSystemInitializer;
12 import com.sri.emo.test.DatabaseTestFixture;
13 import com.sri.emo.test.EmoTestSuite;
14 import com.sri.emo.wizard.Wizard;
15 import com.sri.emo.wizard.WizardPage;
16 import junit.framework.TestCase;
17 import com.jcorporate.expresso.core.security.SuperUser;
18 import com.jcorporate.expresso.core.registry.MutableRequestRegistry;
19
20
21
22 /***
23 * @author Michael Rimov
24 * @todo Test Cancel. We need a special cancel transition set up.
25 */
26 public class TestWizardController extends TestCase {
27 private DatabaseTestFixture databaseTestFixture;
28 private ControllerTestFixture controllerTestFixture;
29
30 public TestWizardController() {
31 super();
32 try {
33 TestSystemInitializer.setUp();
34 new MutableRequestRegistry(TestSystemInitializer.getTestContext(), SuperUser.INSTANCE);
35 } catch (Exception ex) {
36 throw new RuntimeException("Error setting things up",ex);
37 }
38 }
39
40 protected void setUp() throws Exception {
41 super.setUp();
42 databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
43 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
44 controllerTestFixture = new ControllerTestFixture();
45 databaseTestFixture.setUp();
46 }
47
48 protected void tearDown() throws Exception {
49 databaseTestFixture.tearDown();
50
51 databaseTestFixture = null;
52 controllerTestFixture = null;
53 super.tearDown();
54 }
55
56
57 /***
58 * @todo Fix me -- getCancelTransition() is protected.
59 */
60 public void testGetCancelTransition() {
61
62
63
64
65
66 }
67
68
69 public void testGetCurrentWizard() throws Exception {
70 Map params = new HashMap();
71 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
72
73
74 params.put(WizardController.WIZ_DATA_ID, Integer.toString(12345));
75 WizardController c = (WizardController) controllerTestFixture.buildController(WizardController.class);
76 ExpressoRequest request = controllerTestFixture.buildControllerRequest(params, WizardController.STATE_BEGIN);
77 Wizard wiz = c.getCurrentWizard(request);
78 assertTrue(wiz == null);
79
80 ExpressoResponse response = controllerTestFixture.invokeState(WizardController.STATE_BEGIN, c, request);
81 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
82 wiz = c.getCurrentWizard(request);
83 assertTrue(wiz != null);
84 assertEquals(DatabaseTestFixture.DEFAULT_WIZ_ID, ((Integer) wiz.getId()).intValue());
85
86 response = controllerTestFixture.invokeState(WizardController.NEXT_STATE, c, request);
87 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
88 wiz = c.getCurrentWizard(request);
89 assertTrue(wiz != null);
90 assertEquals(DatabaseTestFixture.DEFAULT_WIZ_ID, ((Integer) wiz.getId()).intValue());
91 }
92
93 public void testGetCurrentPage() throws Exception {
94 Map params = new HashMap();
95 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
96 WizardController c = (WizardController) controllerTestFixture.buildController(WizardController.class);
97 ExpressoRequest request = controllerTestFixture.buildControllerRequest(params, WizardController.STATE_BEGIN);
98 ExpressoResponse response = controllerTestFixture.invokeState(WizardController.STATE_BEGIN, c, request);
99 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
100 WizardPage page = c.getCurrentPage(request, c.getCurrentWizard(request));
101 assertTrue(page != null);
102 assertEquals(21, ((Integer) page.getId()).intValue());
103
104 params.put(WizardController.WIZ_PAGE_PARAMETER, "21");
105 params.put(WizardController.WIZ_DATA_ID,"12345");
106 request = controllerTestFixture.buildControllerRequest(params, WizardController.NEXT_STATE);
107 response = controllerTestFixture.invokeState(WizardController.NEXT_STATE, c, request);
108 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
109 page = c.getCurrentPage(request, c.getCurrentWizard(request));
110 assertTrue(page != null);
111
112
113
114 assertEquals(21, ((Integer) page.getId()).intValue());
115
116 params.put(WizardController.WIZ_PAGE_PARAMETER, "22");
117 request = controllerTestFixture.buildControllerRequest(params, WizardController.NEXT_STATE);
118 response = controllerTestFixture.invokeState(WizardController.NEXT_STATE, c, request);
119 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
120 page = c.getCurrentPage(request, c.getCurrentWizard(request));
121 assertEquals(22, ((Integer) page.getId()).intValue());
122
123 }
124
125 public void testRunBeginStateWithNoParameter() throws DBException, NonHandleableException {
126 try {
127 controllerTestFixture.invokeFacade(WizardController.class
128 , new HashMap(), WizardController.STATE_BEGIN);
129 fail("ControllerException should have been thrown because of missing required parameter");
130 } catch (ControllerException ex) {
131
132 assertNotNull(ex.getMessage());
133 }
134 }
135
136 public void testRunBeginState() throws Exception {
137 Map params = new HashMap();
138 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
139 ExpressoResponse response = controllerTestFixture.invokeFacade(WizardController.class
140 , params, WizardController.STATE_BEGIN);
141
142 assertTrue(response != null);
143
144 assertTrue(response.getErrors() == null
145 || response.getErrors().getErrorCount() == 0);
146
147 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
148 assertTrue(wiz != null);
149 assertEquals(DatabaseTestFixture.DEFAULT_WIZ_ID, ((Integer) wiz.getId()).intValue());
150 }
151
152
153 public void testRunPreviousState() throws DBException, ControllerException, NonHandleableException {
154 Map params = new HashMap();
155 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
156 params.put(WizardController.WIZ_DATA_ID,Integer.toString(1239));
157 ExpressoResponse response = controllerTestFixture.invokeFacade(WizardController.class
158 , params, WizardController.STATE_BEGIN);
159
160 assertTrue(response != null);
161
162 assertTrue(response.getErrors() == null
163 || response.getErrors().getErrorCount() == 0);
164
165 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
166 WizardPage page = wiz.getCurrentPage();
167
168 params.put(WizardController.WIZ_PAGE_PARAMETER, page.getId().toString());
169 response = controllerTestFixture.invokeFacade(WizardController.class
170 , params, WizardController.NEXT_STATE);
171
172 assertTrue(response != null);
173
174 assertTrue(response.getErrors() == null
175 || response.getErrors().getErrorCount() == 0);
176
177 assertEquals(wiz,
178 (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID));
179 assertNotSame(page, wiz.getCurrentPage());
180
181 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
182 response = controllerTestFixture.invokeFacade(WizardController.class
183 , params, WizardController.PREVIOUS_STATE);
184
185 assertTrue(response != null);
186
187 assertTrue(response.getErrors() == null
188 || response.getErrors().getErrorCount() == 0);
189
190 assertEquals(wiz,
191 (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID));
192 assertEquals(page, wiz.getCurrentPage());
193 }
194
195
196 /***
197 * @throws Exception
198 * @todo Figure out a good test case for finish.
199 */
200 public void testRunFinishState() throws Exception {
201
202
203 }
204
205 public void testNullDataShouldNotResultInErrorForNoEntryPages() throws Exception {
206
207 Map params = new HashMap();
208 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
209 controllerTestFixture.invokeFacade(WizardController.class
210 , params, WizardController.STATE_BEGIN);
211
212 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
213
214 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
215 ExpressoResponse response = controllerTestFixture.invokeFacade(WizardController.class
216 , params, WizardController.NEXT_STATE);
217
218 assertTrue(response != null);
219 assertTrue(response.getErrors() == null || response.getErrors().getErrorCount() == 0);
220 }
221
222
223 public void testNullDataShouldResultInErrorCollection() throws Exception {
224 Map params = new HashMap();
225 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
226 controllerTestFixture.invokeFacade(WizardController.class
227 , params, WizardController.STATE_BEGIN);
228
229 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
230
231 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
232
233 controllerTestFixture.invokeFacade(WizardController.class
234 , params, WizardController.NEXT_STATE);
235
236 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
237
238 ExpressoResponse response = controllerTestFixture.invokeFacade(WizardController.class
239 , params, WizardController.NEXT_STATE);
240
241 assertTrue(response != null);
242 assertTrue(response.getErrors() != null);
243 assertTrue(response.getErrors().getErrorCount() > 0);
244 }
245
246 public void testNullDataForFinishShouldResultInErrorCollection() throws Exception {
247 Map params = new HashMap();
248 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
249 controllerTestFixture.invokeFacade(WizardController.class
250 , params, WizardController.STATE_BEGIN);
251
252 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
253
254 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
255
256 controllerTestFixture.invokeFacade(WizardController.class
257 , params, WizardController.NEXT_STATE);
258
259 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
260
261 ExpressoResponse response = controllerTestFixture.invokeFacade(WizardController.class
262 , params, WizardController.FINISH_STATE);
263
264 assertTrue(response != null);
265 assertTrue(response.getErrors() != null);
266 assertTrue(response.getErrors().getErrorCount() > 0);
267 }
268
269
270
271
272 public void testSessionExpiredShouldThrowExceptionOnNextOrPrevious() throws Exception {
273 Map params = new HashMap();
274 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
275 controllerTestFixture.invokeFacade(WizardController.class
276 , params, WizardController.STATE_BEGIN);
277
278 controllerTestFixture.newSession();
279
280 controllerTestFixture.invokeFacade(WizardController.class
281 , params, WizardController.STATE_BEGIN);
282
283
284
285 controllerTestFixture.newSession();
286
287 try {
288 controllerTestFixture.invokeFacade(WizardController.class
289 , params, WizardController.NEXT_STATE);
290 fail("Should have thrown an exception after an expired session.");
291 } catch (ControllerException ex) {
292
293 assertNotNull(ex.getMessage());
294 }
295
296 controllerTestFixture.newSession();
297
298 controllerTestFixture.invokeFacade(WizardController.class, params, WizardController.STATE_BEGIN);
299
300 Wizard wiz = (Wizard) controllerTestFixture.getPersistentSession().getAttribute(WizardController.REQUEST_ID);
301 params.put(WizardController.WIZ_PAGE_PARAMETER, wiz.getCurrentPage().getId().toString());
302 params.put(WizardController.WIZ_DATA_ID,"12345");
303 controllerTestFixture.invokeFacade(WizardController.class
304 , params, WizardController.NEXT_STATE);
305 controllerTestFixture.newSession();
306
307 try {
308 controllerTestFixture.invokeFacade(WizardController.class
309 , params, WizardController.PREVIOUS_STATE);
310 fail("Should have thrown an exception after an expired session.");
311 } catch (ControllerException ex) {
312
313 assertNotNull(ex.getMessage());
314 }
315
316 }
317
318 }