1 /* 2 * Copyright (c) 2016, 2018, 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 import java.awt.EventQueue; 25 import java.io.ByteArrayInputStream; 26 import java.io.ByteArrayOutputStream; 27 import java.io.ObjectInputStream; 28 import java.io.ObjectOutputStream; 29 30 import javax.swing.JButton; 31 import javax.swing.JComponent; 32 import javax.swing.SwingUtilities; 33 import javax.swing.UIClientPropertyKey; 34 import javax.swing.UIManager; 35 import javax.swing.UnsupportedLookAndFeelException; 36 37 import static javax.swing.UIManager.getInstalledLookAndFeels; 38 39 /** 40 * @test 41 * @bug 8141544 42 */ 43 public final class UIClientPropertyKeyTest { 44 45 private static Object key = new UIClientPropertyKey() { 46 }; 47 main(final String[] args)48 public static void main(final String[] args) throws Exception { 49 EventQueue.invokeAndWait(UIClientPropertyKeyTest::testSetUI); 50 EventQueue.invokeAndWait(UIClientPropertyKeyTest::testSerialization); 51 } 52 53 /** 54 * UIClientPropertyKey should be removed after deserialization. 55 */ testSerialization()56 private static void testSerialization() { 57 JComponent comp = new JButton(); 58 comp.putClientProperty("key1", "value1"); 59 comp.putClientProperty(key, "value2"); 60 61 comp = serializeDeserialize(comp); 62 63 validate(comp); 64 } 65 66 /** 67 * UIClientPropertyKey should be removed on updateUI(). 68 */ testSetUI()69 private static void testSetUI() { 70 JComponent comp = new JButton(); 71 comp.putClientProperty("key1", "value1"); 72 for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) { 73 comp.putClientProperty(key, "value2"); 74 setLookAndFeel(laf); 75 SwingUtilities.updateComponentTreeUI(comp); 76 validate(comp); 77 } 78 } 79 validate(JComponent comp)80 private static void validate(JComponent comp) { 81 Object value = comp.getClientProperty("key1"); 82 if (!value.equals("value1")) { 83 throw new RuntimeException("Incorrect value: " + value); 84 } 85 value = comp.getClientProperty(key); 86 if (value != null) { 87 throw new RuntimeException("Incorrect value: " + value); 88 } 89 } 90 setLookAndFeel(final UIManager.LookAndFeelInfo laf)91 private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) { 92 try { 93 UIManager.setLookAndFeel(laf.getClassName()); 94 System.out.println("LookAndFeel: " + laf.getClassName()); 95 } catch (final UnsupportedLookAndFeelException ignored){ 96 System.out.println("Unsupported LookAndFeel: " + laf.getClassName()); 97 } catch (ClassNotFoundException | InstantiationException | 98 IllegalAccessException e) { 99 throw new RuntimeException(e); 100 } 101 } 102 serializeDeserialize(JComponent comp)103 private static JComponent serializeDeserialize(JComponent comp) { 104 try { 105 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 106 ObjectOutputStream out = new ObjectOutputStream(byteOut); 107 out.writeObject(comp); 108 out.close(); 109 return (JComponent) new ObjectInputStream(new ByteArrayInputStream( 110 byteOut.toByteArray())).readObject(); 111 } catch (Exception e) { 112 throw new RuntimeException(e); 113 } 114 } 115 } 116