1 15 package org.apache.tapestry.request; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.Enumeration ; 20 import java.util.List ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 import javax.servlet.http.HttpSession ; 25 26 import org.apache.hivemind.test.HiveMindTestCase; 27 import org.easymock.MockControl; 28 29 35 public class TestRequestContext extends HiveMindTestCase 36 { 37 private HttpSession newSession() 38 { 39 return (HttpSession ) newMock(HttpSession .class); 40 } 41 42 private HttpServletRequest newRequest(boolean create, HttpSession session) 43 { 44 MockControl control = newControl(HttpServletRequest .class); 45 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 46 47 request.getSession(create); 48 control.setReturnValue(session); 49 50 return request; 51 } 52 53 private HttpServletRequest newRequest() 54 { 55 return (HttpServletRequest ) newMock(HttpServletRequest .class); 56 } 57 58 private HttpServletResponse newResponse() 59 { 60 return (HttpServletResponse ) newMock(HttpServletResponse .class); 61 } 62 63 public void testGetParameter() 64 { 65 MockControl control = newControl(HttpServletRequest .class); 66 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 67 HttpServletResponse response = newResponse(); 68 69 String value = "VALUE"; 70 71 request.getParameter("myparam"); 72 control.setReturnValue(value); 73 74 replayControls(); 75 76 RequestContext rc = new RequestContext(request, response); 77 78 assertSame(value, rc.getParameter("myparam")); 79 80 verifyControls(); 81 } 82 83 public void testGetParameters() 84 { 85 MockControl control = newControl(HttpServletRequest .class); 86 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 87 HttpServletResponse response = newResponse(); 88 89 String [] values = 90 { "this", "that" }; 91 92 request.getParameterValues("myparam"); 93 control.setReturnValue(values); 94 95 replayControls(); 96 97 RequestContext rc = new RequestContext(request, response); 98 99 assertSame(values, rc.getParameters("myparam")); 100 101 verifyControls(); 102 } 103 104 public void testGetParameterNames() 105 { 106 MockControl control = newControl(HttpServletRequest .class); 107 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 108 HttpServletResponse response = newResponse(); 109 110 List names = new ArrayList (); 111 names.add("fred"); 112 names.add("barney"); 113 114 Enumeration e = Collections.enumeration(names); 115 116 request.getParameterNames(); 117 control.setReturnValue(e); 118 119 replayControls(); 120 121 RequestContext rc = new RequestContext(request, response); 122 123 String [] pnames = rc.getParameterNames(); 124 125 assertListsEqual(new String [] 126 { "fred", "barney" }, pnames); 127 128 verifyControls(); 129 } 130 131 public void testGetRequestAndResponse() 132 { 133 HttpServletRequest request = newRequest(); 134 HttpServletResponse response = newResponse(); 135 136 replayControls(); 137 138 RequestContext rc = new RequestContext(request, response); 139 140 assertSame(request, rc.getRequest()); 141 assertSame(response, rc.getResponse()); 142 143 verifyControls(); 144 } 145 146 public void testGetAttribute() 147 { 148 MockControl control = newControl(HttpServletRequest .class); 149 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 150 HttpServletResponse response = newResponse(); 151 152 Object attribute = new Object (); 153 154 request.getAttribute("myattr"); 155 control.setReturnValue(attribute); 156 157 replayControls(); 158 159 RequestContext rc = new RequestContext(request, response); 160 161 assertSame(attribute, rc.getAttribute("myattr")); 162 163 verifyControls(); 164 } 165 166 public void testGetSession() 167 { 168 HttpSession session = newSession(); 169 HttpServletRequest request = newRequest(false, session); 170 HttpServletResponse response = newResponse(); 171 172 replayControls(); 173 174 RequestContext rc = new RequestContext(request, response); 175 176 assertSame(session, rc.getSession()); 177 178 verifyControls(); 179 } 180 181 public void testCreateSession() 182 { 183 HttpSession session = newSession(); 184 HttpServletRequest request = newRequest(true, session); 185 HttpServletResponse response = newResponse(); 186 187 replayControls(); 188 189 RequestContext rc = new RequestContext(request, response); 190 191 assertSame(session, rc.createSession()); 192 193 verifyControls(); 194 } 195 } | Popular Tags |