001: /*
002: * ========================================================================
003: *
004: * Copyright 2001-2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * Unit tests of the <code>ServletURL</code> class.
026: *
027: * @version $Id: TestServletURL.java 238991 2004-05-22 11:34:50Z vmassol $
028: */
029: public class TestServletURL extends TestCase {
030: /**
031: * Verify that if the context path is not empty or null it must start with
032: * a "/" character.
033: */
034: public void testSetContextPathFirstCharacterNotForwardSlash() {
035: try {
036: new ServletURL(null, "invalidcontextpath", null, null, null);
037: fail("The context path must start with a \"/\" character");
038: } catch (IllegalArgumentException expected) {
039: assertEquals(
040: "The Context Path must start with a \"/\" character.",
041: expected.getMessage());
042: }
043: }
044:
045: /**
046: * Verify that if the context path is not empty or null it must not end
047: * with the "/" character.
048: */
049: public void testSetContextPathLastCharacterNotForwardSlash() {
050: try {
051: new ServletURL(null, "/invalidcontextpath/", null, null,
052: null);
053: fail("The context path must not end with a \"/\" character");
054: } catch (IllegalArgumentException expected) {
055: assertEquals("The Context Path must not end with a \"/\""
056: + " character.", expected.getMessage());
057: }
058: }
059:
060: /**
061: * Verify that the context path can be an empty string.
062: */
063: public void testSetContextPathEmptyString() {
064: ServletURL servletURL = new ServletURL(null, "", null, null,
065: null);
066: assertEquals("", servletURL.getContextPath());
067: }
068:
069: /**
070: * Verify that if the servlet path is not empty or null it must start with
071: * a "/" character.
072: */
073: public void testSetServletPathFirstCharacterNotForwardSlash() {
074: try {
075: new ServletURL(null, null, "invalidservletpath", null, null);
076: fail("The servlet path must start with a \"/\" character");
077: } catch (IllegalArgumentException expected) {
078: assertEquals(
079: "The Servlet Path must start with a \"/\" character.",
080: expected.getMessage());
081: }
082: }
083:
084: /**
085: * Verify that the servlet path can be an empty string.
086: */
087: public void testSetServletPathEmptyString() {
088: ServletURL servletURL = new ServletURL(null, null, "", null,
089: null);
090: assertEquals("", servletURL.getServletPath());
091: }
092:
093: /**
094: * Verify that if the path info is not null it must start with
095: * a "/" character.
096: */
097: public void testSetPathInfoFirstCharacterNotForwardSlash() {
098: try {
099: new ServletURL(null, null, null, "invalidpathinfo", null);
100: fail("The path info must start with a \"/\" character");
101: } catch (IllegalArgumentException expected) {
102: assertEquals(
103: "The Path Info must start with a \"/\" character.",
104: expected.getMessage());
105: }
106: }
107:
108: /**
109: * Verify that the path info cannot be an empty string.
110: */
111: public void testSetPathInfoEmptyNotAllowed() {
112: try {
113: new ServletURL(null, null, null, "", null);
114: fail("The path info must not be an empty string");
115: } catch (IllegalArgumentException expected) {
116: assertEquals(
117: "The Path Info must not be an empty string. Use "
118: + "null if you don't want to have a path info.",
119: expected.getMessage());
120: }
121: }
122:
123: /**
124: * Verify that the <code>getHost()</code> method is returning the correct
125: * host when a port is specified and that the <code>getPort()</code>
126: * method returns the specified port.
127: */
128: public void testGetHostWithPort() {
129: ServletURL servletURL = new ServletURL(
130: "jakarta.apache.org:8080", null, null, null, null);
131:
132: assertEquals("jakarta.apache.org", servletURL.getHost());
133: assertEquals(8080, servletURL.getPort());
134: }
135:
136: /**
137: * Verify that <code>getPort()</code> returns -1 when the port is invalid.
138: */
139: public void testGetPortInvalidPortNumber() {
140: ServletURL servletURL = new ServletURL();
141:
142: servletURL.setServerName("jakarta.apache.org:invalidPort80");
143:
144: int port = servletURL.getPort();
145:
146: assertEquals(-1, port);
147: }
148:
149: /**
150: * Verify that an invalid protocol raises an exception.
151: */
152: public void testSetProtocolInvalidProtocol() {
153: ServletURL servletURL = new ServletURL();
154:
155: try {
156: servletURL.setProtocol("invalid protocol");
157: fail("Should have raised an invalid protocol error");
158: } catch (RuntimeException e) {
159: assertEquals(
160: "Invalid protocol [invalid protocol]. Currently "
161: + "supported protocols are [http] and [https].",
162: e.getMessage());
163: }
164: }
165:
166: /**
167: * Verify that a valid protocol works.
168: */
169: public void testSetProtocolOk() {
170: ServletURL servletURL = new ServletURL();
171:
172: servletURL.setProtocol(ServletURL.PROTOCOL_HTTP);
173: assertEquals(ServletURL.PROTOCOL_HTTP, servletURL.getProtocol());
174:
175: servletURL.setProtocol(ServletURL.PROTOCOL_HTTPS);
176: assertEquals(ServletURL.PROTOCOL_HTTPS, servletURL
177: .getProtocol());
178: }
179:
180: /**
181: * Verify <code>getPath()</code> is ok when all parts are filled.
182: */
183: public void testGetPath() {
184: ServletURL servletURL = new ServletURL();
185:
186: servletURL.setQueryString("param1=value1");
187: servletURL.setContextPath("/context");
188: servletURL.setServletPath("/servletPath");
189: servletURL.setPathInfo("/pathInfo");
190:
191: String path = servletURL.getPath();
192:
193: assertEquals("/context/servletPath/pathInfo", path);
194: }
195:
196: /**
197: * Verify <code>getPath()</code> returns null when no parts are filled.
198: */
199: public void testGetPathNull() {
200: ServletURL servletURL = new ServletURL();
201:
202: String path = servletURL.getPath();
203:
204: assertNull(path);
205: }
206: }
|