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