Path: blob/master/test/jdk/javax/imageio/AppContextTest.java
41145 views
/*1* Copyright (c) 2001, 2017, 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 442119026* @summary Tests that Image I/O statics may be referenced properly from27* multiple AppContexts, as would be the case for multiple Applets in a28* single VM. Each AppContext should get its own copy of the registry29* and the caching parameters in the ImageIO class.30* @modules java.desktop/sun.awt31*/3233import java.io.File;34import java.io.IOException;3536import javax.imageio.ImageIO;37import javax.imageio.spi.IIORegistry;3839import sun.awt.SunToolkit;4041class TestThread extends Thread {4243IIORegistry registry;44boolean useCache;45File cacheDirectory;46boolean cacheSettingsOK = false;47String threadName;4849boolean gotCrosstalk = false;5051public TestThread(ThreadGroup tg,52boolean useCache, File cacheDirectory,53String threadName) {54super(tg, threadName);55this.useCache = useCache;56this.cacheDirectory = cacheDirectory;57this.threadName = threadName;58}5960public void run() {61// System.out.println("Thread " + threadName + " in thread group " +62// getThreadGroup().getName());6364// Create a new AppContext as though we were an applet65SunToolkit.createNewAppContext();6667// Get default registry and store reference68this.registry = IIORegistry.getDefaultInstance();6970for (int i = 0; i < 10; i++) {71// System.out.println(threadName +72// ": setting cache parameters to " +73// useCache + ", " + cacheDirectory);74ImageIO.setUseCache(useCache);75ImageIO.setCacheDirectory(cacheDirectory);7677try {78sleep(1000L);79} catch (InterruptedException e) {80}8182// System.out.println(threadName + ": reading cache parameters");83boolean newUseCache = ImageIO.getUseCache();84File newCacheDirectory = ImageIO.getCacheDirectory();85if (newUseCache != useCache ||86newCacheDirectory != cacheDirectory) {87// System.out.println(threadName + ": got " +88// newUseCache + ", " +89// newCacheDirectory);90// System.out.println(threadName + ": crosstalk encountered!");91gotCrosstalk = true;92}93}94}9596public IIORegistry getRegistry() {97return registry;98}99100public boolean gotCrosstalk() {101return gotCrosstalk;102}103}104105public class AppContextTest {106107public AppContextTest() {108ThreadGroup tg0 = new ThreadGroup("ThreadGroup0");109ThreadGroup tg1 = new ThreadGroup("ThreadGroup1");110111TestThread t0 =112new TestThread(tg0, false, null, "TestThread 0");113TestThread t1 =114new TestThread(tg1, true, new File("."), "TestThread 1");115116t0.start();117t1.start();118119try {120t0.join();121} catch (InterruptedException ie0) {122}123try {124t1.join();125} catch (InterruptedException ie1) {126}127128if (t0.gotCrosstalk() || t1.gotCrosstalk()) {129throw new RuntimeException("ImageIO methods had crosstalk!");130}131132if (t0.getRegistry() == t1.getRegistry()) {133throw new RuntimeException("ThreadGroups had same IIORegistry!");134}135}136137public static void main(String[] args) throws IOException {138new AppContextTest();139}140}141142143