1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.action;
19
20 import junit.framework.AssertionFailedError;
21 import junit.framework.ComparisonFailure;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 import java.util.Map;
26
27 /***
28 * <p>Unit tests for the {@link ActionRedirect} class.</p>
29 *
30 * @version $Rev: 421119 $ $Date: 2006-07-11 21:49:11 -0700 (Tue, 11 Jul 2006) $
31 */
32 public class TestActionRedirect extends TestCase {
33 public TestActionRedirect(String s) {
34 super(s);
35 }
36
37 public static TestSuite getSuite() {
38 return new TestSuite(TestActionRedirect.class);
39 }
40
41 public static void main(String[] args) {
42 junit.textui.TestRunner runner = new junit.textui.TestRunner();
43
44 runner.doRun(TestActionRedirect.getSuite());
45 }
46
47
48
49 /***
50 * Check that the redirect flag is set.
51 */
52 public void testActionRedirectRedirectFlag() {
53 ActionRedirect ar = new ActionRedirect("/path.do");
54
55 assertTrue("Redirect flag should be set to true.", ar.getRedirect());
56 }
57
58 /***
59 * Test all addParameter methods accepting different data types.
60 */
61 public void testActionRedirectAddParameter() {
62 ActionRedirect ar = new ActionRedirect("/path.do");
63
64 ar.addParameter("st", "test");
65 ar.addParameter("obj", new StringBuffer("someString"));
66
67 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
68 assertHasParameter(ar.parameterValues, "st", "test");
69 assertHasParameter(ar.parameterValues, "obj", "someString");
70 }
71
72 /***
73 * Test redirect with anchor.
74 */
75 public void testActionRedirectWithAnchor() {
76 ActionRedirect ar = new ActionRedirect("/path.do");
77
78 ar.addParameter("st", "test");
79 ar.setAnchor("foo");
80
81 assertTrue("Incorrect path", "/path.do?st=test#foo".equals(ar.getPath()));
82 }
83
84 /***
85 * Test adding parameters with the same name.
86 */
87 public void testActionRedirectAddSameNameParameter() {
88 ActionRedirect ar = new ActionRedirect("/path.do");
89
90 ar.addParameter("param", "param1");
91 ar.addParameter("param", "param2");
92 ar.addParameter("param", new StringBuffer("someString"));
93
94 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
95 assertHasParameter(ar.parameterValues, "param", "param1");
96 assertHasParameter(ar.parameterValues, "param", "param2");
97 assertHasParameter(ar.parameterValues, "param", "someString");
98 assertEquals("Incorrect number of parameters", 3,
99 countParameters(ar.parameterValues, "param"));
100 }
101
102 /***
103 * Test creating an ActionRedirect which copies its configuration from an
104 * existing ActionForward.
105 */
106 public void testActionRedirectFromExistingForward() {
107 ActionForward forward = new ActionForward("/path.do?param=param1");
108 forward.setRedirect(false);
109
110 ActionRedirect ar = new ActionRedirect(forward);
111
112 ar.addParameter("param", "param2");
113 ar.addParameter("object1", new StringBuffer("someString"));
114
115 assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
116 assertHasParameter(ar.parameterValues, "param", "param2");
117 assertHasParameter(ar.parameterValues, "object1", "someString");
118 assertEquals("Incorrect original path.", forward.getPath(),
119 ar.getOriginalPath());
120 assertTrue("Original had redirect to false", !ar.getRedirect());
121 }
122
123 /***
124 * Assert that the given parameters contains an entry for
125 * <code>paramValue</code> under the <code>paramName</code> key. <p/>
126 *
127 * @param parameters the map of parameters to check into
128 * @param paramName the key of the value to be checked
129 * @param paramValue the value to check for
130 */
131 static void assertHasParameter(Map parameters, String paramName,
132 String paramValue) {
133 Object value = parameters.get(paramName);
134
135 if (value == null) {
136 throw new AssertionFailedError("Parameter [" + paramName
137 + "] not found");
138 }
139
140 if (value instanceof String) {
141 if (!paramValue.equals(value)) {
142 throw new ComparisonFailure("Incorrect value found",
143 paramValue, (String) value);
144 }
145 } else if (value instanceof String[]) {
146
147 String[] values = (String[]) value;
148
149 for (int i = 0; i < values.length; i++) {
150 if (paramValue.equals(values[i])) {
151 return;
152 }
153 }
154
155 throw new AssertionFailedError(
156 "Expected value not found for parameter [" + paramName + "]");
157 } else {
158
159 throw new AssertionFailedError(
160 "Unexpected type found as parameter value for [" + paramName
161 + "]");
162 }
163 }
164
165 /***
166 * Determine the number of values that are available for a specific
167 * parameter. <p/>
168 *
169 * @param parameters the map of parameters to check into
170 * @param paramName the key of the value(s) to count
171 * @return the number of values for the specified parameter
172 */
173 static int countParameters(Map parameters, String paramName) {
174 Object value = parameters.get(paramName);
175
176 if (value == null) {
177 return 0;
178 }
179
180 if (value instanceof String) {
181 return 1;
182 } else if (value instanceof String[]) {
183 String[] values = (String[]) value;
184
185 return values.length;
186 } else {
187
188 throw new AssertionFailedError(
189 "Unexpected type found as parameter value for [" + paramName
190 + "]");
191 }
192 }
193 }