1 15 package org.apache.tapestry.services.impl; 16 17 import java.net.URL ; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletContext ; 21 import javax.servlet.http.HttpServlet ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.hivemind.Registry; 25 import org.apache.hivemind.Resource; 26 import org.apache.hivemind.impl.DefaultClassResolver; 27 import org.apache.hivemind.impl.RegistryBuilder; 28 import org.apache.hivemind.test.HiveMindTestCase; 29 import org.apache.hivemind.util.ClasspathResource; 30 import org.apache.hivemind.util.ContextResource; 31 import org.apache.tapestry.parse.ISpecificationParser; 32 import org.apache.tapestry.services.ApplicationGlobals; 33 import org.apache.tapestry.services.ApplicationInitializer; 34 import org.apache.tapestry.spec.ApplicationSpecification; 35 import org.apache.tapestry.spec.IApplicationSpecification; 36 import org.easymock.AbstractMatcher; 37 import org.easymock.MockControl; 38 39 45 public class TestApplicationSpecificationInitializer extends HiveMindTestCase 46 { 47 public void testOnClasspath() throws Exception 48 { 49 DefaultClassResolver cr = new DefaultClassResolver(); 50 51 Resource appSpecResource = new ClasspathResource(cr, "/foo/OnClasspath.application"); 52 53 ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer(); 54 55 Log log = (Log) newMock(Log.class); 56 57 i.setLog(log); 58 59 ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl(); 60 cf.setClassResolver(cr); 61 62 i.setClasspathResourceFactory(cf); 63 64 HttpServlet servlet = new ServletFixture(); 65 66 MockControl configControl = newControl(ServletConfig .class); 67 ServletConfig config = (ServletConfig ) configControl.getMock(); 68 69 trainForServletInit(configControl, config); 70 71 config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM); 72 configControl.setReturnValue(appSpecResource.getPath()); 73 74 IApplicationSpecification as = new ApplicationSpecification(); 75 76 MockControl parserControl = newControl(ISpecificationParser.class); 77 ISpecificationParser parser = (ISpecificationParser) parserControl.getMock(); 78 79 i.setParser(parser); 80 81 parser.parseApplicationSpecification(appSpecResource); 82 parserControl.setReturnValue(as); 83 84 ApplicationGlobals ag = new ApplicationGlobalsImpl(); 85 86 i.setGlobals(ag); 87 88 replayControls(); 89 90 servlet.init(config); 91 92 94 i.initialize(servlet); 95 96 assertNotNull(ag.getActivator()); 97 assertSame(as, ag.getSpecification()); 98 99 verifyControls(); 100 } 101 102 private void trainForServletInit(MockControl configControl, ServletConfig config) 103 { 104 MockControl contextControl = newControl(ServletContext .class); 105 ServletContext context = (ServletContext ) contextControl.getMock(); 106 107 config.getServletContext(); 108 configControl.setReturnValue(context); 109 110 config.getServletName(); 111 configControl.setReturnValue("test"); 112 113 context.log("test: init"); 114 } 115 116 public void testInAppContextFolder() throws Exception 117 { 118 DefaultClassResolver cr = new DefaultClassResolver(); 119 120 ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer(); 121 122 MockControl contextControl = newControl(ServletContext .class); 123 ServletContext context = (ServletContext ) contextControl.getMock(); 124 125 MockControl logControl = newControl(Log.class); 126 Log log = (Log) logControl.getMock(); 127 128 i.setLog(log); 129 130 ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl(); 131 cf.setClassResolver(cr); 132 133 i.setClasspathResourceFactory(cf); 134 135 HttpServlet servlet = new ServletFixture(); 136 137 MockControl configControl = newControl(ServletConfig .class); 138 ServletConfig config = (ServletConfig ) configControl.getMock(); 139 140 trainForServletInit(configControl, config); 141 142 config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM); 143 configControl.setReturnValue(null); 144 145 config.getServletContext(); 146 configControl.setReturnValue(context); 147 148 config.getServletName(); 149 configControl.setReturnValue("fred"); 150 151 log.isDebugEnabled(); 152 logControl.setReturnValue(true); 153 154 Resource r = new ContextResource(context, "/WEB-INF/fred/fred.application"); 155 156 log.debug("Checking for existence of " + r); 157 158 context.getResource(r.getPath()); 159 contextControl.setReturnValue(new URL ("file:/context" + r.getPath())); 160 161 log.debug("Found " + r); 162 163 IApplicationSpecification as = new ApplicationSpecification(); 164 165 MockControl parserControl = newControl(ISpecificationParser.class); 166 ISpecificationParser parser = (ISpecificationParser) parserControl.getMock(); 167 168 i.setParser(parser); 169 170 parser.parseApplicationSpecification(r); 171 parserControl.setReturnValue(as); 172 173 ApplicationGlobals ag = new ApplicationGlobalsImpl(); 174 175 i.setGlobals(ag); 176 177 replayControls(); 178 179 servlet.init(config); 180 181 183 i.initialize(servlet); 184 185 verifyControls(); 186 } 187 188 public void testInWebInfFolder() throws Exception 189 { 190 DefaultClassResolver cr = new DefaultClassResolver(); 191 192 ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer(); 193 194 MockControl contextControl = newControl(ServletContext .class); 195 ServletContext context = (ServletContext ) contextControl.getMock(); 196 197 MockControl logControl = newControl(Log.class); 198 Log log = (Log) logControl.getMock(); 199 200 i.setLog(log); 201 202 ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl(); 203 cf.setClassResolver(cr); 204 205 i.setClasspathResourceFactory(cf); 206 207 HttpServlet servlet = new ServletFixture(); 208 209 MockControl configControl = newControl(ServletConfig .class); 210 ServletConfig config = (ServletConfig ) configControl.getMock(); 211 212 trainForServletInit(configControl, config); 213 214 config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM); 215 configControl.setReturnValue(null); 216 217 config.getServletContext(); 218 configControl.setReturnValue(context); 219 220 config.getServletName(); 221 configControl.setReturnValue("barney"); 222 223 log.isDebugEnabled(); 224 logControl.setReturnValue(false); 225 226 Resource r = new ContextResource(context, "/WEB-INF/barney.application"); 227 228 context.getResource("/WEB-INF/barney/barney.application"); 229 contextControl.setReturnValue(null); 230 231 log.isDebugEnabled(); 232 logControl.setReturnValue(false); 233 234 context.getResource(r.getPath()); 235 contextControl.setReturnValue(new URL ("file:/context" + r.getPath())); 236 237 log.debug("Found " + r); 238 239 IApplicationSpecification as = new ApplicationSpecification(); 240 241 MockControl parserControl = newControl(ISpecificationParser.class); 242 ISpecificationParser parser = (ISpecificationParser) parserControl.getMock(); 243 244 i.setParser(parser); 245 246 parser.parseApplicationSpecification(r); 247 parserControl.setReturnValue(as); 248 249 ApplicationGlobals ag = new ApplicationGlobalsImpl(); 250 251 i.setGlobals(ag); 252 253 replayControls(); 254 255 servlet.init(config); 256 257 259 i.initialize(servlet); 260 261 verifyControls(); 262 } 263 264 private static class SmartApplicationSpecificationMatcher extends AbstractMatcher 265 { 266 267 protected boolean argumentMatches(Object expected, Object actual) 268 { 269 if (expected instanceof IApplicationSpecification) 270 { 271 IApplicationSpecification expectedSpec = (IApplicationSpecification) expected; 272 IApplicationSpecification actualSpec = (IApplicationSpecification) actual; 273 274 return expectedSpec.getName().equals(actualSpec.getName()) 275 && expectedSpec.getSpecificationLocation().equals( 276 actualSpec.getSpecificationLocation()); 277 } 278 279 return super.argumentMatches(expected, actual); 280 } 281 282 } 283 284 public void testNoAppSpec() throws Exception 285 { 286 DefaultClassResolver cr = new DefaultClassResolver(); 287 288 ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer(); 289 290 MockControl contextControl = newControl(ServletContext .class); 291 ServletContext context = (ServletContext ) contextControl.getMock(); 292 293 MockControl logControl = newControl(Log.class); 294 Log log = (Log) logControl.getMock(); 295 296 i.setLog(log); 297 298 ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl(); 299 cf.setClassResolver(cr); 300 301 i.setClasspathResourceFactory(cf); 302 303 HttpServlet servlet = new ServletFixture(); 304 305 MockControl configControl = newControl(ServletConfig .class); 306 ServletConfig config = (ServletConfig ) configControl.getMock(); 307 308 trainForServletInit(configControl, config); 309 310 config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM); 311 configControl.setReturnValue(null); 312 313 config.getServletContext(); 314 configControl.setReturnValue(context); 315 316 config.getServletName(); 317 configControl.setReturnValue("wilma"); 318 319 log.isDebugEnabled(); 320 logControl.setReturnValue(false); 321 322 context.getResource("/WEB-INF/wilma/wilma.application"); 323 contextControl.setReturnValue(null); 324 325 log.isDebugEnabled(); 326 logControl.setReturnValue(false); 327 328 context.getResource("/WEB-INF/wilma.application"); 329 contextControl.setReturnValue(null); 330 331 config.getServletName(); 332 configControl.setReturnValue("wilma"); 333 334 log.debug("Could not find an application specification for application servlet wilma."); 335 336 config.getServletName(); 337 configControl.setReturnValue("wilma"); 338 339 config.getServletContext(); 340 configControl.setReturnValue(context); 341 342 ApplicationGlobals ag = new ApplicationGlobalsImpl(); 343 344 i.setGlobals(ag); 345 346 replayControls(); 347 348 servlet.init(config); 349 350 352 i.initialize(servlet); 353 354 verifyControls(); 355 356 IApplicationSpecification as = ag.getSpecification(); 357 358 assertEquals("wilma", as.getName()); 359 assertEquals(new ContextResource(context, "/WEB-INF/wilma.application"), as 360 .getSpecificationLocation()); 361 } 362 363 366 public void testIntegration() throws Exception 367 { 368 MockControl contextControl = newControl(ServletContext .class); 369 ServletContext context = (ServletContext ) contextControl.getMock(); 370 371 HttpServlet servlet = new ServletFixture(); 372 373 MockControl configControl = MockControl.createControl(ServletConfig .class); 375 addControl(configControl); 376 ServletConfig config = (ServletConfig ) configControl.getMock(); 377 378 config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM); 379 configControl.setReturnValue(null); 380 381 config.getServletContext(); 382 configControl.setReturnValue(context, 2); 383 384 config.getServletName(); 385 configControl.setReturnValue("dino", 2); 386 387 context.log("dino: init"); 388 389 context.getResource("/WEB-INF/dino/dino.application"); 390 contextControl.setReturnValue(getClass().getResource("ParseApp.application"), 2); 391 392 replayControls(); 393 394 servlet.init(config); 395 396 Registry registry = RegistryBuilder.constructDefaultRegistry(); 397 398 ApplicationInitializer ai = (ApplicationInitializer) registry.getService( 399 "tapestry.init.ApplicationSpecificationInitializer", 400 ApplicationInitializer.class); 401 402 ai.initialize(servlet); 403 404 ApplicationGlobals ag = (ApplicationGlobals) registry.getService( 405 "tapestry.globals.ApplicationGlobals", 406 ApplicationGlobals.class); 407 408 assertEquals("ParseApp", ag.getSpecification().getName()); 409 410 verifyControls(); 411 412 } 413 } | Popular Tags |