001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.services.impl;
016    
017    import static org.easymock.EasyMock.*;
018    
019    import java.net.URL;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.hivemind.Registry;
023    import org.apache.hivemind.Resource;
024    import org.apache.hivemind.impl.DefaultClassResolver;
025    import org.apache.hivemind.impl.RegistryBuilder;
026    import org.apache.hivemind.util.ClasspathResource;
027    import org.apache.hivemind.util.ContextResource;
028    import org.apache.tapestry.BaseComponentTestCase;
029    import org.apache.tapestry.parse.ISpecificationParser;
030    import org.apache.tapestry.services.ApplicationGlobals;
031    import org.apache.tapestry.services.ApplicationInitializer;
032    import org.apache.tapestry.spec.ApplicationSpecification;
033    import org.apache.tapestry.spec.IApplicationSpecification;
034    import org.testng.annotations.Test;
035    
036    import javax.servlet.ServletConfig;
037    import javax.servlet.ServletContext;
038    import javax.servlet.http.HttpServlet;
039    
040    /**
041     * Tests for the {@link org.apache.tapestry.services.impl.ApplicationSpecificationInitializer}.
042     * 
043     * @author Howard Lewis Ship
044     * @since 4.0
045     */
046    @Test
047    public class TestApplicationSpecificationInitializer extends BaseComponentTestCase
048    {   
049        
050        public void test_On_Classpath() throws Exception
051        {
052            DefaultClassResolver cr = new DefaultClassResolver();
053    
054            Resource appSpecResource = new ClasspathResource(cr, "/foo/OnClasspath.application");
055    
056            ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer();
057    
058            Log log = newMock(Log.class);
059    
060            i.setLog(log);
061    
062            ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl();
063            cf.setClassResolver(cr);
064    
065            i.setClasspathResourceFactory(cf);
066            
067            HttpServlet servlet = new ServletFixture();
068            
069            ServletConfig config = newMock(ServletConfig.class);
070            
071            IApplicationSpecification as = new ApplicationSpecification();
072            
073            ISpecificationParser parser = newMock(ISpecificationParser.class);
074            
075            i.setParser(parser);
076            
077            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM))
078            .andReturn(appSpecResource.getPath());
079            
080            expect(parser.parseApplicationSpecification(appSpecResource)).andReturn(as);
081            
082            ApplicationGlobals ag = new ApplicationGlobalsImpl();
083    
084            i.setGlobals(ag);
085    
086            replay();
087    
088            servlet.init(config);
089    
090            // The real ApplicationServlet will build a Registry and, indirectly, invoke this.
091    
092            i.initialize(servlet);
093    
094            assertNotNull(ag.getActivator());
095            assertSame(as, ag.getSpecification());
096    
097            verify();
098        }
099    
100        public void test_Default_On_Classpath() throws Exception
101        {
102            DefaultClassResolver cr = new DefaultClassResolver();
103            Resource appSpecResource = new ClasspathResource(cr, "Fred.application");
104            
105            ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer();
106            Log log = newMock(Log.class);
107            i.setLog(log);
108            
109            ServletContext context = newMock(ServletContext.class);
110            
111            ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl();
112            cf.setClassResolver(cr);
113    
114            i.setClasspathResourceFactory(cf);
115            
116            HttpServlet servlet = new ServletFixture();
117            
118            ServletConfig config = newMock(ServletConfig.class);
119            checkOrder(config, false);
120            
121            IApplicationSpecification as = new ApplicationSpecification();
122            
123            ISpecificationParser parser = newMock(ISpecificationParser.class);
124            
125            i.setParser(parser);
126            
127            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM)).andReturn(null);
128            
129            expect(config.getServletContext()).andReturn(context);
130            
131            expect(config.getServletName()).andReturn("Fred").anyTimes();
132            
133            // begin testing finding spec
134            
135            expect(log.isDebugEnabled()).andReturn(true);
136            
137            Resource r = new ContextResource(context, "/WEB-INF/Fred/Fred.application");
138            
139            log.debug("Checking for existence of " + r);
140            
141            expect(context.getResource(r.getPath())).andReturn(null);
142            
143            expect(log.isDebugEnabled()).andReturn(true);
144            
145            r = new ContextResource(context, "/WEB-INF/Fred.application");
146            
147            log.debug("Checking for existence of " + r);
148            
149            expect(context.getResource(r.getPath())).andReturn(null);
150            
151            expect(parser.parseApplicationSpecification(appSpecResource)).andReturn(as);
152            
153            ApplicationGlobals ag = new ApplicationGlobalsImpl();
154    
155            i.setGlobals(ag);
156    
157            replay();
158            
159            servlet.init(config);
160            
161            // The real ApplicationServlet will build a Registry and, indirectly, invoke this.
162            
163            i.initialize(servlet);
164            
165            assertNotNull(ag.getActivator());
166            assertSame(as, ag.getSpecification());
167    
168            verify();
169        }
170        
171        public void test_In_App_Context_Folder() throws Exception
172        {
173            DefaultClassResolver cr = new DefaultClassResolver();
174    
175            ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer();
176            
177            ServletContext context = newMock(ServletContext.class);
178            Log log = newLog();
179    
180            i.setLog(log);
181            
182            ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl();
183            cf.setClassResolver(cr);
184            
185            i.setClasspathResourceFactory(cf);
186            
187            HttpServlet servlet = new ServletFixture();
188            
189            ServletConfig config = newMock(ServletConfig.class);
190            
191            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM))
192            .andReturn(null);
193            
194            expect(config.getServletContext()).andReturn(context);
195    
196            expect(config.getServletName()).andReturn("fred");
197    
198            expect(log.isDebugEnabled()).andReturn(true);
199    
200            Resource r = new ContextResource(context, "/WEB-INF/fred/fred.application");
201    
202            log.debug("Checking for existence of " + r);
203    
204            expect(context.getResource(r.getPath()))
205            .andReturn(new URL("file:/context" + r.getPath()));
206    
207            log.debug("Found " + r);
208    
209            IApplicationSpecification as = new ApplicationSpecification();
210            
211            ISpecificationParser parser = newMock(ISpecificationParser.class);
212    
213            i.setParser(parser);
214    
215            expect(parser.parseApplicationSpecification(r)).andReturn(as);
216    
217            ApplicationGlobals ag = new ApplicationGlobalsImpl();
218    
219            i.setGlobals(ag);
220    
221            replay();
222    
223            servlet.init(config);
224    
225            // The real ApplicationServlet will build a Registry and, indirectly, invoke this.
226    
227            i.initialize(servlet);
228    
229            verify();
230        }
231    
232        public void test_In_Web_Inf_Folder() throws Exception
233        {
234            DefaultClassResolver cr = new DefaultClassResolver();
235    
236            ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer();
237    
238            ServletContext context = newMock(ServletContext.class);
239            Log log = newLog();
240    
241            i.setLog(log);
242    
243            ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl();
244            cf.setClassResolver(cr);
245    
246            i.setClasspathResourceFactory(cf);
247    
248            HttpServlet servlet = new ServletFixture();
249    
250            ServletConfig config = newMock(ServletConfig.class);
251            
252            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM))
253            .andReturn(null);
254    
255            expect(config.getServletContext()).andReturn(context);
256    
257            expect(config.getServletName()).andReturn("barney");
258    
259            expect(log.isDebugEnabled()).andReturn(false);
260    
261            Resource r = new ContextResource(context, "/WEB-INF/barney.application");
262    
263            expect(context.getResource("/WEB-INF/barney/barney.application")).andReturn(null);
264    
265            expect(log.isDebugEnabled()).andReturn(false);
266    
267            expect(context.getResource(r.getPath()))
268            .andReturn(new URL("file:/context" + r.getPath()));
269    
270            log.debug("Found " + r);
271    
272            IApplicationSpecification as = new ApplicationSpecification();
273            
274            ISpecificationParser parser = newMock(ISpecificationParser.class);
275    
276            i.setParser(parser);
277    
278            expect(parser.parseApplicationSpecification(r)).andReturn(as);
279    
280            ApplicationGlobals ag = new ApplicationGlobalsImpl();
281    
282            i.setGlobals(ag);
283    
284            replay();
285    
286            servlet.init(config);
287    
288            // The real ApplicationServlet will build a Registry and, indirectly, invoke this.
289    
290            i.initialize(servlet);
291    
292            verify();
293        }
294    
295        public void test_No_App_Spec() throws Exception
296        {
297            DefaultClassResolver cr = new DefaultClassResolver();
298    
299            ApplicationSpecificationInitializer i = new ApplicationSpecificationInitializer();
300    
301            ServletContext context = newMock(ServletContext.class);
302            Log log = newLog();
303    
304            i.setLog(log);
305    
306            ClasspathResourceFactoryImpl cf = new ClasspathResourceFactoryImpl();
307            cf.setClassResolver(cr);
308    
309            i.setClasspathResourceFactory(cf);
310    
311            HttpServlet servlet = new ServletFixture();
312    
313            ServletConfig config = newMock(ServletConfig.class);
314    
315            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM)).andReturn(null);
316    
317            expect(config.getServletContext()).andReturn(context);
318    
319            expect(config.getServletName()).andReturn("wilma");
320    
321            expect(log.isDebugEnabled()).andReturn(false);
322    
323            expect(context.getResource("/WEB-INF/wilma/wilma.application")).andReturn(null);
324    
325            expect(log.isDebugEnabled()).andReturn(false);
326    
327            expect(context.getResource("/WEB-INF/wilma.application")).andReturn(null);
328    
329            expect(config.getServletName()).andReturn("wilma");
330    
331            log.warn("Could not find an application specification for application servlet wilma.");
332    
333            expect(config.getServletName()).andReturn("wilma");
334    
335            expect(config.getServletContext()).andReturn(context);
336    
337            ApplicationGlobals ag = new ApplicationGlobalsImpl();
338    
339            i.setGlobals(ag);
340    
341            replay();
342    
343            servlet.init(config);
344    
345            // The real ApplicationServlet will build a Registry and, indirectly, invoke this.
346    
347            i.initialize(servlet);
348    
349            verify();
350    
351            IApplicationSpecification as = ag.getSpecification();
352    
353            assertEquals("wilma", as.getName());
354            assertEquals(new ContextResource(context, "/WEB-INF/wilma.application"), as
355                    .getSpecificationLocation());
356        }
357    
358        /**
359         * Test within the Registry, to ensure the module deployment descriptor is well configured.
360         */
361        public void test_Integration() throws Exception
362        {
363            ServletContext context = newMock(ServletContext.class);
364    
365            HttpServlet servlet = new ServletFixture();
366            
367            ServletConfig config = newMock(ServletConfig.class);
368            
369            expect(config.getInitParameter(ApplicationSpecificationInitializer.APP_SPEC_PATH_PARAM))
370            .andReturn(null);
371            
372            expect(config.getServletContext()).andReturn(context).anyTimes();
373            
374            expect(config.getServletName()).andReturn("dino").anyTimes();
375            
376            expect(context.getResource("/WEB-INF/dino/dino.application"))
377            .andReturn(getClass().getResource("ParseApp.application")).times(2);
378            
379            replay();
380    
381            servlet.init(config);
382    
383            Registry registry = RegistryBuilder.constructDefaultRegistry();
384    
385            ApplicationInitializer ai = (ApplicationInitializer) registry.getService(
386                    "tapestry.init.ApplicationSpecificationInitializer",
387                    ApplicationInitializer.class);
388    
389            ai.initialize(servlet);
390    
391            ApplicationGlobals ag = (ApplicationGlobals) registry.getService(
392                    "tapestry.globals.ApplicationGlobals",
393                    ApplicationGlobals.class);
394    
395            assertEquals("ParseApp", ag.getSpecification().getName());
396    
397            verify();
398            
399            registry.shutdown();
400        }
401    }