01: /*****************************************************************************
02: * Copyright (C) NanoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: *****************************************************************************/package org.nanocontainer.webcontainer;
09:
10: import java.io.IOException;
11:
12: import org.picocontainer.alternatives.EmptyPicoContainer;
13: import org.picocontainer.defaults.DefaultPicoContainer;
14:
15: public class DependencyInjectionServletTestCase extends
16: WebContainerTestCase {
17:
18: PicoJettyServer server;
19:
20: protected void tearDown() throws Exception {
21: if (server != null) {
22: server.stop();
23: }
24: }
25:
26: public void testCanInstantiateWebContainerContextAndServlet()
27: throws InterruptedException, IOException {
28:
29: final DefaultPicoContainer parentContainer = new DefaultPicoContainer();
30: parentContainer.registerComponentInstance(String.class, "Fred");
31:
32: server = new PicoJettyServer("localhost", 8080, parentContainer);
33: PicoContext barContext = server.createContext("/bar", false);
34: Class servletClass = DependencyInjectionTestServlet.class;
35: PicoServletHolder holder = barContext.addServletWithMapping(
36: servletClass, "/foo");
37: holder.setInitParameter("foo", "bar");
38:
39: server.start();
40:
41: assertEquals("hello Fred bar",
42: getPage("http://localhost:8080/bar/foo"));
43:
44: }
45:
46: public void testCanInstantiateWebContainerContextAndServletInstance()
47: throws InterruptedException, IOException {
48:
49: server = new PicoJettyServer("localhost", 8080,
50: new EmptyPicoContainer());
51: PicoContext barContext = server.createContext("/bar", false);
52:
53: DependencyInjectionTestServlet servlet0 = new DependencyInjectionTestServlet(
54: "Fred");
55: DependencyInjectionTestServlet servlet1 = (DependencyInjectionTestServlet) barContext
56: .addServletWithMapping(servlet0, "/foo");
57: servlet1.setFoo("bar");
58:
59: server.start();
60:
61: assertEquals("hello Fred bar",
62: getPage("http://localhost:8080/bar/foo"));
63:
64: }
65:
66: }
|