186N/A/* @test
186N/A @bug 4714674
186N/A @summary Tests that JEditorPane opens HTTP connection asynchronously
186N/A @author Peter Zhelezniakov
186N/A @run main bug4714674
186N/A*/
186N/A
186N/Aimport javax.swing.*;
186N/A
186N/Aimport com.sun.net.httpserver.HttpExchange;
186N/Aimport com.sun.net.httpserver.HttpHandler;
186N/Aimport com.sun.net.httpserver.HttpServer;
186N/Aimport java.io.IOException;
186N/Aimport java.net.InetSocketAddress;
186N/Aimport java.util.concurrent.Executors;
186N/A
186N/A
186N/Apublic class bug4714674 {
186N/A
186N/A public static void main(String[] args) throws Exception {
186N/A new bug4714674().test();
186N/A }
186N/A
186N/A private void test() throws Exception {
186N/A final DeafServer server = new DeafServer();
186N/A final String baseURL = "http://localhost:" + server.getPort() + "/";
186N/A
186N/A SwingUtilities.invokeLater(new Runnable() {
186N/A public void run() {
186N/A try {
186N/A JEditorPane pane = new JEditorPane();
186N/A ((javax.swing.text.AbstractDocument)pane.getDocument()).
186N/A setAsynchronousLoadPriority(1);
186N/A
186N/A // this will block EDT unless 4714674 is fixed
186N/A pane.setPage(baseURL);
186N/A } catch (IOException e) {
186N/A // should not happen
186N/A throw new Error(e);
186N/A }
186N/A }
186N/A });
186N/A
186N/A // if 4714674 is fixed, this executes before connection times out
186N/A SwingUtilities.invokeLater(new Runnable() {
186N/A public void run() {
186N/A synchronized (server) {
186N/A server.wakeup = true;
186N/A server.notifyAll();
186N/A }
186N/A pass();
186N/A }
186N/A });
186N/A
186N/A // wait, then check test status
186N/A try {
186N/A Thread.sleep(5000);
186N/A if (!passed()) {
186N/A throw new RuntimeException("Failed: EDT was blocked");
186N/A }
186N/A } finally {
186N/A server.destroy();
186N/A }
186N/A }
186N/A
186N/A private boolean passed = false;
186N/A
186N/A private synchronized boolean passed() {
186N/A return passed;
186N/A }
186N/A
186N/A private synchronized void pass() {
186N/A passed = true;
186N/A }
186N/A}
186N/A
186N/A/**
186N/A * A "deaf" HTTP server that does not respond to requests.
186N/A */
186N/Aclass DeafServer {
186N/A HttpServer server;
186N/A boolean wakeup = false;
186N/A
186N/A /**
186N/A * Create and start the HTTP server.
186N/A */
186N/A public DeafServer() throws IOException {
186N/A InetSocketAddress addr = new InetSocketAddress(0);
186N/A server = HttpServer.create(addr, 0);
186N/A HttpHandler handler = new DeafHandler();
186N/A server.createContext("/", handler);
186N/A server.setExecutor(Executors.newCachedThreadPool());
186N/A server.start();
186N/A }
186N/A
186N/A /**
186N/A * Stop server without any delay.
186N/A */
186N/A public void destroy() {
186N/A server.stop(0);
186N/A }
186N/A
186N/A /**
186N/A * Return actual server port number, useful for constructing request URIs.
186N/A */
186N/A public int getPort() {
186N/A return server.getAddress().getPort();
186N/A }
186N/A
186N/A
186N/A class DeafHandler implements HttpHandler {
186N/A public void handle(HttpExchange r) throws IOException {
186N/A synchronized (DeafServer.this) {
186N/A while (! wakeup) {
186N/A try {
186N/A DeafServer.this.wait();
186N/A } catch (InterruptedException e) {
186N/A // just wait again
186N/A }
186N/A }
186N/A }
186N/A }
186N/A }
186N/A}