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-2006 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.test.web;
043:
044: import java.io.BufferedReader;
045: import java.io.DataOutputStream;
046: import java.io.File;
047: import java.io.FileOutputStream;
048: import java.io.IOException;
049: import java.io.InputStream;
050: import java.io.InputStreamReader;
051: import java.net.URL;
052: import java.net.URLConnection;
053: import org.netbeans.jemmy.JemmyException;
054: import org.openide.awt.HtmlBrowser;
055: import org.openide.util.Lookup;
056:
057: /**
058: * Designed to replace default URLDisplayer to better serve automated tests
059: * requirements.
060: * <p>
061: * Usage:<br>
062: * <pre>
063: * TestURLDisplayer displayer = TestURLDisplayer.getInstance();
064: * displayer.invalidateURL();
065: * // e.g. run a .jsp
066: * displayer.waitURL();
067: * String page = displayer.readURL();
068: * ...
069: * </pre>
070: *
071: * @author Martin.Schovanek@sun.com
072: */
073: public final class TestURLDisplayer extends HtmlBrowser.URLDisplayer {
074: private static TestURLDisplayer instance;
075: private boolean isURLValid = false;
076: private URL url = null;
077: private URLConnection con = null;
078:
079: public static synchronized TestURLDisplayer getInstance() {
080: if (instance == null) {
081: // the instance is registered by META-INF/services/org.openide.awt.HtmlBrowser$URLDisplayer
082: Object result = Lookup.getDefault().lookup(
083: HtmlBrowser.URLDisplayer.class);
084: // check the instance
085: if (!result.getClass().equals(TestURLDisplayer.class)) {
086: throw new JemmyException(
087: "URL displayer registration failed");
088: }
089: instance = (TestURLDisplayer) result;
090: }
091: return instance;
092: }
093:
094: public synchronized void showURL(URL u) {
095: url = u;
096: try {
097: con = url.openConnection();
098: } catch (IOException ex) {
099: System.err.println("Cannot open URL: " + url);
100: ex.printStackTrace();
101: }
102: // force to send request
103: final URLConnection fc = con;
104: new Thread() {
105: public void run() {
106: try {
107: fc.getInputStream();
108: } catch (IOException ex) {
109: System.err.println("Cannot read URL: " + url);
110: ex.printStackTrace();
111: }
112: }
113: }.start();
114:
115: isURLValid = true;
116: notifyAll();
117: }
118:
119: public synchronized void invalidateURL() {
120: url = null;
121: con = null;
122: isURLValid = false;
123: }
124:
125: public synchronized URL waitURL() throws InterruptedException {
126: while (!isURLValid) {
127: wait(60000);
128: if (!isURLValid) {
129: throw new IllegalStateException("Timeout expired.");
130: }
131: }
132: return url;
133: }
134:
135: public String readURL() {
136: if (!isURLValid || url == null) {
137: throw new IllegalStateException("URL is not valid.");
138: }
139: StringBuffer sb = new StringBuffer();
140: InputStream is = null;
141: try {
142: is = con.getInputStream();
143: try {
144: BufferedReader reader = new BufferedReader(
145: new InputStreamReader(is));
146: String line = null;
147: while ((line = reader.readLine()) != null) {
148: sb.append(line);
149: sb.append('\n');
150: }
151: } finally {
152: is.close();
153: }
154: } catch (Exception ex) {
155: ex.printStackTrace();
156: }
157: return sb.toString();
158: }
159:
160: public URL getURL() {
161: return url;
162: }
163: }
|