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