KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > util > HttpTestCase


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/HttpTestCase.java,v 1.3 2004/08/18 12:26:09 hkollmann Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/08/18 12:26:09 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.util;
25
26 import com.meterware.httpunit.GetMethodWebRequest;
27 import com.meterware.httpunit.PostMethodWebRequest;
28 import com.meterware.httpunit.WebConversation;
29 import com.meterware.httpunit.WebRequest;
30 import com.meterware.httpunit.WebResponse;
31
32 // imports
33
import junit.framework.TestCase;
34
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38
39
40 // definition of test class
41
public abstract class HttpTestCase extends TestCase {
42    private String JavaDoc paramReplace;
43    private String JavaDoc paramSearch;
44    private String JavaDoc urlReplace;
45    private String JavaDoc urlSearch;
46    private WebConversation wc = new WebConversation();
47    private WebResponse resp = null;
48
49    /**
50     * Creates a new HttpTestCase object.
51     *
52     * @param name DOCUMENT ME!
53     */

54    public HttpTestCase(String JavaDoc name) {
55       super(name);
56
57       String JavaDoc context = System.getProperty("cactus.contextURL");
58
59       if (!Util.isNull(context)) {
60          println("change context to: " + context);
61          urlSearch = "http://localhost/bookstore";
62          urlReplace = context;
63          paramSearch = "/bookstore/";
64          paramReplace = "/dbforms-cactus/";
65       }
66    }
67
68    /**
69     * DOCUMENT ME!
70     *
71     * @param url DOCUMENT ME!
72     *
73     * @throws Exception DOCUMENT ME!
74     */

75    public void get(String JavaDoc url) throws Exception JavaDoc {
76       get(url, null);
77    }
78
79
80    /**
81     * DOCUMENT ME!
82     *
83     * @param url DOCUMENT ME!
84     * @param args DOCUMENT ME!
85     *
86     * @throws Exception DOCUMENT ME!
87     */

88    public void get(String JavaDoc url,
89                    List JavaDoc args) throws Exception JavaDoc {
90       url = replaceURL(url);
91       println("=========================");
92       println("url" + " = " + url);
93       println("=========================");
94
95       WebRequest request = new GetMethodWebRequest(url);
96       doIt(request, args);
97    }
98
99
100    /**
101     * DOCUMENT ME!
102     *
103     * @param url DOCUMENT ME!
104     *
105     * @throws Exception DOCUMENT ME!
106     */

107    public void post(String JavaDoc url) throws Exception JavaDoc {
108       post(url, null);
109    }
110
111
112    /**
113     * DOCUMENT ME!
114     *
115     * @param url DOCUMENT ME!
116     * @param args DOCUMENT ME!
117     *
118     * @throws Exception DOCUMENT ME!
119     */

120    public void post(String JavaDoc url,
121                     List JavaDoc args) throws Exception JavaDoc {
122       url = replaceURL(url);
123       println("=========================");
124       println("url" + " = " + url);
125       println("=========================");
126
127       WebRequest request = new PostMethodWebRequest(url);
128       doIt(request, args);
129    }
130
131
132    /**
133     * DOCUMENT ME!
134     *
135     * @return DOCUMENT ME!
136     */

137    protected WebResponse getResponse() {
138       return resp;
139    }
140
141
142    /**
143     * DOCUMENT ME!
144     *
145     * @throws Exception DOCUMENT ME!
146     */

147    protected void printResponse() throws Exception JavaDoc {
148       println(resp.getText());
149    }
150
151
152    /**
153     * DOCUMENT ME!
154     *
155     * @param s DOCUMENT ME!
156     */

157    protected void println(String JavaDoc s) {
158       System.out.println(s);
159    }
160
161
162    /**
163     * DOCUMENT ME!
164     *
165     * @param text DOCUMENT ME!
166     *
167     * @return DOCUMENT ME!
168     *
169     * @throws Exception DOCUMENT ME!
170     */

171    protected boolean responseContains(String JavaDoc text) throws Exception JavaDoc {
172       if ((resp == null) || (resp.getText() == null)) {
173          return false;
174       }
175
176       return resp.getText()
177                  .indexOf(text) != -1;
178    }
179
180
181    private int getStatusCode() {
182       return resp.getResponseCode();
183    }
184
185
186    private void doIt(WebRequest request,
187                      List JavaDoc args) throws Exception JavaDoc {
188       resp = null;
189
190       if (args != null) {
191          println("parameters");
192          println("=========================");
193
194          Iterator JavaDoc iter = args.iterator();
195
196          while (iter.hasNext()) {
197             KeyValuePair pair = (KeyValuePair) iter.next();
198             println(pair.getKey() + " = " + pair.getValue());
199             request.setParameter(pair.getKey(), replaceParam(pair.getValue()));
200          }
201
202          println("=========================");
203       }
204
205       wc.setExceptionsThrownOnErrorStatus(false);
206       resp = wc.getResponse(request);
207       println("Response code: " + getStatusCode());
208       println("=========================");
209       assertEquals("Page not found: ", 200, getStatusCode());
210    }
211
212
213    private String JavaDoc replace(String JavaDoc from,
214                           String JavaDoc search,
215                           String JavaDoc replace) {
216       if ((search == null) || (replace == null)) {
217          return from;
218       } else {
219          int pos = from.indexOf(search);
220
221          if (pos == -1) {
222             return from;
223          } else {
224             String JavaDoc str = from.substring(0, pos) + replace
225                          + from.substring(pos + search.length());
226
227             return str;
228          }
229       }
230    }
231
232
233    private String JavaDoc replaceParam(String JavaDoc param) {
234       return replace(param, paramSearch, paramReplace);
235    }
236
237
238    private String JavaDoc replaceURL(String JavaDoc url) {
239       return replace(url, urlSearch, urlReplace);
240    }
241 }
242
Popular Tags