1
2
3
4
5
6
7
8
9
10 package com.sri.emo.wizard.selection.management;
11
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16 import com.jcorporate.expresso.core.controller.Block;
17 import com.jcorporate.expresso.core.controller.ControllerException;
18 import com.jcorporate.expresso.core.controller.ExpressoResponse;
19 import com.jcorporate.expresso.core.controller.Input;
20 import com.jcorporate.expresso.core.controller.NonHandleableException;
21 import com.jcorporate.expresso.core.controller.Output;
22 import com.jcorporate.expresso.core.controller.Transition;
23 import com.jcorporate.expresso.core.db.DBException;
24 import com.jcorporate.expresso.services.test.ControllerTestFixture;
25 import com.jcorporate.expresso.services.test.TestSystemInitializer;
26 import com.sri.emo.controller.SelectionWizardManager;
27 import com.sri.emo.dbobj.WizDefinition;
28 import com.sri.emo.dbobj.WizStep;
29 import com.sri.emo.test.DatabaseTestFixture;
30 import com.sri.emo.test.EmoTestSuite;
31 import com.sri.emo.wizard.expressoimpl.WizardController;
32 import junit.framework.TestCase;
33
34 public class TestWizardStepController extends TestCase {
35 private ControllerTestFixture testFixture = null;
36 private DatabaseTestFixture dbTestFixture = null;
37
38 protected void setUp() throws Exception {
39 super.setUp();
40 testFixture = new ControllerTestFixture();
41 testFixture.setUp();
42 dbTestFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(), EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
43 dbTestFixture.setUp();
44 }
45
46 protected void tearDown() throws Exception {
47
48 dbTestFixture.tearDown();
49 dbTestFixture = null;
50
51 testFixture.tearDown();
52 testFixture = null;
53 super.tearDown();
54 }
55
56 /***
57 * Checks that there are no errors defined in the error collection.
58 *
59 * @param response The ExpressoResponse object.
60 * @throws ControllerException
61 */
62 protected static void checkNoErrorsInErrorCollection(ExpressoResponse response) throws
63 ControllerException {
64 assertTrue(response.getErrors() == null
65 || response.getErrors().getErrorCount() == 0);
66 }
67
68
69 public void testPromptTextEntryInputs() throws ControllerException, NonHandleableException, DBException {
70
71 String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
72 Map params = new HashMap();
73 params.put(WizStep.FLD_WIZID, wizId);
74
75 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params, PromptTextEntry.STATE_NAME);
76
77 assertTrue(response != null);
78 checkNoErrorsInErrorCollection(response);
79 assertTrue(response.getBlock("wizstep") != null);
80
81 Block step = response.getBlock("wizstep");
82
83
84 Input title = step.getInput(WizStep.FLD_TITLE);
85 assertTrue(title != null);
86
87 }
88
89 public void testPromptTextEntryTransitions() throws ControllerException, NonHandleableException, DBException {
90 String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
91 Map params = new HashMap();
92 params.put(WizStep.FLD_WIZID, wizId);
93
94 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
95 PromptTextEntry.STATE_NAME);
96
97 assertTrue(response != null);
98 checkNoErrorsInErrorCollection(response);
99
100 Transition cancel = response.getTransition("cancel");
101 assertTrue(cancel != null);
102 assertEquals(SelectionWizardManager.class.getName(), cancel.getControllerObject());
103 assertEquals(SelectionWizardManager.STATE_PROMPT_EDIT, cancel.getState());
104 assertEquals(cancel.getParam(WizardController.WIZ_PARAMETER_ID), wizId);
105
106 Transition save = response.getTransition("save");
107 assertTrue(save != null);
108 assertEquals(WizardStepController.class.getName(), save.getControllerObject());
109 assertEquals(WizardStepController.STATE_DO_ADD, save.getState());
110 assertEquals(wizId, save.getParam(WizStep.FLD_WIZID));
111 }
112
113
114 /***
115 * @throws ControllerException upon error
116 * @throws NonHandleableException upon fatal error
117 * @throws DBException upon database error.
118 */
119 public void testRunDeleteState() throws ControllerException, NonHandleableException, DBException {
120
121 String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
122 WizStep oneStep = new WizStep();
123 oneStep.setField(WizStep.FLD_WIZID, wizId);
124 int numberSteps = oneStep.count();
125
126 Map params = new HashMap();
127 params.put(WizStep.FLD_ID, wizId);
128 WizDefinition wizard = new WizDefinition();
129 wizard.setId(wizId);
130 wizard.retrieve();
131
132 WizStep deleteStep = (WizStep)wizard.getPageDefinitions().get(0);
133 params.put("StepId", deleteStep.getId());
134
135 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
136 WizardStepController.STATE_DO_DELETE);
137 assertTrue(response != null);
138 checkNoErrorsInErrorCollection(response);
139
140
141 int afterNumberSteps = oneStep.count();
142 assertEquals((numberSteps - 1), afterNumberSteps);
143
144
145
146
147
148 for (Iterator i = wizard.getPageDefinitions().iterator();
149 i.hasNext();) {
150 WizStep aStep = (WizStep) i.next();
151 assertNotSame(deleteStep.getId(), aStep.getId());
152 }
153
154 }
155
156 /***
157 * @throws ControllerException upon error
158 * @throws NonHandleableException upon fatal error
159 * @throws DBException upon database error.
160 */
161 public void testRunOrderDownState() throws ControllerException, NonHandleableException, DBException {
162 WizDefinition wizard = new WizDefinition();
163 wizard.setId(DatabaseTestFixture.DEFAULT_WIZ_ID);
164 wizard.retrieve();
165
166 List allSteps = wizard.getPageDefinitions();
167 int beforeIds[] = new int[allSteps.size()];
168 for (int i = 0; i < allSteps.size(); i++) {
169 WizStep oneStep = (WizStep) allSteps.get(i);
170 beforeIds[i] = oneStep.getFieldInt(WizStep.FLD_ID);
171 }
172
173 int expectedAfterIds[] = new int[allSteps.size()];
174
175 for (int i = 0; i < allSteps.size(); i++) {
176 if (i == 0) {
177 expectedAfterIds[0] = beforeIds[1];
178 expectedAfterIds[1] = beforeIds[0];
179 i++;
180 continue;
181 }
182
183 expectedAfterIds[i] = beforeIds[i];
184 }
185
186 Map params = new HashMap();
187
188 WizStep moveStep = (WizStep) wizard.getPageDefinitions().get(0);
189 params.put(WizStep.FLD_ID, moveStep.getId());
190
191 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
192 WizardStepController.STATE_ORDERDOWN);
193
194 assertTrue(response != null);
195 checkNoErrorsInErrorCollection(response);
196
197
198 assertEquals(allSteps.size(),
199 wizard.getPageDefinitions().size());
200
201
202 List finalSteps = wizard.getPageDefinitions();
203 for (int i = 0; i < allSteps.size(); i++) {
204 WizStep oneStep = (WizStep) finalSteps.get(i);
205 assertEquals(expectedAfterIds[i],
206 oneStep.getFieldInt(WizStep.FLD_ID));
207 }
208
209 }
210
211
212 /***
213 * This is nearly identical to order down, EXCEPT that we order up step #1,
214 * which has the same effect as order down of step #0
215 *
216 * @throws ControllerException upon error
217 * @throws NonHandleableException upon fatal error
218 * @throws DBException upon database error.
219 */
220 public void testRunOrderUpState() throws ControllerException, NonHandleableException, DBException {
221 WizDefinition wizard = new WizDefinition();
222 wizard.setId(DatabaseTestFixture.DEFAULT_WIZ_ID);
223 wizard.retrieve();
224
225 List allSteps = wizard.getPageDefinitions();
226 int beforeIds[] = new int[allSteps.size()];
227 for (int i = 0; i < allSteps.size(); i++) {
228 WizStep oneStep = (WizStep) allSteps.get(i);
229 beforeIds[i] = oneStep.getFieldInt(WizStep.FLD_ID);
230 }
231
232 int expectedAfterIds[] = new int[allSteps.size()];
233
234 for (int i = 0; i < allSteps.size(); i++) {
235 if (i == 0) {
236 expectedAfterIds[0] = beforeIds[1];
237 expectedAfterIds[1] = beforeIds[0];
238 i++;
239 continue;
240 }
241
242 expectedAfterIds[i] = beforeIds[i];
243 }
244
245 final String wizId = Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID);
246 Map params = new HashMap();
247 params.put(WizardController.WIZ_PARAMETER_ID, wizId);
248
249 WizStep moveStep = (WizStep) wizard.getPageDefinitions().get(1);
250 params.put("StepId", moveStep.getId());
251
252 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
253 WizardStepController.STATE_ORDERUP);
254
255 assertTrue(response != null);
256 checkNoErrorsInErrorCollection(response);
257
258
259 assertEquals(allSteps.size(),
260 wizard.getPageDefinitions().size());
261
262
263 List finalSteps = wizard.getPageDefinitions();
264 for (int i = 0; i < allSteps.size(); i++) {
265 WizStep oneStep = (WizStep) finalSteps.get(i);
266 assertEquals(expectedAfterIds[i],
267 oneStep.getFieldInt(WizStep.FLD_ID));
268 }
269 }
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 public void testRunPromptDeleteState() throws Exception {
321 DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
322 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
323
324 dtf.setUp();
325 try {
326 WizStep ws = new WizStep();
327 ws.setId(DatabaseTestFixture.DEFAULT_WELCOME_STEP);
328 ws.retrieve();
329
330 Map params = new HashMap();
331 params.put(WizardController.WIZ_PARAMETER_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
332 params.put(WizStep.FLD_ID, Integer.toString(DatabaseTestFixture.DEFAULT_WELCOME_STEP));
333
334 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
335 WizardStepController.STATE_PROMPT_DELETE);
336
337 checkNoErrorsInErrorCollection(response);
338
339 Output o = response.getOutput("prompt");
340 assertEquals(ws.getTitle(), o.getContent());
341
342 Transition yes = response.getTransition("yes");
343 assertTrue(yes != null);
344 assertEquals(ws.getId(), yes.getParam(WizStep.FLD_ID));
345 assertEquals(WizardStepController.STATE_DO_DELETE, yes.getState());
346
347 Transition no = response.getTransition("no");
348 assertTrue(no != null);
349 assertEquals(SelectionWizardManager.STATE_PROMPT_EDIT, no.getState());
350 assertEquals(ws.getWizId(), no.getParam(WizardController.WIZ_PARAMETER_ID));
351 } finally {
352 dtf.tearDown();
353 }
354
355 }
356
357 /***
358 * @todo finish me
359 */
360 public void testRunAddCompleteState() throws Exception {
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 }
398
399 /***
400 * @todo finish me
401 */
402 public void testRunEditState() throws Exception {
403 DatabaseTestFixture dtf = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
404 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
405
406 dtf.setUp();
407 try {
408 Map params = new HashMap();
409 params.put(WizStep.FLD_WIZID, Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID));
410 params.put(WizStep.FLD_ID, Integer.toString(DatabaseTestFixture.DEFAULT_USER_RATING_STEP));
411
412 ExpressoResponse response = testFixture.invokeFacade(WizardStepController.class, params,
413 WizardStepController.STATE_PROMPT_EDIT);
414
415 checkNoErrorsInErrorCollection(response);
416
417 Block b = response.getBlock("wizstep");
418 assertTrue(b != null);
419
420
421 Input i = b.getInput("Title");
422 assertTrue(i != null);
423 assertEquals("User Rating", i.getDefaultValue());
424 assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
425
426 i = b.getInput("Directive");
427 assertTrue(i != null);
428 assertEquals("Choose the user rating you want to see.", i.getDefaultValue());
429 assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
430
431 i = b.getInput("HelpText");
432 assertTrue(i != null);
433 assertEquals("If you want a really bad movie, check the one star movie.", i.getDefaultValue());
434 assertTrue(i.getValidValuesList() == null || i.getValidValuesList().size() == 0);
435
436 } finally {
437 dtf.tearDown();
438 }
439 }
440
441 }