1 package com.sri.emo.wizard.defaults;
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.IOException;
18 import java.io.ObjectInputStream;
19 import java.io.ObjectOutputStream;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import com.jcorporate.expresso.core.controller.Transition;
24 import com.jcorporate.expresso.services.test.TestSystemInitializer;
25 import com.sri.emo.test.EmoWizardDatabaseTestCase;
26 import com.sri.emo.wizard.WizardException;
27 import com.sri.emo.wizard.WizardPage;
28 import com.sri.emo.wizard.expressoimpl.ExpressoLink;
29 import com.sri.emo.wizard.expressoimpl.WizardController;
30 import com.sri.emo.wizard.selection.EmoSelectionWizardFactory;
31 import com.sri.emo.wizard.selection.WizardAction;
32
33 public class TestSequentialWizard extends EmoWizardDatabaseTestCase {
34 int wizardId = 0;
35 private EmoSelectionWizardFactory emoWizardFactory = null;
36 private WizardTestModel testModel = null;
37 private SequentialWizard sequentialWizard = null;
38
39 public TestSequentialWizard(String name) {
40 super(name);
41 }
42
43 protected void setUp() throws Exception {
44 super.setUp();
45 testModel = new WizardTestModel(TestSystemInitializer.getTestContext());
46 wizardId = testModel.intialize();
47 emoWizardFactory = new EmoSelectionWizardFactory();
48 emoWizardFactory.setWizardId(wizardId);
49
50 sequentialWizard = (SequentialWizard) emoWizardFactory.buildWizard();
51 }
52
53 protected void tearDown() throws Exception {
54 emoWizardFactory = null;
55 testModel.destroy();
56 testModel = null;
57 wizardId = 0;
58 super.tearDown();
59 }
60
61
62 public void testBegin() {
63 try {
64 WizardPage page = sequentialWizard.begin();
65 assertTrue(page != null);
66 assertEquals(page.getClass(), EmoWizardPage.class);
67 assertEquals(page.getMetadata().getTitle(),
68 WizardTestModel.WIZ_PAGES[0][WizardTestModel.INDEX_TITLE]);
69 assertEquals(page.getMetadata().getDirective(),
70 WizardTestModel.WIZ_PAGES[0][WizardTestModel.
71 INDEX_DIRECTIVE]);
72 assertEquals(page.getMetadata().getHelpText(),
73 WizardTestModel.WIZ_PAGES[0][WizardTestModel.
74 INDEX_HELPTEXT]);
75
76
77 Transition t = ((ExpressoLink) page.getMetadata().getNext()).
78 getTransition();
79 assertTrue(t != null);
80 assertEquals(t.getState(), WizardController.NEXT_STATE);
81 assertEquals(t.getControllerObject(),
82 WizardAction.class.getName());
83 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
84 Integer.toString(wizardId));
85
86
87 assertEquals(page.getMetadata().getPrevious(), null);
88
89
90 t = ((ExpressoLink) page.getMetadata().getCancel()).getTransition();
91 assertEquals(t.getState(), WizardController.CANCEL_STATE);
92 assertEquals(t.getControllerObject(),
93 WizardAction.class.getName());
94 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
95 Integer.toString(wizardId));
96 } catch (Exception ex) {
97 ex.printStackTrace();
98 fail("Error testing wizard: " + ex.getMessage());
99 }
100
101
102 }
103
104 public void testNext() {
105 try {
106 WizardPage page = sequentialWizard.begin();
107 assertTrue(page != null);
108 WizardPage secondPage = sequentialWizard.next(page, "Test1");
109 assertTrue(secondPage != null);
110
111
112 Transition t = ((ExpressoLink) page.getMetadata().getNext()).
113 getTransition();
114 assertEquals(WizardController.NEXT_STATE, t.getState());
115 assertEquals(t.getControllerObject(),
116 WizardAction.class.getName());
117 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
118 Integer.toString(wizardId));
119
120
121 t = ((ExpressoLink) secondPage.getMetadata().getPrevious()).
122 getTransition();
123 assertEquals(WizardController.PREVIOUS_STATE, t.getState());
124 assertEquals(t.getControllerObject(),
125 WizardAction.class.getName());
126 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
127 Integer.toString(wizardId));
128
129
130 t = ((ExpressoLink) secondPage.getMetadata().getCancel()).
131 getTransition();
132 assertEquals(t.getState(), WizardController.CANCEL_STATE);
133 assertEquals(t.getControllerObject(),
134 WizardAction.class.getName());
135 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
136 Integer.toString(wizardId));
137
138 WizardPage thirdPage = sequentialWizard.next(secondPage, "Test1");
139 assertTrue(thirdPage != null);
140
141 assertNotNull(thirdPage.getMetadata().getNext());
142
143
144 t = ((ExpressoLink) thirdPage.getMetadata().getPrevious()).
145 getTransition();
146 assertEquals(WizardController.PREVIOUS_STATE, t.getState());
147 assertEquals(t.getControllerObject(),
148 WizardAction.class.getName());
149 assertEquals(t.getParam(WizardController.WIZ_PARAMETER_ID),
150 Integer.toString(wizardId));
151
152
153
154
155
156
157
158
159 } catch (Exception ex) {
160 ex.printStackTrace();
161 fail("Error testing wizard: " + ex.getMessage());
162 }
163
164 }
165
166 /***
167 * Test that occurs if the browser back button has occurred and a new
168 * submit has happened.
169 *
170 * @throws WizardException upon error
171 */
172 public void testBackButtonRollback() throws WizardException {
173 WizardPage page = sequentialWizard.begin();
174 assertTrue(page != null);
175 WizardPage secondPage = sequentialWizard.next(page, "Test1");
176 assertTrue(secondPage != null);
177
178 WizardPage testPage = sequentialWizard.next(page, "Test After Rollback");
179 assertEquals(secondPage, testPage);
180
181 assertEquals("Test After Rollback", page.getData());
182 }
183
184
185 public void testPrevious() {
186 try {
187 WizardPage page = sequentialWizard.begin();
188 assertTrue(page != null);
189 WizardPage secondPage = sequentialWizard.next(page, "Test1");
190 assertTrue(secondPage != null);
191
192 WizardPage pageback = sequentialWizard.previous();
193 assertEquals(page, pageback);
194
195 try {
196 sequentialWizard.previous();
197 fail("Next should have thrown an exception");
198 } catch (WizardException ex) {
199 assertNotNull(ex.getMessage());
200
201 }
202 } catch (Exception ex) {
203 ex.printStackTrace();
204 fail("Error testing wizard: " + ex.getMessage());
205 }
206
207 }
208
209 public void testGetResults() {
210 try {
211 WizardPage page = sequentialWizard.begin();
212 assertTrue(page != null);
213 WizardPage secondPage = sequentialWizard.next(page, "Test1");
214 assertTrue(secondPage != null);
215 WizardPage thirdPage = sequentialWizard.next(secondPage, "Test2");
216 sequentialWizard.next(thirdPage, "Test3");
217 Map results = sequentialWizard.getAllData();
218 assertTrue(results != null);
219 assertTrue(results.size() == 3 + 2);
220 assertEquals("Test1", results.get(page.getId()));
221 assertEquals("Test2", results.get(secondPage.getId()));
222 assertEquals("Test3", results.get(thirdPage.getId()));
223 sequentialWizard.destroy();
224 } catch (Exception ex) {
225 ex.printStackTrace();
226 fail("Error testing wizard: " + ex.getMessage());
227 }
228
229 }
230
231 public void testGetStepHistory() throws Exception {
232 WizardPage page = sequentialWizard.begin();
233 assertTrue(sequentialWizard.getStepHistory() != null && sequentialWizard.getStepHistory().size() == 0);
234
235 WizardPage secondPage = sequentialWizard.next(page, "Test1");
236 assertTrue(sequentialWizard.getStepHistory() != null && sequentialWizard.getStepHistory().size() == 1);
237 assertTrue(sequentialWizard.getStepHistory().get(0) == page);
238
239 WizardPage thirdPage = sequentialWizard.next(secondPage, "Test2");
240 assertTrue(sequentialWizard.getStepHistory() != null && sequentialWizard.getStepHistory().size() == 2);
241 assertTrue(sequentialWizard.getStepHistory().get(1) == secondPage);
242 sequentialWizard.processFinish(thirdPage, "Test3", new HashMap());
243
244 assertTrue(sequentialWizard.getStepHistory() != null && sequentialWizard.getStepHistory().size() == 3);
245 assertTrue(sequentialWizard.getStepHistory().get(2) == thirdPage);
246 }
247
248 public void testSerialization() {
249 try {
250 ByteArrayOutputStream bos = new ByteArrayOutputStream();
251 ObjectOutputStream oos = new ObjectOutputStream(bos);
252 oos.writeObject(sequentialWizard);
253
254 byte[] array = bos.toByteArray();
255
256
257
258 assertTrue("Wizard Size: " + array.length + " our goal is < 4096 bytes", array.length < 4096);
259 System.out.println("Sequential Wizard serialized size" + array.length);
260 ByteArrayInputStream bis = new ByteArrayInputStream(array);
261 ObjectInputStream ois = new ObjectInputStream(bis);
262 SequentialWizard seqwiz = (SequentialWizard) ois.readObject();
263 assertEquals(seqwiz, sequentialWizard);
264 } catch (IOException ex) {
265 ex.printStackTrace();
266 fail("Unable to verify successful serialization");
267 } catch (ClassNotFoundException ex) {
268 ex.printStackTrace();
269 fail("Class Cast Exception: Unable to verify successful serialization");
270 } catch (ClassCastException ex) {
271 ex.printStackTrace();
272 fail("Deserialized the wrong type!");
273 }
274
275 }
276
277
278 }