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