View Javadoc

1   /*
2    * $Id: TestActionConfigMatcher.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.TestSuite;
22  
23  import org.apache.struts.action.ActionForward;
24  import org.apache.struts.action.ActionMapping;
25  import org.apache.struts.mock.TestMockBase;
26  
27  /***
28   * <p>Unit tests for <code>org.apache.struts.util.ActionConfigMatcher</code>.</p>
29   *
30   * @version $Rev: 421119 $ $Date: 2005-10-27 23:25:01 -0400 (Thu, 27 Oct 2005)
31   *          $
32   */
33  public class TestActionConfigMatcher extends TestMockBase {
34      // ----------------------------------------------------------------- Basics
35      public TestActionConfigMatcher(String name) {
36          super(name);
37      }
38  
39      public static void main(String[] args) {
40          junit.awtui.TestRunner.main(new String[] {
41                  TestActionConfigMatcher.class.getName()
42              });
43      }
44  
45      public static Test suite() {
46          return (new TestSuite(TestActionConfigMatcher.class));
47      }
48  
49      // ----------------------------------------------------- Instance Variables
50      // ----------------------------------------------------- Setup and Teardown
51      public void setUp() {
52          super.setUp();
53      }
54  
55      public void tearDown() {
56          super.tearDown();
57      }
58  
59      // ------------------------------------------------------- Individual Tests
60      // ---------------------------------------------------------- match()
61      public void testNoMatch() {
62          ActionConfig[] configs = new ActionConfig[1];
63          ActionConfig mapping = buildActionConfig("/foo");
64  
65          configs[0] = mapping;
66  
67          ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
68  
69          assertNull("ActionConfig shouldn't be matched", matcher.match("/test"));
70      }
71  
72      public void testNoWildcardMatch() {
73          ActionConfig[] configs = new ActionConfig[1];
74          ActionConfig mapping = buildActionConfig("/fooBar");
75  
76          configs[0] = mapping;
77  
78          ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
79  
80          assertNull("ActionConfig shouldn't be matched", matcher.match("/fooBar"));
81      }
82  
83      public void testShouldMatch() {
84          ActionConfig[] configs = new ActionConfig[1];
85          ActionConfig mapping = buildActionConfig("/foo*");
86  
87          configs[0] = mapping;
88  
89          ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
90  
91          ActionConfig matched = matcher.match("/fooBar");
92  
93          assertNotNull("ActionConfig should be matched", matched);
94          assertTrue("ActionConfig should have two action forward",
95              matched.findForwardConfigs().length == 2);
96          assertTrue("ActionConfig should have two exception forward",
97              matched.findExceptionConfigs().length == 2);
98          assertTrue("ActionConfig should have properties",
99              matched.getProperties().size() == 2);
100     }
101 
102     public void testCheckSubstitutionsMatch() {
103         ActionConfig[] configs = new ActionConfig[1];
104         ActionConfig mapping = buildActionConfig("/foo*");
105 
106         configs[0] = mapping;
107 
108         ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
109         ActionConfig m = matcher.match("/fooBar");
110 
111         assertTrue("Name hasn't been replaced", "name,Bar".equals(m.getName()));
112         assertTrue("Path hasn't been replaced", "/fooBar".equals(m.getPath()));
113         assertTrue("Scope isn't correct", "request".equals(m.getScope()));
114         assertTrue("Unknown isn't correct", !m.getUnknown());
115         assertTrue("Validate isn't correct", m.getValidate());
116 
117         assertTrue("Prefix hasn't been replaced",
118             "foo,Bar".equals(m.getPrefix()));
119         assertTrue("Suffix hasn't been replaced",
120             "bar,Bar".equals(m.getSuffix()));
121         assertTrue("Type hasn't been replaced",
122             "foo.bar.BarAction".equals(m.getType()));
123         assertTrue("Roles hasn't been replaced",
124             "public,Bar".equals(m.getRoles()));
125         assertTrue("Parameter hasn't been replaced",
126             "param,Bar".equals(m.getParameter()));
127         assertTrue("Attribute hasn't been replaced",
128             "attrib,Bar".equals(m.getAttribute()));
129         assertTrue("Forward hasn't been replaced",
130             "fwd,Bar".equals(m.getForward()));
131         assertTrue("Include hasn't been replaced",
132             "include,Bar".equals(m.getInclude()));
133         assertTrue("Input hasn't been replaced",
134             "input,Bar".equals(m.getInput()));
135 
136         assertTrue("ActionConfig property not replaced",
137             "testBar".equals(m.getProperty("testprop2")));
138 
139         ForwardConfig[] fConfigs = m.findForwardConfigs();
140         boolean found = false;
141 
142         for (int x = 0; x < fConfigs.length; x++) {
143             ForwardConfig cfg = fConfigs[x];
144 
145             if ("name".equals(cfg.getName())) {
146                 found = true;
147                 assertTrue("Path hasn't been replaced",
148                     "path,Bar".equals(cfg.getPath()));
149                 assertTrue("Property foo hasn't been replaced",
150                     "bar,Bar".equals(cfg.getProperty("foo")));
151                 assertTrue("Module hasn't been replaced",
152                     "modBar".equals(cfg.getModule()));
153             }
154         }
155 
156         assertTrue("The forward config 'name' cannot be found", found);
157     }
158 
159     public void testCheckMultipleSubstitutions() {
160         ActionMapping[] mapping = new ActionMapping[1];
161 
162         mapping[0] = new ActionMapping();
163         mapping[0].setPath("/foo*");
164         mapping[0].setName("name,{1}-{1}");
165 
166         ActionConfigMatcher matcher = new ActionConfigMatcher(mapping);
167         ActionConfig m = matcher.match("/fooBar");
168 
169         assertTrue("Name hasn't been replaced correctly: " + m.getName(),
170             "name,Bar-Bar".equals(m.getName()));
171     }
172 
173     private ActionConfig buildActionConfig(String path) {
174         ActionMapping mapping = new ActionMapping();
175 
176         mapping.setName("name,{1}");
177         mapping.setPath(path);
178         mapping.setScope("request");
179         mapping.setUnknown(false);
180         mapping.setValidate(true);
181 
182         mapping.setPrefix("foo,{1}");
183         mapping.setSuffix("bar,{1}");
184 
185         mapping.setType("foo.bar.{1}Action");
186         mapping.setRoles("public,{1}");
187         mapping.setParameter("param,{1}");
188         mapping.setAttribute("attrib,{1}");
189         mapping.setForward("fwd,{1}");
190         mapping.setInclude("include,{1}");
191         mapping.setInput("input,{1}");
192 
193         ForwardConfig cfg = new ActionForward();
194 
195         cfg.setName("name");
196         cfg.setPath("path,{1}");
197         cfg.setModule("mod{1}");
198         cfg.setProperty("foo", "bar,{1}");
199         mapping.addForwardConfig(cfg);
200 
201         cfg = new ActionForward();
202         cfg.setName("name2");
203         cfg.setPath("path2");
204         cfg.setModule("mod{1}");
205         mapping.addForwardConfig(cfg);
206 
207         ExceptionConfig excfg = new ExceptionConfig();
208 
209         excfg.setKey("foo");
210         excfg.setType("foo.Bar");
211         excfg.setPath("path");
212         mapping.addExceptionConfig(excfg);
213 
214         excfg = new ExceptionConfig();
215         excfg.setKey("foo2");
216         excfg.setType("foo.Bar2");
217         excfg.setPath("path2");
218         mapping.addExceptionConfig(excfg);
219 
220         mapping.setProperty("testprop", "testval");
221         mapping.setProperty("testprop2", "test{1}");
222 
223         mapping.freeze();
224 
225         return mapping;
226     }
227 }