KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > unit > TestGlobalBeginEnd


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.sample.servlet.unit;
21
22 import org.apache.cactus.ServletTestCase;
23 import org.apache.cactus.WebRequest;
24 import org.apache.cactus.WebResponse;
25
26 /**
27  * Test global client side <code>begin()</code> and <code>end()</code>
28  * methods.
29  *
30  * @version $Id: TestGlobalBeginEnd.java,v 1.3 2004/02/29 16:36:44 vmassol Exp $
31  */

32 public class TestGlobalBeginEnd extends ServletTestCase
33 {
34     /**
35      * true if <code>end()</code> has been called.
36      */

37     private boolean isClientGlobalEndCalled;
38
39     /**
40      * Verifies that <code>end()</code> has been called correctly.
41      *
42      * @exception Throwable on test failure
43      */

44     protected void runTest() throws Throwable JavaDoc
45     {
46         super.runTest();
47
48         // Make sure we verify if end() has been called only on
49
// the client side. Reason is that the runTest() method is
50
// called both on the client side and on the server side.
51
if (this.request == null)
52         {
53             if (!this.isClientGlobalEndCalled)
54             {
55                 fail("end() has not been called");
56             }
57         }
58     }
59
60     /**
61      * Verify that it is possible to modify the <code>WebRequest</code> in
62      * the common <code>begin()</code> method. It also verifies that
63      * <code>begin()</code> is called at all.
64      *
65      * @param theRequest the request object that serves to initialize the
66      * HTTP connection to the server redirector.
67      */

68     public void begin(WebRequest theRequest)
69     {
70         theRequest.addParameter("param1", "value1");
71     }
72
73     /**
74      * Verify that it is possible to read the connection object once in
75      * endXXX() and then again in <code>end()</code>. It also
76      * verifies that <code>end()</code> is called at all.
77      *
78      * @param theResponse the response from the server side.
79      */

80     public void end(WebResponse theResponse)
81     {
82         assertEquals("Hello there!", theResponse.getText());
83         this.isClientGlobalEndCalled = true;
84     }
85
86     //-------------------------------------------------------------------------
87

88     /**
89      * Verify that it is possible to modify the <code>WebRequest</code> in
90      * the common <code>begin()()</code> method. It also verifies that
91      * <code>begin()</code> is called at all.
92      *
93      * @param theRequest the request object that serves to initialize the
94      * HTTP connection to the server redirector.
95      */

96     public void beginGlobalBeginEnd(WebRequest theRequest)
97     {
98         assertEquals("value1", theRequest.getParameterGet("param1"));
99     }
100
101     /**
102      * Verify that it is possible to modify the <code>WebRequest</code> in
103      * the common <code>begin()()</code> method. It also verifies that
104      * <code>begin()()</code> is called at all.
105      *
106      * @exception Exception on test failure
107      */

108     public void testGlobalBeginEnd() throws Exception JavaDoc
109     {
110         assertEquals("value1", request.getParameter("param1"));
111         response.getWriter().print("Hello there!");
112     }
113
114     /**
115      * Verify that it is possible to read the connection object once in
116      * endXXX() and then again in <code>end()</code>. It also
117      * verifies that <code>end()</code> is called at all.
118      *
119      * @param theResponse the response from the server side.
120      */

121     public void endGlobalBeginEnd(WebResponse theResponse)
122     {
123         assertEquals("Hello there!", theResponse.getText());
124     }
125
126 }
127
Popular Tags