KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > ConditionalValveServletTest


1 package org.apache.turbine;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import junit.framework.Test;
58 import junit.framework.TestCase;
59 import junit.framework.TestSuite;
60
61 import com.meterware.httpunit.WebTable;
62 import com.meterware.httpunit.WebResponse;
63
64 import org.apache.cactus.ServletTestCase;
65 import org.apache.cactus.WebRequest;
66
67 import java.util.Enumeration JavaDoc;
68
69 import com.iv.flash.util.Util;
70 import com.iv.flash.api.FlashFile;
71 import com.iv.flash.api.FlashDef;
72 import com.iv.flash.api.text.FontDef;
73
74 /**
75  * This is the testing servlet for the Turbine core classes. Subsystems like
76  * Torque and Fulcrum are not tested here.
77  *
78  * This test uses a pipeline contaning the conditional target valve to test
79  * different renderers -- velocity, jsp, and jgenerator -- all in the same
80  * pipeline.
81  *
82  * @author <a HREF="mailto:Jeff.Brekke@qg.com">Jeff Brekke</a>
83  * @author <a HREF="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
84  * @author <a HREF="mailto:james@jamestaylor.org">James Taylor</a>
85  * @version $Id: ConditionalValveServletTest.java,v 1.5 2002/04/10 12:29:43 brekke Exp $
86  */

87 public class ConditionalValveServletTest
88     extends ServletTestCase
89 {
90     /**
91      * Core Turbine servlet used for testing.
92      */

93     Turbine turbine;
94
95     /**
96      * Default Constructor.
97      */

98     public ConditionalValveServletTest(String JavaDoc name)
99     {
100         super(name);
101     }
102
103     /**
104      * Default test suite for this test case.
105      */

106     public static Test suite()
107     {
108         return new TestSuite(ConditionalValveServletTest.class);
109     }
110
111     /**
112      * This setup will be running server side. We startup Turbine and
113      * get our test port from the properties. This gets run before
114      * each testXXX test.
115      */

116     protected void setUp()
117         throws Exception JavaDoc
118     {
119         super.setUp();
120         config.setInitParameter("properties",
121                 "/WEB-INF/conf/ConditionalValveTRP.properties");
122         turbine = new Turbine();
123         turbine.init(config);
124     }
125
126     /**
127      * After each testXXX test runs, shut down the Turbine servlet.
128      */

129     protected void tearDown()
130         throws Exception JavaDoc
131     {
132         turbine.destroy();
133         super.tearDown();
134     }
135
136     /**
137      * This begin runs client side before the testHompage test runs.
138      * We'll set up our simualated url here.
139      */

140     public void beginHomepage(WebRequest theRequest)
141     {
142         theRequest.setURL(null, "/test", "/servlet/test", null, null);
143     }
144
145     /**
146      * Run our actual test. Here we just call the doGet on the
147      * servlet.
148      */

149     public void testHomepage()
150         throws Exception JavaDoc
151     {
152         turbine.doGet(request, response);
153     }
154
155     /**
156      * Check the response from our test using HttpUnit.
157      */

158     public void endHomepage(WebResponse theResponse)
159         throws Exception JavaDoc
160     {
161         // Verify the Title
162
assertEquals("Test Application", theResponse.getTitle());
163
164         // Verify the layout
165
WebTable table = theResponse.getTableWithID("layout");
166         assertNotNull("Table: layout was not found.", table);
167         assertEquals("Incorrect number of rows in layout table.",
168                 3, table.getRowCount());
169         assertEquals("Incorrect number of columns in layout table.",
170                 1, table.getColumnCount());
171
172         assertEquals("Turbine Test App",
173                 table.getTableCellWithID("topNav").asText().trim());
174         assertEquals("Powered By Turbine!",
175                 table.getTableCellWithID("bottomNav").asText().trim());
176         assertEquals("Please stand by, This is only a test.",
177                 table.getTableCellWithID("screen").asText().trim());
178     }
179
180     /**
181      * Setup our request url to attempt to load a bogus action
182      * which will cause Turbine to render the Error.vm.
183      */

184     public void beginErrorTemplate(WebRequest theRequest)
185     {
186         theRequest.setURL(null, "/test", "/servlet/test",
187                 "/action/NoSuchAction", null);
188     }
189
190     /**
191      * Call the doGet on the servlet
192      */

193     public void testErrorTemplate()
194         throws Exception JavaDoc
195     {
196         turbine.doGet(request, response);
197     }
198
199     /**
200      * Check our response with HttpUnit. This will be the Error.vm
201      * template with our exception.
202      */

203     public void endErrorTemplate(WebResponse theResponse)
204         throws Exception JavaDoc
205     {
206         // Verify the Title
207
assertEquals("Test Application", theResponse.getTitle());
208
209         // Verify the error table
210
WebTable table = theResponse.getTableWithID("error");
211         assertNotNull("Table: error was not found.", table);
212         assertEquals("Incorrect number of rows in error table.", 2,
213                 table.getRowCount());
214         assertEquals("Incorrect number of columns in error table.", 1,
215                 table.getColumnCount());
216
217         assertEquals("Error", table.getCellAsText(0, 0).trim());
218         assertTrue("No exception or stack trace?",
219                 table.getCellAsText(1, 0).trim().length() > 0);
220     }
221     
222     /**
223      * This begin runs client side before the testHompage test runs.
224      * We'll set up our simualated url here.
225      */

226     public void beginJspPage(WebRequest theRequest)
227     {
228         theRequest.setURL(null, "/test", "/servlet/test",
229                           "/template/Index.jsp", null);
230     }
231
232     /**
233      * Run our actual test. Here we just call the doGet on the
234      * servlet.
235      */

236     public void testJspPage()
237         throws Exception JavaDoc
238     {
239         turbine.doGet(request, response);
240     }
241
242     /**
243      * Check the response from our test using HttpUnit.
244      */

245     public void endJspPage(WebResponse theResponse)
246         throws Exception JavaDoc
247     {
248         assertEquals("Test Application JSP Page", theResponse.getTitle());
249     }
250     
251     /**
252      * This begin runs client side before the testHompage test runs.
253      * We'll set up our simualated url here.
254      */

255     public void beginJGenPage(WebRequest theRequest)
256     {
257         theRequest.setURL(null, "/test", "/servlet/test",
258                           "/template/JGenTestScreen.swt", null);
259     }
260
261     /**
262      * Run our actual test. Here we just call the doGet on the
263      * servlet.
264      */

265     public void testJGenPage()
266         throws Exception JavaDoc
267     {
268         turbine.doGet(request, response);
269     }
270
271     /**
272      * Check the response from our test using HttpUnit.
273      */

274     public void endJGenPage(WebResponse response)
275         throws Exception JavaDoc
276     {
277         // FIXME: Eventually we should be able to use some limited pieces of
278
// jgen (like parsing a file) without needing configuration, but until
279
// then is there a better way to get this path? Using a filter perhaps?
280

281         String JavaDoc baseDir = System.getProperty("basedir");
282         assertNotNull("Could not get the 'basedir' system property.", baseDir);
283         Util.init( baseDir+"/target/rttest/tomcat40/webapps/test/WEB-INF/conf/" );
284         
285         // If it parses it must be a flash file
286

287         FlashFile f = FlashFile.parse( "unknown", response.getInputStream() );
288         
289         // Now we iterate over the FlashDefs and look for a font, which will
290
// only exist if text was added by the Module (otherwise JGen strips
291
// it from the file since it is not used anymore).
292

293         Enumeration JavaDoc defs = f.definitions();
294        
295         boolean fontFound = false;
296        
297         while( defs.hasMoreElements() )
298         {
299             if ( defs.nextElement() instanceof FontDef )
300             {
301                 fontFound = true;
302             }
303         }
304         
305         if ( ! fontFound )
306         {
307             fail( "No font def found in returned flash content." );
308         }
309     }
310 }
311
Popular Tags