1/*
2 * Copyright (c) 2001, 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 * @bug 4421190
27 * @summary Tests that Image I/O statics may be referenced properly from
28 *          multiple AppContexts, as would be the case for multiple Applets in a
29 *          single VM. Each AppContext should get its own copy of the registry
30 *          and the caching parameters in the ImageIO class.
31 * @modules java.desktop/sun.awt
32 */
33
34import java.io.File;
35import java.io.IOException;
36
37import javax.imageio.ImageIO;
38import javax.imageio.spi.IIORegistry;
39
40import sun.awt.SunToolkit;
41
42class TestThread extends Thread {
43
44    IIORegistry registry;
45    boolean useCache;
46    File cacheDirectory;
47    boolean cacheSettingsOK = false;
48    String threadName;
49
50    boolean gotCrosstalk = false;
51
52    public TestThread(ThreadGroup tg,
53                      boolean useCache, File cacheDirectory,
54                      String threadName) {
55        super(tg, threadName);
56        this.useCache = useCache;
57        this.cacheDirectory = cacheDirectory;
58        this.threadName = threadName;
59    }
60
61    public void run() {
62//          System.out.println("Thread " + threadName + " in thread group " +
63//                             getThreadGroup().getName());
64
65        // Create a new AppContext as though we were an applet
66        SunToolkit.createNewAppContext();
67
68        // Get default registry and store reference
69        this.registry = IIORegistry.getDefaultInstance();
70
71        for (int i = 0; i < 10; i++) {
72//              System.out.println(threadName +
73//                                 ": setting cache parameters to " +
74//                                 useCache + ", " + cacheDirectory);
75            ImageIO.setUseCache(useCache);
76            ImageIO.setCacheDirectory(cacheDirectory);
77
78            try {
79                sleep(1000L);
80            } catch (InterruptedException e) {
81            }
82
83//              System.out.println(threadName + ": reading cache parameters");
84            boolean newUseCache = ImageIO.getUseCache();
85            File newCacheDirectory = ImageIO.getCacheDirectory();
86            if (newUseCache != useCache ||
87                newCacheDirectory != cacheDirectory) {
88//                  System.out.println(threadName + ": got " +
89//                                     newUseCache + ", " +
90//                                     newCacheDirectory);
91//                  System.out.println(threadName + ": crosstalk encountered!");
92                gotCrosstalk = true;
93            }
94        }
95    }
96
97    public IIORegistry getRegistry() {
98        return registry;
99    }
100
101    public boolean gotCrosstalk() {
102        return gotCrosstalk;
103    }
104}
105
106public class AppContextTest {
107
108    public AppContextTest() {
109        ThreadGroup tg0 = new ThreadGroup("ThreadGroup0");
110        ThreadGroup tg1 = new ThreadGroup("ThreadGroup1");
111
112        TestThread t0 =
113            new TestThread(tg0, false, null, "TestThread 0");
114        TestThread t1 =
115            new TestThread(tg1, true, new File("."), "TestThread 1");
116
117        t0.start();
118        t1.start();
119
120        try {
121            t0.join();
122        } catch (InterruptedException ie0) {
123        }
124        try {
125            t1.join();
126        } catch (InterruptedException ie1) {
127        }
128
129        if (t0.gotCrosstalk() || t1.gotCrosstalk()) {
130            throw new RuntimeException("ImageIO methods had crosstalk!");
131        }
132
133        if (t0.getRegistry() == t1.getRegistry()) {
134            throw new RuntimeException("ThreadGroups had same IIORegistry!");
135        }
136    }
137
138    public static void main(String[] args) throws IOException {
139        new AppContextTest();
140    }
141}
142