1 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 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 42 43 public class TestTilesPlugin extends TestMockBase { 44 45 46 protected ModuleConfig module1; 47 protected ModuleConfig module2; 48 protected MockActionServlet actionServlet; 49 50 52 53 public TestTilesPlugin(String name) { 54 super(name); 55 } 56 57 58 public static void main(String args[]) { 59 junit.awtui.TestRunner.main 60 (new String [] { TestTilesPlugin.class.getName() } ); 61 } 62 63 64 public static Test suite() { 65 return (new TestSuite(TestTilesPlugin.class)); 66 } 67 68 69 71 72 73 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 94 95 99 public ModuleConfig createModuleConfig( 100 String moduleName, 101 String configFileName, 102 boolean moduleAware) { 103 104 ModuleConfig moduleConfig = 105 ModuleConfigFactory.createFactory().createModuleConfig(moduleName); 106 107 context.setAttribute(Globals.MODULE_KEY + moduleName, moduleConfig); 108 109 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 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 BeanUtils.copyProperty( plugIns[i], "currentPlugInConfigObject", plugInConfigs[i]); 144 plugIns[i].init(actionServlet, moduleConfig); 145 } catch (ServletException e) { 146 e.printStackTrace(); 148 } catch (Exception e) { 150 e.printStackTrace(); 151 } 153 } 154 } 155 156 158 159 162 public void testMultiFactory() { 163 module1 = createModuleConfig("/module1", "tiles-defs.xml", true); 165 module2 = createModuleConfig("/module2", "tiles-defs.xml", true); 166 initModulePlugIns(module1); 167 initModulePlugIns(module2); 168 169 request.setAttribute(Globals.MODULE_KEY, module1); 171 request.setPathElements("/myapp", "/module1/foo.do", null, null); 172 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 request.setAttribute(Globals.MODULE_KEY, module2); 184 request.setPathElements("/myapp", "/module2/foo.do", null, null); 185 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 assertNotSame("Factory from different modules", factory1, factory2); 196 } 197 198 201 public void testSingleSharedFactory() 202 { 203 module1 = createModuleConfig( "/module1", "tiles-defs.xml", false ); 205 module2 = createModuleConfig( "/module2", "tiles-defs.xml", false ); 206 initModulePlugIns(module1); 207 initModulePlugIns(module2); 208 209 request.setAttribute(Globals.MODULE_KEY, module1); 211 request.setPathElements("/myapp", "/module1/foo.do", null, null); 212 DefinitionsFactory factory1 = TilesUtil.getDefinitionsFactory( request, context); 214 assertNotNull( "factory found", factory1); 215 assertEquals( "factory name", "/module1", factory1.getConfig().getFactoryName() ); 216 217 request.setAttribute(Globals.MODULE_KEY, module2); 219 request.setPathElements("/myapp", "/module2/foo.do", null, null); 220 DefinitionsFactory factory2 = TilesUtil.getDefinitionsFactory( request, context); 222 assertNotNull( "factory found", factory2); 223 assertEquals( "factory name", "/module1", factory2.getConfig().getFactoryName() ); 224 225 assertEquals("Same factory", factory1, factory2); 227 } 228 229 230 } 231 232 | Popular Tags |