KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > helper > servlet > TestDefaultServletRequestWrapper


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: TestDefaultServletRequestWrapper.java,v 1.1 2004/02/01 05:16:33 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.helper.servlet;
21
22 import java.io.*;
23 import java.util.*;
24 import java.security.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.w3c.dom.*;
29 import junit.framework.*;
30
31 import org.enhydra.barracuda.plankton.*;
32 import org.enhydra.barracuda.plankton.data.*;
33 import org.enhydra.barracuda.core.util.dom.*;
34 import org.apache.log4j.*;
35 import org.enhydra.barracuda.core.view.*;
36 import org.enhydra.barracuda.examples.xmlc.*;
37 import org.enhydra.barracuda.testbed.*;
38 import org.enhydra.barracuda.testbed.servlet.*;
39
40
41 /**
42  * This test verifies that we can use the ServletRequestWrapper
43  * to add/remove the param values
44  */

45 public class TestDefaultServletRequestWrapper extends DefaultTestCase {
46     //common vars (customize for every test class)
47
private static String testClass = TestDefaultServletRequestWrapper.class.getName();
48     private static Logger logger = Logger.getLogger("test."+testClass);
49
50     //variables
51

52     //-------------------- Basics --------------------------------
53
/**
54      * Public Constructor
55      */

56     public TestDefaultServletRequestWrapper(String name) {
57         super(name);
58     }
59
60     /**
61      * Every test class should have a main method so it can be run
62      * directly (when debugging tests you often will not want to run
63      * the whole suite)
64      *
65      * @param args defined in test.util.TestUtil
66      */

67     public static void main(String args[]) {
68         //check for standard runtime parameters
69
TestUtil.parseParams(args);
70
71         //launch the test
72
if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String[] {testClass});
73         else junit.swingui.TestRunner.main(new String[] {testClass});
74     }
75
76
77     //-------------------- Actual Tests --------------------------
78
//Note: all the methods herein should follow the testXXXX naming convention
79
//Also keep in mind that local vars set in one test method are NOT retained
80
//when the next method is invoked because JUnit makes a separate instance of
81
//the test class for each testXXXX method!!!
82

83     /**
84      * Simple test to make sure the mockup servlet request to
85      * make sure it functions as expected (ie. the necessary methods
86      * do what the wrapper class will expect them to do)
87      */

88     public void testMockup() {
89         if (logger.isInfoEnabled()) logger.info("testing servlet request mockup");
90
91         //vars
92
MockHttpServletRequest req = null;
93
94         //try something simple first (to make sure our mockup is working)
95
//...null param string
96
req = new MockHttpServletRequest(null);
97         assertTrue("mockup check 1a failed - param string not converted correctly", req.paramList.size()==0);
98         assertTrue("mockup check 1b.1 failed - returned bad value", req.getParameter("foo1")==null);
99         assertTrue("mockup check 1c.1 failed - returned non-empty enum", getSize(req.getParameterNames())==0);
100         assertTrue("mockup check 1d.1 failed - returned non-null values", req.getParameterValues("foo1")==null);
101
102         //...unique param values
103
req = new MockHttpServletRequest("foo1=blah1&foo2=blah2&foo3=blah3");
104         assertTrue("mockup check 2a failed - param string not converted correctly", req.paramList.size()==3);
105         assertTrue("mockup check 2b.1 failed - returned bad value", "blah1".equals(req.getParameter("foo1")));
106         assertTrue("mockup check 2b.2 failed - returned bad value", "blah2".equals(req.getParameter("foo2")));
107         assertTrue("mockup check 2b.3 failed - returned bad value", "blah3".equals(req.getParameter("foo3")));
108         assertTrue("mockup check 2b.4 failed - returned bad value", req.getParameter("foo4")==null);
109         assertTrue("mockup check 2c.1 failed - returned wrong enum size", getSize(req.getParameterNames())==3);
110         assertTrue("mockup check 2d.1 failed - returned wrong length", req.getParameterValues("foo1").length==1);
111         assertTrue("mockup check 2d.2 failed - returned wrong length", req.getParameterValues("foo2").length==1);
112         assertTrue("mockup check 2d.3 failed - returned wrong length", req.getParameterValues("foo3").length==1);
113         assertTrue("mockup check 2d.4 failed - returned wrong length", req.getParameterValues("foo4")==null);
114
115         //...duplicate param values
116
req = new MockHttpServletRequest("foo1=blah1.a&foo2=blah2&foo3=blah3&foo1=blah1.b&foo1=blah1.c");
117         assertTrue("mockup check 3a failed - param string not converted correctly", req.paramList.size()==5);
118         assertTrue("mockup check 3b.1 failed - returned bad value", "blah1.a".equals(req.getParameter("foo1")));
119         assertTrue("mockup check 3b.2 failed - returned bad value", "blah2".equals(req.getParameter("foo2")));
120         assertTrue("mockup check 3b.3 failed - returned bad value", "blah3".equals(req.getParameter("foo3")));
121         assertTrue("mockup check 3b.4 failed - returned bad value", req.getParameter("foo4")==null);
122         assertTrue("mockup check 3c.1 failed - returned wrong enum size", getSize(req.getParameterNames())==3);
123         assertTrue("mockup check 3d.1 failed - returned wrong length", req.getParameterValues("foo1").length==3);
124         assertTrue("mockup check 3d.2 failed - returned wrong length", req.getParameterValues("foo2").length==1);
125         assertTrue("mockup check 3d.3 failed - returned wrong length", req.getParameterValues("foo3").length==1);
126         assertTrue("mockup check 3d.4 failed - returned wrong length", req.getParameterValues("foo4")==null);
127     }
128
129     /**
130      * Simple test to make sure the wrapper allows us to
131      * add/remove parameters properly.
132      */

133     public void testWrapper() {
134         if (logger.isInfoEnabled()) logger.info("testing servlet request wrapper");
135
136         //vars
137
MockHttpServletRequest req = null;
138         DefaultServletRequestWrapper wrapper = null;
139
140         //try something simple first (to make sure our mockup is working)
141
//...null param string
142
req = new MockHttpServletRequest(null);
143         wrapper = new DefaultServletRequestWrapper(req);
144         assertTrue("wrapper check 1a failed - param string not converted correctly", req.paramList.size()==0);
145         assertTrue("wrapper check 1b failed - param string not converted correctly", wrapper.paramList==null);
146         assertTrue("wrapper check 1b.1 failed - returned bad value", wrapper.getParameter("foo1")==null);
147         assertTrue("wrapper check 1c.1 failed - returned non-empty enum", getSize(wrapper.getParameterNames())==0);
148         assertTrue("wrapper check 1d.1 failed - returned non-null values", wrapper.getParameterValues("foo1")==null);
149         wrapper.addParameter("foo99","blah1.w");
150         wrapper.addParameter("foo99","blah1.x");
151         wrapper.addParameter("foo99","blah1.y");
152         wrapper.addParameter("foo99","blah1.z");
153         assertTrue("wrapper check 1e.1 failed - returned bad value", "blah1.w".equals(wrapper.getParameter("foo99")));
154         assertTrue("wrapper check 1e.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==4);
155         wrapper.removeParameter("foo99");
156         assertTrue("wrapper check 1f.1 failed - returned bad value", "blah1.x".equals(wrapper.getParameter("foo99")));
157         assertTrue("wrapper check 1f.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==3);
158         wrapper.removeAllParameters("foo99");
159         assertTrue("wrapper check 1g.1 failed - returned bad value", wrapper.getParameter("foo99")==null);
160         assertTrue("wrapper check 1g.2 failed - returned wrong length", wrapper.getParameterValues("foo99")==null);
161         assertTrue("wrapper check 1g failed - param list wrong size", wrapper.paramList!=null);
162         wrapper.resetParameters();
163         assertTrue("wrapper check 1h failed - param list wrong size", wrapper.paramList==null);
164
165         //...unique param values
166
req = new MockHttpServletRequest("foo1=blah1&foo2=blah2&foo3=blah3");
167         wrapper = new DefaultServletRequestWrapper(req);
168         assertTrue("wrapper check 2a.1 failed - param string not converted correctly", req.paramList.size()==3);
169         assertTrue("wrapper check 2a.2 failed - param string not converted correctly", wrapper.paramList==null);
170         assertTrue("wrapper check 2b.1 failed - returned bad value", "blah1".equals(wrapper.getParameter("foo1")));
171         assertTrue("wrapper check 2b.2 failed - returned bad value", "blah2".equals(wrapper.getParameter("foo2")));
172         assertTrue("wrapper check 2b.3 failed - returned bad value", "blah3".equals(wrapper.getParameter("foo3")));
173         assertTrue("wrapper check 2b.4 failed - returned bad value", wrapper.getParameter("foo4")==null);
174         assertTrue("wrapper check 2c.1 failed - returned wrong enum size", getSize(wrapper.getParameterNames())==3);
175         assertTrue("wrapper check 2d.1 failed - returned wrong length", wrapper.getParameterValues("foo1").length==1);
176         assertTrue("wrapper check 2d.2 failed - returned wrong length", wrapper.getParameterValues("foo2").length==1);
177         assertTrue("wrapper check 2d.3 failed - returned wrong length", wrapper.getParameterValues("foo3").length==1);
178         assertTrue("wrapper check 2d.4 failed - returned wrong length", wrapper.getParameterValues("foo4")==null);
179         wrapper.addParameter("foo99","blah1.w");
180         wrapper.addParameter("foo99","blah1.x");
181         wrapper.addParameter("foo99","blah1.y");
182         wrapper.addParameter("foo99","blah1.z");
183         assertTrue("wrapper check 2e.1 failed - returned bad value", "blah1.w".equals(wrapper.getParameter("foo99")));
184         assertTrue("wrapper check 2e.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==4);
185         wrapper.removeParameter("foo99");
186         assertTrue("wrapper check 2f.1 failed - returned bad value", "blah1.x".equals(wrapper.getParameter("foo99")));
187         assertTrue("wrapper check 2f.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==3);
188         wrapper.removeAllParameters("foo99");
189         assertTrue("wrapper check 2g.1 failed - returned bad value", wrapper.getParameter("foo99")==null);
190         assertTrue("wrapper check 2g.2 failed - returned wrong length", wrapper.getParameterValues("foo99")==null);
191         assertTrue("wrapper check 2g failed - param list wrong size", wrapper.paramList!=null);
192         wrapper.resetParameters();
193         assertTrue("wrapper check 2h failed - param list wrong size", wrapper.paramList==null);
194
195         //...duplicate param values
196
req = new MockHttpServletRequest("foo1=blah1.a&foo2=blah2&foo3=blah3&foo1=blah1.b&foo1=blah1.c");
197         wrapper = new DefaultServletRequestWrapper(req);
198         assertTrue("wrapper check 3a.1 failed - param string not converted correctly", req.paramList.size()==5);
199         assertTrue("wrapper check 3a.2 failed - param string not converted correctly", wrapper.paramList==null);
200         assertTrue("wrapper check 3b.1 failed - returned bad value", "blah1.a".equals(wrapper.getParameter("foo1")));
201         assertTrue("wrapper check 3b.2 failed - returned bad value", "blah2".equals(wrapper.getParameter("foo2")));
202         assertTrue("wrapper check 3b.3 failed - returned bad value", "blah3".equals(wrapper.getParameter("foo3")));
203         assertTrue("wrapper check 3b.4 failed - returned bad value", wrapper.getParameter("foo4")==null);
204         assertTrue("wrapper check 3c.1 failed - returned wrong enum size", getSize(wrapper.getParameterNames())==3);
205         assertTrue("wrapper check 3d.1 failed - returned wrong length", wrapper.getParameterValues("foo1").length==3);
206         assertTrue("wrapper check 3d.2 failed - returned wrong length", wrapper.getParameterValues("foo2").length==1);
207         assertTrue("wrapper check 3d.3 failed - returned wrong length", wrapper.getParameterValues("foo3").length==1);
208         assertTrue("wrapper check 3d.4 failed - returned wrong length", wrapper.getParameterValues("foo4")==null);
209         wrapper.addParameter("foo99","blah1.w");
210         wrapper.addParameter("foo99","blah1.x");
211         wrapper.addParameter("foo99","blah1.y");
212         wrapper.addParameter("foo99","blah1.z");
213         assertTrue("wrapper check 3e.1 failed - returned bad value", "blah1.w".equals(wrapper.getParameter("foo99")));
214         assertTrue("wrapper check 3e.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==4);
215         wrapper.removeParameter("foo99");
216         assertTrue("wrapper check 3f.1 failed - returned bad value", "blah1.x".equals(wrapper.getParameter("foo99")));
217         assertTrue("wrapper check 3f.2 failed - returned wrong length", wrapper.getParameterValues("foo99").length==3);
218         wrapper.removeAllParameters("foo99");
219         assertTrue("wrapper check 3g.1 failed - returned bad value", wrapper.getParameter("foo99")==null);
220         assertTrue("wrapper check 3g.2 failed - returned wrong length", wrapper.getParameterValues("foo99")==null);
221         assertTrue("wrapper check 3g failed - param list wrong size", wrapper.paramList!=null);
222         wrapper.resetParameters();
223         assertTrue("wrapper check 3h failed - param list wrong size", wrapper.paramList==null);
224     }
225
226     private int getSize(Enumeration enum) {
227         int cntr = 0;
228         while (enum.hasMoreElements()) {
229             cntr++;
230             enum.nextElement();
231         }
232         return cntr;
233     }
234
235 }
236
Popular Tags