1 20 package org.apache.cactus; 21 22 import junit.framework.TestCase; 23 24 29 public class TestServletURL extends TestCase 30 { 31 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 expected) 43 { 44 assertEquals("The Context Path must start with a \"/\" character.", 45 expected.getMessage()); 46 } 47 } 48 49 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 expected) 61 { 62 assertEquals("The Context Path must not end with a \"/\"" 63 + " character.", expected.getMessage()); 64 } 65 } 66 67 70 public void testSetContextPathEmptyString() 71 { 72 ServletURL servletURL = new ServletURL(null, "", null, null, null); 73 assertEquals("", servletURL.getContextPath()); 74 } 75 76 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 expected) 88 { 89 assertEquals("The Servlet Path must start with a \"/\" character.", 90 expected.getMessage()); 91 } 92 } 93 94 97 public void testSetServletPathEmptyString() 98 { 99 ServletURL servletURL = new ServletURL(null, null, "", null, null); 100 assertEquals("", servletURL.getServletPath()); 101 } 102 103 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 expected) 115 { 116 assertEquals("The Path Info must start with a \"/\" character.", 117 expected.getMessage()); 118 } 119 } 120 121 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 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 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 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 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 e) 180 { 181 assertEquals("Invalid protocol [invalid protocol]. Currently " 182 + "supported protocols are [http] and [https].", 183 e.getMessage()); 184 } 185 } 186 187 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 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 path = servletURL.getPath(); 214 215 assertEquals("/context/servletPath/pathInfo", path); 216 } 217 218 221 public void testGetPathNull() 222 { 223 ServletURL servletURL = new ServletURL(); 224 225 String path = servletURL.getPath(); 226 227 assertNull(path); 228 } 229 } 230 | Popular Tags |