1 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 ; 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 { 32 if (pico != null) { 33 pico.stop(); 34 } 35 Thread.sleep(1 * 1000); 36 } 37 38 public void testCanComposeWebContainerContextAndFilter() throws InterruptedException , 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 " 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 "}\n"); 58 59 assertPageIsHostedWithContents(script, "hello Fred Filtered!(int= 5 bau)", "http://localhost:8080/bar/foo2"); 60 } 61 62 public void testCanComposeWebContainerContextAndServlet() throws InterruptedException , 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 " 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 "}\n"); 78 79 assertPageIsHostedWithContents(script, "hello Fred bar", "http://localhost:8080/bar/foo"); 80 } 81 82 public void testCanComposeWebContainerContextAndServletInstance() throws InterruptedException , IOException { 83 Reader script = new StringReader("" + 84 "package org.nanocontainer.script.groovy\n" + 85 "builder = new GroovyNodeBuilder()\n" + 86 "nano = builder.container {\n" + 87 " webContainer(port:8080) {\n" + 89 " context(path:'/bar') {\n" + 90 " servlet(path:'/foo', instance:new org.nanocontainer.webcontainer.DependencyInjectionTestServlet('Fred')) {\n" + 91 " }\n" + 93 " }\n" + 94 " }\n" + 95 "}\n"); 97 98 assertPageIsHostedWithContents(script, "hello Fred", "http://localhost:8080/bar/foo"); 99 } 100 101 102 public void testCanComposeWebContainerContextWithExplicitConnector() throws InterruptedException , 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 " 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 "}\n"); 117 118 assertPageIsHostedWithContents(script, "hello Fred", "http://localhost:8080/bar/foo"); 119 } 120 121 public void testCanComposeWebContainerAndWarFile() throws InterruptedException , 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 " webContainer() {\n" + 134 " blockingChannelConnector(host:'localhost', port:8080)\n" + 135 " xmlWebApplication(path:'/bar', warfile:'"+testWar.getAbsolutePath().replace('\\','/')+"')" + 136 " }\n" + 137 "}\n"); 139 140 assertPageIsHostedWithContents(script, "hello Fred bar", "http://localhost:8080/bar/foo"); 141 assertEquals("-contextInitialized", pico.getComponentInstance(StringBuffer .class).toString()); 142 } 143 144 public void testCanComposeWebContainerContextAndListener() throws InterruptedException , 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 " webContainer(port:8080) {\n" + 153 " context(path:'/bar') {\n" + 154 " listener(class:org.nanocontainer.webcontainer.DependencyInjectionTestListener)\n" + 155 " }\n" + 156 " }\n" + 157 "}\n"); 159 160 assertPageIsHostedWithContents(script, "", "http://localhost:8080/bar/"); 161 162 StringBuffer stringBuffer = (StringBuffer ) pico.getComponentInstance(StringBuffer .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 , IOException { 174 175 File testWar = TestHelper.getTestWarFile(); 176 177 String 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 " webContainer(port:8080) {\n" + 186 " context(path:'/bar') {\n" + 187 " staticContent(path:'"+absolutePath+"')\n" + 188 " }\n" + 189 " }\n" + 190 "}\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 , IOException { 208 209 File testWar = TestHelper.getTestWarFile(); 210 211 String 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 " webContainer(port:8080) {\n" + 220 " context(path:'/bar') {\n" + 221 " staticContent(path:'" + absolutePath + "', welcomePage:'hello.html')\n" + 222 " }\n" + 223 " }\n" + 224 "}\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 message, String url) throws InterruptedException , IOException { 243 pico = buildContainer(script, null, "SOME_SCOPE"); 244 assertNotNull(pico); 245 246 Thread.sleep(2 * 1000); 247 248 String actual = null; 249 try { 250 actual = IO.toString(new URL (url).openStream()); 251 } catch (FileNotFoundException e) { 252 actual = ""; 253 } 254 assertEquals(message, actual); 255 } 256 257 private PicoContainer buildContainer(Reader script, PicoContainer parent, Object 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 |