1 package com.sri.emo.wizard.selection.management;
2
3 import java.util.Vector;
4 import com.jcorporate.expresso.core.controller.ExpressoRequest;
5 import com.jcorporate.expresso.core.controller.ExpressoResponse;
6 import com.jcorporate.expresso.core.controller.Input;
7 import com.sri.emo.test.DatabaseTestFixture;
8 import com.sri.emo.test.StateHandlerTestFixture;
9 import junit.framework.TestCase;
10
11 /***
12 * Tests the prompt step type.
13 *
14 * @author Michael Rimov
15 */
16 public class TestPromptStepType extends TestCase {
17
18 private PromptStepType promptStepType = null;
19 private StateHandlerTestFixture fixture = new StateHandlerTestFixture();
20
21 protected void setUp() throws Exception {
22 super.setUp();
23 fixture = new StateHandlerTestFixture();
24 fixture.setUp();
25 }
26
27 protected void tearDown() throws Exception {
28 promptStepType = null;
29 fixture.tearDown();
30 fixture = null;
31 super.tearDown();
32 }
33
34 public void testIsEditing() throws Exception {
35 promptStepType = new PromptStepType(Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID)
36 , null);
37 ExpressoRequest request = fixture.getControllerRequest(DatabaseTestFixture.DEFAULT_WELCOME_STEP,
38 PromptStepType.STATE_NAME);
39 ExpressoResponse response = fixture.getControllerResponse(request);
40
41 promptStepType.handleRequest(request, response);
42 assertFalse(promptStepType.isEditing());
43
44 promptStepType = new PromptStepType(Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID)
45 , Integer.toString(DatabaseTestFixture.DEFAULT_WELCOME_STEP));
46
47 request = fixture.getControllerRequest(DatabaseTestFixture.DEFAULT_WIZ_ID,
48 DatabaseTestFixture.DEFAULT_WELCOME_STEP, PromptStepType.STATE_NAME);
49 response = fixture.getControllerResponse(request);
50 promptStepType.handleRequest(request, response);
51 assertTrue(promptStepType.isEditing());
52 }
53
54 public void testHandleNewRequest() throws Exception {
55 promptStepType = new PromptStepType(Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID)
56 , null);
57
58 ExpressoRequest request = fixture.getControllerRequest(DatabaseTestFixture.DEFAULT_WIZ_ID,
59 PromptStepType.STATE_NAME);
60 ExpressoResponse response = fixture.getControllerResponse(request);
61
62
63 promptStepType.handleRequest(request, response);
64 assertFalse(promptStepType.isEditing());
65
66
67 Vector inputs = response.getInputs();
68 assertTrue(inputs != null);
69 assertTrue(inputs.size() == promptStepType.menuitems.length);
70 boolean foundOneInput = false;
71
72 for (int i = 0; i < inputs.size(); i++) {
73 Input input = (Input) inputs.get(i);
74 assertTrue(input != null);
75 assertTrue(input.getAttribute("Explanation") != null);
76
77 if ("Y".equals(input.getDefaultValue())) {
78 if (foundOneInput) {
79 fail("Found Duplicate Inputs: i=" + i);
80 } else {
81 foundOneInput = true;
82 }
83 }
84 }
85
86 assertTrue("Should have one and only one input marked with default value", foundOneInput);
87 }
88
89 public void testHandleEditRequestWithInstructions() throws Exception {
90 promptStepType = new PromptStepType(Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID)
91 , Integer.toString(DatabaseTestFixture.DEFAULT_WELCOME_STEP));
92
93 ExpressoRequest request = fixture.getControllerRequest(DatabaseTestFixture.DEFAULT_WIZ_ID,
94 DatabaseTestFixture.DEFAULT_WELCOME_STEP, PromptStepType.STATE_NAME);
95 ExpressoResponse response = fixture.getControllerResponse(request);
96 promptStepType.handleRequest(request, response);
97 assertTrue(promptStepType.isEditing());
98
99 Vector inputs = response.getInputs();
100 assertTrue(inputs != null);
101 assertTrue(inputs.size() == promptStepType.menuitems.length);
102 boolean foundOneInput = false;
103
104
105 for (int i = 0; i < inputs.size(); i++) {
106 Input input = (Input) inputs.get(i);
107 assertTrue(input != null);
108 assertTrue(input.getAttribute("Explanation") != null);
109
110 if ("Y".equals(input.getDefaultValue())) {
111 if (foundOneInput) {
112 fail("Found Duplicate Inputs: i=" + i);
113 } else {
114 foundOneInput = true;
115 assertTrue("Should be input type: 'Instructions Only'",
116 "5".equals(input.getAttribute("StepTypeId")));
117 }
118 }
119 }
120
121 assertTrue("Should have one and only one input marked with default value", foundOneInput);
122
123 }
124
125 public void testHandleEditRequestWithPerModelOptions() throws Exception {
126 promptStepType = new PromptStepType(Integer.toString(DatabaseTestFixture.DEFAULT_WIZ_ID)
127 , "22");
128
129 ExpressoRequest request = fixture.getControllerRequest(DatabaseTestFixture.DEFAULT_WIZ_ID, 22,
130 PromptStepType.STATE_NAME);
131 ExpressoResponse response = fixture.getControllerResponse(request);
132 promptStepType.handleRequest(request, response);
133 assertTrue(promptStepType.isEditing());
134
135 Vector inputs = response.getInputs();
136 assertTrue(inputs != null);
137 assertTrue(inputs.size() == promptStepType.menuitems.length);
138 boolean foundOneInput = false;
139
140
141 for (int i = 0; i < inputs.size(); i++) {
142 Input input = (Input) inputs.get(i);
143 assertTrue(input != null);
144 assertTrue(input.getAttribute("Explanation") != null);
145
146 if ("Y".equals(input.getDefaultValue())) {
147 if (foundOneInput) {
148 fail("Found Duplicate Inputs: i=" + i);
149 } else {
150 foundOneInput = true;
151 assertTrue("Should be input type: 'Instructions Only'",
152 "1".equals(input.getAttribute("StepTypeId")));
153 }
154 }
155 }
156
157 assertTrue("Should have one and only one input marked with default value", foundOneInput);
158
159 }
160
161 }