View Javadoc

1   /*
2    * $Id: TestModuleConfig.java 421119 2006-07-12 04:49:11Z wsmoak $
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  package org.apache.struts.config;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  
24  import org.apache.commons.digester.Digester;
25  
26  import java.io.InputStream;
27  
28  /***
29   * Unit tests for the <code>org.apache.struts.config</code> package.
30   *
31   * @version $Rev: 421119 $ $Date: 2005-03-01 20:26:14 -0500 (Tue, 01 Mar 2005)
32   *          $
33   */
34  public class TestModuleConfig extends TestCase {
35      // ----------------------------------------------------- Instance Variables
36  
37      /***
38       * The ModuleConfig we are testing.
39       */
40      protected ModuleConfig config = null;
41  
42      // ----------------------------------------------------------- Constructors
43  
44      /***
45       * Construct a new instance of this test case.
46       *
47       * @param name Name of the test case
48       */
49      public TestModuleConfig(String name) {
50          super(name);
51      }
52  
53      // --------------------------------------------------------- Public Methods
54  
55      /***
56       * Set up instance variables required by this test case.
57       */
58      public void setUp() {
59          ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
60  
61          config = factoryObject.createModuleConfig("");
62      }
63  
64      /***
65       * Return the tests included in this test suite.
66       */
67      public static Test suite() {
68          return (new TestSuite(TestModuleConfig.class));
69      }
70  
71      /***
72       * Tear down instance variables required by this test case.
73       */
74      public void tearDown() {
75          config = null;
76      }
77  
78      // ------------------------------------------------ Individual Test Methods
79      private void parseConfig(String publicId, String entityURL,
80          String strutsConfig) {
81          // Prepare a Digester for parsing a struts-config.xml file
82          Digester digester = new Digester();
83  
84          digester.push(config);
85          digester.setNamespaceAware(true);
86          digester.setValidating(true);
87          digester.addRuleSet(new ConfigRuleSet());
88          digester.register(publicId,
89              this.getClass().getResource(entityURL).toString());
90  
91          // Parse the test struts-config.xml file
92          try {
93              InputStream input =
94                  this.getClass().getResourceAsStream(strutsConfig);
95  
96              assertNotNull("Got an input stream for " + strutsConfig, input);
97              digester.parse(input);
98              input.close();
99          } catch (Throwable t) {
100             t.printStackTrace(System.out);
101             fail("Parsing threw exception:  " + t);
102         }
103     }
104 
105     /***
106      * Test parsing of a struts-config.xml file.
107      */
108     public void testParse() {
109         testParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
110             "/org/apache/struts/resources/struts-config_1_2.dtd",
111             "/org/apache/struts/config/struts-config.xml");
112     }
113 
114     public void testParse1_1() {
115         testParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
116             "/org/apache/struts/resources/struts-config_1_1.dtd",
117             "/org/apache/struts/config/struts-config-1.1.xml");
118     }
119 
120     public void testParseBase(String publicId, String entityURL,
121         String strutsConfig) {
122         parseConfig(publicId, entityURL, strutsConfig);
123 
124         // Perform assertion tests on the parsed information
125         FormBeanConfig[] fbcs = config.findFormBeanConfigs();
126 
127         assertNotNull("Found our form bean configurations", fbcs);
128         assertEquals("Found three form bean configurations", 3, fbcs.length);
129 
130         ForwardConfig[] fcs = config.findForwardConfigs();
131 
132         assertNotNull("Found our forward configurations", fcs);
133         assertEquals("Found three forward configurations", 3, fcs.length);
134 
135         ActionConfig logon = config.findActionConfig("/logon");
136 
137         assertNotNull("Found logon action configuration", logon);
138         assertEquals("Found correct logon configuration", "logonForm",
139             logon.getName());
140     }
141 
142     /***
143      * Tests a struts-config.xml that contains a custom mapping and property.
144      */
145     public void testCustomMappingParse() {
146         // Prepare a Digester for parsing a struts-config.xml file
147         testCustomMappingParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
148             "/org/apache/struts/resources/struts-config_1_2.dtd",
149             "/org/apache/struts/config/struts-config-custom-mapping.xml");
150     }
151 
152     /***
153      * Tests a struts-config.xml that contains a custom mapping and property.
154      */
155     public void testCustomMappingParse1_1() {
156         // Prepare a Digester for parsing a struts-config.xml file
157         testCustomMappingParseBase("-//Apache Software Foundation//DTD Struts Configuration 1.1//EN",
158             "/org/apache/struts/resources/struts-config_1_1.dtd",
159             "/org/apache/struts/config/struts-config-custom-mapping-1.1.xml");
160     }
161 
162     /***
163      * Tests a struts-config.xml that contains a custom mapping and property.
164      */
165     private void testCustomMappingParseBase(String publicId, String entityURL,
166         String strutsConfig) {
167         parseConfig(publicId, entityURL, strutsConfig);
168 
169         // Perform assertion tests on the parsed information
170         CustomMappingTest map =
171             (CustomMappingTest) config.findActionConfig("/editRegistration");
172 
173         assertNotNull("Cannot find editRegistration mapping", map);
174         assertTrue("The custom mapping attribute has not been set",
175             map.getPublic());
176     }
177 
178     /***
179      * Test order of action mappings defined perserved.
180      */
181     public void testPreserveActionMappingsOrder() {
182         parseConfig("-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
183             "/org/apache/struts/resources/struts-config_1_2.dtd",
184             "/org/apache/struts/config/struts-config.xml");
185 
186         String[] paths =
187             new String[] {
188                 "/editRegistration", "/editSubscription", "/logoff", "/logon",
189                 "/saveRegistration", "/saveSubscription", "/tour"
190             };
191 
192         ActionConfig[] actions = config.findActionConfigs();
193 
194         for (int x = 0; x < paths.length; x++) {
195             assertTrue("Action config out of order:" + actions[x].getPath(),
196                 paths[x].equals(actions[x].getPath()));
197         }
198     }
199 }