01: package org.nanocontainer.webcontainer;
02:
03: import java.io.IOException;
04: import java.net.URL;
05:
06: import junit.framework.TestCase;
07:
08: import org.picocontainer.alternatives.EmptyPicoContainer;
09: import org.picocontainer.defaults.DefaultPicoContainer;
10:
11: public class DependencyInjectionListenerTestCase extends TestCase {
12:
13: PicoJettyServer server;
14:
15: protected void tearDown() throws Exception {
16: if (server != null) {
17: server.stop();
18: }
19: }
20:
21: public void testCanInstantiateWebContainerContextAndListener()
22: throws InterruptedException, IOException {
23:
24: final DefaultPicoContainer parentContainer = new DefaultPicoContainer();
25: StringBuffer sb = new StringBuffer();
26: parentContainer.registerComponentInstance(StringBuffer.class,
27: sb);
28:
29: server = new PicoJettyServer("localhost", 8080, parentContainer);
30: PicoContext barContext = server.createContext("/bar", false);
31: Class listenerClass = DependencyInjectionTestListener.class;
32: barContext.addListener(listenerClass);
33:
34: server.start();
35:
36: assertEquals("-contextInitialized", sb.toString());
37:
38: server.stop();
39:
40: assertEquals("-contextInitialized-contextDestroyed", sb
41: .toString());
42:
43: }
44:
45: public void testListenerInvokedBeforeFilterBeforeServlet()
46: throws InterruptedException, IOException {
47:
48: final DefaultPicoContainer parentContainer = new DefaultPicoContainer();
49: StringBuffer sb = new StringBuffer();
50: parentContainer.registerComponentInstance(StringBuffer.class,
51: sb);
52:
53: server = new PicoJettyServer("localhost", 8080, parentContainer);
54: PicoContext barContext = server.createContext("/bar", false);
55: Class listenerClass = DependencyInjectionTestListener.class;
56: barContext.addListener(listenerClass);
57: barContext.addServletWithMapping(
58: DependencyInjectionTestServlet2.class, "/foo");
59: barContext.addFilterWithMapping(
60: DependencyInjectionTestFilter2.class, "/foo", 0);
61:
62: server.start();
63:
64: URL url = new URL("http://localhost:8080/bar/foo");
65: url.openStream();
66:
67: assertEquals("-contextInitialized-Filter-Servlet", sb
68: .toString());
69:
70: server.stop();
71:
72: assertEquals(
73: "-contextInitialized-Filter-Servlet-contextDestroyed",
74: sb.toString());
75:
76: }
77:
78: public void testCanInstantiateWebContainerContextAndListenerInstance()
79: throws InterruptedException, IOException {
80:
81: StringBuffer sb = new StringBuffer();
82:
83: server = new PicoJettyServer("localhost", 8080,
84: new EmptyPicoContainer());
85: PicoContext barContext = server.createContext("/bar", false);
86: barContext.addListener(new DependencyInjectionTestListener(sb));
87:
88: server.start();
89:
90: assertEquals("-contextInitialized", sb.toString());
91:
92: server.stop();
93:
94: assertEquals("-contextInitialized-contextDestroyed", sb
95: .toString());
96:
97: }
98:
99: }
|