test/jdk/javax/imageio/AppContextTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45351 jdk/test/javax/imageio/AppContextTest.java@f7f615a3aaff
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45351
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     1
/*
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     2
 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     4
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     8
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    13
 * accompanied this code).
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    14
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    18
 *
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    21
 * questions.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    22
 */
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    23
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    24
/*
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    25
 * @test
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    26
 * @bug 4421190
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    27
 * @summary Tests that Image I/O statics may be referenced properly from
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    28
 *          multiple AppContexts, as would be the case for multiple Applets in a
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    29
 *          single VM. Each AppContext should get its own copy of the registry
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    30
 *          and the caching parameters in the ImageIO class.
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    31
 * @modules java.desktop/sun.awt
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    32
 */
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    33
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    34
import java.io.File;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    35
import java.io.IOException;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    36
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    37
import javax.imageio.ImageIO;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    38
import javax.imageio.spi.IIORegistry;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    39
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    40
import sun.awt.SunToolkit;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    41
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    42
class TestThread extends Thread {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    43
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    44
    IIORegistry registry;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    45
    boolean useCache;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    46
    File cacheDirectory;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    47
    boolean cacheSettingsOK = false;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    48
    String threadName;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    49
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    50
    boolean gotCrosstalk = false;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    51
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    52
    public TestThread(ThreadGroup tg,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    53
                      boolean useCache, File cacheDirectory,
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    54
                      String threadName) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    55
        super(tg, threadName);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    56
        this.useCache = useCache;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    57
        this.cacheDirectory = cacheDirectory;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    58
        this.threadName = threadName;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    59
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    60
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    61
    public void run() {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    62
//          System.out.println("Thread " + threadName + " in thread group " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    63
//                             getThreadGroup().getName());
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    64
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    65
        // Create a new AppContext as though we were an applet
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    66
        SunToolkit.createNewAppContext();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    67
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    68
        // Get default registry and store reference
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    69
        this.registry = IIORegistry.getDefaultInstance();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    70
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    71
        for (int i = 0; i < 10; i++) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    72
//              System.out.println(threadName +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    73
//                                 ": setting cache parameters to " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    74
//                                 useCache + ", " + cacheDirectory);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    75
            ImageIO.setUseCache(useCache);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    76
            ImageIO.setCacheDirectory(cacheDirectory);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    77
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    78
            try {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    79
                sleep(1000L);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    80
            } catch (InterruptedException e) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    81
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    82
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    83
//              System.out.println(threadName + ": reading cache parameters");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    84
            boolean newUseCache = ImageIO.getUseCache();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    85
            File newCacheDirectory = ImageIO.getCacheDirectory();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    86
            if (newUseCache != useCache ||
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    87
                newCacheDirectory != cacheDirectory) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    88
//                  System.out.println(threadName + ": got " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    89
//                                     newUseCache + ", " +
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    90
//                                     newCacheDirectory);
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    91
//                  System.out.println(threadName + ": crosstalk encountered!");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    92
                gotCrosstalk = true;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    93
            }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    94
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    95
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    96
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    97
    public IIORegistry getRegistry() {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    98
        return registry;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
    99
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   100
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   101
    public boolean gotCrosstalk() {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   102
        return gotCrosstalk;
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   103
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   104
}
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   105
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   106
public class AppContextTest {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   107
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   108
    public AppContextTest() {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   109
        ThreadGroup tg0 = new ThreadGroup("ThreadGroup0");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   110
        ThreadGroup tg1 = new ThreadGroup("ThreadGroup1");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   111
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   112
        TestThread t0 =
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   113
            new TestThread(tg0, false, null, "TestThread 0");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   114
        TestThread t1 =
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   115
            new TestThread(tg1, true, new File("."), "TestThread 1");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   116
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   117
        t0.start();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   118
        t1.start();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   119
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   120
        try {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   121
            t0.join();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   122
        } catch (InterruptedException ie0) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   123
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   124
        try {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   125
            t1.join();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   126
        } catch (InterruptedException ie1) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   127
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   128
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   129
        if (t0.gotCrosstalk() || t1.gotCrosstalk()) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   130
            throw new RuntimeException("ImageIO methods had crosstalk!");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   131
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   132
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   133
        if (t0.getRegistry() == t1.getRegistry()) {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   134
            throw new RuntimeException("ThreadGroups had same IIORegistry!");
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   135
        }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   136
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   137
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   138
    public static void main(String[] args) throws IOException {
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   139
        new AppContextTest();
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   140
    }
f7f615a3aaff 8177628: Opensource unit/regression tests for ImageIO
serb
parents:
diff changeset
   141
}