KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > TestModuleConfig


1 /*
2  * $Id: TestModuleConfig.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 package org.apache.struts.config;
20
21
22 import java.io.InputStream JavaDoc;
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 /**
33  * Unit tests for the <code>org.apache.struts.config</code> package.
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  */

37
38 public class TestModuleConfig extends TestCase {
39
40
41     // ----------------------------------------------------- Instance Variables
42

43
44     /**
45      * The ModuleConfig we are testing.
46      */

47     protected ModuleConfig config = null;
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     /**
54      * Construct a new instance of this test case.
55      *
56      * @param name Name of the test case
57      */

58     public TestModuleConfig(String JavaDoc name) {
59
60         super(name);
61
62     }
63
64
65     // --------------------------------------------------------- Public Methods
66

67
68     /**
69      * Set up instance variables required by this test case.
70      */

71     public void setUp() {
72
73         ModuleConfigFactory factoryObject =
74             ModuleConfigFactory.createFactory();
75         config = factoryObject.createModuleConfig("");
76
77     }
78
79
80     /**
81      * Return the tests included in this test suite.
82      */

83     public static Test suite() {
84
85         return (new TestSuite(TestModuleConfig.class));
86
87     }
88
89
90     /**
91      * Tear down instance variables required by this test case.
92      */

93     public void tearDown() {
94
95         config = null;
96
97     }
98
99
100     // ------------------------------------------------ Individual Test Methods
101
private void parseConfig(String JavaDoc publicId, String JavaDoc entityURL,String JavaDoc strutsConfig) {
102
103
104         // Prepare a Digester for parsing a struts-config.xml file
105
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         // Parse the test struts-config.xml file
116
try {
117             InputStream JavaDoc input = this.getClass().getResourceAsStream(strutsConfig);
118             assertNotNull("Got an input stream for "+strutsConfig, input);
119             digester.parse(input);
120             input.close();
121         } catch (Throwable JavaDoc t) {
122             t.printStackTrace(System.out);
123             fail("Parsing threw exception: " + t);
124         }
125
126
127
128     }
129
130
131     /**
132      * Test parsing of a struts-config.xml file.
133      */

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 JavaDoc publicId, String JavaDoc entityURL,String JavaDoc strutsConfig) {
151
152         parseConfig(publicId,entityURL, strutsConfig);
153
154         // Perform assertion tests on the parsed information
155

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 JavaDoc) dsc.getProperties().get("driverClass"));
162
163         assertEquals("Data source description",
164                      "Example Data Source Configuration",
165                      (String JavaDoc) 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    /**
188      * Tests a struts-config.xml that contains a custom mapping and property.
189      */

190     public void testCustomMappingParse() {
191
192        // Prepare a Digester for parsing a struts-config.xml file
193
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     /**
200       * Tests a struts-config.xml that contains a custom mapping and property.
201       */

202      public void testCustomMappingParse1_1() {
203
204
205          // Prepare a Digester for parsing a struts-config.xml file
206
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     /**
215       * Tests a struts-config.xml that contains a custom mapping and property.
216       */

217      private void testCustomMappingParseBase(String JavaDoc publicId, String JavaDoc entityURL,String JavaDoc strutsConfig) {
218
219
220          parseConfig(publicId,entityURL, strutsConfig);
221
222          // Perform assertion tests on the parsed information
223
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     /**
230      * Test order of action mappings defined perserved.
231      */

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 JavaDoc[] paths = new String JavaDoc[] {"/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