View Javadoc

1   /*
2    * $Id: KeyedDefinitionsFactoryTilesContainerTest.java 573651 2007-09-07 18:09:41Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.impl;
22  
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Vector;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpSession;
33  
34  import junit.framework.TestCase;
35  
36  import org.apache.tiles.TilesException;
37  import org.apache.tiles.definition.DefinitionsFactory;
38  import org.apache.tiles.factory.KeyedDefinitionsFactoryTilesContainerFactory;
39  import org.apache.tiles.factory.TilesContainerFactory;
40  import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.DefaultKeyExtractor;
41  import org.apache.tiles.locale.impl.DefaultLocaleResolver;
42  import org.apache.tiles.util.RollingVectorEnumeration;
43  import org.easymock.EasyMock;
44  
45  
46  /***
47   * @version $Rev: 573651 $ $Date: 2007-09-07 20:09:41 +0200 (Fri, 07 Sep 2007) $
48   */
49  public class KeyedDefinitionsFactoryTilesContainerTest extends TestCase {
50  
51      /***
52       * The Tiles container.
53       */
54      private KeyedDefinitionsFactoryTilesContainer container;
55  
56      /***
57       * Default configuration parameters.
58       */
59      private Map<String, String> defaults;
60  
61      /*** {@inheritDoc} */
62      @Override
63      public void setUp() {
64          defaults = new HashMap<String, String>();
65          defaults.put(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM,
66                  KeyedDefinitionsFactoryTilesContainerFactory.class.getName());
67  
68          ServletContext context = EasyMock.createMock(ServletContext.class);
69  
70          Vector<String> v = new Vector<String>();
71          v.add(KeyedDefinitionsFactoryTilesContainerFactory.CONTAINER_KEYS_INIT_PARAM);
72          v.add(KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
73                  + "one");
74          v.add(KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
75                  + "two");
76  
77          EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
78          EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null);
79          EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null);
80          EasyMock.expect(context.getInitParameter(
81                  KeyedDefinitionsFactoryTilesContainerFactory.CONTAINER_KEYS_INIT_PARAM))
82                  .andReturn("one,two").anyTimes();
83          EasyMock.expect(context.getInitParameter(
84                  KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
85                  + "one")).andReturn("/WEB-INF/tiles-one.xml").anyTimes();
86          EasyMock.expect(context.getInitParameter(
87                  KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX
88                  + "two")).andReturn("/WEB-INF/tiles-two.xml").anyTimes();
89          EasyMock.expect(context.getInitParameter(EasyMock.isA(String.class))).andReturn(null).anyTimes();
90          EasyMock.expect(context.getInitParameterNames()).andReturn(new RollingVectorEnumeration<String>(v)).anyTimes();
91          try {
92              URL url = getClass().getResource("/org/apache/tiles/factory/test-defs.xml");
93              EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url);
94              url = getClass().getResource("/org/apache/tiles/factory/test-defs-key-one.xml");
95              EasyMock.expect(context.getResource("/WEB-INF/tiles-one.xml")).andReturn(url);
96              url = getClass().getResource("/org/apache/tiles/factory/test-defs-key-two.xml");
97              EasyMock.expect(context.getResource("/WEB-INF/tiles-two.xml")).andReturn(url);
98          } catch (MalformedURLException e) {
99              throw new RuntimeException("Error getting Tiles configuration URL",
100                     e);
101         }
102         EasyMock.replay(context);
103         try {
104             TilesContainerFactory factory = TilesContainerFactory.getFactory(context, defaults);
105             container = (KeyedDefinitionsFactoryTilesContainer) factory.createContainer(context);
106         } catch (TilesException e) {
107             throw new RuntimeException("Error initializing factory", e);
108         }
109     }
110 
111     /***
112      * Tests container initialization.
113      */
114     public void testInitialization() {
115         assertNotNull(container);
116         assertNotNull(container.getContextFactory());
117         assertNotNull(container.getPreparerFactory());
118         assertNotNull(container.getDefinitionsFactory());
119         assertNotNull(container.getProperDefinitionsFactory("one"));
120         assertNotNull(container.getProperDefinitionsFactory("two"));
121     }
122 
123     /***
124      * Tests initialization for postponed definitions factories.
125      *
126      * @throws MalformedURLException If sources are not valid (that should not
127      * happen).
128      * @throws TilesException If something goes wrong.
129      */
130     public void testPostponedDefinitionsFactoryInitialization() throws MalformedURLException, TilesException {
131         KeyedDefinitionsFactoryTilesContainer container;
132         ServletContext context = EasyMock.createMock(ServletContext.class);
133 
134         Vector<String> v = new Vector<String>();
135 
136         EasyMock.reset(context);
137         EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTAINER_FACTORY_INIT_PARAM)).andReturn(null);
138         EasyMock.expect(context.getInitParameter(TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM)).andReturn(null);
139         EasyMock.expect(context.getInitParameter(TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM)).andReturn(null);
140         EasyMock.expect(context.getInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG)).andReturn(null);
141         EasyMock.expect(context.getInitParameter("definitions-config")).andReturn(null);
142         EasyMock.expect(context.getInitParameter(TilesContainerFactory
143                 .CONTAINER_FACTORY_MUTABLE_INIT_PARAM)).andReturn(null);
144         EasyMock.expect(context.getInitParameter(
145                 KeyedDefinitionsFactoryTilesContainerFactory.CONTAINER_KEYS_INIT_PARAM))
146                 .andReturn(null);
147         URL url = getClass().getResource("/org/apache/tiles/factory/test-defs.xml");
148         EasyMock.expect(context.getResource("/WEB-INF/tiles.xml")).andReturn(url);
149         url = getClass().getResource("/org/apache/tiles/factory/test-defs-key-one.xml");
150         EasyMock.expect(context.getResource("/WEB-INF/tiles-one.xml")).andReturn(url);
151         url = getClass().getResource("/org/apache/tiles/factory/test-defs-key-two.xml");
152         EasyMock.expect(context.getResource("/WEB-INF/tiles-two.xml")).andReturn(url);
153         EasyMock.expect(context.getInitParameterNames()).andReturn(v.elements()).anyTimes();
154         EasyMock.replay(context);
155         KeyedDefinitionsFactoryTilesContainerFactory factory =
156             (KeyedDefinitionsFactoryTilesContainerFactory)
157             TilesContainerFactory.getFactory(context, defaults);
158         container = (KeyedDefinitionsFactoryTilesContainer) factory.createContainer(context);
159 
160         assertNotNull(container);
161         assertNotNull(container.getDefinitionsFactory());
162         assertNull(container.getProperDefinitionsFactory("one"));
163         assertNull(container.getProperDefinitionsFactory("two"));
164 
165         Map<String, String> initParams = new HashMap<String, String>();
166         initParams.put(BasicTilesContainer.DEFINITIONS_CONFIG,
167                 "/WEB-INF/tiles-one.xml");
168         DefinitionsFactory defsFactory = factory.createDefinitionsFactory(context);
169         container.setDefinitionsFactory("one", defsFactory, initParams);
170         initParams.put(BasicTilesContainer.DEFINITIONS_CONFIG,
171                 "/WEB-INF/tiles-two.xml");
172         defsFactory = factory.createDefinitionsFactory(context);
173         container.setDefinitionsFactory("two", defsFactory, initParams);
174         assertNotNull(container.getProperDefinitionsFactory("one"));
175         assertNotNull(container.getProperDefinitionsFactory("two"));
176     }
177 
178     /***
179      * Tests if the definitions factory has been used.
180      */
181     public void testDefinitionsFactoryUse() {
182         HttpServletRequest request = EasyMock.createMock(
183                 HttpServletRequest.class);
184         HttpSession session = EasyMock.createMock(HttpSession.class);
185         HttpServletResponse response = EasyMock.createMock(
186                 HttpServletResponse.class);
187 
188         EasyMock.reset(request);
189         EasyMock.reset(session);
190         EasyMock.reset(response);
191         EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
192         EasyMock.expect(session.getAttribute(DefaultLocaleResolver.LOCALE_KEY)).andReturn(null).anyTimes();
193         EasyMock.expect(request.getLocale()).andReturn(null).anyTimes();
194         EasyMock.expect(request.getAttribute(
195                 DefaultKeyExtractor.DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME))
196                 .andReturn(null).anyTimes();
197         EasyMock.replay(request);
198         EasyMock.replay(session);
199         EasyMock.replay(response);
200         assertTrue(container.isValidDefinition("test.def1", request, response));
201         assertFalse(container.isValidDefinition("test.def.one", request, response));
202         assertFalse(container.isValidDefinition("test.def.two", request, response));
203 
204         EasyMock.reset(request);
205         EasyMock.reset(session);
206         EasyMock.reset(response);
207         EasyMock.expect(request.getAttribute(
208                 DefaultKeyExtractor.DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME))
209                 .andReturn("one").anyTimes();
210         EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
211         EasyMock.expect(session.getAttribute(DefaultLocaleResolver.LOCALE_KEY)).andReturn(null).anyTimes();
212         EasyMock.expect(request.getLocale()).andReturn(null).anyTimes();
213         EasyMock.replay(request);
214         EasyMock.replay(session);
215         EasyMock.replay(response);
216         assertTrue(container.isValidDefinition("test.def1", request, response));
217         assertTrue(container.isValidDefinition("test.def.one", request, response));
218         assertFalse(container.isValidDefinition("test.def.two", request, response));
219 
220         EasyMock.reset(request);
221         EasyMock.reset(session);
222         EasyMock.reset(response);
223         EasyMock.expect(request.getAttribute(
224                 DefaultKeyExtractor.DEFINITIONS_FACTORY_KEY_ATTRIBUTE_NAME))
225                 .andReturn("two").anyTimes();
226         EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
227         EasyMock.expect(session.getAttribute(DefaultLocaleResolver.LOCALE_KEY)).andReturn(null).anyTimes();
228         EasyMock.expect(request.getLocale()).andReturn(null).anyTimes();
229         EasyMock.replay(request);
230         EasyMock.replay(session);
231         EasyMock.replay(response);
232         assertTrue(container.isValidDefinition("test.def1", request, response));
233         assertFalse(container.isValidDefinition("test.def.one", request, response));
234         assertTrue(container.isValidDefinition("test.def.two", request, response));
235     }
236 }