Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/AppContextTest.java
38833 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*/3132import java.io.File;33import java.io.IOException;3435import javax.imageio.ImageIO;36import javax.imageio.spi.IIORegistry;3738import sun.awt.SunToolkit;3940class TestThread extends Thread {4142IIORegistry registry;43boolean useCache;44File cacheDirectory;45boolean cacheSettingsOK = false;46String threadName;4748boolean gotCrosstalk = false;4950public TestThread(ThreadGroup tg,51boolean useCache, File cacheDirectory,52String threadName) {53super(tg, threadName);54this.useCache = useCache;55this.cacheDirectory = cacheDirectory;56this.threadName = threadName;57}5859public void run() {60// System.out.println("Thread " + threadName + " in thread group " +61// getThreadGroup().getName());6263// Create a new AppContext as though we were an applet64SunToolkit.createNewAppContext();6566// Get default registry and store reference67this.registry = IIORegistry.getDefaultInstance();6869for (int i = 0; i < 10; i++) {70// System.out.println(threadName +71// ": setting cache parameters to " +72// useCache + ", " + cacheDirectory);73ImageIO.setUseCache(useCache);74ImageIO.setCacheDirectory(cacheDirectory);7576try {77sleep(1000L);78} catch (InterruptedException e) {79}8081// System.out.println(threadName + ": reading cache parameters");82boolean newUseCache = ImageIO.getUseCache();83File newCacheDirectory = ImageIO.getCacheDirectory();84if (newUseCache != useCache ||85newCacheDirectory != cacheDirectory) {86// System.out.println(threadName + ": got " +87// newUseCache + ", " +88// newCacheDirectory);89// System.out.println(threadName + ": crosstalk encountered!");90gotCrosstalk = true;91}92}93}9495public IIORegistry getRegistry() {96return registry;97}9899public boolean gotCrosstalk() {100return gotCrosstalk;101}102}103104public class AppContextTest {105106public AppContextTest() {107ThreadGroup tg0 = new ThreadGroup("ThreadGroup0");108ThreadGroup tg1 = new ThreadGroup("ThreadGroup1");109110TestThread t0 =111new TestThread(tg0, false, null, "TestThread 0");112TestThread t1 =113new TestThread(tg1, true, new File("."), "TestThread 1");114115t0.start();116t1.start();117118try {119t0.join();120} catch (InterruptedException ie0) {121}122try {123t1.join();124} catch (InterruptedException ie1) {125}126127if (t0.gotCrosstalk() || t1.gotCrosstalk()) {128throw new RuntimeException("ImageIO methods had crosstalk!");129}130131if (t0.getRegistry() == t1.getRegistry()) {132throw new RuntimeException("ThreadGroups had same IIORegistry!");133}134}135136public static void main(String[] args) throws IOException {137new AppContextTest();138}139}140141142