1 package com.sri.emo.controller;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import com.jcorporate.expresso.core.controller.ControllerException;
6 import com.jcorporate.expresso.core.controller.ErrorCollection;
7 import com.jcorporate.expresso.core.controller.ExpressoResponse;
8 import com.jcorporate.expresso.core.controller.Input;
9 import com.jcorporate.expresso.core.controller.NonHandleableException;
10 import com.jcorporate.expresso.core.controller.Transition;
11 import com.jcorporate.expresso.core.db.DBException;
12 import com.jcorporate.expresso.core.dbobj.ValidValue;
13 import com.jcorporate.expresso.services.test.ControllerTestFixture;
14 import com.jcorporate.expresso.services.test.TestSystemInitializer;
15 import com.sri.emo.test.DatabaseTestFixture;
16 import com.sri.emo.test.EmoTestSuite;
17 import junit.framework.TestCase;
18
19 /***
20 * Test case for import/export
21 *
22 * @author Michael Rimov
23 * @todo Test the actual importing and exporting.
24 */
25 public class TestImportExportModel extends TestCase {
26 private ControllerTestFixture testFixture = null;
27 private DatabaseTestFixture dbFixture = null;
28
29 protected void setUp() throws Exception {
30 super.setUp();
31 testFixture = new ControllerTestFixture();
32 dbFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
33 EmoTestSuite.class.getResourceAsStream("WizardTestData.xml"));
34
35 }
36
37 /***
38 * Checks that there are no errors defined in the error collection.
39 *
40 * @param response The ExpressoResponse object.
41 * @throws ControllerException
42 */
43 protected static void checkNoErrorsInErrorCollection(ExpressoResponse
44 response) throws
45 ControllerException {
46 assertTrue(response.getErrors() == null
47 || response.getErrors().getErrorCount() == 0);
48 }
49
50 protected void tearDown() throws Exception {
51 dbFixture.tearDown();
52 dbFixture = null;
53 super.tearDown();
54 }
55
56 public void testRunExportModelState() throws ControllerException
57 , NonHandleableException, DBException {
58 ExpressoResponse response = testFixture.invokeFacade(ImportExportModel.class, new HashMap()
59 , ImportExportModel.STATE_PROMPT_EXPORT);
60
61 assertTrue(response != null);
62 checkNoErrorsInErrorCollection(response);
63
64 }
65
66 public void testRunImportModelState() throws ControllerException
67 , NonHandleableException, DBException {
68
69 }
70
71 public void testRunPromptExportModelState() throws ControllerException
72 , NonHandleableException, DBException {
73 ExpressoResponse response = testFixture.invokeFacade(ImportExportModel.class,
74 new HashMap()
75 , ImportExportModel.STATE_PROMPT_EXPORT);
76
77 assertTrue(response != null);
78 checkNoErrorsInErrorCollection(response);
79 Input i = response.getInput("model");
80 assertTrue(i != null);
81 List vv = i.getValidValuesList();
82 assertTrue(vv != null);
83 assertTrue(vv.size() > 0);
84 assertEquals(((ValidValue) vv.get(0)).getKey(), "");
85
86 i = response.getInput("exportNodes");
87 assertEquals("Y", i.getAttribute(Input.ATTRIBUTE_CHECKBOX));
88 assertEquals("", i.getDefaultValue());
89 assertTrue(i != null);
90
91 Transition t = response.getTransition("export");
92 assertTrue(t != null);
93 assertEquals(ImportExportModel.STATE_EXPORTMODEL, t.getState());
94 assertEquals(ImportExportModel.class.getName(), t.getControllerObject());
95
96 }
97
98 public void testRunPromptImportModelState() throws ControllerException
99 , NonHandleableException, DBException {
100 ExpressoResponse response = testFixture.invokeFacade(ImportExportModel.class,
101 new HashMap()
102 , ImportExportModel.STATE_PROMPT_IMPORTMODEL);
103
104 assertTrue(response != null);
105 checkNoErrorsInErrorCollection(response);
106 Input i = response.getInput("data");
107 assertTrue(i != null);
108
109 i = response.getInput("importNode");
110 assertTrue(i != null);
111
112 i = response.getInput("file");
113 assertTrue(i != null);
114
115 Transition t = response.getTransition("import");
116 assertTrue(t != null);
117 assertEquals(ImportExportModel.STATE_IMPORTMODEL, t.getState());
118 assertEquals(ImportExportModel.class.getName(), t.getControllerObject());
119 }
120
121 /***
122 * Test with lacking inputs.
123 *
124 * @throws ControllerException
125 * @throws NonHandleableException
126 * @throws DBException
127 */
128 public void testRunImportModelWithBadInput() throws ControllerException
129 , NonHandleableException, DBException {
130 ExpressoResponse response = testFixture.invokeFacade(ImportExportModel.class,
131 new HashMap()
132 , ImportExportModel.STATE_IMPORTMODEL);
133
134 assertTrue(response != null);
135 ErrorCollection ec = response.getErrors();
136 assertTrue(ec != null);
137 assertTrue(ec.getErrorCount() > 0);
138 }
139
140 public void testRunPromptState() throws ControllerException
141 , NonHandleableException, DBException {
142 ExpressoResponse response = testFixture.invokeFacade(ImportExportModel.class,
143 new HashMap(), null);
144
145 assertTrue(response != null);
146 checkNoErrorsInErrorCollection(response);
147
148 Transition submit = response.getTransition("submit");
149 assertTrue(submit != null);
150 assertEquals(ImportExportModel.class.getName()
151 , submit.getControllerObject());
152
153 Input state = response.getInput("fauxstate");
154 assertTrue(state != null);
155 assertTrue(state.getValidValuesList() != null);
156
157 assertTrue(state.getValidValuesList().size() > 0);
158 }
159
160 }