1 /*
   2  * Copyright (c) 2009, 2015, 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 6657138
  27  * @summary Verifies that buttons and labels don't share their ui's across appContexts
  28  * @author Alexander Potochkin
  29  * @modules java.desktop/sun.awt
  30  */
  31 
  32 import sun.awt.SunToolkit;
  33 
  34 import javax.swing.*;
  35 import javax.swing.plaf.ButtonUI;
  36 import javax.swing.plaf.ComponentUI;
  37 import java.util.Collections;
  38 import java.util.HashMap;
  39 import java.util.Map;
  40 import java.util.Set;
  41 
  42 public class bug6657138 implements Runnable {
  43 
  44     private static Map<JComponent, Map<String, ComponentUI>> componentMap =
  45             Collections.synchronizedMap(
  46             new HashMap<JComponent, Map<String, ComponentUI>>());
  47 
  48     public void run() {
  49         SunToolkit.createNewAppContext();
  50         try {
  51             testUIMap();
  52         } catch (Exception e) {
  53             throw new RuntimeException(e);
  54         }
  55     }
  56 
  57     private static void testUIMap() throws Exception {
  58         UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
  59         Set<JComponent> components = componentMap.keySet();
  60         for (JComponent c : components) {
  61             Map<String, ComponentUI> uiMap = componentMap.get(c);
  62 
  63             for (UIManager.LookAndFeelInfo laf : lafs) {
  64                 if ("Nimbus".equals(laf.getName())) {
  65                     // for some unclear reasons
  66                     // Nimbus ui delegate for a button is null
  67                     // when this method is called from the new AppContext
  68                     continue;
  69                 }
  70                 String className = laf.getClassName();
  71                 UIManager.setLookAndFeel(className);
  72                 ComponentUI ui = UIManager.getUI(c);
  73                 if (ui == null) {
  74                     throw new RuntimeException("UI is null for " + c);
  75                 }
  76                 if (ui == uiMap.get(laf.getName())) {
  77                     throw new RuntimeException(
  78                             "Two AppContexts share the same UI delegate! \n" +
  79                                     c + "\n" + ui);
  80                 }
  81                 uiMap.put(laf.getName(), ui);
  82             }
  83         }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         componentMap.put(new JButton("JButton"),
  88                 new HashMap<String, ComponentUI>());
  89         componentMap.put(new JToggleButton("JToggleButton"),
  90                 new HashMap<String, ComponentUI>());
  91         componentMap.put(new JRadioButton("JRadioButton"),
  92                 new HashMap<String, ComponentUI>());
  93         componentMap.put(new JCheckBox("JCheckBox"),
  94                 new HashMap<String, ComponentUI>());
  95         componentMap.put(new JCheckBox("JLabel"),
  96                 new HashMap<String, ComponentUI>());
  97         testUIMap();
  98         ThreadGroup group = new ThreadGroup("6657138");
  99         Thread thread = new Thread(group, new bug6657138());
 100         thread.start();
 101         thread.join();
 102     }
 103 }
 104