KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
68  * This is the testing servlet for the Turbine core
69  * classes. Subsystems like Torque and Fulcrum are not
70  * tested here.
71  *
72  * @author <a HREF="mailto:jeff.Brekke@qg.com">Jeff Brekke</a>
73  * @author <a HREF="mailto:jvanzyl@zenplex.com">Jason van Zyl</a>
74  * @version $Id: TurbineServletTest.java,v 1.5 2002/01/23 04:35:59 brekke Exp $
75  */

76 public class TurbineServletTest
77     extends ServletTestCase
78 {
79     /**
80      * Core Turbine servlet used for testing.
81      */

82     Turbine turbine;
83
84     /**
85      * Default Constructor.
86      */

87     public TurbineServletTest(String JavaDoc name)
88     {
89         super(name);
90     }
91
92     /**
93      * Default test suite for this test case.
94      */

95     public static Test suite()
96     {
97         return new TestSuite(TurbineServletTest.class);
98     }
99
100     /**
101      * This setup will be running server side. We startup Turbine and
102      * get our test port from the properties. This gets run before
103      * each testXXX test.
104      */

105     protected void setUp()
106         throws Exception JavaDoc
107     {
108         super.setUp();
109         config.setInitParameter("properties",
110                 "/WEB-INF/conf/TurbineResources.properties");
111         turbine = new Turbine();
112         turbine.init(config);
113     }
114
115     /**
116      * After each testXXX test runs, shut down the Turbine servlet.
117      */

118     protected void tearDown()
119         throws Exception JavaDoc
120     {
121         turbine.destroy();
122         super.tearDown();
123     }
124
125     /**
126      * This begin runs client side before the testHompage test runs.
127      * We'll set up our simualated url here.
128      */

129     public void beginHomepage(WebRequest theRequest)
130     {
131         theRequest.setURL(null, "/test", "/servlet/test", null, null);
132     }
133
134     /**
135      * Run our actual test. Here we just call the doGet on the
136      * servlet.
137      */

138     public void testHomepage()
139         throws Exception JavaDoc
140     {
141         turbine.doGet(request, response);
142     }
143
144     /**
145      * Check the response from our test using HttpUnit.
146      */

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

173     public void beginErrorTemplate(WebRequest theRequest)
174     {
175         theRequest.setURL(null, "/test", "/servlet/test",
176                 "/action/NoSuchAction", null);
177     }
178
179     /**
180      * Call the doGet on the servlet
181      */

182     public void testErrorTemplate()
183         throws Exception JavaDoc
184     {
185         turbine.doGet(request, response);
186     }
187
188     /**
189      * Check our response with HttpUnit. This will be the Error.vm
190      * template with our exception.
191      */

192     public void endErrorTemplate(WebResponse theResponse)
193         throws Exception JavaDoc
194     {
195         // Verify the Title
196
assertEquals("Test Application", theResponse.getTitle());
197
198         // Verify the error table
199
WebTable table = theResponse.getTableWithID("error");
200         assertNotNull("Table: error was not found.", table);
201         assertEquals("Incorrect number of rows in error table.", 2,
202                 table.getRowCount());
203         assertEquals("Incorrect number of columns in error table.", 1,
204                 table.getColumnCount());
205
206         assertEquals("Error", table.getCellAsText(0, 0).trim());
207         assertTrue("No exception or stack trace?",
208                 table.getCellAsText(1, 0).trim().length() > 0);
209     }
210 }
211
Popular Tags