001    // Copyright 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.portlet;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.net.URL;
020    
021    import org.apache.hivemind.Resource;
022    import org.apache.tapestry.BaseComponentTestCase;
023    import org.apache.tapestry.parse.ISpecificationParser;
024    import org.apache.tapestry.services.ApplicationGlobals;
025    import org.apache.tapestry.services.impl.ApplicationGlobalsImpl;
026    import org.apache.tapestry.spec.IApplicationSpecification;
027    import org.apache.tapestry.web.WebContext;
028    import org.apache.tapestry.web.WebContextResource;
029    import org.testng.annotations.Test;
030    
031    import javax.portlet.PortletConfig;
032    
033    /**
034     * Tests for {@link PortletApplicationSpecificationInitializer}.
035     * 
036     * @author Howard M. Lewis Ship
037     * @since 4.0
038     */
039    @Test(sequential=true)
040    public class TestPortletApplicationSpecificationInitializer extends BaseComponentTestCase
041    {
042        private PortletConfig newConfig(String name)
043        {
044            PortletConfig config = newMock(PortletConfig.class);
045            
046            expect(config.getPortletName()).andReturn(name);
047            
048            return config;
049        }
050    
051        private IApplicationSpecification newSpecification()
052        {
053            return newMock(IApplicationSpecification.class);
054        }
055    
056        private ISpecificationParser newParser(Resource input, IApplicationSpecification specification)
057        {
058            ISpecificationParser parser = newMock(ISpecificationParser.class);
059    
060            expect(parser.parseApplicationSpecification(input)).andReturn(specification);
061            
062            return parser;
063        }
064    
065        private ApplicationGlobals newGlobals()
066        {
067            return newMock(ApplicationGlobals.class);
068        }
069    
070        public void testFoundInSubdir() throws Exception
071        {
072            PortletConfig config = newConfig("myportlet");
073            
074            WebContext context = newMock(WebContext.class);
075    
076            IApplicationSpecification specification = newSpecification();
077    
078            // Any arbitrary file will work here.
079    
080            URL fakeURL = getClass().getResource("hivemodule.xml");
081    
082            expect(context.getResource("/WEB-INF/myportlet/myportlet.application")).andReturn(fakeURL);
083            
084            Resource expectedResource = new WebContextResource(context,
085                    "/WEB-INF/myportlet/myportlet.application");
086    
087            ISpecificationParser parser = newParser(expectedResource, specification);
088    
089            ApplicationGlobals globals = newGlobals();
090    
091            globals.storeSpecification(specification);
092    
093            replay();
094    
095            PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
096            init.setContext(context);
097            init.setGlobals(globals);
098            init.setParser(parser);
099    
100            init.initialize(config);
101    
102            verify();
103        }
104    
105        public void testFoundInRootDir() throws Exception
106        {
107            PortletConfig config = newConfig("myportlet");
108    
109            WebContext context = newMock(WebContext.class);
110    
111            IApplicationSpecification specification = newSpecification();
112    
113            // Any arbitrary file will work here.
114    
115            URL fakeURL = getClass().getResource("hivemodule.xml");
116    
117            expect(context.getResource("/WEB-INF/myportlet/myportlet.application")).andReturn(null);
118    
119            expect(context.getResource("/WEB-INF/myportlet.application")).andReturn(fakeURL);
120    
121            Resource expectedResource = new WebContextResource(context,
122                    "/WEB-INF/myportlet.application");
123    
124            ISpecificationParser parser = newParser(expectedResource, specification);
125    
126            ApplicationGlobals globals = newGlobals();
127    
128            globals.storeSpecification(specification);
129    
130            replay();
131    
132            PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
133            init.setContext(context);
134            init.setGlobals(globals);
135            init.setParser(parser);
136    
137            init.initialize(config);
138    
139            verify();
140        }
141    
142        public void testNotFound() throws Exception
143        {
144            PortletConfig config = newConfig("myportlet");
145    
146            WebContext context = newMock(WebContext.class);
147    
148            expect(context.getResource("/WEB-INF/myportlet/myportlet.application")).andReturn(null);
149            
150            expect(context.getResource("/WEB-INF/myportlet.application")).andReturn(null);
151            
152            replay();
153    
154            ApplicationGlobals globals = new ApplicationGlobalsImpl();
155    
156            PortletApplicationSpecificationInitializer init = new PortletApplicationSpecificationInitializer();
157            init.setContext(context);
158            init.setGlobals(globals);
159    
160            init.initialize(config);
161    
162            verify();
163    
164            IApplicationSpecification spec = globals.getSpecification();
165    
166            assertEquals("myportlet", spec.getName());
167            assertEquals(new WebContextResource(context, "/WEB-INF/myportlet.application"), spec
168                    .getSpecificationLocation());
169        }
170    }