1 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 45 public class TestDefaultServletRequestWrapper extends DefaultTestCase { 46 private static String testClass = TestDefaultServletRequestWrapper.class.getName(); 48 private static Logger logger = Logger.getLogger("test."+testClass); 49 50 52 56 public TestDefaultServletRequestWrapper(String name) { 57 super(name); 58 } 59 60 67 public static void main(String args[]) { 68 TestUtil.parseParams(args); 70 71 if (TestUtil.BATCH_MODE) junit.textui.TestRunner.main(new String[] {testClass}); 73 else junit.swingui.TestRunner.main(new String[] {testClass}); 74 } 75 76 77 83 88 public void testMockup() { 89 if (logger.isInfoEnabled()) logger.info("testing servlet request mockup"); 90 91 MockHttpServletRequest req = null; 93 94 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 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 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 133 public void testWrapper() { 134 if (logger.isInfoEnabled()) logger.info("testing servlet request wrapper"); 135 136 MockHttpServletRequest req = null; 138 DefaultServletRequestWrapper wrapper = null; 139 140 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 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 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 |