001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.gravy;
043:
044: import com.meterware.httpunit.*;
045: import junit.framework.Test;
046: import junit.framework.TestCase;
047: import junit.framework.TestSuite;
048: import java.io.File;
049: import org.netbeans.junit.NbTestCase;
050:
051: /**
052: * This class allows to check a real execution of deployed web-application
053: * inside running automated test.
054: */
055: public class HttpTestCase extends NbTestCase implements Test {
056: private WebConversation conversation;
057: private WebRequest request;
058: private WebResponse response;
059: private long waitTime = 10000;
060: private long waitDelta = 100;
061: TestProperties props;
062:
063: /**
064: * Creates a new instance of this class.
065: * @param testName a name of new created test.
066: */
067: public HttpTestCase(String testName) {
068: super (testName);
069: conversation = new WebConversation();
070: props = new TestProperties();
071: }
072:
073: /**
074: * Initializes an instance of class WebConversation, which will
075: * check an execution of web-application.
076: * @return the created instance of WebConversation
077: */
078: public WebConversation initConversation() {
079: getLog().println("Creating WebConversation");
080: conversation = new WebConversation();
081: return (conversation);
082: }
083:
084: /**
085: * Returns an instance of initialized WebConversation.
086: * @return the instance of WebConversation
087: */
088: public WebConversation getConversation() {
089: return (conversation);
090: }
091:
092: /**
093: * Initializes an instance of http-request.
094: * @param path an absolute URL to a deployed web-application
095: * @return the instance of http-request
096: */
097: public WebRequest initRequest(String path) {
098: getLog().println("Creating request for \"" + path + "\"");
099: request = new GetMethodWebRequest(path);
100: return (request);
101: }
102:
103: /**
104: * Returns an instance of initialized http-request.
105: * @return the instance of http-request
106: */
107: public WebRequest getRequest() {
108: return (request);
109: }
110:
111: /**
112: * Returns a http-response from web-server.
113: * @return the instance of http-response
114: */
115: public WebResponse getResponse() throws Exception {
116: getLog().println("Getting response for " + request);
117: Exception exc = null;
118: long startTime = System.currentTimeMillis();
119: do {
120: try {
121: response = conversation.getResponse(request);
122: } catch (Exception e) {
123: exc = e;
124: Thread.sleep(waitDelta);
125: }
126: } while (exc != null
127: && (System.currentTimeMillis() - startTime) < waitTime);
128: if (exc != null) {
129: System.out
130: .println("WebRequest.getResponse() did not succed in 1 minute");
131: System.out.println("Ended up with exception:");
132: exc.printStackTrace();
133: throw (exc);
134: }
135: return (response);
136: }
137:
138: /**
139: * Returns the last received web-response or waits, until the first web-response
140: * is received, and returns it.
141: * @return the instance of WebResponse
142: */
143: public WebResponse lastResponse() throws Exception {
144: if (response == null) {
145: return (getResponse());
146: } else {
147: return (response);
148: }
149: }
150:
151: /**
152: * Returns test properties (TestProperties object).
153: * @return test properties
154: */
155: protected TestProperties getTestProperties() {
156: return (props);
157: }
158: }
|