test/jdk/javax/swing/Security/6657138/bug6657138.java
author serb
Fri, 16 Feb 2018 12:24:38 -0800
changeset 49090 82c1fe23c469
parent 47216 71c04702a3d5
permissions -rw-r--r--
8196196: Headful tests should not be run in headless mode Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     1
/*
49090
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     4
 *
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     7
 * published by the Free Software Foundation.
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     8
 *
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    13
 * accompanied this code).
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    14
 *
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4225
diff changeset
    21
 * questions.
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    22
 */
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    23
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    24
/*
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    25
 * @test
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    26
 * @bug 6657138
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    27
 * @summary Verifies that buttons and labels don't share their ui's across appContexts
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    28
 * @author Alexander Potochkin
31448
1066345d2a8a 8076468: Add @modules to tests in jdk_desktop test group
yan
parents: 5506
diff changeset
    29
 * @modules java.desktop/sun.awt
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    30
 */
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    31
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    32
import sun.awt.SunToolkit;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    33
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    34
import javax.swing.*;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    35
import javax.swing.plaf.ButtonUI;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    36
import javax.swing.plaf.ComponentUI;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    37
import java.util.Collections;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    38
import java.util.HashMap;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    39
import java.util.Map;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    40
import java.util.Set;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    41
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    42
public class bug6657138 implements Runnable {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    43
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    44
    private static Map<JComponent, Map<String, ComponentUI>> componentMap =
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    45
            Collections.synchronizedMap(
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    46
            new HashMap<JComponent, Map<String, ComponentUI>>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    47
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    48
    public void run() {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    49
        SunToolkit.createNewAppContext();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    50
        try {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    51
            testUIMap();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    52
        } catch (Exception e) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    53
            throw new RuntimeException(e);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    54
        }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    55
    }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    56
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    57
    private static void testUIMap() throws Exception {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    58
        UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    59
        Set<JComponent> components = componentMap.keySet();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    60
        for (JComponent c : components) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    61
            Map<String, ComponentUI> uiMap = componentMap.get(c);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    62
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    63
            for (UIManager.LookAndFeelInfo laf : lafs) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    64
                if ("Nimbus".equals(laf.getName())) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    65
                    // for some unclear reasons
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    66
                    // Nimbus ui delegate for a button is null
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    67
                    // when this method is called from the new AppContext
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    68
                    continue;
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    69
                }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    70
                String className = laf.getClassName();
49090
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    71
                try {
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    72
                    UIManager.setLookAndFeel(className);
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    73
                } catch (final UnsupportedLookAndFeelException ignored) {
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    74
                    continue;
82c1fe23c469 8196196: Headful tests should not be run in headless mode
serb
parents: 47216
diff changeset
    75
                }
4225
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    76
                ComponentUI ui = UIManager.getUI(c);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    77
                if (ui == null) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    78
                    throw new RuntimeException("UI is null for " + c);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    79
                }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    80
                if (ui == uiMap.get(laf.getName())) {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    81
                    throw new RuntimeException(
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    82
                            "Two AppContexts share the same UI delegate! \n" +
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    83
                                    c + "\n" + ui);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    84
                }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    85
                uiMap.put(laf.getName(), ui);
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    86
            }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    87
        }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    88
    }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    89
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    90
    public static void main(String[] args) throws Exception {
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    91
        componentMap.put(new JButton("JButton"),
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    92
                new HashMap<String, ComponentUI>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    93
        componentMap.put(new JToggleButton("JToggleButton"),
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    94
                new HashMap<String, ComponentUI>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    95
        componentMap.put(new JRadioButton("JRadioButton"),
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    96
                new HashMap<String, ComponentUI>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    97
        componentMap.put(new JCheckBox("JCheckBox"),
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    98
                new HashMap<String, ComponentUI>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
    99
        componentMap.put(new JCheckBox("JLabel"),
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   100
                new HashMap<String, ComponentUI>());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   101
        testUIMap();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   102
        ThreadGroup group = new ThreadGroup("6657138");
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   103
        Thread thread = new Thread(group, new bug6657138());
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   104
        thread.start();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   105
        thread.join();
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   106
    }
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   107
}
57ec8171fcd5 6657138: Mutable statics in Windows PL&F (findbugs)
alexp
parents:
diff changeset
   108