001: /*
002: * $Id: TestModuleConfig.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: package org.apache.struts.config;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: import org.apache.commons.digester.Digester;
028:
029: import java.io.InputStream;
030:
031: /**
032: * Unit tests for the <code>org.apache.struts.config</code> package.
033: *
034: * @version $Rev: 471754 $ $Date: 2005-03-01 20:26:14 -0500 (Tue, 01 Mar 2005)
035: * $
036: */
037: public class TestModuleConfig extends TestCase {
038: // ----------------------------------------------------- Instance Variables
039:
040: /**
041: * The ModuleConfig we are testing.
042: */
043: protected ModuleConfig config = null;
044:
045: // ----------------------------------------------------------- Constructors
046:
047: /**
048: * Construct a new instance of this test case.
049: *
050: * @param name Name of the test case
051: */
052: public TestModuleConfig(String name) {
053: super (name);
054: }
055:
056: // --------------------------------------------------------- Public Methods
057:
058: /**
059: * Set up instance variables required by this test case.
060: */
061: public void setUp() {
062: ModuleConfigFactory factoryObject = ModuleConfigFactory
063: .createFactory();
064:
065: config = factoryObject.createModuleConfig("");
066: }
067:
068: /**
069: * Return the tests included in this test suite.
070: */
071: public static Test suite() {
072: return (new TestSuite(TestModuleConfig.class));
073: }
074:
075: /**
076: * Tear down instance variables required by this test case.
077: */
078: public void tearDown() {
079: config = null;
080: }
081:
082: // ------------------------------------------------ Individual Test Methods
083: private void parseConfig(String publicId, String entityURL,
084: String strutsConfig) {
085: // Prepare a Digester for parsing a struts-config.xml file
086: Digester digester = new Digester();
087:
088: digester.push(config);
089: digester.setNamespaceAware(true);
090: digester.setValidating(true);
091: digester.addRuleSet(new ConfigRuleSet());
092: digester.register(publicId, this .getClass().getResource(
093: entityURL).toString());
094:
095: // Parse the test struts-config.xml file
096: try {
097: InputStream input = this .getClass().getResourceAsStream(
098: strutsConfig);
099:
100: assertNotNull("Got an input stream for " + strutsConfig,
101: input);
102: digester.parse(input);
103: input.close();
104: } catch (Throwable t) {
105: t.printStackTrace(System.out);
106: fail("Parsing threw exception: " + t);
107: }
108: }
109:
110: /**
111: * Test parsing of a struts-config.xml file.
112: */
113: public void testParse() {
114: testParseBase(
115: "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
116: "/org/apache/struts/resources/struts-config_1_2.dtd",
117: "/org/apache/struts/config/struts-config.xml");
118: }
119:
120: public void testParse1_1() {
121: testParseBase(
122: "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
123: "/org/apache/struts/resources/struts-config_1_1.dtd",
124: "/org/apache/struts/config/struts-config-1.1.xml");
125: }
126:
127: public void testParseBase(String publicId, String entityURL,
128: String strutsConfig) {
129: parseConfig(publicId, entityURL, strutsConfig);
130:
131: // Perform assertion tests on the parsed information
132: FormBeanConfig[] fbcs = config.findFormBeanConfigs();
133:
134: assertNotNull("Found our form bean configurations", fbcs);
135: assertEquals("Found three form bean configurations", 3,
136: fbcs.length);
137:
138: ForwardConfig[] fcs = config.findForwardConfigs();
139:
140: assertNotNull("Found our forward configurations", fcs);
141: assertEquals("Found three forward configurations", 3,
142: fcs.length);
143:
144: ActionConfig logon = config.findActionConfig("/logon");
145:
146: assertNotNull("Found logon action configuration", logon);
147: assertEquals("Found correct logon configuration", "logonForm",
148: logon.getName());
149: }
150:
151: /**
152: * Tests a struts-config.xml that contains a custom mapping and property.
153: */
154: public void testCustomMappingParse() {
155: // Prepare a Digester for parsing a struts-config.xml file
156: testCustomMappingParseBase(
157: "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
158: "/org/apache/struts/resources/struts-config_1_2.dtd",
159: "/org/apache/struts/config/struts-config-custom-mapping.xml");
160: }
161:
162: /**
163: * Tests a struts-config.xml that contains a custom mapping and property.
164: */
165: public void testCustomMappingParse1_1() {
166: // Prepare a Digester for parsing a struts-config.xml file
167: testCustomMappingParseBase(
168: "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
169: "/org/apache/struts/resources/struts-config_1_1.dtd",
170: "/org/apache/struts/config/struts-config-custom-mapping-1.1.xml");
171: }
172:
173: /**
174: * Tests a struts-config.xml that contains a custom mapping and property.
175: */
176: private void testCustomMappingParseBase(String publicId,
177: String entityURL, String strutsConfig) {
178: parseConfig(publicId, entityURL, strutsConfig);
179:
180: // Perform assertion tests on the parsed information
181: CustomMappingTest map = (CustomMappingTest) config
182: .findActionConfig("/editRegistration");
183:
184: assertNotNull("Cannot find editRegistration mapping", map);
185: assertTrue("The custom mapping attribute has not been set", map
186: .getPublic());
187: }
188:
189: /**
190: * Test order of action mappings defined perserved.
191: */
192: public void testPreserveActionMappingsOrder() {
193: parseConfig(
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.xml");
197:
198: String[] paths = new String[] { "/editRegistration",
199: "/editSubscription", "/logoff", "/logon",
200: "/saveRegistration", "/saveSubscription", "/tour" };
201:
202: ActionConfig[] actions = config.findActionConfigs();
203:
204: for (int x = 0; x < paths.length; x++) {
205: assertTrue("Action config out of order:"
206: + actions[x].getPath(), paths[x].equals(actions[x]
207: .getPath()));
208: }
209: }
210: }
|