001: /*
002: * $Id: TestTilesPlugin.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.tiles;
023:
024: import org.apache.struts.config.ModuleConfig;
025: import org.apache.struts.config.ModuleConfigFactory;
026: import org.apache.struts.config.PlugInConfig;
027: import org.apache.struts.mock.MockActionServlet;
028: import org.apache.struts.mock.TestMockBase;
029: import org.apache.struts.Globals;
030: import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
031: import org.apache.struts.util.RequestUtils;
032: import org.apache.struts.action.PlugIn;
033: import org.apache.commons.beanutils.BeanUtils;
034: import junit.framework.Test;
035: import junit.framework.TestSuite;
036:
037: import javax.servlet.ServletException;
038: import java.util.Locale;
039: import java.util.Map;
040: import java.util.HashMap;
041:
042: public class TestTilesPlugin extends TestMockBase {
043:
044: protected ModuleConfig module1;
045: protected ModuleConfig module2;
046: protected MockActionServlet actionServlet;
047:
048: // ----------------------------------------------------------------- Basics
049:
050: public TestTilesPlugin(String name) {
051: super (name);
052: }
053:
054: public static void main(String args[]) {
055: junit.awtui.TestRunner
056: .main(new String[] { TestTilesPlugin.class.getName() });
057: }
058:
059: public static Test suite() {
060: return (new TestSuite(TestTilesPlugin.class));
061: }
062:
063: // ----------------------------------------------------- Instance Variables
064:
065: // ----------------------------------------------------- Setup and Teardown
066:
067: public void setUp() {
068:
069: super .setUp();
070: TilesUtil.testReset();
071: actionServlet = new MockActionServlet(context, config);
072: }
073:
074: public void tearDown() {
075:
076: super .tearDown();
077:
078: }
079:
080: // ------------------------------------------------------- Individual Tests
081:
082: /**
083: * Create a module configuration
084: * @param moduleName
085: */
086: public ModuleConfig createModuleConfig(String moduleName,
087: String configFileName, boolean moduleAware) {
088:
089: ModuleConfig moduleConfig = ModuleConfigFactory.createFactory()
090: .createModuleConfig(moduleName);
091:
092: context.setAttribute(Globals.MODULE_KEY + moduleName,
093: moduleConfig);
094:
095: // Set tiles plugin
096: PlugInConfig pluginConfig = new PlugInConfig();
097: pluginConfig
098: .setClassName("org.apache.struts.tiles.TilesPlugin");
099:
100: pluginConfig.addProperty("moduleAware",
101: (moduleAware == true ? "true" : "false"));
102:
103: pluginConfig.addProperty("definitions-config",
104: "/org/apache/struts/tiles/config/" + configFileName);
105:
106: moduleConfig.addPlugInConfig(pluginConfig);
107: return moduleConfig;
108: }
109:
110: /**
111: * Fake call to init module plugins
112: * @param moduleConfig
113: */
114: public void initModulePlugIns(ModuleConfig moduleConfig) {
115: PlugInConfig plugInConfigs[] = moduleConfig.findPlugInConfigs();
116: PlugIn plugIns[] = new PlugIn[plugInConfigs.length];
117:
118: context.setAttribute(Globals.PLUG_INS_KEY
119: + moduleConfig.getPrefix(), plugIns);
120: for (int i = 0; i < plugIns.length; i++) {
121: try {
122: plugIns[i] = (PlugIn) RequestUtils
123: .applicationInstance(plugInConfigs[i]
124: .getClassName());
125: BeanUtils.populate(plugIns[i], plugInConfigs[i]
126: .getProperties());
127: // Pass the current plugIn config object to the PlugIn.
128: // The property is set only if the plugin declares it.
129: // This plugin config object is needed by Tiles
130: BeanUtils.copyProperty(plugIns[i],
131: "currentPlugInConfigObject", plugInConfigs[i]);
132: plugIns[i].init(actionServlet, moduleConfig);
133: } catch (ServletException e) {
134: // Lets propagate
135: e.printStackTrace();
136: //throw e;
137: } catch (Exception e) {
138: e.printStackTrace();
139: //throw e;
140: }
141: }
142: }
143:
144: // ---------------------------------------------------------- absoluteURL()
145:
146: /**
147: * Test multi factory creation when moduleAware=true.
148: */
149: public void testMultiFactory() {
150: // init TilesPlugin
151: module1 = createModuleConfig("/module1", "tiles-defs.xml", true);
152: module2 = createModuleConfig("/module2", "tiles-defs.xml", true);
153: initModulePlugIns(module1);
154: initModulePlugIns(module2);
155:
156: // mock request context
157: request.setAttribute(Globals.MODULE_KEY, module1);
158: request
159: .setPathElements("/myapp", "/module1/foo.do", null,
160: null);
161: // Retrieve factory for module1
162: DefinitionsFactory factory1 = TilesUtil.getDefinitionsFactory(
163: request, context);
164:
165: assertNotNull("factory found", factory1);
166: assertEquals("factory name", "/module1", factory1.getConfig()
167: .getFactoryName());
168:
169: // mock request context
170: request.setAttribute(Globals.MODULE_KEY, module2);
171: request
172: .setPathElements("/myapp", "/module2/foo.do", null,
173: null);
174: // Retrieve factory for module2
175: DefinitionsFactory factory2 = TilesUtil.getDefinitionsFactory(
176: request, context);
177: assertNotNull("factory found", factory2);
178: assertEquals("factory name", "/module2", factory2.getConfig()
179: .getFactoryName());
180:
181: // Check that factory are different
182: assertNotSame("Factory from different modules", factory1,
183: factory2);
184: }
185:
186: /**
187: * Test single factory creation when moduleAware=false.
188: */
189: public void testSingleSharedFactory() {
190: // init TilesPlugin
191: module1 = createModuleConfig("/module1", "tiles-defs.xml",
192: false);
193: module2 = createModuleConfig("/module2", "tiles-defs.xml",
194: false);
195: initModulePlugIns(module1);
196: initModulePlugIns(module2);
197:
198: // mock request context
199: request.setAttribute(Globals.MODULE_KEY, module1);
200: request
201: .setPathElements("/myapp", "/module1/foo.do", null,
202: null);
203: // Retrieve factory for module1
204: DefinitionsFactory factory1 = TilesUtil.getDefinitionsFactory(
205: request, context);
206: assertNotNull("factory found", factory1);
207: assertEquals("factory name", "/module1", factory1.getConfig()
208: .getFactoryName());
209:
210: // mock request context
211: request.setAttribute(Globals.MODULE_KEY, module2);
212: request
213: .setPathElements("/myapp", "/module2/foo.do", null,
214: null);
215: // Retrieve factory for module2
216: DefinitionsFactory factory2 = TilesUtil.getDefinitionsFactory(
217: request, context);
218: assertNotNull("factory found", factory2);
219: assertEquals("factory name", "/module1", factory2.getConfig()
220: .getFactoryName());
221:
222: // Check that factory are different
223: assertEquals("Same factory", factory1, factory2);
224: }
225:
226: /**
227: * Test I18nFactorySet.
228: */
229: public void testI18FactorySet_A() {
230:
231: Locale locale = null;
232: ComponentDefinition definition = null;
233: org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
234:
235: Map properties = new HashMap();
236:
237: // Set the file name
238: properties.put(
239: I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
240: "config/I18nFactorySet-A.xml");
241:
242: try {
243: CustomI18nFactorySet i18nFactorySet = new CustomI18nFactorySet(
244: context, properties);
245: String defName = "A-DEFAULT";
246:
247: // Default Locale
248: locale = new Locale("", "", "");
249: factory = i18nFactorySet.createFactory(locale, request,
250: context);
251: assertNotNull("DefinitionsFactory is nullfor locale='"
252: + print(locale) + "'", factory);
253: definition = factory.getDefinition(defName, request,
254: context);
255: assertNotNull("Definition '" + defName
256: + "' Not Found for locale='" + print(locale) + "'",
257: definition);
258: assertEquals("Definition '" + defName + "' for locale='"
259: + print(locale) + "'", defName, definition
260: .getName());
261:
262: // Variant Only
263: locale = new Locale("", "", "XX");
264: factory = i18nFactorySet.createFactory(locale, request,
265: context);
266: assertNotNull("DefinitionsFactory is null for locale='"
267: + print(locale) + "'", factory);
268: definition = factory.getDefinition(defName, request,
269: context);
270: assertNotNull("Definition '" + defName
271: + "' Not Found for locale='" + print(locale) + "'",
272: definition);
273: assertEquals("Definition '" + defName + "' for locale='"
274: + print(locale) + "'", defName, definition
275: .getName());
276:
277: // No Language, Country & Variant Locale
278: locale = new Locale("", "US", "XX");
279: factory = i18nFactorySet.createFactory(locale, request,
280: context);
281: assertNotNull("DefinitionsFactory is null for locale='"
282: + print(locale) + "'", factory);
283: definition = factory.getDefinition(defName, request,
284: context);
285: assertNotNull("Definition '" + defName
286: + "' Not Found for locale='" + print(locale) + "'",
287: definition);
288: assertEquals("Definition '" + defName + "' for locale='"
289: + print(locale) + "'", defName, definition
290: .getName());
291:
292: // Language & Country
293: locale = new Locale("en", "US");
294: factory = i18nFactorySet.createFactory(locale, request,
295: context);
296: assertNotNull("DefinitionsFactory is null for locale='"
297: + print(locale) + "'", factory);
298: definition = factory.getDefinition(defName, request,
299: context);
300: assertNotNull("Definition '" + defName
301: + "' Not Found for locale='" + print(locale) + "'",
302: definition);
303: assertEquals("Definition '" + defName + "' for locale='"
304: + print(locale) + "'", defName, definition
305: .getName());
306:
307: } catch (Exception ex) {
308: fail(ex.toString());
309: }
310: }
311:
312: /**
313: * Test I18nFactorySet.
314: */
315: public void testI18FactorySet_B() {
316:
317: Locale locale = null;
318: ComponentDefinition definition = null;
319: org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
320:
321: Map properties = new HashMap();
322:
323: // Set the file name
324: properties.put(
325: I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
326: "config/I18nFactorySet-B.xml");
327:
328: try {
329:
330: CustomI18nFactorySet i18nFactorySet = new CustomI18nFactorySet(
331: context, properties);
332: String defName = null;
333:
334: // Default Locale
335: locale = new Locale("", "", "");
336: factory = i18nFactorySet.createFactory(locale, request,
337: context);
338: assertNotNull("1. DefinitionsFactory is nullfor locale='"
339: + print(locale) + "'", factory);
340: defName = "B-DEFAULT";
341: definition = factory.getDefinition(defName, request,
342: context);
343: assertNotNull("2. Definition '" + defName
344: + "' Not Found for locale='" + print(locale) + "'",
345: definition);
346: assertEquals("3. Definition '" + defName + "' for locale='"
347: + print(locale) + "'", defName, definition
348: .getName());
349:
350: // Variant Only
351: locale = new Locale("", "", "XX");
352: factory = i18nFactorySet.createFactory(locale, request,
353: context);
354: assertNotNull("4. DefinitionsFactory is null for locale='"
355: + print(locale) + "'", factory);
356: defName = "B___XX";
357: definition = factory.getDefinition(defName, request,
358: context);
359: assertNotNull("5. Definition '" + defName
360: + "' Not Found for locale='" + print(locale) + "'",
361: definition);
362: assertEquals("6. Definition '" + defName + "' for locale='"
363: + print(locale) + "'", defName, definition
364: .getName());
365: defName = "B-DEFAULT";
366: definition = factory.getDefinition(defName, request,
367: context);
368: assertNotNull("7. Definition '" + defName
369: + "' Not Found for locale='" + print(locale) + "'",
370: definition);
371: assertEquals("8. Definition '" + defName + "' for locale='"
372: + print(locale) + "'", defName, definition
373: .getName());
374:
375: // No Language, Country & Unknown Variant
376: locale = new Locale("", "US", "XX");
377: factory = i18nFactorySet.createFactory(locale, request,
378: context);
379: assertNotNull("9. DefinitionsFactory is null for locale='"
380: + print(locale) + "'", factory);
381: defName = "B__US";
382: definition = factory.getDefinition(defName, request,
383: context);
384: assertNotNull("10. Definition '" + defName
385: + "' Not Found for locale='" + print(locale) + "'",
386: definition);
387: assertEquals("11. Definition '" + defName
388: + "' for locale='" + print(locale) + "'", defName,
389: definition.getName());
390:
391: // Language & Country
392: locale = new Locale("en", "US");
393: factory = i18nFactorySet.createFactory(locale, request,
394: context);
395: assertNotNull("12. DefinitionsFactory is null for locale='"
396: + print(locale) + "'", factory);
397: defName = "B_en_US";
398: definition = factory.getDefinition(defName, request,
399: context);
400: assertNotNull("13. Definition '" + defName
401: + "' Not Found for locale='" + print(locale) + "'",
402: definition);
403: assertEquals("14. Definition '" + defName
404: + "' for locale='" + print(locale) + "'", defName,
405: definition.getName());
406:
407: // Language, Country & Unknown Variant
408: locale = new Locale("en", "GB", "XX");
409: factory = i18nFactorySet.createFactory(locale, request,
410: context);
411: assertNotNull("15. DefinitionsFactory is null for locale='"
412: + print(locale) + "'", factory);
413: defName = "B_en_GB";
414: definition = factory.getDefinition(defName, request,
415: context);
416: assertNotNull("16. Definition '" + defName
417: + "' Not Found for locale='" + print(locale) + "'",
418: definition);
419: assertEquals("17. Definition '" + defName
420: + "' for locale='" + print(locale) + "'", defName,
421: definition.getName());
422:
423: // Language, Unknown Country & Unknown Variant
424: locale = new Locale("en", "FR", "XX");
425: factory = i18nFactorySet.createFactory(locale, request,
426: context);
427: assertNotNull("18. DefinitionsFactory is null for locale='"
428: + print(locale) + "'", factory);
429: defName = "B_en";
430: definition = factory.getDefinition(defName, request,
431: context);
432: assertNotNull("19. Definition '" + defName
433: + "' Not Found for locale='" + print(locale) + "'",
434: definition);
435: assertEquals("20. Definition '" + defName
436: + "' for locale='" + print(locale) + "'", defName,
437: definition.getName());
438:
439: } catch (Exception ex) {
440: fail(ex.toString());
441: }
442:
443: }
444:
445: /**
446: * String representation of a Locale. A bug in the
447: * Locale.toString() method results in Locales with
448: * just a variant being incorrectly displayed.
449: */
450: private String print(Locale locale) {
451:
452: return locale.getLanguage() + "_" + locale.getCountry() + "_"
453: + locale.getVariant();
454: }
455:
456: }
|