KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > TestURLDisplayer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.web;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.DataOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import org.netbeans.jemmy.JemmyException;
32 import org.openide.awt.HtmlBrowser;
33 import org.openide.util.Lookup;
34
35 /**
36  * Designed to replace default URLDisplayer to better serve automated tests
37  * requirements.
38  * <p>
39  * Usage:<br>
40  * <pre>
41  * TestURLDisplayer displayer = TestURLDisplayer.getInstance();
42  * displayer.invalidateURL();
43  * // e.g. run a .jsp
44  * displayer.waitURL();
45  * String page = displayer.readURL();
46  * ...
47  * </pre>
48  *
49  * @author Martin.Schovanek@sun.com
50  */

51 public final class TestURLDisplayer extends HtmlBrowser.URLDisplayer {
52     private static TestURLDisplayer instance;
53     private boolean isURLValid = false;
54     private URL JavaDoc url = null;
55     private URLConnection JavaDoc con = null;
56     
57     public static synchronized TestURLDisplayer getInstance() {
58         if (instance==null) {
59             instance=registerTestURLDisplayer();
60         }
61         return instance;
62     }
63     
64     public synchronized void showURL(URL JavaDoc u) {
65         url=u;
66         try {
67             con = url.openConnection();
68         } catch (IOException JavaDoc ex) {
69             System.err.println("Cannot open URL: "+url);
70             ex.printStackTrace();
71         }
72         // force to send request
73
final URLConnection JavaDoc fc = con;
74         new Thread JavaDoc () {
75             public void run() {
76                 try {
77                     fc.getInputStream();
78                 } catch (IOException JavaDoc ex) {
79                     System.err.println("Cannot read URL: "+url);
80                     ex.printStackTrace();
81                 }
82             }
83         }.start();
84
85         notifyAll();
86         isURLValid=true;
87     }
88     
89     public synchronized void invalidateURL() {
90         url = null;
91         con = null;
92         isURLValid = false;
93     }
94     
95     public synchronized URL JavaDoc waitURL() throws InterruptedException JavaDoc {
96         while (!isURLValid) {
97             wait(60000);
98             if (!isURLValid) {
99                 throw new IllegalStateException JavaDoc("Timeout expired.");
100             }
101         }
102         return url;
103     }
104     
105     public String JavaDoc readURL() {
106         if (!isURLValid || url == null) {
107             throw new IllegalStateException JavaDoc("URL is not valid.");
108         }
109         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
110         InputStream JavaDoc is = null;
111         try{
112             is = con.getInputStream();
113             BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
114             String JavaDoc line = null;
115             while((line=reader.readLine()) != null){
116                 sb.append(line+"\n");
117             }
118         }catch(Exception JavaDoc ex){
119             ex.printStackTrace();
120         }finally{
121             try{
122                 is.close();
123             }catch(Exception JavaDoc ex){
124                 // nothing to do
125
}
126         }
127         return sb.toString();
128     }
129     
130     public URL JavaDoc getURL() {
131         return url;
132     }
133     
134     private static TestURLDisplayer registerTestURLDisplayer() {
135         try {
136             File JavaDoc file = new File JavaDoc(System.getProperty("xtest.workdir")+"/sys/tests" +
137                     "/META-INF/services/org.openide.awt.HtmlBrowser$URLDisplayer");
138             DataOutputStream JavaDoc dout = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
139             dout.writeBytes(
140                     "#-org.netbeans.core.NbTopManager$NbURLDisplayer\n"+
141                     "org.netbeans.test.web.TestURLDisplayer\n");
142             dout.close();
143         } catch (IOException JavaDoc ex) {
144             throw new JemmyException("Cannot register URL displayer.", ex);
145         }
146         Object JavaDoc displayer = Lookup.getDefault().lookup(HtmlBrowser.URLDisplayer.class);
147         if (!displayer.getClass().equals(TestURLDisplayer.class)) {
148             throw new JemmyException("URL displayer registration failed");
149         }
150         return (TestURLDisplayer) displayer;
151     }
152 }
153
Popular Tags