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