bug4492274.java revision 11018:66682f651425
1/*
2 * Copyright (c) 2007, 2011, 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 4492274
26 * @summary  Tests if JEditorPane.getPage() correctly returns anchor reference.
27 * @author Denis Sharypov
28 * @run main bug4492274
29 */
30
31import javax.swing.*;
32import javax.swing.text.html.HTMLEditorKit;
33import java.awt.*;
34import java.io.File;
35import java.net.URL;
36
37public class bug4492274 {
38
39    private static URL page;
40
41    private static JEditorPane jep;
42
43    public static void main(String args[]) throws Exception {
44
45        Robot robot = new Robot();
46        SwingUtilities.invokeAndWait(new Runnable() {
47            @Override
48            public void run() {
49                createAndShowGUI();
50            }
51        });
52
53        robot.waitForIdle();
54
55        SwingUtilities.invokeAndWait(new Runnable() {
56            @Override
57            public void run() {
58                try {
59                    page = new URL(page, "#linkname");
60                    jep.setPage(page);
61                } catch (Exception e) {
62                    throw new RuntimeException(e);
63                }
64            }
65        });
66
67        robot.waitForIdle();
68
69        if (getPageAnchor() == null) {
70            throw new RuntimeException("JEditorPane.getPage() returns null anchor reference");
71        }
72
73    }
74
75    private static String getPageAnchor() throws Exception {
76        final String[] result = new String[1];
77
78        SwingUtilities.invokeAndWait(new Runnable() {
79            @Override
80            public void run() {
81                result[0] = jep.getPage().getRef();
82            }
83        });
84
85        return result[0];
86    }
87
88    private static void createAndShowGUI() {
89        try {
90            File file = new File(System.getProperty("test.src", "."), "test.html");
91            page = file.toURI().toURL();
92
93            JFrame f = new JFrame();
94
95            jep = new JEditorPane();
96            jep.setEditorKit(new HTMLEditorKit());
97            jep.setEditable(false);
98            jep.setPage(page);
99
100            JScrollPane sp = new JScrollPane(jep);
101
102            f.getContentPane().add(sp);
103            f.setSize(500, 500);
104            f.setVisible(true);
105
106        } catch (Exception e) {
107            throw new RuntimeException(e);
108        }
109    }
110}
111