001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Cactus", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: * formatted with JxBeauty (c) johann.langhofer@nextra.at
055: */
056:
057: package org.hamletsoft.enhydra.cactus.business.sample;
058:
059: import org.hamletsoft.enhydra.cactus.business.EnhydraAppTestCase;
060: import org.hamletsoft.enhydra.cactus.presentation.sample.SamplePO;
061: import junit.framework.Test;
062: import junit.framework.TestSuite;
063: import org.apache.cactus.WebResponse;
064: import java.io.IOException;
065: import org.apache.cactus.WebRequest;
066: import java.util.Map;
067: import org.apache.cactus.client.authentication.BasicAuthentication;
068: import com.lutris.http.BasicAuth;
069: import com.lutris.http.BasicAuthResult;
070: import org.apache.log4j.PropertyConfigurator;
071:
072: // import junit.ui.*;
073: /**
074: * SampleEnhydraAppTestCase.java
075: *
076: *
077: * Created: Thu Jan 31 11:39:48 2002
078: *
079: * @author <a href="mailto:hattori@hitachizosen.co.jp">Ryuji Hattori</a>
080: * @version 1.0
081: */
082: public class SampleEnhydraAppTestCase extends EnhydraAppTestCase {
083: static {
084: // instructs PropertyConfigurator to parse a configuration file and set up logging accordingly
085: PropertyConfigurator.configure("log_client.properties");
086: }
087:
088: public SampleEnhydraAppTestCase(String name) {
089: super (name);
090: }
091:
092: /**
093: * Start the tests.
094: *
095: * @param theArgs the arguments. Not used
096: */
097: public static void main(String[] theArgs) {
098: junit.awtui.TestRunner
099: .main(new String[] { SampleEnhydraAppTestCase.class
100: .getName() });
101: }
102:
103: /*
104: public static void main(String[] theArgs) {
105: TestRunner.main(new String[] {
106: SampleEnhydraAppTestCase.class.getName()});
107: }
108: */
109: /**
110: * @return a test suite (<code>TestSuite</code>) that includes all methods
111: * starting with "test"
112: */
113: public static Test suite() {
114: // All methods starting with "test" will be executed in the test suite.
115: return new TestSuite(SampleEnhydraAppTestCase.class);
116: }
117:
118: /**
119: * Verify that we can assert the servlet output stream.
120: */
121: public void testReadOutputStream() throws Exception {
122: SamplePO samplePO = new SamplePO();
123: samplePO.run(comms);
124: }
125:
126: /**
127: * Verify that we can assert the servlet output stream.
128: *
129: * @param theResponse the response from the server side.
130: */
131: public void endReadOutputStream(WebResponse theResponse)
132: throws IOException {
133: String expected = SamplePO.Answer1;
134: //"<html><head/><body>A GET request</body></html>";
135: String result = theResponse.getText();
136: assertEquals(expected, result);
137: }
138:
139: /**
140: * Verify that we can simulate a GET request to a PO.
141: *
142: * @param theRequest the request object that serves to initialize the
143: * HTTP connection to the server redirector.
144: */
145: public void beginPostMethod(WebRequest theRequest) {
146: theRequest.addParameter("param1", "value1",
147: 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 {
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: /**
172: * Verify that we can simulate a GET request to a PO
173: */
174: public void testGetMethod() throws Exception {
175: SamplePO samplePO = new SamplePO();
176: samplePO.run(comms);
177: assertEquals("GET", samplePO.checkMethod());
178: }
179:
180: /**
181: * Verify that we can simulate HTTP parameters in the HTTP request.
182: *
183: * @param theRequest the request object that serves to initialize the
184: * HTTP connection to the server redirector.
185: */
186: public void beginSendParams(WebRequest theRequest) {
187: theRequest.addParameter("param1", "value1");
188: theRequest.addParameter("param2", "value2");
189: }
190:
191: /**
192: * Verify that we can send several parameters in the HTTP request.
193: */
194: public void testSendParams() throws Exception {
195: SamplePO samplePO = new SamplePO();
196: samplePO.run(comms);
197: Map params = samplePO.getRequestParameters();
198: assertTrue(params.get("param1") != null);
199: assertTrue(params.get("param2") != null);
200: assertEquals("value1", params.get("param1"));
201: assertEquals("value2", params.get("param2"));
202: }
203:
204: /**
205: * Verify that we can simulate HTTP headers in the HTTP request.
206: *
207: * @param theRequest the request object that serves to initialize the
208: * HTTP connection to the server redirector.
209: */
210: public void beginSendHeader(WebRequest theRequest) {
211: theRequest.addHeader("testheader", "this is a header test");
212: }
213:
214: /**
215: * Verify that we can simulate HTTP headers in the HTTP request.
216: */
217: public void testSendHeader() throws Exception {
218: SamplePO samplePO = new SamplePO();
219: samplePO.run(comms);
220: String headerValue = samplePO.getRequestHeader("testheader");
221: assertEquals("this is a header test", headerValue);
222: }
223:
224: //-------------------------------------------------------------------------
225: /**
226: * Verify that we can simulate a single cookie sent in the HTTP request.
227: *
228: * @param theRequest the request object that serves to initialize the
229: * HTTP connection to the server redirector.
230: */
231: public void beginSendCookie(WebRequest theRequest) {
232: // Note: The cookie value that was chosen is a string without spaces
233: // because there is a problem with Resin 1.2.1 which does not support
234: // quoted cookies. It has been fixed since the 15/12/2000 release of
235: // Resin.
236: theRequest.addCookie("testcookie", "thisisacookie");
237: }
238:
239: /**
240: * Verify that we can simulate a single cookie sent in the HTTP request.
241: */
242: public void testSendCookie() throws Exception {
243: SamplePO samplePO = new SamplePO();
244: samplePO.run(comms);
245: Map cookies = samplePO.getRequestCookies();
246: assertTrue("Cannot find [testcookie] cookie in request",
247: cookies.get("testcookie") != null);
248: assertEquals("thisisacookie", cookies.get("testcookie"));
249: }
250:
251: /**
252: * Verify that we can simulate a HTTP Basic Auth in the HTTP request.
253: */
254: public void beginBasicAuthentication(WebRequest theRequest) {
255: theRequest.setAuthentication(new BasicAuthentication(
256: "testuser", "testpwd"));
257: }
258:
259: /**
260: * Verify that we can simulate a HTTP Basic Auth in the HTTP request.
261: */
262: public void testBasicAuthentication() throws Exception {
263: BasicAuthResult authResult = BasicAuth
264: .getAuthentication(comms.request);
265: assertEquals("testuser", authResult.username);
266: assertEquals("testpwd", authResult.password);
267: }
268: } // SampleEnhydraAppTestCase
|