1/* 2 * Copyright (c) 2008, 2015, 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 @modules java.desktop 29 jdk.httpserver 30 @run main bug4714674 31*/ 32 33import javax.swing.*; 34 35import com.sun.net.httpserver.HttpExchange; 36import com.sun.net.httpserver.HttpHandler; 37import com.sun.net.httpserver.HttpServer; 38import java.io.IOException; 39import java.net.InetSocketAddress; 40import java.util.concurrent.Executors; 41 42 43public class bug4714674 { 44 45 public static void main(String[] args) throws Exception { 46 new bug4714674().test(); 47 } 48 49 private void test() throws Exception { 50 final DeafServer server = new DeafServer(); 51 final String baseURL = "http://localhost:" + server.getPort() + "/"; 52 53 SwingUtilities.invokeLater(new Runnable() { 54 public void run() { 55 try { 56 JEditorPane pane = new JEditorPane(); 57 ((javax.swing.text.AbstractDocument)pane.getDocument()). 58 setAsynchronousLoadPriority(1); 59 60 // this will block EDT unless 4714674 is fixed 61 pane.setPage(baseURL); 62 } catch (IOException e) { 63 // should not happen 64 throw new Error(e); 65 } 66 } 67 }); 68 69 // if 4714674 is fixed, this executes before connection times out 70 SwingUtilities.invokeLater(new Runnable() { 71 public void run() { 72 synchronized (server) { 73 server.wakeup = true; 74 server.notifyAll(); 75 } 76 pass(); 77 } 78 }); 79 80 // wait, then check test status 81 try { 82 Thread.sleep(5000); 83 if (!passed()) { 84 throw new RuntimeException("Failed: EDT was blocked"); 85 } 86 } finally { 87 server.destroy(); 88 } 89 } 90 91 private boolean passed = false; 92 93 private synchronized boolean passed() { 94 return passed; 95 } 96 97 private synchronized void pass() { 98 passed = true; 99 } 100} 101 102/** 103 * A "deaf" HTTP server that does not respond to requests. 104 */ 105class DeafServer { 106 HttpServer server; 107 boolean wakeup = false; 108 109 /** 110 * Create and start the HTTP server. 111 */ 112 public DeafServer() throws IOException { 113 InetSocketAddress addr = new InetSocketAddress(0); 114 server = HttpServer.create(addr, 0); 115 HttpHandler handler = new DeafHandler(); 116 server.createContext("/", handler); 117 server.setExecutor(Executors.newCachedThreadPool()); 118 server.start(); 119 } 120 121 /** 122 * Stop server without any delay. 123 */ 124 public void destroy() { 125 server.stop(0); 126 } 127 128 /** 129 * Return actual server port number, useful for constructing request URIs. 130 */ 131 public int getPort() { 132 return server.getAddress().getPort(); 133 } 134 135 136 class DeafHandler implements HttpHandler { 137 public void handle(HttpExchange r) throws IOException { 138 synchronized (DeafServer.this) { 139 while (! wakeup) { 140 try { 141 DeafServer.this.wait(); 142 } catch (InterruptedException e) { 143 // just wait again 144 } 145 } 146 } 147 } 148 } 149} 150