Path: blob/master/test/jdk/javax/swing/JEditorPane/4492274/bug4492274.java
58462 views
/*1* Copyright (c) 2007, 2011, 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/*24* @test25* @key headful26* @bug 449227427* @summary Tests if JEditorPane.getPage() correctly returns anchor reference.28* @author Denis Sharypov29* @run main bug449227430*/3132import javax.swing.*;33import javax.swing.text.html.HTMLEditorKit;34import java.awt.*;35import java.io.File;36import java.net.URL;3738public class bug4492274 {3940private static URL page;4142private static JEditorPane jep;4344private static JFrame f;4546public static void main(String args[]) throws Exception {47try {48Robot robot = new Robot();49SwingUtilities.invokeAndWait(new Runnable() {50@Override51public void run() {52createAndShowGUI();53}54});5556robot.waitForIdle();5758SwingUtilities.invokeAndWait(new Runnable() {59@Override60public void run() {61try {62page = new URL(page, "#linkname");63jep.setPage(page);64} catch (Exception e) {65throw new RuntimeException(e);66}67}68});6970robot.waitForIdle();7172if (getPageAnchor() == null) {73throw new RuntimeException("JEditorPane.getPage() returns null anchor reference");74}75} finally {76if (f != null) SwingUtilities.invokeAndWait(() -> f.dispose());77}78}7980private static String getPageAnchor() throws Exception {81final String[] result = new String[1];8283SwingUtilities.invokeAndWait(new Runnable() {84@Override85public void run() {86result[0] = jep.getPage().getRef();87}88});8990return result[0];91}9293private static void createAndShowGUI() {94try {95File file = new File(System.getProperty("test.src", "."), "test.html");96page = file.toURI().toURL();9798f = new JFrame();99100jep = new JEditorPane();101jep.setEditorKit(new HTMLEditorKit());102jep.setEditable(false);103jep.setPage(page);104105JScrollPane sp = new JScrollPane(jep);106107f.getContentPane().add(sp);108f.setSize(500, 500);109f.setVisible(true);110111} catch (Exception e) {112throw new RuntimeException(e);113}114}115}116117118