Path: blob/master/test/jdk/javax/swing/ClientProperty/UIClientPropertyKeyTest/UIClientPropertyKeyTest.java
41154 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.EventQueue;24import java.io.ByteArrayInputStream;25import java.io.ByteArrayOutputStream;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;2829import javax.swing.JButton;30import javax.swing.JComponent;31import javax.swing.SwingUtilities;32import javax.swing.UIClientPropertyKey;33import javax.swing.UIManager;34import javax.swing.UnsupportedLookAndFeelException;3536import static javax.swing.UIManager.getInstalledLookAndFeels;3738/**39* @test40* @bug 814154441*/42public final class UIClientPropertyKeyTest {4344private static Object key = new UIClientPropertyKey() {45};4647public static void main(final String[] args) throws Exception {48EventQueue.invokeAndWait(UIClientPropertyKeyTest::testSetUI);49EventQueue.invokeAndWait(UIClientPropertyKeyTest::testSerialization);50}5152/**53* UIClientPropertyKey should be removed after deserialization.54*/55private static void testSerialization() {56JComponent comp = new JButton();57comp.putClientProperty("key1", "value1");58comp.putClientProperty(key, "value2");5960comp = serializeDeserialize(comp);6162validate(comp);63}6465/**66* UIClientPropertyKey should be removed on updateUI().67*/68private static void testSetUI() {69JComponent comp = new JButton();70comp.putClientProperty("key1", "value1");71for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {72comp.putClientProperty(key, "value2");73setLookAndFeel(laf);74SwingUtilities.updateComponentTreeUI(comp);75validate(comp);76}77}7879private static void validate(JComponent comp) {80Object value = comp.getClientProperty("key1");81if (!value.equals("value1")) {82throw new RuntimeException("Incorrect value: " + value);83}84value = comp.getClientProperty(key);85if (value != null) {86throw new RuntimeException("Incorrect value: " + value);87}88}8990private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {91try {92UIManager.setLookAndFeel(laf.getClassName());93System.out.println("LookAndFeel: " + laf.getClassName());94} catch (final UnsupportedLookAndFeelException ignored){95System.out.println("Unsupported LookAndFeel: " + laf.getClassName());96} catch (ClassNotFoundException | InstantiationException |97IllegalAccessException e) {98throw new RuntimeException(e);99}100}101102private static JComponent serializeDeserialize(JComponent comp) {103try {104ByteArrayOutputStream byteOut = new ByteArrayOutputStream();105ObjectOutputStream out = new ObjectOutputStream(byteOut);106out.writeObject(comp);107out.close();108return (JComponent) new ObjectInputStream(new ByteArrayInputStream(109byteOut.toByteArray())).readObject();110} catch (Exception e) {111throw new RuntimeException(e);112}113}114}115116117