Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/Security/6938813/bug6938813.java
38854 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 693881326* @summary Swing mutable statics27* @author Pavel Porvatov28*/2930import sun.awt.AppContext;31import sun.awt.SunToolkit;3233import javax.swing.text.html.HTMLEditorKit;34import javax.swing.text.html.StyleSheet;35import javax.swing.text.html.parser.DTD;36import javax.swing.text.html.parser.ParserDelegator;37import java.lang.reflect.Field;3839public class bug6938813 {40public static final String DTD_KEY = "dtd_key";4142private static volatile StyleSheet styleSheet;4344public static void main(String[] args) throws Exception {45// Run validation and init values for this AppContext46validate();4748Thread thread = new ThreadInAnotherAppContext();4950thread.start();51thread.join();52}5354private static void validate() throws Exception {55AppContext appContext = AppContext.getAppContext();5657assertTrue(DTD.getDTD(DTD_KEY).getName().equals(DTD_KEY), "DTD.getDTD() mixed AppContexts");5859// Spoil hash value60DTD invalidDtd = DTD.getDTD("invalid DTD");6162DTD.putDTDHash(DTD_KEY, invalidDtd);6364assertTrue(DTD.getDTD(DTD_KEY) == invalidDtd, "Something wrong with DTD.getDTD()");6566Object dtdKey = getParserDelegator_DTD_KEY();6768assertTrue(appContext.get(dtdKey) == null, "ParserDelegator mixed AppContexts");6970// Init default DTD71new ParserDelegator();7273Object dtdValue = appContext.get(dtdKey);7475assertTrue(dtdValue != null, "ParserDelegator.defaultDTD isn't initialized");7677// Try reinit default DTD78new ParserDelegator();7980assertTrue(dtdValue == appContext.get(dtdKey), "ParserDelegator.defaultDTD created a duplicate");8182HTMLEditorKit htmlEditorKit = new HTMLEditorKit();8384if (styleSheet == null) {85// First AppContext86styleSheet = htmlEditorKit.getStyleSheet();8788assertTrue(styleSheet != null, "htmlEditorKit.getStyleSheet() returns null");89assertTrue(htmlEditorKit.getStyleSheet() == styleSheet, "Something wrong with htmlEditorKit.getStyleSheet()");90} else {91assertTrue(htmlEditorKit.getStyleSheet() != styleSheet, "HtmlEditorKit.getStyleSheet() mixed AppContexts");92}93}9495private static void assertTrue(boolean b, String msg) {96if (!b) {97throw new RuntimeException("Test failed: " + msg);98}99}100101private static Object getParserDelegator_DTD_KEY() throws Exception {102Field field = ParserDelegator.class.getDeclaredField("DTD_KEY");103104field.setAccessible(true);105106return field.get(null);107}108109private static class ThreadInAnotherAppContext extends Thread {110public ThreadInAnotherAppContext() {111super(new ThreadGroup("6938813"), "ThreadInAnotherAppContext");112}113114public void run() {115SunToolkit.createNewAppContext();116117try {118validate();119} catch (Exception e) {120throw new RuntimeException(e);121}122}123}124}125126127