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