1 18 19 package org.apache.struts.config; 20 21 22 import java.io.InputStream ; 23 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 28 import org.apache.commons.digester.Digester; 29 import org.apache.struts.Globals; 30 31 32 37 38 public class TestModuleConfig extends TestCase { 39 40 41 43 44 47 protected ModuleConfig config = null; 48 49 50 52 53 58 public TestModuleConfig(String name) { 59 60 super(name); 61 62 } 63 64 65 67 68 71 public void setUp() { 72 73 ModuleConfigFactory factoryObject = 74 ModuleConfigFactory.createFactory(); 75 config = factoryObject.createModuleConfig(""); 76 77 } 78 79 80 83 public static Test suite() { 84 85 return (new TestSuite(TestModuleConfig.class)); 86 87 } 88 89 90 93 public void tearDown() { 94 95 config = null; 96 97 } 98 99 100 private void parseConfig(String publicId, String entityURL,String strutsConfig) { 102 103 104 Digester digester = new Digester(); 106 digester.push(config); 107 digester.setNamespaceAware(true); 108 digester.setValidating(true); 109 digester.addRuleSet(new ConfigRuleSet()); 110 digester.register 111 (publicId, 112 this.getClass().getResource 113 (entityURL).toString()); 114 115 try { 117 InputStream input = this.getClass().getResourceAsStream(strutsConfig); 118 assertNotNull("Got an input stream for "+strutsConfig, input); 119 digester.parse(input); 120 input.close(); 121 } catch (Throwable t) { 122 t.printStackTrace(System.out); 123 fail("Parsing threw exception: " + t); 124 } 125 126 127 128 } 129 130 131 134 public void testParse() { 135 136 testParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", 137 "/org/apache/struts/resources/struts-config_1_2.dtd", 138 "/org/apache/struts/config/struts-config.xml"); 139 140 } 141 142 public void testParse1_1() { 143 144 testParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", 145 "/org/apache/struts/resources/struts-config_1_1.dtd", 146 "/org/apache/struts/config/struts-config-1.1.xml"); 147 148 } 149 150 public void testParseBase(String publicId, String entityURL,String strutsConfig) { 151 152 parseConfig(publicId,entityURL, strutsConfig); 153 154 156 DataSourceConfig dsc = 157 config.findDataSourceConfig(Globals.DATA_SOURCE_KEY); 158 assertNotNull("Found our data source configuration", dsc); 159 assertEquals("Data source driverClass", 160 "org.postgresql.Driver", 161 (String ) dsc.getProperties().get("driverClass")); 162 163 assertEquals("Data source description", 164 "Example Data Source Configuration", 165 (String ) dsc.getProperties().get("description")); 166 167 FormBeanConfig fbcs[] = config.findFormBeanConfigs(); 168 assertNotNull("Found our form bean configurations", fbcs); 169 assertEquals("Found three form bean configurations", 170 3, fbcs.length); 171 172 ForwardConfig fcs[] = config.findForwardConfigs(); 173 assertNotNull("Found our forward configurations", fcs); 174 assertEquals("Found three forward configurations", 175 3, fcs.length); 176 177 ActionConfig logon = config.findActionConfig("/logon"); 178 assertNotNull("Found logon action configuration", logon); 179 assertEquals("Found correct logon configuration", 180 "logonForm", 181 logon.getName()); 182 183 184 } 185 186 187 190 public void testCustomMappingParse() { 191 192 testCustomMappingParseBase 194 ("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", 195 "/org/apache/struts/resources/struts-config_1_2.dtd", 196 "/org/apache/struts/config/struts-config-custom-mapping.xml"); 197 } 198 199 202 public void testCustomMappingParse1_1() { 203 204 205 testCustomMappingParseBase 207 ("-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", 208 "/org/apache/struts/resources/struts-config_1_1.dtd", 209 "/org/apache/struts/config/struts-config-custom-mapping.xml"); 210 211 } 212 213 214 217 private void testCustomMappingParseBase(String publicId, String entityURL,String strutsConfig) { 218 219 220 parseConfig(publicId,entityURL, strutsConfig); 221 222 CustomMappingTest map = (CustomMappingTest)config.findActionConfig("/editRegistration"); 224 assertNotNull("Cannot find editRegistration mapping", map); 225 assertTrue("The custom mapping attribute has not been set", map.getPublic()); 226 227 } 228 229 232 public void testPreserveActionMappingsOrder() { 233 234 parseConfig("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", 235 "/org/apache/struts/resources/struts-config_1_2.dtd", 236 "/org/apache/struts/config/struts-config.xml"); 237 238 String [] paths = new String [] {"/editRegistration", 239 "/editSubscription", "/logoff", "/logon", "/saveRegistration", 240 "/saveSubscription", "/tour" 241 }; 242 243 ActionConfig[] actions = config.findActionConfigs(); 244 for (int x=0; x<paths.length; x++) { 245 assertTrue("Action config out of order:"+actions[x].getPath(), 246 paths[x].equals(actions[x].getPath())); 247 } 248 } 249 250 251 252 } 253 | Popular Tags |