1 package org.nanocontainer.webcontainer; 2 3 import junit.framework.TestCase; 4 5 import java.io.IOException ; 6 import java.io.File ; 7 import java.io.FileNotFoundException ; 8 import java.net.URL ; 9 10 import org.picocontainer.alternatives.EmptyPicoContainer; 11 import org.mortbay.util.IO; 12 13 public class StaticFileTestCase extends TestCase { 14 15 PicoJettyServer server; 16 protected void tearDown() throws Exception { 17 if (server != null) { 18 server.stop(); 19 } 20 } 21 22 public void testStaticFile() throws InterruptedException , IOException { 23 24 File warFile = TestHelper.getTestWarFile(); 25 26 server = new PicoJettyServer("localhost", 8080, new EmptyPicoContainer()); 27 PicoContextHandler barContext = server.createContext("/bar", false); 28 barContext.setStaticContext(warFile.getParentFile().getAbsolutePath()); 29 30 server.start(); 31 32 Thread.sleep(2 * 1000); 33 34 URL url = new URL ("http://localhost:8080/bar/hello.html"); 35 assertEquals("<html>\n" + 36 " <body>\n" + 37 " hello\n" + 38 " </body>\n" + 39 "</html>", IO.toString(url.openStream())); 40 41 Thread.sleep(1 * 1000); 42 43 } 44 45 public void testDifferentWelcomePage() throws InterruptedException , IOException { 46 47 File warFile = TestHelper.getTestWarFile(); 48 49 server = new PicoJettyServer("localhost", 8080, new EmptyPicoContainer()); 50 PicoContextHandler barContext = server.createContext("/bar", false); 51 barContext.setStaticContext(warFile.getParentFile().getAbsolutePath(), "hello.html"); 52 53 server.start(); 54 55 Thread.sleep(2 * 1000); 56 57 URL url = new URL ("http://localhost:8080/bar/"); 58 assertEquals("<html>\n" + 59 " <body>\n" + 60 " hello\n" + 61 " </body>\n" + 62 "</html>", IO.toString(url.openStream())); 63 64 Thread.sleep(1 * 1000); 65 66 } 67 68 public void testMissingPage() throws InterruptedException , IOException { 69 70 File warFile = TestHelper.getTestWarFile(); 71 72 server = new PicoJettyServer("localhost", 8080, new EmptyPicoContainer()); 73 PicoContextHandler barContext = server.createContext("/bar", false); 74 barContext.setStaticContext(warFile.getParentFile().getAbsolutePath()); 75 76 server.start(); 77 78 Thread.sleep(2 * 1000); 79 80 URL url = new URL ("http://localhost:8080/bar/HearMeRoar!"); 81 try { 82 url.openStream(); 83 fail("should have barfed"); 84 } catch (FileNotFoundException e) { 85 } 87 88 Thread.sleep(1 * 1000); 89 90 } 91 92 } 93 | Popular Tags |