KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > SwingBrowserTest


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.openide.awt;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.StringBufferInputStream JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.lang.reflect.InvocationTargetException JavaDoc;
32 import java.net.HttpURLConnection JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.net.URLConnection JavaDoc;
35 import java.net.URLStreamHandler JavaDoc;
36 import java.util.concurrent.Semaphore JavaDoc;
37 import javax.swing.JFrame JavaDoc;
38 import javax.swing.SwingUtilities JavaDoc;
39 import junit.framework.TestCase;
40
41 /**
42  *
43  * @author Radim
44  */

45 public class SwingBrowserTest extends TestCase {
46
47     public SwingBrowserTest(String JavaDoc testName) {
48         super(testName);
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         
53     }
54
55     public void testSimpleUseOfBrowser() throws Exception JavaDoc {
56         System.out.println("testSimpleUseOfBrowser");
57         // simulates 41891, maybe
58
final HtmlBrowser.Impl impl = new SwingBrowserImpl();
59         final JFrame JavaDoc f = new JFrame JavaDoc();
60         final URL JavaDoc url = new URL JavaDoc("test", "localhost", -1, "simple", new MyStreamHandler());
61         
62         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
63             public void run() {
64                 Component JavaDoc comp = impl.getComponent();
65                 f.add(comp);
66                 f.setVisible(true);
67                 impl.setURL(url);
68             }
69         });
70         waitForLoading(url, f, impl);
71         
72     }
73
74     public void testDeadlockWithDebug() throws Exception JavaDoc {
75         System.out.println("testDeadlockWithDebug");
76         // simulates 71450 - without this special property it fails but that's why we debug with this property set
77
String JavaDoc oldPropValue = System.getProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt");
78         System.setProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt", "true");
79         
80         final HtmlBrowser.Impl impl = new SwingBrowserImpl();
81         final JFrame JavaDoc f = new JFrame JavaDoc();
82         final Semaphore JavaDoc s = new Semaphore JavaDoc(1);
83         final URL JavaDoc url = new URL JavaDoc("test", "localhost", -1, "simple", new MyStreamHandler(s, null));
84         
85         s.acquire();
86         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
87             public void run() {
88                 Component JavaDoc comp = impl.getComponent();
89                 f.add(comp);
90                 f.setVisible(true);
91                 impl.setURL(url);
92             }
93         });
94         System.out.println("browser visible, URL set");
95         // now the browser is waiting for input stream
96

97         // when failing it waits for Semaphore s but this is almost the same
98
// as waiting for reading from socket
99
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
100             public void run() {
101                 impl.reloadDocument();
102             }
103         });
104         System.out.println("reload called");
105         s.release();
106         System.setProperty("org.openide.awt.SwingBrowserImpl.do-not-block-awt", (oldPropValue != null)? oldPropValue: "false");
107         
108         waitForLoading(url, f, impl);
109     }
110
111     public void testDeadlockWithDebugWithoutProperty() throws Exception JavaDoc {
112         System.out.println("testDeadlockWithDebug");
113         // simulates 71450
114

115         final HtmlBrowser.Impl impl = new SwingBrowserImpl();
116         final JFrame JavaDoc f = new JFrame JavaDoc();
117         final Semaphore JavaDoc s = new Semaphore JavaDoc(1);
118         final URL JavaDoc url = new URL JavaDoc("test", "localhost", -1, "simple", new MyStreamHandler(s, null));
119         
120         s.acquire();
121         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
122             public void run() {
123                 Component JavaDoc comp = impl.getComponent();
124                 f.add(comp);
125                 f.setVisible(true);
126                 impl.setURL(url);
127             }
128         });
129         System.out.println("browser visible, URL set");
130         // now the browser is waiting for input stream
131

132         // when failing it waits for Semaphore s but this is almost the same
133
// as waiting for reading from socket
134
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
135             public void run() {
136                 impl.reloadDocument();
137             }
138         });
139         System.out.println("reload called");
140         s.release();
141         
142         waitForLoading(url, f, impl);
143     }
144
145     public void testDeadlockWithJdk6() throws Exception JavaDoc {
146         System.out.println("testDeadlockWithJdk6");
147         // simulates another problem in 71450
148
// fails on JDK6.0 b99 (passes on JDK6b92 or JDK5u9)
149
final HtmlBrowser.Impl impl = new SwingBrowserImpl();
150         final JFrame JavaDoc f = new JFrame JavaDoc();
151         final Semaphore JavaDoc s = new Semaphore JavaDoc(1);
152         final Semaphore JavaDoc s2 = new Semaphore JavaDoc(1);
153         final URL JavaDoc url = new URL JavaDoc("test", "localhost", -1, "simple", new MyStreamHandler(s, s2));
154         final URL JavaDoc url2 = new URL JavaDoc("test", "localhost", -1, "simple2", new MyStreamHandler(null, null));
155         
156         s.acquire();
157         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
158             public void run() {
159                 Component JavaDoc comp = impl.getComponent();
160                 f.add(comp);
161                 f.setVisible(true);
162                 impl.setURL(url);
163             }
164         });
165         System.out.println("browser visible, URL set");
166         // now the browser is waiting for input stream
167

168         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
169             public void run() {
170                 System.out.println("before 2nd setURL");
171                 // allow to read the stream
172
s.release();
173                 try {
174                     s2.acquire();
175                 } catch (InterruptedException JavaDoc ex) {
176                     ex.printStackTrace();
177                     fail(ex.getMessage());
178                 }
179                 
180                 impl.setURL(url2);
181                 s2.release();
182                 System.out.println("after 2nd setURL");
183             }
184         });
185         System.out.println("new URL requested");
186         
187         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
188             public void run() {
189                 impl.getURL();
190             }
191         });
192         System.out.println("getURL called");
193         waitForLoading(url2, f, impl);
194     }
195     
196     private void waitForLoading(final URL JavaDoc url, final JFrame JavaDoc f, final HtmlBrowser.Impl impl)
197             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
198
199         for (int i = 0; i < 10 && f.isVisible(); i++) {
200             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
201                 public void run() {
202                     URL JavaDoc current = impl.getURL();
203                     if (url.equals(current)) {
204                         f.setVisible(false);
205                         f.dispose();
206                     }
207                 }
208             });
209             Thread.sleep(i*100);
210         }
211     }
212     
213     private static class MyStreamHandler extends URLStreamHandler JavaDoc {
214         private Semaphore JavaDoc sIn;
215         private Semaphore JavaDoc sOut;
216         
217         MyStreamHandler() {}
218         MyStreamHandler(Semaphore JavaDoc s, Semaphore JavaDoc s2) {
219             sIn = s;
220             sOut = s2;
221             try {
222                 if (sOut != null) {
223                     sOut.acquire();
224                 }
225             } catch (InterruptedException JavaDoc ex) {
226                 ex.printStackTrace();
227                 fail(ex.getMessage());
228             }
229         }
230         
231         protected URLConnection JavaDoc openConnection(URL JavaDoc u) throws IOException JavaDoc {
232             return new MyConnection(sIn, sOut, u);
233         }
234     }
235     
236     private static class MyConnection extends HttpURLConnection JavaDoc {
237         private Semaphore JavaDoc sIn;
238         private Semaphore JavaDoc sOut;
239         
240         protected MyConnection(Semaphore JavaDoc s, Semaphore JavaDoc s2, URL JavaDoc u) {
241             super(u);
242             sIn = s;
243             sOut = s2;
244         }
245
246         public void connect() throws IOException JavaDoc {
247         }
248
249         public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
250             return new StringBufferInputStream JavaDoc("blabla");
251         }
252
253         public void disconnect() {
254             // noop
255
}
256
257         public boolean usingProxy() {
258             return false;
259         }
260
261         public int getResponseCode() throws IOException JavaDoc {
262             System.out.println("connecting "+toString()+" ... isEDT = "+SwingUtilities.isEventDispatchThread());
263 // Thread.dumpStack();
264
if (sIn != null) {
265                 try {
266                     sIn.acquire();
267                     if (sOut != null) {
268                         sOut.release();
269                     }
270                     System.out.println("aquired in lock, released out "+toString());
271                 } catch (InterruptedException JavaDoc ex) {
272                     ex.printStackTrace();
273                     fail(ex.getMessage());
274                 }
275                 sIn.release();
276             }
277             System.out.println("... connected "+toString());
278             
279             return super.getResponseCode();
280         }
281
282         public String JavaDoc toString() {
283             return "MyConnection ["+url.toExternalForm()+" sIn "+sIn+" sOut "+sOut+"]";
284         }
285         
286
287     }
288 }
289
Popular Tags