Path: blob/master/test/jdk/javax/swing/JEditorPane/bug4714674.java
58328 views
/*1* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 471467425@summary Tests that JEditorPane opens HTTP connection asynchronously26@author Peter Zhelezniakov27@modules java.desktop28jdk.httpserver29@run main/othervm bug471467430*/3132import javax.swing.*;3334import com.sun.net.httpserver.HttpExchange;35import com.sun.net.httpserver.HttpHandler;36import com.sun.net.httpserver.HttpServer;37import java.io.IOException;38import java.net.InetSocketAddress;39import java.util.concurrent.Executors;404142public class bug4714674 {4344public static void main(String[] args) throws Exception {45new bug4714674().test();46}4748private void test() throws Exception {49final DeafServer server = new DeafServer();50final String baseURL = "http://localhost:" + server.getPort() + "/";5152SwingUtilities.invokeLater(new Runnable() {53public void run() {54try {55JEditorPane pane = new JEditorPane();56((javax.swing.text.AbstractDocument)pane.getDocument()).57setAsynchronousLoadPriority(1);5859// this will block EDT unless 4714674 is fixed60pane.setPage(baseURL);61} catch (IOException e) {62// should not happen63throw new Error(e);64}65}66});6768// if 4714674 is fixed, this executes before connection times out69SwingUtilities.invokeLater(new Runnable() {70public void run() {71synchronized (server) {72server.wakeup = true;73server.notifyAll();74}75pass();76}77});7879// wait, then check test status80try {81Thread.sleep(5000);82if (!passed()) {83throw new RuntimeException("Failed: EDT was blocked");84}85} finally {86server.destroy();87}88}8990private boolean passed = false;9192private synchronized boolean passed() {93return passed;94}9596private synchronized void pass() {97passed = true;98}99}100101/**102* A "deaf" HTTP server that does not respond to requests.103*/104class DeafServer {105HttpServer server;106boolean wakeup = false;107108/**109* Create and start the HTTP server.110*/111public DeafServer() throws IOException {112InetSocketAddress addr = new InetSocketAddress(0);113server = HttpServer.create(addr, 0);114HttpHandler handler = new DeafHandler();115server.createContext("/", handler);116server.setExecutor(Executors.newCachedThreadPool());117server.start();118}119120/**121* Stop server without any delay.122*/123public void destroy() {124server.stop(0);125}126127/**128* Return actual server port number, useful for constructing request URIs.129*/130public int getPort() {131return server.getAddress().getPort();132}133134135class DeafHandler implements HttpHandler {136public void handle(HttpExchange r) throws IOException {137synchronized (DeafServer.this) {138while (! wakeup) {139try {140DeafServer.this.wait();141} catch (InterruptedException e) {142// just wait again143}144}145}146}147}148}149150151