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