1 37 package net.sourceforge.cruisecontrol.servlet; 38 39 import java.io.File ; 40 import java.io.IOException ; 41 import java.io.StringWriter ; 42 import java.io.Writer ; 43 import java.util.List ; 44 45 import javax.servlet.ServletConfig ; 46 import javax.servlet.ServletException ; 47 import javax.servlet.ServletContext ; 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 51 import junit.framework.TestCase; 52 import net.sourceforge.cruisecontrol.mock.MockServletConfig; 53 import net.sourceforge.cruisecontrol.mock.MockServletContext; 54 import net.sourceforge.cruisecontrol.mock.MockServletRequest; 55 import net.sourceforge.cruisecontrol.mock.MockServletResponse; 56 57 60 public class FileServletTest extends TestCase { 61 62 private FileServlet servlet; 63 64 public FileServletTest(String testName) { 65 super(testName); 66 } 67 68 protected void setUp() { 69 servlet = new FileServlet(); 70 } 71 72 protected void tearDown() { 73 servlet = null; 74 } 75 76 public void testGetRootDir() { 77 MockServletConfig config = new MockServletConfig(); 78 MockServletContext context = new MockServletContext(); 79 config.setServletContext(context); 80 81 try { 82 servlet.getRootDir(config); 83 fail("should have exception when required attributes not set"); 84 } catch (ServletException e) { 85 } 86 87 config.setInitParameter("rootDir", "."); 88 try { 89 servlet.getRootDir(config); 90 } catch (ServletException e) { 91 fail("shouldn't throw exception when valid rootDir parameter set"); 92 } 93 94 context.setInitParameter("logDir", "this directory does not exist"); 95 try { 96 servlet.getRootDir(config); 97 } catch (ServletException e) { 98 fail("good rootDir but bad logDir should work"); 99 } 100 101 config = new MockServletConfig(); 102 context = new MockServletContext(); 103 config.setServletContext(context); 104 105 context.setInitParameter("logDir", "."); 106 try { 107 servlet.getRootDir(config); 108 } catch (ServletException e) { 109 fail("shouldn't throw exception when valid logDir parameter set"); 110 } 111 112 config.setInitParameter("rootDir", "this directory does not exist"); 113 try { 114 servlet.getRootDir(config); 115 } catch (ServletException e) { 116 fail("bad rootDir but good logDir should work"); 117 } 118 } 119 120 123 public void testServiceInvalidFile() throws ServletException , IOException { 124 MockServletRequest request = new MockServletRequest(); 125 MockServletResponse response = new MockServletResponse(); 126 127 final String fileName = "tmp12345.html"; 128 request.setPathInfo(fileName); 129 130 servlet.service(request, response); 131 String actual = response.getWritten(); 132 String expected = "<html><body><h1>" + fileName + "</h1><h1>Invalid File or Directory</h1></body></html>"; 133 assertEquals(expected, actual); 134 String actualMimeType = response.getContentType(); 135 assertEquals("text/html", actualMimeType); 136 assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus()); 137 } 138 139 142 public void testServiceFile() throws ServletException , IOException { 143 MockServletRequest request = new MockServletRequest(); 144 MockServletResponse response = new MockServletResponse(); 145 File file = File.createTempFile("tmp", ".html"); 146 file.deleteOnExit(); 147 final File dir = file.getParentFile(); 148 149 request.setPathInfo(file.getName()); 150 servlet = new FileServlet() { 151 protected File getRootDir(ServletConfig servletconfig) { 152 return dir; 153 } 154 155 protected String getMimeType(String filename) { 156 if (filename.endsWith(".html")) { 157 return "text/html"; 158 } 159 return null; 160 } 161 }; 162 final MockServletConfig servletconfig = new MockServletConfig(); 163 servletconfig.setServletContext(new MockServletContext()); 164 servlet.init(servletconfig); 165 servlet.service(request, response); 166 String actual = response.getWritten(); 167 String expected = ""; 168 assertEquals(expected, actual); 169 String actualMimeType = response.getContentType(); 170 assertEquals("text/html", actualMimeType); 171 } 172 173 176 public void testServiceParametrizedMimeType() throws ServletException , IOException { 177 MockServletRequest request = new MockServletRequest(); 178 MockServletResponse response = new MockServletResponse(); 179 File file = File.createTempFile("tmp", ".html"); 180 file.deleteOnExit(); 181 final File dir = file.getParentFile(); 182 183 request.setPathInfo(file.getName()); 184 request.addParameter("mimetype", "text/plain"); 185 servlet = new FileServlet() { 186 protected File getRootDir(ServletConfig servletconfig) { 187 return dir; 188 } 189 190 protected String getMimeType(String filename) { 191 if (filename.endsWith(".html")) { 192 return "text/html"; 193 } 194 return null; 195 } 196 }; 197 final MockServletConfig servletconfig = new MockServletConfig(); 198 servletconfig.setServletContext(new MockServletContext()); 199 servlet.init(servletconfig); 200 servlet.service(request, response); 201 String actual = response.getWritten(); 202 String expected = ""; 203 assertEquals(expected, actual); 204 String actualMimeType = response.getContentType(); 205 assertEquals("text/plain", actualMimeType); 206 207 request.setPathInfo(file.getName()); 208 209 } 210 211 public void testGetIndexes() throws ServletException , IOException { 212 MockServletConfig config = new MockServletConfig(); 213 MockServletContext context = new MockServletContext(); 214 config.setServletContext(context); 215 216 List indexes; 217 218 indexes = servlet.getIndexFiles(config); 220 assertNotNull(indexes); 221 assertEquals(0, indexes.size()); 222 223 context.setInitParameter("fileServlet.welcomeFiles", null); 225 indexes = servlet.getIndexFiles(config); 226 assertNotNull(indexes); 227 assertEquals(0, indexes.size()); 228 229 context.setInitParameter("fileServlet.welcomeFiles", ""); 231 indexes = servlet.getIndexFiles(config); 232 assertNotNull(indexes); 233 assertEquals(0, indexes.size()); 234 235 context.setInitParameter("fileServlet.welcomeFiles", "index.htm index.html"); 237 indexes = servlet.getIndexFiles(config); 238 assertNotNull(indexes); 239 assertEquals(2, indexes.size()); 240 assertEquals("index.htm", indexes.get(0)); 241 assertEquals("index.html", indexes.get(1)); 242 243 context.setInitParameter("fileServlet.welcomeFiles", " index.html index.htm "); 245 indexes = servlet.getIndexFiles(config); 246 assertNotNull(indexes); 247 assertEquals(2, indexes.size()); 248 assertEquals("index.html", indexes.get(0)); 249 assertEquals("index.htm", indexes.get(1)); 250 } 251 252 253 static class MockWebFile extends WebFile { 254 private String [] subFiles; 255 256 public MockWebFile(File root, String path, String [] subFiles) { 257 super(root, path); 258 this.subFiles = subFiles; 259 } 260 261 public String [] list() { 262 return subFiles; 263 } 264 } 265 266 270 public void testPrintDirs() throws IOException { 271 FileServlet fileServlet = new FileServlet() { 272 protected WebFile getSubWebFile(final String subFilePath) { 273 return new WebFile(getRootDir(), subFilePath) { 274 public boolean isDir() { 275 String lastPathElt = subFilePath.substring(subFilePath.lastIndexOf('/') + 1); 276 final boolean b = lastPathElt.indexOf(".") == -1; 277 return b; 278 } 279 }; 280 } 281 }; 282 283 String [] files = 284 { 285 new String ("log1.txt"), 286 new String ("log2") 287 }; 288 289 final StringWriter writer1 = new StringWriter (); 290 291 final MockServletRequest request1 = new MockServletRequest() { 292 public String getRequestURI() { 293 return "/artifacts/abc"; 294 } 295 }; 296 fileServlet.printDirs(request1, new MockWebFile(new File ("notimportant"), "notimportant", files), writer1); 297 298 final String expectedOutput1 = 299 "<ul>" 300 + "<li><a HREF=\"/artifacts/abc/log1.txt\">log1.txt</a></li>" 301 + "<li><a HREF=\"/artifacts/abc/log2\">log2/</a></li>" 302 + "</ul>"; 303 304 assertEquals(expectedOutput1, writer1.getBuffer().toString()); 305 306 307 final StringWriter writer2 = new StringWriter (); 308 final MockServletRequest request2 = new MockServletRequest() { 309 public String getRequestURI() { 310 return "/artifacts/abc;jsessionid=012456789ABCDEF"; 311 } 312 }; 313 fileServlet.printDirs(request2, new MockWebFile(new File ("/tmp"), "test", files), writer2); 314 315 final String expectedOutput2 = 316 "<ul>" 317 + "<li><a HREF=\"/artifacts/abc/log1.txt;jsessionid=012456789ABCDEF\">log1.txt</a></li>" 318 + "<li><a HREF=\"/artifacts/abc/log2;jsessionid=012456789ABCDEF\">log2/</a></li>" 319 + "</ul>"; 320 321 assertEquals(expectedOutput2, writer2.getBuffer().toString()); 322 } 323 324 public void testServiceIndexFile() throws ServletException , IOException { 325 MockServletRequest request = new MockServletRequest() { 326 public String getRequestURI() { 327 return ""; 328 } 329 }; 330 MyMockServletResponse response1 = new MyMockServletResponse(); 331 final File dir = new File (System.getProperty("java.io.tmpdir")); 332 333 MyMockFileServlet myServlet = new MyMockFileServlet(); 334 myServlet.setRootDir(dir); 335 336 MockServletConfig config = new MockServletConfig(); 337 MockServletContext context = new MockServletContext() { 338 public String getMimeType(String s) { 339 return "text/html"; 340 } 341 }; 342 config.setServletContext(context); 343 myServlet.init(config); 344 345 File indexFile = new File (dir, "index.html"); 346 indexFile.deleteOnExit(); 347 assertFalse("cannot test service index if index.html already exists", indexFile.exists()); 348 boolean created = indexFile.createNewFile(); 349 assertTrue(created); 350 351 request.setPathInfo(""); 353 myServlet.service(request, response1); 354 response1.ensureRedirect("/"); 355 assertEquals(0, myServlet.printDirCalls); 356 357 myServlet.init(); 359 MyMockServletResponse response2 = new MyMockServletResponse(); 360 request.setPathInfo("/"); 361 myServlet.service(request, response2); 362 String actual = response2.getWritten(); 363 assertTrue(actual.startsWith("<html><body><h1>")); 364 assertTrue(myServlet.printDirCalls > 0); 365 String actualMimeType = response2.getContentType(); 366 assertEquals("text/html", actualMimeType); 367 368 myServlet.init(); 370 MyMockServletResponse response3 = new MyMockServletResponse(); 371 context.setInitParameter("fileServlet.welcomeFiles", "index.html"); 372 config.setServletContext(context); 373 myServlet.init(config); 374 375 request.setPathInfo("/"); 376 myServlet.service(request, response3); 377 actual = response3.getWritten(); 378 String expected = ""; 379 assertEquals(expected, actual); 380 actualMimeType = response3.getContentType(); 381 assertEquals("text/html", actualMimeType); 382 assertEquals(0, myServlet.printDirCalls); 383 } 384 385 public void testGetMimeType() { 386 MockServletContext context = new MockServletContext() { 387 public String getMimeType(String s) { 388 return "text/html"; 389 } 390 }; 391 servlet = new TestFileServlet(context); 392 assertEquals("text/html", servlet.getMimeType("")); 393 394 context = new MockServletContext() { 395 public String getMimeType(String s) { 396 return null; 397 } 398 }; 399 servlet = new TestFileServlet(context); 400 assertEquals("text/plain", servlet.getMimeType("")); 401 } 402 403 private final class TestFileServlet extends FileServlet { 404 private MockServletContext context; 405 406 private TestFileServlet(MockServletContext msc) { 407 super(); 408 context = msc; 409 } 410 411 public ServletContext getServletContext() { 412 return context; 413 } 414 } 415 416 static class MyMockServletResponse extends MockServletResponse { 417 private String redirected; 418 public void sendRedirect(String arg0) throws IOException { 419 redirected = arg0; 420 } 421 422 public void ensureRedirect(String expectedRedirect) { 423 assertEquals(expectedRedirect, redirected); 424 } 425 426 public String encodeRedirectURL(String arg0) { 427 return arg0; 428 } 429 } 430 431 static class MyMockFileServlet extends FileServlet { 432 private File rootDir; 433 private int printDirCalls; 434 435 public void init() { 436 printDirCalls = 0; 437 } 438 439 public void setRootDir(File rootDir) { 440 this.rootDir = rootDir; 441 } 442 443 protected File getRootDir(ServletConfig servletconfig) { 444 return rootDir; 445 } 446 447 void printDirs(HttpServletRequest request, WebFile file, Writer writer) throws IOException { 448 super.printDirs(request, file, writer); 449 printDirCalls++; 450 } 451 } 452 } 453 | Popular Tags |