KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > tests > WebFlowTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4
5 package com.tctest.spring.integrationtests.tests;
6
7 import com.meterware.httpunit.WebConversation;
8 import com.meterware.httpunit.WebResponse;
9 import com.tctest.spring.bean.WebFlowBean;
10 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
11 import com.tctest.spring.integrationtests.framework.Deployment;
12 import com.tctest.spring.integrationtests.framework.ServerTestSetup;
13 import com.tctest.spring.integrationtests.framework.WebApplicationServer;
14
15 import java.util.Collections JavaDoc;
16
17 import junit.framework.Test;
18
19
20 /**
21  * Test Spring WebFlow fail-over
22  *
23  * LKC-1175: Test: Spring WebFlow fail-over
24  * https://jira.terracotta.lan/jira/browse/LKC-1175
25  *
26  * Test startup
27  * Test fail-over
28  * More??
29  *
30  * Spring Webflow flows are stateful (by nature), they are backed up
31  * by the HttpContext so it will be a no brainer to support.
32  * Need to work out config, if it is needed and if so what it should be.
33  *
34  * 1. ContinuationFlowExecutionRepositoryFactory
35  * 2. DefaultFlowExecutionRepositoryFactory
36  * 3. SingleKeyFlowExecutionRepositoryFactory (extends 2)
37  */

38 public class WebFlowTest extends AbstractTwoServerDeploymentTest {
39
40   public WebFlowTest() {
41 // this.disableAllUntil("2006-12-01");
42
}
43
44   protected void tearDown() throws Exception JavaDoc {
45     super.tearDown();
46     stopAllWebServers();
47   }
48   
49   public static Test suite() {
50     return new WebFlowTestSetup();
51   }
52   
53   public void testDefaultFlowExecution() throws Exception JavaDoc {
54     checkWebFlow("webflow2.htm", false);
55   }
56   
57   public void testSingleKeyFlowExecution() throws Exception JavaDoc {
58     checkWebFlow("webflow3.htm", false);
59   }
60   
61   public void testContinuationFlowExecution() throws Exception JavaDoc {
62     checkWebFlow("webflow.htm", true);
63   }
64   
65   
66   private void checkWebFlow(String JavaDoc controller, boolean withStepBack) throws Exception JavaDoc {
67     server1.start();
68     
69     WebConversation webConversation1 = new WebConversation();
70
71     Response response1 = request(server1, webConversation1, null, null, controller);
72     assertTrue("Expecting non-empty flow execution key; "+response1, response1.flowExecutionKey.length()>0);
73     assertEquals("", response1.result);
74     
75     server2.start();
76     
77     Response response2 = request(server2, webConversation1, response1.flowExecutionKey, "valueA", controller);
78     assertTrue("Expecting non-empty flow execution key; "+response2, response2.flowExecutionKey.length()>0);
79     assertEquals("Invalid state; "+response2, WebFlowBean.STATEB, response2.result);
80     assertEquals("valueA", response2.valueA);
81
82     Response response3 = request(server1, webConversation1, response2.flowExecutionKey, "valueB", controller);
83     assertTrue("Expecting non-empty flow execution key; "+response3, response3.flowExecutionKey.length()>0);
84     assertEquals("Invalid state; "+response3, WebFlowBean.STATEC, response3.result);
85     assertEquals("Invalid value; "+response3, "valueB", response3.valueB);
86     
87     server1.stop();
88     server2.stop(); // both servers are down
89

90     server1.start();
91     
92     if(withStepBack) {
93       // step back
94
Response response3a = request(server1, webConversation1, response2.flowExecutionKey, "valueB1", controller);
95       assertTrue("Expecting non-empty flow execution key; "+response3a, response3a.flowExecutionKey.length()>0);
96       assertEquals("Invalid state; "+response3a, WebFlowBean.STATEC, response3a.result);
97       assertEquals("Invalid value; "+response3a, "valueB1", response3a.valueB);
98     }
99     
100     // throw away the step back and continue
101
Response response4 = request(server1, webConversation1, response3.flowExecutionKey, "valueC", controller);
102     assertTrue("Expecting non-empty flow execution key; "+response4, response4.flowExecutionKey.length()>0);
103     assertEquals("Invalid state; "+response4, WebFlowBean.STATED, response4.result);
104     assertEquals("Invalid value; "+response4, "valueC", response4.valueC);
105     
106     server2.start();
107
108     Response response5 = request(server2, webConversation1, response4.flowExecutionKey, "valueD", controller);
109     // flowExecutionKey is empty since flow is completed
110
assertEquals("Invalid state; "+response5, WebFlowBean.COMPLETE, response5.result);
111     assertEquals("Invalid value; "+response5, "valueD", response5.valueD);
112   }
113   
114   private Response request(WebApplicationServer server, WebConversation conversation, String JavaDoc flowExecutionKey, String JavaDoc value, String JavaDoc controllerName) throws Exception JavaDoc {
115     String JavaDoc params = "_eventId=submit";
116     if(value!=null) {
117       params += "&value="+value;
118     }
119     if(flowExecutionKey!=null) {
120       params += "&_flowExecutionKey="+flowExecutionKey.trim();
121     } else {
122       params += "&_flowId=webflow";
123     }
124     
125     WebResponse response = server.ping("/webflow/" + controllerName + "?"+params, conversation);
126     return new Response(response.getText().trim());
127   }
128
129
130 // public void DONTtestHighlow() throws Exception {
131
// if(server1.isStopped()) {
132
// server1.start();
133
// }
134
//
135
// WebConversation webConversation1 = new WebConversation();
136
//
137
// String flowExecutionKey = null;
138
// int lowGuess = 0;
139
// int highGuess = 100;
140
// int n = 0;
141
// while(n<100) {
142
// int guess = (highGuess-lowGuess)/2 + lowGuess;
143
// String response = takeGuess(server1, webConversation1, guess, flowExecutionKey);
144
// String[] params = response.split(";");
145
// assertTrue("Expected at least two parameters: "+response, params.length>1);
146
//
147
// if(HigherLowerGame.CORRECT.equals(params[0])) {
148
// break;
149
// } else if(HigherLowerGame.TOO_HIGH.equals(params[0])) {
150
// highGuess = guess;
151
// } else if(HigherLowerGame.TOO_LOW.equals(params[0])) {
152
// lowGuess = guess;
153
// } else if(HigherLowerGame.INVALID.equals(params[0])) {
154
// fail("Invalid execution "+params[1]);
155
// }
156
//
157
// flowExecutionKey = params[1];
158
// }
159
// assertTrue("Too many iterations", n<100);
160
// }
161

162 // private String takeGuess(WebApplicationServer server, WebConversation conversation, int newGuess, String flowExecutionKey) throws Exception {
163
// String params = "guess="+newGuess;
164
// if(flowExecutionKey!=null) {
165
// params += "&_flowExecutionKey="+flowExecutionKey.trim();
166
// } else {
167
// params += "&_flowId=higherlower";
168
// }
169
// params += "&_eventId=submit";
170
//
171
// WebResponse response = server.ping("/higherlower/higherlower.htm?"+params, conversation);
172
// return response.getText().trim();
173
// }
174

175   
176   private static class WebFlowTestSetup extends ServerTestSetup {
177
178     public WebFlowTestSetup() {
179       super(WebFlowTest.class);
180     }
181
182     protected void setUp() throws Exception JavaDoc {
183       try {
184       super.setUp();
185
186 // Deployment deployment1 = new WARBuilder("higherlower.war")
187
// .addDirectoryOrJARContainingClass(WebFlowTestSetup.class)
188
// .addDirectoryContainingResource("/tc-config-files/webflow-tc-config.xml")
189
//
190
// .addDirectoryOrJARContainingClass(org.apache.taglibs.standard.Version.class) // standard-1.0.6.jar
191
// .addDirectoryOrJARContainingClass(javax.servlet.jsp.jstl.core.Config.class) // jstl-1.0.jar
192
// .addDirectoryOrJARContainingClass(org.springframework.webflow.registry.XmlFlowRegistryFactoryBean.class) // spring-webflow-1.0-rc3.jar
193
// .addDirectoryOrJARContainingClass(org.springframework.binding.convert.Converter.class) // spring-binding-1.0-rc3.jar
194
// .addDirectoryOrJARContainingClass(org.apache.commons.codec.StringDecoder.class) // commons-codec-1.3.jar
195
// .addDirectoryOrJARContainingClass(ognl.Ognl.class) // ognl-2.7.jar
196
//
197
// .addResource("/web-resources", "higherlower.jsp", "/WEB-INF")
198
// .addResource("/com/tctest/spring", "higherlower.xml", "/WEB-INF")
199
// .addResource("/com/tctest/spring", "higherlower-beans.xml", "/WEB-INF")
200
// .addResource("/com/tctest/spring", "higherlower-servlet.xml", "/WEB-INF")
201
//
202
// .addServlet("higherlower", "*.htm", org.springframework.web.servlet.DispatcherServlet.class,
203
// Collections.singletonMap("contextConfigLocation", "/WEB-INF/higherlower-servlet.xml"), true)
204
// .makeDeployment();
205

206       Deployment deployment2 = makeDeploymentBuilder("webflow.war")
207           .addDirectoryOrJARContainingClass(WebFlowTestSetup.class)
208           .addDirectoryContainingResource("/tc-config-files/webflow-tc-config.xml")
209           
210           .addDirectoryOrJARContainingClass(org.apache.taglibs.standard.Version.class) // standard-1.0.6.jar
211
.addDirectoryOrJARContainingClass(javax.servlet.jsp.jstl.core.Config.class) // jstl-1.0.jar
212
// .addDirectoryOrJARContainingClass(org.springframework.webflow.registry.XmlFlowRegistryFactoryBean.class) // spring-webflow-1.0-rc3.jar
213
.addDirectoryOrJARContainingClass(org.springframework.webflow.engine.builder.xml.XmlFlowRegistryFactoryBean.class) // spring-webflow-1.0-rc4.jar
214
.addDirectoryOrJARContainingClass(org.springframework.binding.convert.Converter.class) // spring-binding-1.0-rc3.jar
215
.addDirectoryOrJARContainingClass(org.apache.commons.codec.StringDecoder.class) // commons-codec-1.3.jar
216
.addDirectoryOrJARContainingClass(ognl.Ognl.class) // ognl-2.7.jar
217
.addDirectoryOrJARContainingClass(EDU.oswego.cs.dl.util.concurrent.ReentrantLock.class) // concurrent-1.3.4.jar for SWF on jdk1.4
218

219           .addResource("/web-resources", "webflow.jsp", "WEB-INF")
220           .addResource("/com/tctest/spring", "webflow.xml", "WEB-INF")
221           .addResource("/com/tctest/spring", "webflow-beans.xml", "WEB-INF")
222           .addResource("/com/tctest/spring", "webflow-servlet.xml", "WEB-INF")
223           .addResource("/web-resources", "weblogic.xml", "WEB-INF")
224           
225           .addServlet("webflow", "*.htm", org.springframework.web.servlet.DispatcherServlet.class,
226               Collections.singletonMap("contextConfigLocation", "/WEB-INF/webflow-servlet.xml"), true)
227           .makeDeployment();
228       
229       server1 = createServer()
230 // .addWarDeployment(deployment1, "higherlower")
231
.addWarDeployment(deployment2, "webflow");
232
233       server2 = createServer()
234 // .addWarDeployment(deployment1, "higherlower")
235
.addWarDeployment(deployment2, "webflow");
236       } catch (Exception JavaDoc ex) {
237         ex.printStackTrace();
238       }
239     }
240
241     private WebApplicationServer createServer() throws Exception JavaDoc {
242       return sm.makeWebApplicationServer("/tc-config-files/webflow-tc-config.xml");
243     }
244     
245   }
246   
247   
248   private static class Response {
249     public final String JavaDoc text;
250     public final String JavaDoc flowExecutionKey;
251     public final String JavaDoc result;
252     public final String JavaDoc valueA;
253     public final String JavaDoc valueB;
254     public final String JavaDoc valueC;
255     public final String JavaDoc valueD;
256     
257     public Response(String JavaDoc text) {
258       this.text = text;
259       
260       String JavaDoc[] values = text.split(";");
261       assertTrue("Expected at least two parameters; "+text, values.length>1);
262       
263       this.result = values[0];
264       this.flowExecutionKey = values[1];
265       this.valueA = values.length>2 ? values[2] : null;
266       this.valueB = values.length>3 ? values[3] : null;
267       this.valueC = values.length>4 ? values[4] : null;
268       this.valueD = values.length>5 ? values[5] : null;
269     }
270     
271     public String JavaDoc toString() {
272       return text;
273     }
274     
275   }
276
277 }
278
279
Popular Tags