1 /*
   2  * Copyright (c) 2010, 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  * @bug 6938813
  27  * @summary Swing mutable statics
  28  * @author Pavel Porvatov
  29  */
  30 
  31 import sun.awt.AppContext;
  32 import sun.awt.SunToolkit;
  33 
  34 import javax.swing.text.html.HTMLEditorKit;
  35 import javax.swing.text.html.StyleSheet;
  36 import javax.swing.text.html.parser.DTD;
  37 import javax.swing.text.html.parser.ParserDelegator;
  38 import java.lang.reflect.Field;
  39 
  40 public class bug6938813 {
  41     public static final String DTD_KEY = "dtd_key";
  42 
  43     private static volatile StyleSheet styleSheet;
  44 
  45     public static void main(String[] args) throws Exception {
  46         // Run validation and init values for this AppContext
  47         validate();
  48 
  49         Thread thread = new ThreadInAnotherAppContext();
  50 
  51         thread.start();
  52         thread.join();
  53     }
  54 
  55     private static void validate() throws Exception {
  56         AppContext appContext = AppContext.getAppContext();
  57 
  58         assertTrue(DTD.getDTD(DTD_KEY).getName().equals(DTD_KEY), "DTD.getDTD() mixed AppContexts");
  59 
  60         // Spoil hash value
  61         DTD invalidDtd = DTD.getDTD("invalid DTD");
  62 
  63         DTD.putDTDHash(DTD_KEY, invalidDtd);
  64 
  65         assertTrue(DTD.getDTD(DTD_KEY) == invalidDtd, "Something wrong with DTD.getDTD()");
  66 
  67         Object dtdKey = getParserDelegator_DTD_KEY();
  68 
  69         assertTrue(appContext.get(dtdKey) == null, "ParserDelegator mixed AppContexts");
  70 
  71         // Init default DTD
  72         new ParserDelegator();
  73 
  74         Object dtdValue = appContext.get(dtdKey);
  75 
  76         assertTrue(dtdValue != null, "ParserDelegator.defaultDTD isn't initialized");
  77 
  78         // Try reinit default DTD
  79         new ParserDelegator();
  80 
  81         assertTrue(dtdValue == appContext.get(dtdKey), "ParserDelegator.defaultDTD created a duplicate");
  82 
  83         HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
  84 
  85         if (styleSheet == null) {
  86             // First AppContext
  87             styleSheet = htmlEditorKit.getStyleSheet();
  88 
  89             assertTrue(styleSheet != null, "htmlEditorKit.getStyleSheet() returns null");
  90             assertTrue(htmlEditorKit.getStyleSheet() == styleSheet, "Something wrong with htmlEditorKit.getStyleSheet()");
  91         } else {
  92             assertTrue(htmlEditorKit.getStyleSheet() != styleSheet, "HtmlEditorKit.getStyleSheet() mixed AppContexts");
  93         }
  94     }
  95 
  96     private static void assertTrue(boolean b, String msg) {
  97         if (!b) {
  98             throw new RuntimeException("Test failed: " + msg);
  99         }
 100     }
 101 
 102     private static Object getParserDelegator_DTD_KEY() throws Exception {
 103         Field field = ParserDelegator.class.getDeclaredField("DTD_KEY");
 104 
 105         field.setAccessible(true);
 106 
 107         return field.get(null);
 108     }
 109 
 110     private static class ThreadInAnotherAppContext extends Thread {
 111         public ThreadInAnotherAppContext() {
 112             super(new ThreadGroup("6938813"), "ThreadInAnotherAppContext");
 113         }
 114 
 115         public void run() {
 116             SunToolkit.createNewAppContext();
 117 
 118             try {
 119                 validate();
 120             } catch (Exception e) {
 121                 throw new RuntimeException(e);
 122             }
 123         }
 124     }
 125 }