KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > action > TestActionRedirect


1 /*
2  * $Id: TestActionRedirect.java 164750 2005-04-26 06:23:59Z hrabago $
3  *
4  * Copyright 2000-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
19 package org.apache.struts.action;
20
21 import junit.framework.TestSuite;
22 import junit.framework.TestCase;
23 import junit.framework.ComparisonFailure;
24 import junit.framework.AssertionFailedError;
25
26 import java.util.Map JavaDoc;
27
28 /**
29  * <p>Unit tests for the {@link ActionRedirect} class.</p>
30  *
31  * @version $Rev: 164750 $ $Date: 2005-04-26 07:23:59 +0100 (Tue, 26 Apr 2005) $
32  */

33 public class TestActionRedirect extends TestCase {
34
35     
36     public TestActionRedirect(String JavaDoc s) {
37         super(s);
38     }
39
40     
41     public static TestSuite getSuite() {
42         return new TestSuite(TestActionRedirect.class);
43     }
44
45     
46     public static void main(String JavaDoc[] args) {
47         junit.textui.TestRunner runner = new junit.textui.TestRunner();
48         runner.doRun(TestActionRedirect.getSuite());
49     }
50
51    
52     
53     // ----------------------------------------------------- Test Methods
54

55     
56     /**
57      * Check that the redirect flag is set.
58      */

59     public void testActionRedirectRedirectFlag() {
60         ActionRedirect ar = new ActionRedirect("/path.do");
61         assertTrue("Redirect flag should be set to true.",ar.getRedirect());
62     }
63
64
65     /**
66      * Test all addParameter methods accepting different data types.
67      */

68     public void testActionRedirectAddParameter() {
69         ActionRedirect ar = new ActionRedirect("/path.do");
70
71         ar.addParameter("st","test");
72         ar.addParameter("obj",new StringBuffer JavaDoc("someString"));
73         
74         assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
75         assertHasParameter(ar.parameterValues, "st", "test");
76         assertHasParameter(ar.parameterValues, "obj", "someString");
77     }
78
79     
80     /**
81      * Test adding parameters with the same name.
82      */

83     public void testActionRedirectAddSameNameParameter() {
84         ActionRedirect ar = new ActionRedirect("/path.do");
85
86         ar.addParameter("param","param1");
87         ar.addParameter("param","param2");
88         ar.addParameter("param",new StringBuffer JavaDoc("someString"));
89         
90         assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
91         assertHasParameter(ar.parameterValues, "param", "param1");
92         assertHasParameter(ar.parameterValues, "param", "param2");
93         assertHasParameter(ar.parameterValues, "param", "someString");
94         assertEquals("Incorrect number of parameters", 3, countParameters(ar.parameterValues, "param"));
95     }
96
97     
98     /**
99      * Test creating an ActionRedirect which copies its configuration
100      * from an existing ActionForward.
101      */

102     public void testActionRedirectFromExistingForward() {
103         ActionForward forward = new ActionForward("/path.do?param=param1");
104
105         ActionRedirect ar = new ActionRedirect(forward);
106
107         ar.addParameter("param","param2");
108         ar.addParameter("object1",new StringBuffer JavaDoc("someString"));
109
110         assertTrue("Incorrect path", ar.getPath().indexOf("/path.do") == 0);
111         assertHasParameter(ar.parameterValues, "param", "param2");
112         assertHasParameter(ar.parameterValues, "object1", "someString");
113         assertEquals("Incorrect original path.",forward.getPath(),ar.getOriginalPath());
114     }
115
116
117
118     /**
119      * Assert that the given parameters contains an entry for
120      * <code>paramValue</code> under the <code>paramName</code> key.
121      * <p/>
122      * @param parameters the map of parameters to check into
123      * @param paramName the key of the value to be checked
124      * @param paramValue the value to check for
125      */

126     static void assertHasParameter(
127             Map JavaDoc parameters,
128             String JavaDoc paramName,
129             String JavaDoc paramValue) {
130         Object JavaDoc value = parameters.get(paramName);
131         if (value == null) {
132             throw new AssertionFailedError("Parameter [" + paramName + "] not found");
133         }
134         
135         if (value instanceof String JavaDoc) {
136             if (!paramValue.equals(value)) {
137                 throw new ComparisonFailure("Incorrect value found",
138                         paramValue, (String JavaDoc) value);
139             }
140         } else if (value instanceof String JavaDoc[]) {
141             // see if our value is among those in the array
142
String JavaDoc[] values = (String JavaDoc[]) value;
143             for (int i = 0; i < values.length; i++) {
144                 if (paramValue.equals(values[i])) {
145                     return;
146                 }
147             }
148             throw new AssertionFailedError("Expected value not found for parameter [" + paramName + "]");
149         } else {
150             // can't recognize the value
151
throw new AssertionFailedError("Unexpected type found as parameter value for [" + paramName + "]");
152         }
153     }
154     
155     /**
156      * Determine the number of values that are available for a
157      * specific parameter.
158      * <p/>
159      * @param parameters the map of parameters to check into
160      * @param paramName the key of the value(s) to count
161      * @return the number of values for the specified parameter
162      */

163     static int countParameters(Map JavaDoc parameters,
164                                String JavaDoc paramName) {
165         Object JavaDoc value = parameters.get(paramName);
166         if (value == null) {
167             return 0;
168         }
169         
170         if (value instanceof String JavaDoc) {
171             return 1;
172         } else if (value instanceof String JavaDoc[]) {
173             String JavaDoc[] values = (String JavaDoc[]) value;
174             return values.length;
175         } else {
176             // can't recognize the value
177
throw new AssertionFailedError("Unexpected type found as parameter value for [" + paramName + "]");
178         }
179     }
180     
181
182 }
183
Popular Tags