1   package org.apache.maven.struts;
2   
3   /* ====================================================================
4    *   Copyright 2001-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  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * Unit tests for {@link Struts10WarFile}
27   *
28   * @author dion
29   * @version $Id: Struts10WarFileTest.java 170200 2005-05-15 06:24:19Z brett $
30   */
31  public class Struts10WarFileTest extends TestCase {
32  
33      /** instance to be tested */
34      private Struts10WarFile instance;
35      /** test war file */
36      private String testWarFileName;
37  
38      /** Creates a new instance of Struts10WarFileTest
39       * @param testName the name of the test
40       */
41      public Struts10WarFileTest(java.lang.String testName) {
42          super(testName);
43      }
44  
45      /**
46       * Initialize per test data
47       * @throws Exception when there is an unexpected problem
48       */
49      public void setUp() throws Exception
50      {
51          String baseDir = System.getProperty("basedir");
52          assertNotNull("The system property basedir was not defined.", baseDir);
53          testWarFileName = baseDir + "/src/test-resources/struts/struts-example.war";
54      }
55  
56      /** Test that the constructor succeeds
57       * @throws Exception when there is an unexpected problem
58       */
59      public void testConstructor() throws Exception
60      {
61          instance = new Struts10WarFile(testWarFileName);
62      }
63  
64      /** test that the config is defaulted correctly
65       * @throws Exception when there is an unexpected problem
66       */
67      public void testDefaultConfig() throws Exception
68      {
69          testConstructor();
70          assertEquals("Default config isn't WEB-INF/struts-config.xml",
71              Struts10WarFile.DEFAULT_CONFIG,
72              instance.getConfig());
73      }
74  
75      /** test the the config property works
76       * @throws Exception when there is an unexpected problem
77       */
78      public void testConfig() throws Exception
79      {
80          testDefaultConfig();
81          String dummyConfig = "WEB-INF/struts.xml";
82          instance.setConfig(dummyConfig);
83          assertEquals("Set or get for config failed", dummyConfig,
84              instance.getConfig());
85      }
86  
87      /** test retrieving the struts config as a JarEntry
88       * @throws Exception when there is an unexpected problem
89       */
90      public void testConfigEntry() throws Exception
91      {
92          testConstructor();
93          assertNotNull("config entry was null", instance.getStrutsConfigEntry());
94      }
95  
96      /** test the retrieval of form beans
97       * @throws Exception when there is an unexpected problem
98       */
99      public void testFormBeans() throws Exception
100     {
101         testConstructor();
102         assertEquals("Number of form beans returned was wrong", 3,
103             instance.getFormBeans().size());
104         List formBeans = instance.getFormBeans();
105         assertEquals("first form bean isn't logonForm", "logonForm",
106             ((FormBean)formBeans.get(0)).getName());
107         assertEquals("second form bean isn't registrationForm",
108             "registrationForm",
109             ((FormBean)formBeans.get(1)).getName());
110         assertEquals("third form bean isn't subscriptionForm",
111             "subscriptionForm",
112             ((FormBean)formBeans.get(2)).getName());
113         assertEquals("form beans type is not default",
114             "org.apache.struts.action.ActionFormBean",
115             instance.getFormBeansType());
116     }
117 
118     /** test the retrieval of actions
119      * @throws Exception when there is an unexpected problem
120      */
121     public void testActions() throws Exception
122     {
123         testConstructor();
124         assertEquals("Number of actions returned was wrong", 14,
125             instance.getActions().size());
126         List actions = instance.getActions();
127         String[] types = new String[]
128             {
129             "org.apache.struts.webapp.example.EditRegistrationAction",
130             "org.apache.struts.webapp.example.EditSubscriptionAction",
131             "org.apache.struts.webapp.example.LogoffAction",
132             "org.apache.struts.webapp.example.LogonAction",
133             "org.apache.struts.webapp.example.SaveRegistrationAction",
134             "org.apache.struts.webapp.example.SaveSubscriptionAction",
135             null,
136             "org.apache.struts.actions.AddFormBeanAction",
137             "org.apache.struts.actions.AddForwardAction",
138             "org.apache.struts.actions.AddMappingAction",
139             "org.apache.struts.actions.ReloadAction",
140             "org.apache.struts.actions.RemoveFormBeanAction",
141             "org.apache.struts.actions.RemoveForwardAction",
142             "org.apache.struts.actions.RemoveMappingAction"
143             };
144         for (int index = 0; index < types.length; index++)
145         {
146             assertEquals("action number " + index + " isn't " + types[index],
147                 types[index], ((Action) actions.get(index)).getType());
148         }
149     }
150 
151     /** test the forwards
152      * @throws Exception when there is an unexpected problem
153      */
154     public void testForwards() throws Exception
155     {
156         testConstructor();
157         assertEquals("Global forwards type is not correct",
158             "org.apache.struts.action.ActionForward",
159             instance.getGlobalForwardsType());
160         assertEquals("Wrong number of forwards found", 3,
161             instance.getForwards().size());
162         String[] names = new String[] {"logoff", "logon", "success"};
163         Forward forward = null;
164         List forwards = instance.getForwards();
165         for (int index = 0; index < names.length; index++)
166         {
167             forward = (Forward) forwards.get(index);
168             assertEquals("Forward number " + index + " isn't "+ names[index],
169                 names[index], forward.getName());
170         }
171     }
172 
173     /** test the action servlet name property
174      * @throws Exception when there is an unexpected problem
175      */
176     public void testActionServletName() throws Exception
177     {
178         testConstructor();
179         assertEquals("default action servlet name is not 'action'", "action",
180             Struts10WarFile.DEFAULT_ACTIONSERVLET_NAME);
181         String dummy = "dummyName";
182         instance.setActionServletName(dummy);
183         assertEquals("action servlet name property get or set failed", dummy,
184             instance.getActionServletName());
185     }
186 
187     /** test the action servlet pattern method
188      * @throws Exception when there is an unexpected problem
189      */
190     public void testActionServletPattern() throws Exception
191     {
192         testConstructor();
193         assertEquals("action servlet pattern is wrong", "*.do",
194             instance.getActionServletPattern());
195     }
196 }