1 package org.nanocontainer.webcontainer; 2 3 import junit.framework.TestCase; 4 5 import java.io.IOException ; 6 import java.net.URL ; 7 8 import org.picocontainer.defaults.DefaultPicoContainer; 9 import org.mortbay.util.IO; 10 11 import javax.servlet.Filter ; 12 13 public class DependencyInjectionFilterTestCase extends TestCase { 14 15 PicoJettyServer server; 16 protected void tearDown() throws Exception { 17 if (server != null) { 18 server.stop(); 19 } 20 Thread.sleep(1 * 1000); 21 } 22 23 public void testCanInstantiateWebContainerContextAndFilter() throws InterruptedException , IOException { 24 25 final DefaultPicoContainer parentContainer = new DefaultPicoContainer(); 26 parentContainer.registerComponentInstance(String .class, "Fred"); 27 parentContainer.registerComponentInstance(Integer .class, new Integer (5)); 28 29 30 server = new PicoJettyServer("localhost", 8080, parentContainer); 31 PicoContextHandler barContext = server.createContext("/bar", false); 32 PicoFilterHolder filterHolder = barContext.addFilterWithMapping(DependencyInjectionTestFilter.class, "/*", 0); 33 filterHolder.setInitParameter("foo", "bau"); 34 barContext.addServletWithMapping(DependencyInjectionTestServlet.class, "/foo2"); 35 server.start(); 36 37 Thread.sleep(2 * 1000); 38 39 URL url = new URL ("http://localhost:8080/bar/foo2"); 40 41 assertEquals("hello Fred Filtered!(int= 5 bau)", IO.toString(url.openStream())); 42 43 44 } 45 46 public void testCanInstantiateWebContainerContextAndFilterInstance() throws InterruptedException , IOException { 47 48 final DefaultPicoContainer parentContainer = new DefaultPicoContainer(); 49 parentContainer.registerComponentInstance(String .class, "Fred"); 50 51 52 server = new PicoJettyServer("localhost", 8080, parentContainer); 53 PicoContextHandler barContext = server.createContext("/bar", false); 54 DependencyInjectionTestFilter filter = (DependencyInjectionTestFilter) barContext.addFilterWithMapping(new DependencyInjectionTestFilter(new Integer (5)), "/*", 0); 55 filter.setFoo("bau"); 56 barContext.addServletWithMapping(DependencyInjectionTestServlet.class, "/foo2"); 57 server.start(); 58 59 Thread.sleep(2 * 1000); 60 61 URL url = new URL ("http://localhost:8080/bar/foo2"); 62 63 assertEquals("hello Fred Filtered!(int= 5 bau)", IO.toString(url.openStream())); 64 65 66 } 67 68 69 public void testCanInstantiateWebContainerContextAndServlet() throws InterruptedException , IOException { 70 71 final DefaultPicoContainer parentContainer = new DefaultPicoContainer(); 72 parentContainer.registerComponentInstance(String .class, "Fred"); 73 parentContainer.registerComponentInstance(Integer .class, new Integer (5)); 74 75 server = new PicoJettyServer("localhost", 8080, parentContainer); 76 PicoContextHandler barContext = server.createContext("/bar", false); 77 barContext.addFilterWithMapping(DependencyInjectionTestFilter.class, "/*", 0); 78 PicoServletHolder holder = barContext.addServletWithMapping(DependencyInjectionTestServlet.class, "/foo2"); 79 holder.setInitParameter("foo", "bau"); 80 server.start(); 81 82 Thread.sleep(2 * 1000); 83 84 URL url = new URL ("http://localhost:8080/bar/foo2"); 85 86 assertEquals("hello Fred Filtered!(int= 5) bau", IO.toString(url.openStream())); 87 88 } 89 90 91 } 92 | Popular Tags |