001: /*
002: * $Id: TestActionRedirect.java 514052 2007-03-03 02:00:37Z pbenedict $
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.action;
022:
023: import junit.framework.AssertionFailedError;
024: import junit.framework.ComparisonFailure;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import java.util.Map;
029:
030: /**
031: * <p>Unit tests for the {@link ActionRedirect} class.</p>
032: *
033: * @version $Rev: 514052 $ $Date: 2007-03-02 20:00:37 -0600 (Fri, 02 Mar 2007) $
034: */
035: public class TestActionRedirect extends TestCase {
036: public TestActionRedirect(String s) {
037: super (s);
038: }
039:
040: public static TestSuite getSuite() {
041: return new TestSuite(TestActionRedirect.class);
042: }
043:
044: public static void main(String[] args) {
045: junit.textui.TestRunner runner = new junit.textui.TestRunner();
046:
047: runner.doRun(TestActionRedirect.getSuite());
048: }
049:
050: // ----------------------------------------------------- Test Methods
051:
052: /**
053: * Check that the redirect flag is set.
054: */
055: public void testActionRedirectRedirectFlag() {
056: ActionRedirect ar = new ActionRedirect("/path.do");
057:
058: assertTrue("Redirect flag should be set to true.", ar
059: .getRedirect());
060: }
061:
062: /**
063: * Test all addParameter methods accepting different data types.
064: */
065: public void testActionRedirectAddParameter() {
066: ActionRedirect ar = new ActionRedirect("/path.do");
067:
068: ar.addParameter("st", "test");
069: ar.addParameter("obj", new StringBuffer("someString"));
070:
071: assertTrue("Incorrect path",
072: ar.getPath().indexOf("/path.do") == 0);
073: assertHasParameter(ar.parameterValues, "st", "test");
074: assertHasParameter(ar.parameterValues, "obj", "someString");
075: }
076:
077: /**
078: * Test redirect with anchor.
079: */
080: public void testActionRedirectWithAnchor() {
081: ActionRedirect ar = new ActionRedirect("/path.do");
082:
083: ar.addParameter("st", "test");
084: ar.setAnchor("foo");
085:
086: assertTrue("Incorrect path", "/path.do?st=test#foo".equals(ar
087: .getPath()));
088: }
089:
090: /**
091: * Test adding parameters with the same name.
092: */
093: public void testActionRedirectAddSameNameParameter() {
094: ActionRedirect ar = new ActionRedirect("/path.do");
095:
096: ar.addParameter("param", "param1");
097: ar.addParameter("param", "param2");
098: ar.addParameter("param", new StringBuffer("someString"));
099:
100: assertTrue("Incorrect path",
101: ar.getPath().indexOf("/path.do") == 0);
102: assertHasParameter(ar.parameterValues, "param", "param1");
103: assertHasParameter(ar.parameterValues, "param", "param2");
104: assertHasParameter(ar.parameterValues, "param", "someString");
105: assertEquals("Incorrect number of parameters", 3,
106: countParameters(ar.parameterValues, "param"));
107: }
108:
109: /**
110: * Test creating an ActionRedirect which copies its configuration from an
111: * existing ActionForward (except for the "redirect" property).
112: */
113: public void testActionRedirectFromExistingForward() {
114: ActionForward forward = new ActionForward(
115: "/path.do?param=param1");
116: forward.setRedirect(false);
117: forward.setProperty("key", "value");
118:
119: ActionRedirect ar = new ActionRedirect(forward);
120:
121: ar.addParameter("param", "param2");
122: ar.addParameter("object1", new StringBuffer("someString"));
123:
124: assertTrue("Incorrect path",
125: ar.getPath().indexOf("/path.do") == 0);
126: assertHasParameter(ar.parameterValues, "param", "param2");
127: assertHasParameter(ar.parameterValues, "object1", "someString");
128: assertEquals("Incorrect original path.", forward.getPath(), ar
129: .getOriginalPath());
130: assertEquals("Incorrect or missing property", "value", ar
131: .getProperty("key"));
132: assertTrue("Original had redirect to false", ar.getRedirect());
133: }
134:
135: /**
136: * Assert that the given parameters contains an entry for
137: * <code>paramValue</code> under the <code>paramName</code> key. <p/>
138: *
139: * @param parameters the map of parameters to check into
140: * @param paramName the key of the value to be checked
141: * @param paramValue the value to check for
142: */
143: static void assertHasParameter(Map parameters, String paramName,
144: String paramValue) {
145: Object value = parameters.get(paramName);
146:
147: if (value == null) {
148: throw new AssertionFailedError("Parameter [" + paramName
149: + "] not found");
150: }
151:
152: if (value instanceof String) {
153: if (!paramValue.equals(value)) {
154: throw new ComparisonFailure("Incorrect value found",
155: paramValue, (String) value);
156: }
157: } else if (value instanceof String[]) {
158: // see if our value is among those in the array
159: String[] values = (String[]) value;
160:
161: for (int i = 0; i < values.length; i++) {
162: if (paramValue.equals(values[i])) {
163: return;
164: }
165: }
166:
167: throw new AssertionFailedError(
168: "Expected value not found for parameter ["
169: + paramName + "]");
170: } else {
171: // can't recognize the value
172: throw new AssertionFailedError(
173: "Unexpected type found as parameter value for ["
174: + paramName + "]");
175: }
176: }
177:
178: /**
179: * Determine the number of values that are available for a specific
180: * parameter. <p/>
181: *
182: * @param parameters the map of parameters to check into
183: * @param paramName the key of the value(s) to count
184: * @return the number of values for the specified parameter
185: */
186: static int countParameters(Map parameters, String paramName) {
187: Object value = parameters.get(paramName);
188:
189: if (value == null) {
190: return 0;
191: }
192:
193: if (value instanceof String) {
194: return 1;
195: } else if (value instanceof String[]) {
196: String[] values = (String[]) value;
197:
198: return values.length;
199: } else {
200: // can't recognize the value
201: throw new AssertionFailedError(
202: "Unexpected type found as parameter value for ["
203: + paramName + "]");
204: }
205: }
206: }
|