KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 /**
26  * Test the {@link WebRequest#setURL} method.
27  *
28  * @version $Id: TestSetURL.java,v 1.6 2004/02/29 16:36:44 vmassol Exp $
29  */

30 public class TestSetURL extends ServletTestCase
31 {
32     /**
33      * Verify that we can simulate the basic parts of the URL : server name,
34      * default server port of 80, root servlet context, URI.
35      *
36      * @param theRequest the request object that serves to initialize the
37      * HTTP connection to the server redirector.
38      */

39     public void beginSimulatedURLBasics(WebRequest theRequest)
40     {
41         theRequest.setURL("jakarta.apache.org", "", "/test/test.jsp", null,
42             null);
43     }
44
45     /**
46      * Verify that we can simulate the basic parts of the URL : server name,
47      * default server port of 80, no servlet context, servlet path.
48      */

49     public void testSimulatedURLBasics()
50     {
51         assertEquals("/test/test.jsp", request.getRequestURI());
52         assertEquals("jakarta.apache.org", request.getServerName());
53         assertEquals(80, request.getServerPort());
54         assertEquals("", request.getContextPath());
55     }
56
57     //-------------------------------------------------------------------------
58

59     /**
60      * Verify that we can simulate different parts of the URL.
61      *
62      * @param theRequest the request object that serves to initialize the
63      * HTTP connection to the server redirector.
64      */

65     public void beginSimulatedURL1(WebRequest theRequest)
66     {
67         theRequest.setURL("jakarta.apache.org", "/mywebapp", "/test/test.jsp",
68             null, null);
69     }
70
71     /**
72      * Verify that we can simulate different parts of the URL.
73      */

74     public void testSimulatedURL1()
75     {
76         assertEquals("/mywebapp/test/test.jsp", request.getRequestURI());
77         assertEquals("jakarta.apache.org", request.getServerName());
78         assertEquals(80, request.getServerPort());
79         assertEquals("/mywebapp", request.getContextPath());
80     }
81
82     //-------------------------------------------------------------------------
83

84     /**
85      * Verify that we can simulate different parts of the URL.
86      *
87      * @param theRequest the request object that serves to initialize the
88      * HTTP connection to the server redirector.
89      */

90     public void beginSimulatedURL2(WebRequest theRequest)
91     {
92         theRequest.setURL("jakarta.apache.org", "/catalog", "/lawn",
93             "/index.html", null);
94     }
95
96     /**
97      * Verify that we can simulate different parts of the URL.
98      */

99     public void testSimulatedURL2()
100     {
101         assertEquals("jakarta.apache.org", request.getServerName());
102         assertEquals("/catalog/lawn/index.html", request.getRequestURI());
103         assertEquals(80, request.getServerPort());
104         assertEquals("/catalog", request.getContextPath());
105         assertEquals("/lawn", request.getServletPath());
106         assertEquals("/index.html", request.getPathInfo());
107     }
108
109     //-------------------------------------------------------------------------
110

111     /**
112      * Verify that we can simulate different parts of the URL.
113      *
114      * @param theRequest the request object that serves to initialize the
115      * HTTP connection to the server redirector.
116      */

117     public void beginSimulatedURL3(WebRequest theRequest)
118     {
119         theRequest.setURL("jakarta.apache.org", "/catalog", "/garden",
120             "/implements/", null);
121     }
122
123     /**
124      * Verify that we can simulate different parts of the URL.
125      */

126     public void testSimulatedURL3()
127     {
128         assertEquals("jakarta.apache.org", request.getServerName());
129         assertEquals("/catalog/garden/implements/", request.getRequestURI());
130         assertEquals(80, request.getServerPort());
131         assertEquals("/catalog", request.getContextPath());
132         assertEquals("/garden", request.getServletPath());
133         assertEquals("/implements/", request.getPathInfo());
134     }
135
136     //-------------------------------------------------------------------------
137

138     /**
139      * Verify that we can simulate different parts of the URL.
140      *
141      * @param theRequest the request object that serves to initialize the
142      * HTTP connection to the server redirector.
143      */

144     public void beginSimulatedURL4(WebRequest theRequest)
145     {
146         theRequest.setURL("jakarta.apache.org", "/catalog",
147             "/help/feedback.jsp", null, null);
148     }
149
150     /**
151      * Verify that we can simulate different parts of the URL.
152      */

153     public void testSimulatedURL4()
154     {
155         assertEquals("jakarta.apache.org", request.getServerName());
156         assertEquals("/catalog/help/feedback.jsp", request.getRequestURI());
157         assertEquals(80, request.getServerPort());
158         assertEquals("/catalog", request.getContextPath());
159         assertEquals("/help/feedback.jsp", request.getServletPath());
160     }
161
162     //-------------------------------------------------------------------------
163

164     /**
165      * Verify that we can simulate different parts of the URL. Also verify
166      * that HTTP parameters put in the simulation URL will be
167      * available on the server side as real HTTP parameters.
168      *
169      * @param theRequest the request object that serves to initialize the
170      * HTTP connection to the server redirector.
171      */

172     public void beginSimulatedURL5(WebRequest theRequest)
173     {
174         theRequest.setURL("jakarta.apache.org", "/catalog",
175             "/help/feedback.jsp", null, "PARAM1=param1&PARAM2=&PARAM3=param3");
176     }
177
178     /**
179      * Verify that we can simulate different parts of the URL. Also verify
180      * that HTTP parameters put in the simulation URL will be
181      * available on the server side as real HTTP parameters.
182      */

183     public void testSimulatedURL5()
184     {
185         assertEquals("jakarta.apache.org", request.getServerName());
186         assertEquals("/catalog/help/feedback.jsp", request.getRequestURI());
187         assertEquals(80, request.getServerPort());
188         assertEquals("/catalog", request.getContextPath());
189         assertEquals("/help/feedback.jsp", request.getServletPath());
190         assertEquals("PARAM1=param1&PARAM2=&PARAM3=param3",
191             request.getQueryString());
192         assertEquals(request.getParameter("PARAM1"), "param1");
193         assertEquals(request.getParameter("PARAM2"), "");
194         assertEquals(request.getParameter("PARAM3"), "param3");
195     }
196
197     //-------------------------------------------------------------------------
198

199     /**
200      * Verify values used by the framework when all values defined in
201      * <code>setURL()</code> are null.
202      *
203      * @param theRequest the request object that serves to initialize the
204      * HTTP connection to the server redirector.
205      */

206     public void beginSimulatedURLNullValues(WebRequest theRequest)
207     {
208         theRequest.setURL(null, null, null, null, null);
209     }
210
211     /**
212      * Verify values used by the framework when all values defined in
213      * <code>setURL()</code> are null.
214      */

215     public void testSimulatedURLNullValues()
216     {
217         assertNotNull(request.getServerName());
218         assertTrue(request.getServerPort() > 0);
219         assertNotNull(request.getContextPath());
220         assertNotNull(request.getServletPath());
221         assertNull(request.getPathInfo());
222         assertNull(request.getQueryString());
223     }
224 }
225
Popular Tags