KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > webcontainer > groovy > WebContainerBuilderTestCase


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * *
9  *****************************************************************************/

10
11 package org.nanocontainer.webcontainer.groovy;
12
13 import junit.framework.TestCase;
14 import org.nanocontainer.script.groovy.GroovyContainerBuilder;
15 import org.nanocontainer.webcontainer.TestHelper;
16 import org.picocontainer.PicoContainer;
17 import org.picocontainer.defaults.ObjectReference;
18 import org.picocontainer.defaults.SimpleReference;
19 import org.mortbay.util.IO;
20
21 import java.io.*;
22 import java.net.URL JavaDoc;
23
24 public class WebContainerBuilderTestCase extends TestCase {
25
26     private ObjectReference containerRef = new SimpleReference();
27     private ObjectReference parentContainerRef = new SimpleReference();
28
29     private PicoContainer pico;
30
31     protected void tearDown() throws Exception JavaDoc {
32         if (pico != null) {
33             pico.stop();
34         }
35         Thread.sleep(1 * 1000);
36     }
37
38     public void testCanComposeWebContainerContextAndFilter() throws InterruptedException JavaDoc, IOException {
39         Reader script = new StringReader("" +
40                 "package org.nanocontainer.script.groovy\n" +
41                 "builder = new GroovyNodeBuilder()\n" +
42                 "nano = builder.container {\n" +
43                 " component(instance:'Fred')\n" +
44                 " component(instance:new Integer(5))\n" +
45                 // declare the web container
46
" webContainer(port:8080) {\n" +
47                 " context(path:'/bar') {\n" +
48                 " filter(path:'/*', class:org.nanocontainer.webcontainer.DependencyInjectionTestFilter," +
49                 " dispatchers: new Integer(0)){\n" +
50                 " initParam(name:'foo', value:'bau')\n" +
51                 " }\n" +
52                 " servlet(path:'/foo2', class:org.nanocontainer.webcontainer.DependencyInjectionTestServlet)\n" +
53
54                 " }\n" +
55                 " }\n" +
56                 // end declaration
57
"}\n");
58
59         assertPageIsHostedWithContents(script, "hello Fred Filtered!(int= 5 bau)", "http://localhost:8080/bar/foo2");
60     }
61
62     public void testCanComposeWebContainerContextAndServlet() throws InterruptedException JavaDoc, IOException {
63         Reader script = new StringReader("" +
64                 "package org.nanocontainer.script.groovy\n" +
65                 "builder = new GroovyNodeBuilder()\n" +
66                 "nano = builder.container {\n" +
67                 " component(instance:'Fred')\n" +
68                 // declare the web container
69
" webContainer(port:8080) {\n" +
70                 " context(path:'/bar') {\n" +
71                 " servlet(path:'/foo', class:org.nanocontainer.webcontainer.DependencyInjectionTestServlet){\n" +
72                 " initParam(name:'foo', value:'bar')\n" +
73                 " }\n" +
74                 " }\n" +
75                 " }\n" +
76                 // end declaration
77
"}\n");
78
79         assertPageIsHostedWithContents(script, "hello Fred bar", "http://localhost:8080/bar/foo");
80     }
81
82     public void testCanComposeWebContainerContextAndServletInstance() throws InterruptedException JavaDoc, IOException {
83         Reader script = new StringReader("" +
84                 "package org.nanocontainer.script.groovy\n" +
85                 "builder = new GroovyNodeBuilder()\n" +
86                 "nano = builder.container {\n" +
87                 // declare the web container
88
" webContainer(port:8080) {\n" +
89                 " context(path:'/bar') {\n" +
90                 " servlet(path:'/foo', instance:new org.nanocontainer.webcontainer.DependencyInjectionTestServlet('Fred')) {\n" +
91                 //" setFoo(foo:'bar')\n" +
92
" }\n" +
93                 " }\n" +
94                 " }\n" +
95                 // end declaration
96
"}\n");
97
98         assertPageIsHostedWithContents(script, "hello Fred", "http://localhost:8080/bar/foo");
99     }
100
101     
102     public void testCanComposeWebContainerContextWithExplicitConnector() throws InterruptedException JavaDoc, IOException {
103         Reader script = new StringReader("" +
104                 "package org.nanocontainer.script.groovy\n" +
105                 "builder = new GroovyNodeBuilder()\n" +
106                 "nano = builder.container {\n" +
107                 " component(instance:'Fred')\n" +
108                 // declare the web container
109
" webContainer() {\n" +
110                 " blockingChannelConnector(host:'localhost', port:8080)\n" +
111                 " context(path:'/bar') {\n" +
112                 " servlet(path:'/foo', class:org.nanocontainer.webcontainer.DependencyInjectionTestServlet)\n" +
113                 " }\n" +
114                 " }\n" +
115                 // end declaration
116
"}\n");
117
118         assertPageIsHostedWithContents(script, "hello Fred", "http://localhost:8080/bar/foo");
119     }
120
121     public void testCanComposeWebContainerAndWarFile() throws InterruptedException JavaDoc, IOException {
122
123         File testWar = TestHelper.getTestWarFile();
124
125         Reader script = new StringReader("" +
126                 "package org.nanocontainer.script.groovy\n" +
127                 "builder = new GroovyNodeBuilder()\n" +
128                 "nano = builder.container {\n" +
129                 " component(instance:'Fred')\n" +
130                 " component(instance:new Integer(5))\n" +
131                 " component(key:StringBuffer.class, instance:new StringBuffer())\n" +
132                 // declare the web container
133
" webContainer() {\n" +
134                 " blockingChannelConnector(host:'localhost', port:8080)\n" +
135                 " xmlWebApplication(path:'/bar', warfile:'"+testWar.getAbsolutePath().replace('\\','/')+"')" +
136                 " }\n" +
137                 // end declaration
138
"}\n");
139
140         assertPageIsHostedWithContents(script, "hello Fred bar", "http://localhost:8080/bar/foo");
141         assertEquals("-contextInitialized", pico.getComponentInstance(StringBuffer JavaDoc.class).toString());
142     }
143
144     public void testCanComposeWebContainerContextAndListener() throws InterruptedException JavaDoc, IOException {
145
146         Reader script = new StringReader("" +
147                 "package org.nanocontainer.script.groovy\n" +
148                 "builder = new GroovyNodeBuilder()\n" +
149                 "nano = builder.container {\n" +
150                 " component(class:StringBuffer.class)\n" +
151                 // declare the web container
152
" webContainer(port:8080) {\n" +
153                 " context(path:'/bar') {\n" +
154                 " listener(class:org.nanocontainer.webcontainer.DependencyInjectionTestListener)\n" +
155                 " }\n" +
156                 " }\n" +
157                 // end declaration
158
"}\n");
159
160         assertPageIsHostedWithContents(script, "", "http://localhost:8080/bar/");
161
162         StringBuffer JavaDoc stringBuffer = (StringBuffer JavaDoc) pico.getComponentInstance(StringBuffer JavaDoc.class);
163
164         assertEquals("-contextInitialized", stringBuffer.toString());
165
166         pico.stop();
167         pico = null;
168
169         assertEquals("-contextInitialized-contextDestroyed", stringBuffer.toString());
170
171     }
172
173     public void testStaticContentCanBeServed() throws InterruptedException JavaDoc, IOException {
174
175         File testWar = TestHelper.getTestWarFile();
176
177         String JavaDoc absolutePath = testWar.getParentFile().getAbsolutePath();
178         absolutePath = absolutePath.replace('\\', '/');
179         
180         Reader script = new StringReader("" +
181                 "package org.nanocontainer.script.groovy\n" +
182                 "builder = new GroovyNodeBuilder()\n" +
183                 "nano = builder.container {\n" +
184                 // declare the web container
185
" webContainer(port:8080) {\n" +
186                 " context(path:'/bar') {\n" +
187                 " staticContent(path:'"+absolutePath+"')\n" +
188                 " }\n" +
189                 " }\n" +
190                 // end declaration
191
"}\n");
192
193         assertPageIsHostedWithContents(script, "<html>\n" +
194                 " <body>\n" +
195                 " hello\n" +
196                 " </body>\n" +
197                 "</html>", "http://localhost:8080/bar/hello.html");
198
199         Thread.sleep(1 * 1000);
200
201         pico.stop();
202         pico = null;
203
204
205     }
206
207     public void testStaticContentCanBeServedWithDefaultWelcomePage() throws InterruptedException JavaDoc, IOException {
208
209         File testWar = TestHelper.getTestWarFile();
210
211         String JavaDoc absolutePath = testWar.getParentFile().getAbsolutePath();
212         absolutePath = absolutePath.replace('\\', '/');
213         
214         Reader script = new StringReader("" +
215                 "package org.nanocontainer.script.groovy\n" +
216                 "builder = new GroovyNodeBuilder()\n" +
217                 "nano = builder.container {\n" +
218                 // declare the web container
219
" webContainer(port:8080) {\n" +
220                 " context(path:'/bar') {\n" +
221                 " staticContent(path:'" + absolutePath + "', welcomePage:'hello.html')\n" +
222                 " }\n" +
223                 " }\n" +
224                 // end declaration
225
"}\n");
226
227         assertPageIsHostedWithContents(script, "<html>\n" +
228                 " <body>\n" +
229                 " hello\n" +
230                 " </body>\n" +
231                 "</html>", "http://localhost:8080/bar/");
232
233         Thread.sleep(1 * 1000);
234
235         pico.stop();
236         pico = null;
237
238
239     }
240
241
242     private void assertPageIsHostedWithContents(Reader script, String JavaDoc message, String JavaDoc url) throws InterruptedException JavaDoc, IOException {
243         pico = buildContainer(script, null, "SOME_SCOPE");
244         assertNotNull(pico);
245         
246         Thread.sleep(2 * 1000);
247
248         String JavaDoc actual = null;
249         try {
250             actual = IO.toString(new URL JavaDoc(url).openStream());
251         } catch (FileNotFoundException e) {
252             actual = "";
253         }
254         assertEquals(message, actual);
255     }
256
257     private PicoContainer buildContainer(Reader script, PicoContainer parent, Object JavaDoc scope) {
258         parentContainerRef.set(parent);
259         new GroovyContainerBuilder(script, getClass().getClassLoader()).buildContainer(containerRef, parentContainerRef, scope, true);
260         return (PicoContainer) containerRef.get();
261     }
262 }
263
Popular Tags