1 package org.nanocontainer.webcontainer; 2 3 import junit.framework.TestCase; 4 5 import java.io.IOException ; 6 7 import org.picocontainer.defaults.DefaultPicoContainer; 8 import org.picocontainer.alternatives.EmptyPicoContainer; 9 import org.nanocontainer.webcontainer.PicoJettyServer; 10 import org.nanocontainer.webcontainer.PicoContextHandler; 11 12 public class DependencyInjectionListenerTestCase extends TestCase { 13 14 PicoJettyServer server; 15 protected void tearDown() throws Exception { 16 if (server != null) { 17 server.stop(); 18 } 19 Thread.sleep(1 * 1000); 20 } 21 22 public void testCanInstantiateWebContainerContextAndListener() throws InterruptedException , IOException { 23 24 final DefaultPicoContainer parentContainer = new DefaultPicoContainer(); 25 StringBuffer sb = new StringBuffer (); 26 parentContainer.registerComponentInstance(StringBuffer .class, sb); 27 28 server = new PicoJettyServer("localhost", 8080, parentContainer); 29 PicoContextHandler barContext = server.createContext("/bar", false); 30 Class listenerClass = DependencyInjectionTestListener.class; 31 barContext.addListener(listenerClass); 32 33 server.start(); 34 35 assertEquals("-contextInitialized", sb.toString()); 36 37 server.stop(); 38 39 assertEquals("-contextInitialized-contextDestroyed", sb.toString()); 40 41 } 42 43 public void testCanInstantiateWebContainerContextAndListenerInstance() throws InterruptedException , IOException { 44 45 StringBuffer sb = new StringBuffer (); 46 47 server = new PicoJettyServer("localhost", 8080, new EmptyPicoContainer()); 48 PicoContextHandler barContext = server.createContext("/bar", false); 49 barContext.addListener(new DependencyInjectionTestListener(sb)); 50 51 server.start(); 52 53 assertEquals("-contextInitialized", sb.toString()); 54 55 server.stop(); 56 57 assertEquals("-contextInitialized-contextDestroyed", sb.toString()); 58 59 } 60 61 62 63 64 65 } 66 | Popular Tags |