View Javadoc

1   /*
2    * $Id: TestTilesPlugin.java 267476 2005-09-03 15:56:27Z niallp $ 
3    *
4    * Copyright 1999-2005 The Apache Software Foundation.
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  
20  package org.apache.struts.tiles;
21  
22  
23  import javax.servlet.ServletException;
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import java.util.Map;
28  import java.util.HashMap;
29  import java.util.Locale;
30  import javax.servlet.ServletContext;
31  import javax.servlet.ServletRequest;
32  import org.apache.commons.beanutils.BeanUtils;
33  import org.apache.struts.Globals;
34  import org.apache.struts.action.PlugIn;
35  import org.apache.struts.config.ModuleConfig;
36  import org.apache.struts.config.ModuleConfigFactory;
37  import org.apache.struts.config.PlugInConfig;
38  import org.apache.struts.mock.MockActionServlet;
39  import org.apache.struts.mock.TestMockBase;
40  import org.apache.struts.util.RequestUtils;
41  import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
42  
43  /***
44   * <p>Unit tests for <code>org.apache.struts.tiles.*</code>.</p>
45   *
46   * @version $Rev: 267476 $ $Date: 2005-09-03 08:56:27 -0700 (Sat, 03 Sep 2005) $
47   */
48  
49  public class TestTilesPlugin extends TestMockBase {
50  
51  
52    protected ModuleConfig module1;
53    protected ModuleConfig module2;
54    protected MockActionServlet actionServlet;
55  
56      // ----------------------------------------------------------------- Basics
57  
58  
59      public TestTilesPlugin(String name) {
60          super(name);
61      }
62  
63  
64      public static void main(String args[]) {
65          junit.awtui.TestRunner.main
66              (new String[] { TestTilesPlugin.class.getName() } );
67      }
68  
69  
70      public static Test suite() {
71          return (new TestSuite(TestTilesPlugin.class));
72      }
73  
74  
75      // ----------------------------------------------------- Instance Variables
76  
77  
78  
79      // ----------------------------------------------------- Setup and Teardown
80  
81  
82      public void setUp()
83      {
84  
85      super.setUp();
86      TilesUtil.testReset();
87      actionServlet = new MockActionServlet(context, config);
88      }
89  
90  
91      public void tearDown() {
92  
93          super.tearDown();
94  
95      }
96  
97  
98      // ------------------------------------------------------- Individual Tests
99  
100 
101     /***
102      * Create a module configuration
103      * @param moduleName
104      */
105     public ModuleConfig createModuleConfig(
106     	String moduleName,
107     	String configFileName,
108     	boolean moduleAware) {
109             
110     	ModuleConfig moduleConfig =
111     		ModuleConfigFactory.createFactory().createModuleConfig(moduleName);
112             
113     	context.setAttribute(Globals.MODULE_KEY + moduleName, moduleConfig);
114     
115     	// Set tiles plugin
116     	PlugInConfig pluginConfig = new PlugInConfig();
117     	pluginConfig.setClassName("org.apache.struts.tiles.TilesPlugin");
118         
119     	pluginConfig.addProperty(
120     		"moduleAware",
121     		(moduleAware == true ? "true" : "false"));
122             
123     	pluginConfig.addProperty(
124     		"definitions-config",
125     		"/org/apache/struts/tiles/config/" + configFileName);
126             
127     	moduleConfig.addPlugInConfig(pluginConfig);
128     	return moduleConfig;
129     }
130 
131     /***
132      * Fake call to init module plugins
133      * @param config
134      */
135   public void initModulePlugIns( ModuleConfig moduleConfig)
136   {
137   PlugInConfig plugInConfigs[] = moduleConfig.findPlugInConfigs();
138   PlugIn plugIns[] = new PlugIn[plugInConfigs.length];
139 
140   context.setAttribute(Globals.PLUG_INS_KEY + moduleConfig.getPrefix(), plugIns);
141   for (int i = 0; i < plugIns.length; i++) {
142       try {
143           plugIns[i] =
144               (PlugIn)RequestUtils.applicationInstance(plugInConfigs[i].getClassName());
145           BeanUtils.populate(plugIns[i], plugInConfigs[i].getProperties());
146             // Pass the current plugIn config object to the PlugIn.
147             // The property is set only if the plugin declares it.
148             // This plugin config object is needed by Tiles
149           BeanUtils.copyProperty( plugIns[i], "currentPlugInConfigObject", plugInConfigs[i]);
150           plugIns[i].init(actionServlet, moduleConfig);
151       } catch (ServletException e) {
152           // Lets propagate
153           e.printStackTrace();
154           //throw e;
155       } catch (Exception e) {
156           e.printStackTrace();
157           //throw e;
158       }
159   }
160   }
161 
162     // ---------------------------------------------------------- absoluteURL()
163 
164 
165     /***
166      * Test multi factory creation when moduleAware=true.
167      */
168     public void testMultiFactory() {
169     	// init TilesPlugin
170     	module1 = createModuleConfig("/module1", "tiles-defs.xml", true);
171     	module2 = createModuleConfig("/module2", "tiles-defs.xml", true);
172     	initModulePlugIns(module1);
173     	initModulePlugIns(module2);
174     
175     	// mock request context
176     	request.setAttribute(Globals.MODULE_KEY, module1);
177     	request.setPathElements("/myapp", "/module1/foo.do", null, null);
178     	// Retrieve factory for module1
179     	DefinitionsFactory factory1 =
180     		TilesUtil.getDefinitionsFactory(request, context);
181             
182     	assertNotNull("factory found", factory1);
183     	assertEquals(
184     		"factory name",
185     		"/module1",
186     		factory1.getConfig().getFactoryName());
187     
188     	// mock request context
189     	request.setAttribute(Globals.MODULE_KEY, module2);
190     	request.setPathElements("/myapp", "/module2/foo.do", null, null);
191     	// Retrieve factory for module2
192     	DefinitionsFactory factory2 =
193     		TilesUtil.getDefinitionsFactory(request, context);
194     	assertNotNull("factory found", factory2);
195     	assertEquals(
196     		"factory name",
197     		"/module2",
198     		factory2.getConfig().getFactoryName());
199     
200     	// Check that factory are different
201     	assertNotSame("Factory from different modules", factory1, factory2);
202     }
203 
204     /***
205      * Test single factory creation when moduleAware=false.
206      */
207   public void testSingleSharedFactory()
208   {
209     // init TilesPlugin
210   module1 = createModuleConfig( "/module1", "tiles-defs.xml", false );
211   module2 = createModuleConfig( "/module2", "tiles-defs.xml", false );
212   initModulePlugIns(module1);
213   initModulePlugIns(module2);
214 
215     // mock request context
216   request.setAttribute(Globals.MODULE_KEY, module1);
217   request.setPathElements("/myapp", "/module1/foo.do", null, null);
218     // Retrieve factory for module1
219   DefinitionsFactory factory1 = TilesUtil.getDefinitionsFactory( request, context);
220   assertNotNull( "factory found", factory1);
221   assertEquals( "factory name", "/module1", factory1.getConfig().getFactoryName() );
222 
223     // mock request context
224   request.setAttribute(Globals.MODULE_KEY, module2);
225   request.setPathElements("/myapp", "/module2/foo.do", null, null);
226     // Retrieve factory for module2
227   DefinitionsFactory factory2 = TilesUtil.getDefinitionsFactory( request, context);
228   assertNotNull( "factory found", factory2);
229   assertEquals( "factory name", "/module1", factory2.getConfig().getFactoryName() );
230 
231     // Check that factory are different
232   assertEquals("Same factory", factory1, factory2);
233   }
234 
235   /***
236    * Test I18nFactorySet.
237    */
238   public void testI18FactorySet_A() {
239 
240      Locale locale = null;
241      ComponentDefinition definition = null;
242      org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
243 
244      Map properties = new HashMap();
245 
246      // Set the file name
247      properties.put(I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
248                     "config/I18nFactorySet-A.xml");
249 
250      try {
251          CustomI18nFactorySet i18nFactorySet = new CustomI18nFactorySet(context, properties);
252          String defName = "A-DEFAULT";
253 
254          // Default Locale
255          locale = new Locale("", "", "");
256          factory = i18nFactorySet.createFactory(locale , request, context);
257          assertNotNull("DefinitionsFactory is nullfor locale='" + print(locale) + "'", factory);
258          definition = factory.getDefinition(defName, request, context);
259          assertNotNull("Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
260          assertEquals("Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
261 
262          // Variant Only
263          locale = new Locale("", "", "XX");
264          factory = i18nFactorySet.createFactory(locale , request, context);
265          assertNotNull("DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
266          definition = factory.getDefinition(defName, request, context);
267          assertNotNull("Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
268          assertEquals("Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
269 
270          // No Language, Country & Variant Locale
271          locale = new Locale("", "US", "XX");
272          factory = i18nFactorySet.createFactory(locale , request, context);
273          assertNotNull("DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
274          definition = factory.getDefinition(defName, request, context);
275          assertNotNull("Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
276          assertEquals("Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
277 
278          // Language & Country
279          locale = new Locale("en", "US");
280          factory = i18nFactorySet.createFactory(locale , request, context);
281          assertNotNull("DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
282          definition = factory.getDefinition(defName, request, context);
283          assertNotNull("Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
284          assertEquals("Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
285 
286      } catch(Exception ex) {
287          fail(ex.toString());
288      }
289   }
290 
291 
292   /***
293    * Test I18nFactorySet.
294    */
295   public void testI18FactorySet_B() {
296 
297      Locale locale = null;
298      ComponentDefinition definition = null;
299      org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
300 
301      Map properties = new HashMap();
302 
303      // Set the file name
304      properties.put(I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
305                     "config/I18nFactorySet-B.xml");
306 
307      try {
308 
309          CustomI18nFactorySet i18nFactorySet = new CustomI18nFactorySet(context, properties);
310          String defName = null;
311 
312          // Default Locale
313          locale = new Locale("", "", "");
314          factory = i18nFactorySet.createFactory(locale , request, context);
315          assertNotNull("1. DefinitionsFactory is nullfor locale='" + print(locale) + "'", factory);
316          defName = "B-DEFAULT";
317          definition = factory.getDefinition(defName, request, context);
318          assertNotNull("2. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
319          assertEquals("3. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
320 
321          // Variant Only
322          locale = new Locale("", "", "XX");
323          factory = i18nFactorySet.createFactory(locale , request, context);
324          assertNotNull("4. DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
325          defName = "B___XX";
326          definition = factory.getDefinition(defName, request, context);
327          assertNotNull("5. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
328          assertEquals("6. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
329          defName = "B-DEFAULT";
330          definition = factory.getDefinition(defName, request, context);
331          assertNotNull("7. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
332          assertEquals("8. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
333 
334          // No Language, Country & Unknown Variant
335          locale = new Locale("", "US", "XX");
336          factory = i18nFactorySet.createFactory(locale , request, context);
337          assertNotNull("9. DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
338          defName = "B__US";
339          definition = factory.getDefinition(defName, request, context);
340          assertNotNull("10. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
341          assertEquals("11. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
342 
343          // Language & Country
344          locale = new Locale("en", "US");
345          factory = i18nFactorySet.createFactory(locale , request, context);
346          assertNotNull("12. DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
347          defName = "B_en_US";
348          definition = factory.getDefinition(defName, request, context);
349          assertNotNull("13. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
350          assertEquals("14. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
351 
352          // Language, Country & Unknown Variant
353          locale = new Locale("en", "GB", "XX");
354          factory = i18nFactorySet.createFactory(locale , request, context);
355          assertNotNull("15. DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
356          defName = "B_en_GB";
357          definition = factory.getDefinition(defName, request, context);
358          assertNotNull("16. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
359          assertEquals("17. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
360 
361          // Language, Unknown Country & Unknown Variant
362          locale = new Locale("en", "FR", "XX");
363          factory = i18nFactorySet.createFactory(locale , request, context);
364          assertNotNull("18. DefinitionsFactory is null for locale='" + print(locale) + "'", factory);
365          defName = "B_en";
366          definition = factory.getDefinition(defName, request, context);
367          assertNotNull("19. Definition '" + defName + "' Not Found for locale='" + print(locale) + "'", definition);
368          assertEquals("20. Definition '" + defName + "' for locale='" + print(locale) + "'", defName, definition.getName());
369 
370      } catch(Exception ex) {
371          fail(ex.toString());
372      }
373 
374   }
375 
376   /***
377    * String representation of a Locale. A bug in the
378    * Locale.toString() method results in Locales with
379    * just a variant being incorrectly displayed.
380    */
381   private String print(Locale locale) {
382          
383       return locale.getLanguage() + "_" +
384                 locale.getCountry() + "_" +
385                 locale.getVariant();
386   }
387 
388 
389 
390 }
391