001: package com.meterware.pseudoserver;
002:
003: /********************************************************************************************************************
004: * $Id: HttpUserAgentTest.java,v 1.14 2004/09/24 20:13:16 russgold Exp $
005: *
006: * Copyright (c) 2002-2004, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.util.Vector;
024: import java.util.Enumeration;
025: import java.util.StringTokenizer;
026: import java.io.IOException;
027:
028: import junit.framework.TestCase;
029:
030: /**
031: * A base class for test cases that use the pseudo server.
032: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
033: **/
034: public class HttpUserAgentTest extends TestCase {
035:
036: private String _hostPath;
037: private PseudoServer _server;
038:
039: public HttpUserAgentTest(String name) {
040: super (name);
041: }
042:
043: public void setUp() throws Exception {
044: _server = new PseudoServer();
045: _hostPath = "http://localhost:" + _server.getConnectedPort();
046: }
047:
048: public void tearDown() throws Exception {
049: if (_server != null)
050: _server.shutDown();
051: }
052:
053: protected void defineResource(String resourceName,
054: PseudoServlet servlet) {
055: _server.setResource(resourceName, servlet);
056: }
057:
058: protected void defineResource(String resourceName, String value) {
059: _server.setResource(resourceName, value);
060: }
061:
062: protected void defineResource(String resourceName, byte[] value,
063: String contentType) {
064: _server.setResource(resourceName, value, contentType);
065: }
066:
067: protected void defineResource(String resourceName, String value,
068: int statusCode) {
069: _server.setErrorResource(resourceName, statusCode, value);
070: }
071:
072: protected void defineResource(String resourceName, String value,
073: String contentType) {
074: _server.setResource(resourceName, value, contentType);
075: }
076:
077: protected void addResourceHeader(String resourceName, String header) {
078: _server.addResourceHeader(resourceName, header);
079: }
080:
081: protected void setResourceCharSet(String resourceName,
082: String setName, boolean reportCharSet) {
083: _server.setCharacterSet(resourceName, setName);
084: _server.setSendCharacterSet(resourceName, reportCharSet);
085: }
086:
087: protected void defineWebPage(String pageName, String body) {
088: defineResource(pageName + ".html", "<html><head><title>"
089: + pageName + "</title></head>\n" + "<body>" + body
090: + "</body></html>");
091: }
092:
093: protected void mapToClasspath(String directory) {
094: _server.mapToClasspath(directory);
095: }
096:
097: protected PseudoServer getServer() {
098: return _server;
099: }
100:
101: protected void setServerDebug(boolean enabled) {
102: _server.setDebug(enabled);
103: }
104:
105: protected String getHostPath() {
106: return _hostPath;
107: }
108:
109: protected int getHostPort() throws IOException {
110: return _server.getConnectedPort();
111: }
112:
113: protected void assertEqualQueries(String query1, String query2) {
114: assertEquals(new QuerySpec(query1), new QuerySpec(query2));
115: }
116:
117: protected void assertEquals(String comment, Object[] expected,
118: Object[] found) {
119: if (!equals(expected, found)) {
120: fail(comment + " expected: " + asText(expected)
121: + " but found " + asText(found));
122: }
123: }
124:
125: private boolean equals(Object[] first, Object[] second) {
126: if (first.length != second.length)
127: return false;
128: for (int i = 0; i < first.length; i++) {
129: if (!first[i].equals(second[i]))
130: return false;
131: }
132: return true;
133: }
134:
135: protected void assertImplement(String comment, Object[] objects,
136: Class expectedClass) {
137: if (objects.length == 0)
138: fail("No " + comment + " found.");
139: for (int i = 0; i < objects.length; i++) {
140: assertImplements(comment, objects[i], expectedClass);
141: }
142: }
143:
144: protected void assertImplements(String comment, Object object,
145: Class expectedClass) {
146: if (object == null) {
147: fail(comment + " should be of class "
148: + expectedClass.getName() + " but is null");
149: } else if (!expectedClass.isInstance(object)) {
150: fail(comment + " should be of class "
151: + expectedClass.getName() + " but is "
152: + object.getClass().getName());
153: }
154: }
155:
156: protected void assertMatchingSet(String comment, Object[] expected,
157: Enumeration found) {
158: Vector foundItems = new Vector();
159: while (found.hasMoreElements())
160: foundItems.addElement(found.nextElement());
161:
162: assertMatchingSet(comment, expected, foundItems);
163: }
164:
165: private void assertMatchingSet(String comment, Object[] expected,
166: Vector foundItems) {
167: Vector expectedItems = new Vector();
168: for (int i = 0; i < expected.length; i++)
169: expectedItems.addElement(expected[i]);
170: for (int i = 0; i < expected.length; i++) {
171: if (!foundItems.contains(expected[i])) {
172: fail(comment + ": expected " + asText(expected)
173: + " but missing " + expected[i]);
174: } else {
175: foundItems.removeElement(expected[i]);
176: }
177: }
178:
179: if (!foundItems.isEmpty())
180: fail(comment + ": expected " + asText(expected)
181: + " but found superfluous"
182: + foundItems.firstElement());
183: }
184:
185: public static void assertMatchingSet(String comment,
186: Object[] expected, Object[] found) {
187: Vector foundItems = new Vector();
188: for (int i = 0; i < found.length; i++)
189: foundItems.addElement(found[i]);
190:
191: Vector expectedItems = new Vector();
192:
193: for (int i = 0; i < expected.length; i++)
194: expectedItems.addElement(expected[i]);
195:
196: for (int i = 0; i < expected.length; i++) {
197: if (!foundItems.contains(expected[i])) {
198: fail(comment + ": expected " + asText(expected)
199: + " but found " + asText(found));
200: } else {
201: foundItems.removeElement(expected[i]);
202: }
203: }
204:
205: for (int i = 0; i < found.length; i++) {
206: if (!expectedItems.contains(found[i])) {
207: fail(comment + ": expected " + asText(expected)
208: + " but found " + asText(found));
209: } else {
210: expectedItems.removeElement(found[i]);
211: }
212: }
213:
214: if (!foundItems.isEmpty())
215: fail(comment + ": expected " + asText(expected)
216: + " but found " + asText(found));
217: }
218:
219: public static String asText(Object[] args) {
220: StringBuffer sb = new StringBuffer("{");
221: for (int i = 0; i < args.length; i++) {
222: if (i != 0)
223: sb.append(",");
224: sb.append('"').append(args[i]).append('"');
225: }
226: sb.append("}");
227: return sb.toString();
228: }
229:
230: protected String asBytes(String s) {
231: StringBuffer sb = new StringBuffer();
232: char[] chars = s.toCharArray();
233: for (int i = 0; i < chars.length; i++) {
234: sb.append(Integer.toHexString(chars[i])).append(" ");
235: }
236: return sb.toString();
237: }
238:
239: protected void assertEquals(String comment, byte[] expected,
240: byte[] actual) {
241: if (!equals(expected, actual)) {
242: fail(comment + " expected:\n" + toString(expected)
243: + ", but was:\n" + toString(actual));
244: }
245: }
246:
247: private boolean equals(byte[] first, byte[] second) {
248: if (first.length != second.length)
249: return false;
250: for (int i = 0; i < first.length; i++) {
251: if (first[i] != second[i])
252: return false;
253: }
254: return true;
255: }
256:
257: private String toString(byte[] message) {
258: StringBuffer sb = new StringBuffer();
259: for (int i = 0; i < message.length; i++) {
260: if (i != 0 && (i % 4) == 0)
261: sb.append(' ');
262: if (message[i] >= 0 && message[i] < 16)
263: sb.append('0');
264: sb.append(Integer.toHexString(0xff & (int) message[i]));
265: }
266: return sb.toString();
267: }
268:
269: static class QuerySpec {
270: QuerySpec(String urlString) {
271: if (urlString.indexOf('?') < 0) {
272: _path = urlString;
273: } else {
274: _path = urlString.substring(0, urlString.indexOf('?'));
275: }
276: _fullString = urlString;
277:
278: StringTokenizer st = new StringTokenizer(urlString
279: .substring(urlString.indexOf('?') + 1), "&");
280: while (st.hasMoreTokens())
281: _parameters.addElement(st.nextToken());
282: }
283:
284: public String toString() {
285: return _fullString;
286: }
287:
288: public boolean equals(Object o) {
289: return getClass().equals(o.getClass())
290: && equals((QuerySpec) o);
291: }
292:
293: public int hashCode() {
294: return _path.hashCode() ^ _parameters.size();
295: }
296:
297: private String _path;
298: private String _fullString;
299: private Vector _parameters = new Vector();
300:
301: private boolean equals(QuerySpec o) {
302: if (!_path.equals(o._path)) {
303: return false;
304: } else if (_parameters.size() != o._parameters.size()) {
305: return false;
306: } else {
307: for (Enumeration e = o._parameters.elements(); e
308: .hasMoreElements();) {
309: if (!_parameters.contains(e.nextElement()))
310: return false;
311: }
312: return true;
313: }
314: }
315: }
316: }
|