Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JEditorPane/bug4714674.java
38838 views
/*1* Copyright (c) 2008, 2013, 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@run main bug471467428*/2930import javax.swing.*;3132import com.sun.net.httpserver.HttpExchange;33import com.sun.net.httpserver.HttpHandler;34import com.sun.net.httpserver.HttpServer;35import java.io.IOException;36import java.net.InetSocketAddress;37import java.util.concurrent.Executors;383940public class bug4714674 {4142public static void main(String[] args) throws Exception {43new bug4714674().test();44}4546private void test() throws Exception {47final DeafServer server = new DeafServer();48final String baseURL = "http://localhost:" + server.getPort() + "/";4950SwingUtilities.invokeLater(new Runnable() {51public void run() {52try {53JEditorPane pane = new JEditorPane();54((javax.swing.text.AbstractDocument)pane.getDocument()).55setAsynchronousLoadPriority(1);5657// this will block EDT unless 4714674 is fixed58pane.setPage(baseURL);59} catch (IOException e) {60// should not happen61throw new Error(e);62}63}64});6566// if 4714674 is fixed, this executes before connection times out67SwingUtilities.invokeLater(new Runnable() {68public void run() {69synchronized (server) {70server.wakeup = true;71server.notifyAll();72}73pass();74}75});7677// wait, then check test status78try {79Thread.sleep(5000);80if (!passed()) {81throw new RuntimeException("Failed: EDT was blocked");82}83} finally {84server.destroy();85}86}8788private boolean passed = false;8990private synchronized boolean passed() {91return passed;92}9394private synchronized void pass() {95passed = true;96}97}9899/**100* A "deaf" HTTP server that does not respond to requests.101*/102class DeafServer {103HttpServer server;104boolean wakeup = false;105106/**107* Create and start the HTTP server.108*/109public DeafServer() throws IOException {110InetSocketAddress addr = new InetSocketAddress(0);111server = HttpServer.create(addr, 0);112HttpHandler handler = new DeafHandler();113server.createContext("/", handler);114server.setExecutor(Executors.newCachedThreadPool());115server.start();116}117118/**119* Stop server without any delay.120*/121public void destroy() {122server.stop(0);123}124125/**126* Return actual server port number, useful for constructing request URIs.127*/128public int getPort() {129return server.getAddress().getPort();130}131132133class DeafHandler implements HttpHandler {134public void handle(HttpExchange r) throws IOException {135synchronized (DeafServer.this) {136while (! wakeup) {137try {138DeafServer.this.wait();139} catch (InterruptedException e) {140// just wait again141}142}143}144}145}146}147148149