KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > TestServletURL


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;
21
22 import junit.framework.TestCase;
23
24 /**
25  * Unit tests of the <code>ServletURL</code> class.
26  *
27  * @version $Id: TestServletURL.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
28  */

29 public class TestServletURL extends TestCase
30 {
31     /**
32      * Verify that if the context path is not empty or null it must start with
33      * a "/" character.
34      */

35     public void testSetContextPathFirstCharacterNotForwardSlash()
36     {
37         try
38         {
39             new ServletURL(null, "invalidcontextpath", null, null, null);
40             fail("The context path must start with a \"/\" character");
41         }
42         catch (IllegalArgumentException JavaDoc expected)
43         {
44             assertEquals("The Context Path must start with a \"/\" character.",
45                 expected.getMessage());
46         }
47     }
48
49     /**
50      * Verify that if the context path is not empty or null it must not end
51      * with the "/" character.
52      */

53     public void testSetContextPathLastCharacterNotForwardSlash()
54     {
55         try
56         {
57             new ServletURL(null, "/invalidcontextpath/", null, null, null);
58             fail("The context path must not end with a \"/\" character");
59         }
60         catch (IllegalArgumentException JavaDoc expected)
61         {
62             assertEquals("The Context Path must not end with a \"/\""
63                 + " character.", expected.getMessage());
64         }
65     }
66
67     /**
68      * Verify that the context path can be an empty string.
69      */

70     public void testSetContextPathEmptyString()
71     {
72         ServletURL servletURL = new ServletURL(null, "", null, null, null);
73         assertEquals("", servletURL.getContextPath());
74     }
75
76     /**
77      * Verify that if the servlet path is not empty or null it must start with
78      * a "/" character.
79      */

80     public void testSetServletPathFirstCharacterNotForwardSlash()
81     {
82         try
83         {
84             new ServletURL(null, null, "invalidservletpath", null, null);
85             fail("The servlet path must start with a \"/\" character");
86         }
87         catch (IllegalArgumentException JavaDoc expected)
88         {
89             assertEquals("The Servlet Path must start with a \"/\" character.",
90                 expected.getMessage());
91         }
92     }
93
94     /**
95      * Verify that the servlet path can be an empty string.
96      */

97     public void testSetServletPathEmptyString()
98     {
99         ServletURL servletURL = new ServletURL(null, null, "", null, null);
100         assertEquals("", servletURL.getServletPath());
101     }
102
103     /**
104      * Verify that if the path info is not null it must start with
105      * a "/" character.
106      */

107     public void testSetPathInfoFirstCharacterNotForwardSlash()
108     {
109         try
110         {
111             new ServletURL(null, null, null, "invalidpathinfo", null);
112             fail("The path info must start with a \"/\" character");
113         }
114         catch (IllegalArgumentException JavaDoc expected)
115         {
116             assertEquals("The Path Info must start with a \"/\" character.",
117                 expected.getMessage());
118         }
119     }
120
121     /**
122      * Verify that the path info cannot be an empty string.
123      */

124     public void testSetPathInfoEmptyNotAllowed()
125     {
126         try
127         {
128             new ServletURL(null, null, null, "", null);
129             fail("The path info must not be an empty string");
130         }
131         catch (IllegalArgumentException JavaDoc expected)
132         {
133             assertEquals("The Path Info must not be an empty string. Use "
134                 + "null if you don't want to have a path info.",
135                 expected.getMessage());
136         }
137     }
138
139     /**
140      * Verify that the <code>getHost()</code> method is returning the correct
141      * host when a port is specified and that the <code>getPort()</code>
142      * method returns the specified port.
143      */

144     public void testGetHostWithPort()
145     {
146         ServletURL servletURL = new ServletURL("jakarta.apache.org:8080",
147             null, null, null, null);
148
149         assertEquals("jakarta.apache.org", servletURL.getHost());
150         assertEquals(8080, servletURL.getPort());
151     }
152
153     /**
154      * Verify that <code>getPort()</code> returns -1 when the port is invalid.
155      */

156     public void testGetPortInvalidPortNumber()
157     {
158         ServletURL servletURL = new ServletURL();
159
160         servletURL.setServerName("jakarta.apache.org:invalidPort80");
161
162         int port = servletURL.getPort();
163
164         assertEquals(-1, port);
165     }
166
167     /**
168      * Verify that an invalid protocol raises an exception.
169      */

170     public void testSetProtocolInvalidProtocol()
171     {
172         ServletURL servletURL = new ServletURL();
173
174         try
175         {
176             servletURL.setProtocol("invalid protocol");
177             fail("Should have raised an invalid protocol error");
178         }
179         catch (RuntimeException JavaDoc e)
180         {
181             assertEquals("Invalid protocol [invalid protocol]. Currently "
182                 + "supported protocols are [http] and [https].",
183                 e.getMessage());
184         }
185     }
186
187     /**
188      * Verify that a valid protocol works.
189      */

190     public void testSetProtocolOk()
191     {
192         ServletURL servletURL = new ServletURL();
193
194         servletURL.setProtocol(ServletURL.PROTOCOL_HTTP);
195         assertEquals(ServletURL.PROTOCOL_HTTP, servletURL.getProtocol());
196
197         servletURL.setProtocol(ServletURL.PROTOCOL_HTTPS);
198         assertEquals(ServletURL.PROTOCOL_HTTPS, servletURL.getProtocol());
199     }
200
201     /**
202      * Verify <code>getPath()</code> is ok when all parts are filled.
203      */

204     public void testGetPath()
205     {
206         ServletURL servletURL = new ServletURL();
207
208         servletURL.setQueryString("param1=value1");
209         servletURL.setContextPath("/context");
210         servletURL.setServletPath("/servletPath");
211         servletURL.setPathInfo("/pathInfo");
212
213         String JavaDoc path = servletURL.getPath();
214
215         assertEquals("/context/servletPath/pathInfo", path);
216     }
217
218     /**
219      * Verify <code>getPath()</code> returns null when no parts are filled.
220      */

221     public void testGetPathNull()
222     {
223         ServletURL servletURL = new ServletURL();
224
225         String JavaDoc path = servletURL.getPath();
226
227         assertNull(path);
228     }
229 }
230
Popular Tags