KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > tiles > TestTilesPlugin


1 /*
2  * $Id: TestTilesPlugin.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 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 JavaDoc;
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.commons.beanutils.BeanUtils;
28 import org.apache.struts.Globals;
29 import org.apache.struts.action.PlugIn;
30 import org.apache.struts.config.ModuleConfig;
31 import org.apache.struts.config.ModuleConfigFactory;
32 import org.apache.struts.config.PlugInConfig;
33 import org.apache.struts.mock.MockActionServlet;
34 import org.apache.struts.mock.TestMockBase;
35 import org.apache.struts.util.RequestUtils;
36
37 /**
38  * <p>Unit tests for <code>org.apache.struts.tiles.*</code>.</p>
39  *
40  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
41  */

42
43 public class TestTilesPlugin extends TestMockBase {
44
45
46   protected ModuleConfig module1;
47   protected ModuleConfig module2;
48   protected MockActionServlet actionServlet;
49
50     // ----------------------------------------------------------------- Basics
51

52
53     public TestTilesPlugin(String JavaDoc name) {
54         super(name);
55     }
56
57
58     public static void main(String JavaDoc args[]) {
59         junit.awtui.TestRunner.main
60             (new String JavaDoc[] { TestTilesPlugin.class.getName() } );
61     }
62
63
64     public static Test suite() {
65         return (new TestSuite(TestTilesPlugin.class));
66     }
67
68
69     // ----------------------------------------------------- Instance Variables
70

71
72
73     // ----------------------------------------------------- Setup and Teardown
74

75
76     public void setUp()
77     {
78
79     super.setUp();
80     TilesUtil.testReset();
81     actionServlet = new MockActionServlet(context, config);
82     }
83
84
85     public void tearDown() {
86
87         super.tearDown();
88
89     }
90
91
92     // ------------------------------------------------------- Individual Tests
93

94
95     /**
96      * Create a module configuration
97      * @param moduleName
98      */

99     public ModuleConfig createModuleConfig(
100         String JavaDoc moduleName,
101         String JavaDoc configFileName,
102         boolean moduleAware) {
103             
104         ModuleConfig moduleConfig =
105             ModuleConfigFactory.createFactory().createModuleConfig(moduleName);
106             
107         context.setAttribute(Globals.MODULE_KEY + moduleName, moduleConfig);
108     
109         // Set tiles plugin
110
PlugInConfig pluginConfig = new PlugInConfig();
111         pluginConfig.setClassName("org.apache.struts.tiles.TilesPlugin");
112         
113         pluginConfig.addProperty(
114             "moduleAware",
115             (moduleAware == true ? "true" : "false"));
116             
117         pluginConfig.addProperty(
118             "definitions-config",
119             "/org/apache/struts/tiles/config/" + configFileName);
120             
121         moduleConfig.addPlugInConfig(pluginConfig);
122         return moduleConfig;
123     }
124
125     /**
126      * Fake call to init module plugins
127      * @param config
128      */

129   public void initModulePlugIns( ModuleConfig moduleConfig)
130   {
131   PlugInConfig plugInConfigs[] = moduleConfig.findPlugInConfigs();
132   PlugIn plugIns[] = new PlugIn[plugInConfigs.length];
133
134   context.setAttribute(Globals.PLUG_INS_KEY + moduleConfig.getPrefix(), plugIns);
135   for (int i = 0; i < plugIns.length; i++) {
136       try {
137           plugIns[i] =
138               (PlugIn)RequestUtils.applicationInstance(plugInConfigs[i].getClassName());
139           BeanUtils.populate(plugIns[i], plugInConfigs[i].getProperties());
140             // Pass the current plugIn config object to the PlugIn.
141
// The property is set only if the plugin declares it.
142
// This plugin config object is needed by Tiles
143
BeanUtils.copyProperty( plugIns[i], "currentPlugInConfigObject", plugInConfigs[i]);
144           plugIns[i].init(actionServlet, moduleConfig);
145       } catch (ServletException JavaDoc e) {
146           // Lets propagate
147
e.printStackTrace();
148           //throw e;
149
} catch (Exception JavaDoc e) {
150           e.printStackTrace();
151           //throw e;
152
}
153   }
154   }
155
156     // ---------------------------------------------------------- absoluteURL()
157

158
159     /**
160      * Test multi factory creation when moduleAware=true.
161      */

162     public void testMultiFactory() {
163         // init TilesPlugin
164
module1 = createModuleConfig("/module1", "tiles-defs.xml", true);
165         module2 = createModuleConfig("/module2", "tiles-defs.xml", true);
166         initModulePlugIns(module1);
167         initModulePlugIns(module2);
168     
169         // mock request context
170
request.setAttribute(Globals.MODULE_KEY, module1);
171         request.setPathElements("/myapp", "/module1/foo.do", null, null);
172         // Retrieve factory for module1
173
DefinitionsFactory factory1 =
174             TilesUtil.getDefinitionsFactory(request, context);
175             
176         assertNotNull("factory found", factory1);
177         assertEquals(
178             "factory name",
179             "/module1",
180             factory1.getConfig().getFactoryName());
181     
182         // mock request context
183
request.setAttribute(Globals.MODULE_KEY, module2);
184         request.setPathElements("/myapp", "/module2/foo.do", null, null);
185         // Retrieve factory for module2
186
DefinitionsFactory factory2 =
187             TilesUtil.getDefinitionsFactory(request, context);
188         assertNotNull("factory found", factory2);
189         assertEquals(
190             "factory name",
191             "/module2",
192             factory2.getConfig().getFactoryName());
193     
194         // Check that factory are different
195
assertNotSame("Factory from different modules", factory1, factory2);
196     }
197
198     /**
199      * Test single factory creation when moduleAware=false.
200      */

201   public void testSingleSharedFactory()
202   {
203     // init TilesPlugin
204
module1 = createModuleConfig( "/module1", "tiles-defs.xml", false );
205   module2 = createModuleConfig( "/module2", "tiles-defs.xml", false );
206   initModulePlugIns(module1);
207   initModulePlugIns(module2);
208
209     // mock request context
210
request.setAttribute(Globals.MODULE_KEY, module1);
211   request.setPathElements("/myapp", "/module1/foo.do", null, null);
212     // Retrieve factory for module1
213
DefinitionsFactory factory1 = TilesUtil.getDefinitionsFactory( request, context);
214   assertNotNull( "factory found", factory1);
215   assertEquals( "factory name", "/module1", factory1.getConfig().getFactoryName() );
216
217     // mock request context
218
request.setAttribute(Globals.MODULE_KEY, module2);
219   request.setPathElements("/myapp", "/module2/foo.do", null, null);
220     // Retrieve factory for module2
221
DefinitionsFactory factory2 = TilesUtil.getDefinitionsFactory( request, context);
222   assertNotNull( "factory found", factory2);
223   assertEquals( "factory name", "/module1", factory2.getConfig().getFactoryName() );
224
225     // Check that factory are different
226
assertEquals("Same factory", factory1, factory2);
227   }
228
229
230 }
231
232
Popular Tags