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;
058:
059: import java.lang.reflect.*;
060: import javax.servlet.*;
061: import javax.servlet.http.*;
062: import java.net.*;
063: import junit.framework.*;
064: import org.apache.cactus.client.*;
065: import org.apache.cactus.server.*;
066: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
067: import com.lutris.appserver.server.Application;
068: import com.lutris.appserver.server.httpPresentation.HttpPresentationResponse;
069: import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
070: import org.hamletsoft.enhydra.cactus.business.*;
071: import org.apache.cactus.AbstractTestCase;
072: import org.apache.cactus.AbstractWebTestCase;
073: import javax.servlet.http.HttpServletRequest;
074: import org.apache.cactus.ServletTestCase;
075: import java.net.MalformedURLException;
076: import java.net.URL;
077: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
078: import java.util.Hashtable;
079:
080: /**
081: * Test classes that need access to valid EnhydraApp implicit objects (such as the
082: * the HTTP request, the HTTP response, the servlet config, ...) must subclass
083: * this class.
084: *
085: * @author <a href="mailto:hattori@hitachizosen.co.jp">Ryuji "The Hamlet" Hattori</a>
086: *
087: * @version $Id: EnhydraAppTestCase.java,v 1.1 2006-09-11 12:30:51 sinisa Exp $
088: */
089: public class EnhydraAppTestCase extends AbstractWebTestCase {
090: /**
091: * Valid <code>HttpPresentationComms</code> object that you can access from
092: * the <code>testXXX()</code>, <code>setUp</code> and
093: * <code>tearDown()</code> methods. If you try to access it from either the
094: * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
095: * have the <code>null</code> value.
096: */
097: public HttpPresentationComms comms;
098: /**
099: * Valid <code>Application</code> object that you can access from
100: * the <code>testXXX()</code>, <code>setUp</code> and
101: * <code>tearDown()</code> methods. If you try to access it from either the
102: * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
103: * have the <code>null</code> value.
104: */
105: public Application application;
106: /**
107: * Valid <code>HttpPresentationRequest</code> object that you can access from
108: * the <code>testXXX()</code>, <code>setUp</code> and
109: * <code>tearDown()</code> methods. If you try to access it from either the
110: * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
111: * have the <code>null</code> value.
112: */
113: public HttpPresentationRequest request;
114: /**
115: * Valid <code>HttpPresentationResponse</code> object that you can access from
116: * the <code>testXXX()</code>, <code>setUp</code> and
117: * <code>tearDown()</code> methods. If you try to access it from either the
118: * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
119: * have the <code>null</code> value.
120: */
121: public HttpPresentationResponse response;
122:
123: /**
124: * Constructs a JUnit test case with the given name.
125: *
126: * @param theName the name of the test case
127: */
128: public EnhydraAppTestCase(String theName) {
129: super (theName);
130: }
131:
132: /**
133: * Runs a test case. This method is overriden from the JUnit
134: * <code>TestCase</code> class in order to seamlessly call the
135: * Cactus redirection servlet.
136: */
137: protected void runTest() throws Throwable {
138: runGenericTest(new EnhydraAppHttpClient());
139: }
140:
141: /**
142: * Overwrite Methods. because of need to handle ClinetSideRedirection
143: *
144: * Run the test that was specified in the constructor on the server side,
145: */
146: protected void runServerTest() throws Throwable {
147: try {
148: super .runServerTest();
149: } catch (ClientPageRedirectException cpre) {
150: clientSideRedirect(cpre.getUrl());
151: }
152: }
153:
154: private void clientSideRedirect(String url) throws Throwable {
155: url = makeAbsolute(url, comms.request.getHttpServletRequest());
156: hookForClientPageRedirect(url, getRedirectParameters(url));
157: comms.response.setHeader("Location", url);
158: comms.response.setStatus(
159: HttpPresentationResponse.SC_MOVED_TEMPORARILY,
160: "Redirected to new location.");
161: }
162:
163: private String makeAbsolute(String location,
164: HttpServletRequest request) {
165: URL url = null;
166: try {
167: // Try making a URL out of the location
168: // Throws an exception if the location is relative
169: url = new URL(location);
170: } catch (MalformedURLException e) {
171: String requrl = HttpUtils.getRequestURL(request).toString();
172: try {
173: url = new URL(new URL(requrl), location);
174: } catch (MalformedURLException ignored) {
175: return location; // Give up
176: }
177: }
178: return url.toString();
179: }
180:
181: /**
182: * getting Parameter table for the url of client page redirection.
183: *
184: * @param url URL of Page Redirection.
185: * @return HTTP parameter table for the URL of Page Redirection.<br>
186: * (Class of the Hashtable's value is <b>not String but String[]</b>)<br>
187: * if the url has no parameter, this returns null.
188: */
189: protected Hashtable getRedirectParameters(String url) {
190: Hashtable ht = null;
191: if (url.indexOf("?") > -1)
192: ht = HttpUtils.parseQueryString(url.substring(url
193: .indexOf("?") + 1));
194: return ht;
195: }
196:
197: /**
198: * Hook method for all Test on server side for client page redirection.
199: *
200: * @param url URL of Page Redirection.
201: * @param ht HTTP parameter table for the URL of Page Redirection.<br>
202: * (Class of the Hashtable's value is <b>not String but String[]</b>)
203: */
204: protected void hookForClientPageRedirect(String url, Hashtable ht) {
205: ;
206: }
207: }
|