001: /*
002: * $Id: TestActionConfigMatcher.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.TestSuite;
025:
026: import org.apache.struts.action.ActionForward;
027: import org.apache.struts.action.ActionMapping;
028: import org.apache.struts.mock.TestMockBase;
029:
030: /**
031: * <p>Unit tests for <code>org.apache.struts.util.ActionConfigMatcher</code>.</p>
032: *
033: * @version $Rev: 471754 $ $Date: 2005-10-27 23:25:01 -0400 (Thu, 27 Oct 2005)
034: * $
035: */
036: public class TestActionConfigMatcher extends TestMockBase {
037: // ----------------------------------------------------------------- Basics
038: public TestActionConfigMatcher(String name) {
039: super (name);
040: }
041:
042: public static void main(String[] args) {
043: junit.awtui.TestRunner
044: .main(new String[] { TestActionConfigMatcher.class
045: .getName() });
046: }
047:
048: public static Test suite() {
049: return (new TestSuite(TestActionConfigMatcher.class));
050: }
051:
052: // ----------------------------------------------------- Instance Variables
053: // ----------------------------------------------------- Setup and Teardown
054: public void setUp() {
055: super .setUp();
056: }
057:
058: public void tearDown() {
059: super .tearDown();
060: }
061:
062: // ------------------------------------------------------- Individual Tests
063: // ---------------------------------------------------------- match()
064: public void testNoMatch() {
065: ActionConfig[] configs = new ActionConfig[1];
066: ActionConfig mapping = buildActionConfig("/foo");
067:
068: configs[0] = mapping;
069:
070: ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
071:
072: assertNull("ActionConfig shouldn't be matched", matcher
073: .match("/test"));
074: }
075:
076: public void testNoWildcardMatch() {
077: ActionConfig[] configs = new ActionConfig[1];
078: ActionConfig mapping = buildActionConfig("/fooBar");
079:
080: configs[0] = mapping;
081:
082: ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
083:
084: assertNull("ActionConfig shouldn't be matched", matcher
085: .match("/fooBar"));
086: }
087:
088: public void testShouldMatch() {
089: ActionConfig[] configs = new ActionConfig[1];
090: ActionConfig mapping = buildActionConfig("/foo*");
091:
092: configs[0] = mapping;
093:
094: ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
095:
096: ActionConfig matched = matcher.match("/fooBar");
097:
098: assertNotNull("ActionConfig should be matched", matched);
099: assertTrue("ActionConfig should have two action forward",
100: matched.findForwardConfigs().length == 2);
101: assertTrue("ActionConfig should have two exception forward",
102: matched.findExceptionConfigs().length == 2);
103: assertTrue("ActionConfig should have properties", matched
104: .getProperties().size() == 2);
105: }
106:
107: public void testCheckSubstitutionsMatch() {
108: ActionConfig[] configs = new ActionConfig[1];
109: ActionConfig mapping = buildActionConfig("/foo*");
110:
111: configs[0] = mapping;
112:
113: ActionConfigMatcher matcher = new ActionConfigMatcher(configs);
114: ActionConfig m = matcher.match("/fooBar");
115:
116: assertTrue("Name hasn't been replaced", "name,Bar".equals(m
117: .getName()));
118: assertTrue("Path hasn't been replaced", "/fooBar".equals(m
119: .getPath()));
120: assertTrue("Scope isn't correct", "request"
121: .equals(m.getScope()));
122: assertTrue("Unknown isn't correct", !m.getUnknown());
123: assertTrue("Validate isn't correct", m.getValidate());
124:
125: assertTrue("Prefix hasn't been replaced", "foo,Bar".equals(m
126: .getPrefix()));
127: assertTrue("Suffix hasn't been replaced", "bar,Bar".equals(m
128: .getSuffix()));
129: assertTrue("Type hasn't been replaced", "foo.bar.BarAction"
130: .equals(m.getType()));
131: assertTrue("Roles hasn't been replaced", "public,Bar".equals(m
132: .getRoles()));
133: assertTrue("Parameter hasn't been replaced", "param,Bar"
134: .equals(m.getParameter()));
135: assertTrue("Attribute hasn't been replaced", "attrib,Bar"
136: .equals(m.getAttribute()));
137: assertTrue("Forward hasn't been replaced", "fwd,Bar".equals(m
138: .getForward()));
139: assertTrue("Include hasn't been replaced", "include,Bar"
140: .equals(m.getInclude()));
141: assertTrue("Input hasn't been replaced", "input,Bar".equals(m
142: .getInput()));
143:
144: assertTrue("ActionConfig property not replaced", "testBar"
145: .equals(m.getProperty("testprop2")));
146:
147: ForwardConfig[] fConfigs = m.findForwardConfigs();
148: boolean found = false;
149:
150: for (int x = 0; x < fConfigs.length; x++) {
151: ForwardConfig cfg = fConfigs[x];
152:
153: if ("name".equals(cfg.getName())) {
154: found = true;
155: assertTrue("Path hasn't been replaced", "path,Bar"
156: .equals(cfg.getPath()));
157: assertTrue("Property foo hasn't been replaced",
158: "bar,Bar".equals(cfg.getProperty("foo")));
159: assertTrue("Module hasn't been replaced", "modBar"
160: .equals(cfg.getModule()));
161: }
162: }
163:
164: assertTrue("The forward config 'name' cannot be found", found);
165: }
166:
167: public void testCheckMultipleSubstitutions() {
168: ActionMapping[] mapping = new ActionMapping[1];
169:
170: mapping[0] = new ActionMapping();
171: mapping[0].setPath("/foo*");
172: mapping[0].setName("name,{1}-{1}");
173:
174: ActionConfigMatcher matcher = new ActionConfigMatcher(mapping);
175: ActionConfig m = matcher.match("/fooBar");
176:
177: assertTrue("Name hasn't been replaced correctly: "
178: + m.getName(), "name,Bar-Bar".equals(m.getName()));
179: }
180:
181: private ActionConfig buildActionConfig(String path) {
182: ActionMapping mapping = new ActionMapping();
183:
184: mapping.setName("name,{1}");
185: mapping.setPath(path);
186: mapping.setScope("request");
187: mapping.setUnknown(false);
188: mapping.setValidate(true);
189:
190: mapping.setPrefix("foo,{1}");
191: mapping.setSuffix("bar,{1}");
192:
193: mapping.setType("foo.bar.{1}Action");
194: mapping.setRoles("public,{1}");
195: mapping.setParameter("param,{1}");
196: mapping.setAttribute("attrib,{1}");
197: mapping.setForward("fwd,{1}");
198: mapping.setInclude("include,{1}");
199: mapping.setInput("input,{1}");
200:
201: ForwardConfig cfg = new ActionForward();
202:
203: cfg.setName("name");
204: cfg.setPath("path,{1}");
205: cfg.setModule("mod{1}");
206: cfg.setProperty("foo", "bar,{1}");
207: mapping.addForwardConfig(cfg);
208:
209: cfg = new ActionForward();
210: cfg.setName("name2");
211: cfg.setPath("path2");
212: cfg.setModule("mod{1}");
213: mapping.addForwardConfig(cfg);
214:
215: ExceptionConfig excfg = new ExceptionConfig();
216:
217: excfg.setKey("foo");
218: excfg.setType("foo.Bar");
219: excfg.setPath("path");
220: mapping.addExceptionConfig(excfg);
221:
222: excfg = new ExceptionConfig();
223: excfg.setKey("foo2");
224: excfg.setType("foo.Bar2");
225: excfg.setPath("path2");
226: mapping.addExceptionConfig(excfg);
227:
228: mapping.setProperty("testprop", "testval");
229: mapping.setProperty("testprop2", "test{1}");
230:
231: mapping.freeze();
232:
233: return mapping;
234: }
235: }
|