KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hamletsoft > enhydra > cactus > business > EnhydraAppTestCase


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;
59
60 import java.lang.reflect.*;
61 import javax.servlet.*;
62 import javax.servlet.http.*;
63 import java.net.*;
64 import junit.framework.*;
65 import org.apache.cactus.client.*;
66 import org.apache.cactus.server.*;
67 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
68 import com.lutris.appserver.server.Application;
69 import com.lutris.appserver.server.httpPresentation.HttpPresentationResponse;
70 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest;
71 import org.hamletsoft.enhydra.cactus.business.*;
72 import org.apache.cactus.AbstractTestCase;
73 import org.apache.cactus.AbstractWebTestCase;
74 import javax.servlet.http.HttpServletRequest JavaDoc;
75 import org.apache.cactus.ServletTestCase;
76 import java.net.MalformedURLException JavaDoc;
77 import java.net.URL JavaDoc;
78 import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
79 import java.util.Hashtable JavaDoc;
80
81
82 /**
83  * Test classes that need access to valid EnhydraApp implicit objects (such as the
84  * the HTTP request, the HTTP response, the servlet config, ...) must subclass
85  * this class.
86  *
87  * @author <a HREF="mailto:hattori@hitachizosen.co.jp">Ryuji "The Hamlet" Hattori</a>
88  *
89  * @version $Id: EnhydraAppTestCase.java,v 1.1 2004/08/16 09:35:01 slobodan Exp $
90  */

91 public class EnhydraAppTestCase extends AbstractWebTestCase {
92   /**
93    * Valid <code>HttpPresentationComms</code> object that you can access from
94    * the <code>testXXX()</code>, <code>setUp</code> and
95    * <code>tearDown()</code> methods. If you try to access it from either the
96    * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
97    * have the <code>null</code> value.
98    */

99   public HttpPresentationComms comms;
100   /**
101    * Valid <code>Application</code> object that you can access from
102    * the <code>testXXX()</code>, <code>setUp</code> and
103    * <code>tearDown()</code> methods. If you try to access it from either the
104    * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
105    * have the <code>null</code> value.
106    */

107   public Application application;
108   /**
109    * Valid <code>HttpPresentationRequest</code> object that you can access from
110    * the <code>testXXX()</code>, <code>setUp</code> and
111    * <code>tearDown()</code> methods. If you try to access it from either the
112    * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
113    * have the <code>null</code> value.
114    */

115   public HttpPresentationRequest request;
116   /**
117    * Valid <code>HttpPresentationResponse</code> object that you can access from
118    * the <code>testXXX()</code>, <code>setUp</code> and
119    * <code>tearDown()</code> methods. If you try to access it from either the
120    * <code>beginXXX()</code> or <code>endXXX()</code> methods it will
121    * have the <code>null</code> value.
122    */

123   public HttpPresentationResponse response;
124
125   /**
126    * Constructs a JUnit test case with the given name.
127    *
128    * @param theName the name of the test case
129    */

130   public EnhydraAppTestCase (String JavaDoc theName) {
131     super(theName);
132   }
133
134   /**
135    * Runs a test case. This method is overriden from the JUnit
136    * <code>TestCase</code> class in order to seamlessly call the
137    * Cactus redirection servlet.
138    */

139   protected void runTest () throws Throwable JavaDoc {
140     runGenericTest(new EnhydraAppHttpClient());
141   }
142
143   /**
144    * Overwrite Methods. because of need to handle ClinetSideRedirection
145    *
146    * Run the test that was specified in the constructor on the server side,
147    */

148   protected void runServerTest () throws Throwable JavaDoc {
149     try {
150       super.runServerTest();
151     } catch (ClientPageRedirectException cpre) {
152       clientSideRedirect(cpre.getUrl());
153     }
154   }
155
156   private void clientSideRedirect (String JavaDoc url) throws Throwable JavaDoc {
157     url = makeAbsolute(url, comms.request.getHttpServletRequest());
158     hookForClientPageRedirect(url, getRedirectParameters(url));
159     comms.response.setHeader("Location", url);
160     comms.response.setStatus(HttpPresentationResponse.SC_MOVED_TEMPORARILY,
161         "Redirected to new location.");
162   }
163
164   private String JavaDoc makeAbsolute (String JavaDoc location, HttpServletRequest JavaDoc request) {
165     URL JavaDoc 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 JavaDoc(location);
170     } catch (MalformedURLException JavaDoc e) {
171       String JavaDoc requrl = HttpUtils.getRequestURL(request).toString();
172       try {
173         url = new URL JavaDoc(new URL JavaDoc(requrl), location);
174       } catch (MalformedURLException JavaDoc 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 JavaDoc getRedirectParameters (String JavaDoc url) {
190     Hashtable JavaDoc ht = null;
191     if (url.indexOf("?") > -1)
192       ht = HttpUtils.parseQueryString(url.substring(url.indexOf("?") + 1));
193     return ht;
194   }
195
196   /**
197    * Hook method for all Test on server side for client page redirection.
198    *
199    * @param url URL of Page Redirection.
200    * @param ht HTTP parameter table for the URL of Page Redirection.<br>
201    * (Class of the Hashtable's value is <b>not String but String[]</b>)
202    */

203   protected void hookForClientPageRedirect (String JavaDoc url, Hashtable JavaDoc ht) {
204     ;
205   }
206 }
207
208
209
210
Popular Tags