1/* 2 * Copyright (c) 2016, 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 24import java.awt.EventQueue; 25import java.io.ByteArrayInputStream; 26import java.io.ByteArrayOutputStream; 27import java.io.ObjectInputStream; 28import java.io.ObjectOutputStream; 29 30import javax.swing.JButton; 31import javax.swing.JComponent; 32import javax.swing.SwingUtilities; 33import javax.swing.UIClientPropertyKey; 34import javax.swing.UIManager; 35import javax.swing.UnsupportedLookAndFeelException; 36 37import static javax.swing.UIManager.getInstalledLookAndFeels; 38 39/** 40 * @test 41 * @bug 8141544 42 */ 43public final class UIClientPropertyKeyTest { 44 45 private static Object key = new UIClientPropertyKey() { 46 }; 47 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 */ 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 */ 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 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 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 (ClassNotFoundException | InstantiationException | 96 UnsupportedLookAndFeelException | IllegalAccessException e) { 97 throw new RuntimeException(e); 98 } 99 } 100 101 private static JComponent serializeDeserialize(JComponent comp) { 102 try { 103 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 104 ObjectOutputStream out = new ObjectOutputStream(byteOut); 105 out.writeObject(comp); 106 out.close(); 107 return (JComponent) new ObjectInputStream(new ByteArrayInputStream( 108 byteOut.toByteArray())).readObject(); 109 } catch (Exception e) { 110 throw new RuntimeException(e); 111 } 112 } 113} 114