KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hamletsoft > enhydra > cactus > business > sample > SampleEnhydraAppTestCase


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1999 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Cactus", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * formatted with JxBeauty (c) johann.langhofer@nextra.at
55  */

56
57
58 package org.hamletsoft.enhydra.cactus.business.sample;
59
60 import org.hamletsoft.enhydra.cactus.business.EnhydraAppTestCase;
61 import org.hamletsoft.enhydra.cactus.presentation.sample.SamplePO;
62 import junit.framework.Test;
63 import junit.framework.TestSuite;
64 import org.apache.cactus.WebResponse;
65 import java.io.IOException JavaDoc;
66 import org.apache.cactus.WebRequest;
67 import java.util.Map JavaDoc;
68 import org.apache.cactus.client.authentication.BasicAuthentication;
69 import com.lutris.http.BasicAuth;
70 import com.lutris.http.BasicAuthResult;
71 import org.apache.log4j.PropertyConfigurator;
72
73
74 // import junit.ui.*;
75
/**
76  * SampleEnhydraAppTestCase.java
77  *
78  *
79  * Created: Thu Jan 31 11:39:48 2002
80  *
81  * @author <a HREF="mailto:hattori@hitachizosen.co.jp">Ryuji Hattori</a>
82  * @version 1.0
83  */

84 public class SampleEnhydraAppTestCase extends EnhydraAppTestCase {
85   static {
86     // instructs PropertyConfigurator to parse a configuration file and set up logging accordingly
87
PropertyConfigurator.configure("log_client.properties");
88   }
89
90   public SampleEnhydraAppTestCase (String JavaDoc name) {
91     super(name);
92   }
93
94   /**
95    * Start the tests.
96    *
97    * @param theArgs the arguments. Not used
98    */

99   public static void main (String JavaDoc[] theArgs) {
100     junit.awtui.TestRunner.main(new String JavaDoc[] {
101       SampleEnhydraAppTestCase.class.getName()
102     });
103   }
104
105   /*
106    public static void main(String[] theArgs) {
107    TestRunner.main(new String[] {
108    SampleEnhydraAppTestCase.class.getName()});
109    }
110    */

111   /**
112    * @return a test suite (<code>TestSuite</code>) that includes all methods
113    * starting with "test"
114    */

115   public static Test suite () {
116     // All methods starting with "test" will be executed in the test suite.
117
return new TestSuite(SampleEnhydraAppTestCase.class);
118   }
119
120   /**
121    * Verify that we can assert the servlet output stream.
122    */

123   public void testReadOutputStream () throws Exception JavaDoc {
124     SamplePO samplePO = new SamplePO();
125     samplePO.run(comms);
126   }
127
128   /**
129    * Verify that we can assert the servlet output stream.
130    *
131    * @param theResponse the response from the server side.
132    */

133   public void endReadOutputStream (WebResponse theResponse) throws IOException JavaDoc {
134     String JavaDoc expected = SamplePO.Answer1;
135     //"<html><head/><body>A GET request</body></html>";
136
String JavaDoc result = theResponse.getText();
137     assertEquals(expected, result);
138   }
139
140   /**
141    * Verify that we can simulate a GET request to a PO.
142    *
143    * @param theRequest the request object that serves to initialize the
144    * HTTP connection to the server redirector.
145    */

146   public void beginPostMethod (WebRequest theRequest) {
147     theRequest.addParameter("param1", "value1", WebRequest.POST_METHOD);
148   }
149
150   /**
151    * Verify that we can simulate a POST request to a servlet. Note that
152    * the POST method is the default method so we don't need to initialize
153    * any parameter in <code>beginPostMethod()</code>.
154    */

155   public void testPostMethod () throws Exception JavaDoc {
156     SamplePO samplePO = new SamplePO();
157     samplePO.run(comms);
158     assertEquals("POST", samplePO.checkMethod());
159   }
160
161   //-------------------------------------------------------------------------
162
/**
163    * Verify that we can simulate a GET request to a PO.
164    *
165    * @param theRequest the request object that serves to initialize the
166    * HTTP connection to the server redirector.
167    */

168   public void beginGetMethod (WebRequest theRequest) {}
169
170   /**
171    * Verify that we can simulate a GET request to a PO
172    */

173   public void testGetMethod () throws Exception JavaDoc {
174     SamplePO samplePO = new SamplePO();
175     samplePO.run(comms);
176     assertEquals("GET", samplePO.checkMethod());
177   }
178
179   /**
180    * Verify that we can simulate HTTP parameters in the HTTP request.
181    *
182    * @param theRequest the request object that serves to initialize the
183    * HTTP connection to the server redirector.
184    */

185   public void beginSendParams (WebRequest theRequest) {
186     theRequest.addParameter("param1", "value1");
187     theRequest.addParameter("param2", "value2");
188   }
189
190   /**
191    * Verify that we can send several parameters in the HTTP request.
192    */

193   public void testSendParams () throws Exception JavaDoc {
194     SamplePO samplePO = new SamplePO();
195     samplePO.run(comms);
196     Map JavaDoc params = samplePO.getRequestParameters();
197     assertTrue(params.get("param1") != null);
198     assertTrue(params.get("param2") != null);
199     assertEquals("value1", params.get("param1"));
200     assertEquals("value2", params.get("param2"));
201   }
202
203   /**
204    * Verify that we can simulate HTTP headers in the HTTP request.
205    *
206    * @param theRequest the request object that serves to initialize the
207    * HTTP connection to the server redirector.
208    */

209   public void beginSendHeader (WebRequest theRequest) {
210     theRequest.addHeader("testheader", "this is a header test");
211   }
212
213   /**
214    * Verify that we can simulate HTTP headers in the HTTP request.
215    */

216   public void testSendHeader () throws Exception JavaDoc {
217     SamplePO samplePO = new SamplePO();
218     samplePO.run(comms);
219     String JavaDoc headerValue = samplePO.getRequestHeader("testheader");
220     assertEquals("this is a header test", headerValue);
221   }
222
223   //-------------------------------------------------------------------------
224
/**
225    * Verify that we can simulate a single cookie sent in the HTTP request.
226    *
227    * @param theRequest the request object that serves to initialize the
228    * HTTP connection to the server redirector.
229    */

230   public void beginSendCookie (WebRequest theRequest) {
231     // Note: The cookie value that was chosen is a string without spaces
232
// because there is a problem with Resin 1.2.1 which does not support
233
// quoted cookies. It has been fixed since the 15/12/2000 release of
234
// Resin.
235
theRequest.addCookie("testcookie", "thisisacookie");
236   }
237
238   /**
239    * Verify that we can simulate a single cookie sent in the HTTP request.
240    */

241   public void testSendCookie () throws Exception JavaDoc {
242     SamplePO samplePO = new SamplePO();
243     samplePO.run(comms);
244     Map JavaDoc cookies = samplePO.getRequestCookies();
245     assertTrue("Cannot find [testcookie] cookie in request", cookies.get("testcookie")
246         != null);
247     assertEquals("thisisacookie", cookies.get("testcookie"));
248   }
249
250   /**
251    * Verify that we can simulate a HTTP Basic Auth in the HTTP request.
252    */

253   public void beginBasicAuthentication (WebRequest theRequest) {
254     theRequest.setAuthentication(new BasicAuthentication("testuser", "testpwd"));
255   }
256
257   /**
258    * Verify that we can simulate a HTTP Basic Auth in the HTTP request.
259    */

260   public void testBasicAuthentication () throws Exception JavaDoc {
261     BasicAuthResult authResult = BasicAuth.getAuthentication(comms.request);
262     assertEquals("testuser", authResult.username);
263     assertEquals("testpwd", authResult.password);
264   }
265 } // SampleEnhydraAppTestCase
266

267
268
269
Popular Tags