1 9 10 package org.nanocontainer.webcontainer; 11 12 import java.io.IOException ; 13 import java.net.URL ; 14 15 import junit.framework.TestCase; 16 17 import org.mortbay.util.IO; 18 import org.picocontainer.defaults.DefaultPicoContainer; 19 import org.picocontainer.alternatives.EmptyPicoContainer; 20 21 public class DependencyInjectionServletTestCase extends TestCase { 22 23 PicoJettyServer server; 24 protected void tearDown() throws Exception { 25 if (server != null) { 26 server.stop(); 27 } 28 Thread.sleep(1 * 1000); 29 } 30 31 public void testCanInstantiateWebContainerContextAndServlet() throws InterruptedException , IOException { 32 33 final DefaultPicoContainer parentContainer = new DefaultPicoContainer(); 34 parentContainer.registerComponentInstance(String .class, "Fred"); 35 36 server = new PicoJettyServer("localhost", 8080, parentContainer); 37 PicoContextHandler barContext = server.createContext("/bar", false); 38 Class servletClass = DependencyInjectionTestServlet.class; 39 PicoServletHolder holder = barContext.addServletWithMapping(servletClass, "/foo"); 40 holder.setInitParameter("foo", "bar"); 41 42 server.start(); 43 44 Thread.sleep(2 * 1000); 45 46 URL url = new URL ("http://localhost:8080/bar/foo"); 47 assertEquals("hello Fred bar", IO.toString(url.openStream())); 48 49 50 } 51 52 public void testCanInstantiateWebContainerContextAndServletInstance() throws InterruptedException , IOException { 53 54 55 server = new PicoJettyServer("localhost", 8080, new EmptyPicoContainer()); 56 PicoContextHandler barContext = server.createContext("/bar", false); 57 58 DependencyInjectionTestServlet servlet0 = new DependencyInjectionTestServlet("Fred"); 59 DependencyInjectionTestServlet servlet1 = (DependencyInjectionTestServlet) 60 barContext.addServletWithMapping(servlet0, "/foo"); 61 servlet1.setFoo("bar"); 62 63 server.start(); 64 65 Thread.sleep(2 * 1000); 66 67 URL url = new URL ("http://localhost:8080/bar/foo"); 68 assertEquals("hello Fred bar", IO.toString(url.openStream())); 69 70 71 } 72 73 74 75 } | Popular Tags |