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