bug4714674.java revision 8729:0242fce0f717
1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug 4714674
26   @summary Tests that JEditorPane opens HTTP connection asynchronously
27   @author Peter Zhelezniakov
28   @run main bug4714674
29*/
30
31import javax.swing.*;
32
33import com.sun.net.httpserver.HttpExchange;
34import com.sun.net.httpserver.HttpHandler;
35import com.sun.net.httpserver.HttpServer;
36import java.io.IOException;
37import java.net.InetSocketAddress;
38import java.util.concurrent.Executors;
39
40
41public class bug4714674 {
42
43    public static void main(String[] args) throws Exception {
44        new bug4714674().test();
45    }
46
47    private void test() throws Exception {
48        final DeafServer server = new DeafServer();
49        final String baseURL = "http://localhost:" + server.getPort() + "/";
50
51        SwingUtilities.invokeLater(new Runnable() {
52            public void run() {
53                try {
54                    JEditorPane pane = new JEditorPane();
55                    ((javax.swing.text.AbstractDocument)pane.getDocument()).
56                            setAsynchronousLoadPriority(1);
57
58                    // this will block EDT unless 4714674 is fixed
59                    pane.setPage(baseURL);
60                } catch (IOException e) {
61                    // should not happen
62                    throw new Error(e);
63                }
64            }
65        });
66
67        // if 4714674 is fixed, this executes before connection times out
68        SwingUtilities.invokeLater(new Runnable() {
69            public void run() {
70                synchronized (server) {
71                    server.wakeup = true;
72                    server.notifyAll();
73                }
74                pass();
75            }
76        });
77
78        // wait, then check test status
79        try {
80            Thread.sleep(5000);
81            if (!passed()) {
82                throw new RuntimeException("Failed: EDT was blocked");
83            }
84        } finally {
85            server.destroy();
86        }
87    }
88
89    private boolean passed = false;
90
91    private synchronized boolean passed() {
92        return passed;
93    }
94
95    private synchronized void pass() {
96        passed = true;
97    }
98}
99
100/**
101 * A "deaf" HTTP server that does not respond to requests.
102 */
103class DeafServer {
104    HttpServer server;
105    boolean wakeup = false;
106
107    /**
108     * Create and start the HTTP server.
109     */
110    public DeafServer() throws IOException {
111        InetSocketAddress addr = new InetSocketAddress(0);
112        server = HttpServer.create(addr, 0);
113        HttpHandler handler = new DeafHandler();
114        server.createContext("/", handler);
115        server.setExecutor(Executors.newCachedThreadPool());
116        server.start();
117    }
118
119    /**
120     * Stop server without any delay.
121     */
122    public void destroy() {
123        server.stop(0);
124    }
125
126    /**
127     * Return actual server port number, useful for constructing request URIs.
128     */
129    public int getPort() {
130        return server.getAddress().getPort();
131    }
132
133
134    class DeafHandler implements HttpHandler {
135        public void handle(HttpExchange r) throws IOException {
136            synchronized (DeafServer.this) {
137                while (! wakeup) {
138                    try {
139                        DeafServer.this.wait();
140                    } catch (InterruptedException e) {
141                        // just wait again
142                    }
143                }
144            }
145        }
146    }
147}
148