Path: blob/master/test/jdk/javax/swing/JSpinner/SerializationTest.java
41152 views
/*1* Copyright (c) 2020, 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.InputStream;27import java.io.ObjectInputStream;28import java.io.ObjectOutputStream;29import java.io.Serializable;3031import javax.swing.JSpinner;32import javax.swing.UIManager;33import javax.swing.UnsupportedLookAndFeelException;3435import static javax.swing.UIManager.getInstalledLookAndFeels;3637/**38* @test39* @bug 7124397 825637640* @key headful41* @summary Verifies that JSpinner can be serialized/deserialized correctly.42* @run main/othervm SerializationTest43* @run main/othervm -Djava.awt.headless=true SerializationTest44*/45public final class SerializationTest {4647public static void main(String[] argv) throws Exception {48for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {49EventQueue.invokeAndWait(() -> setLookAndFeel(laf));50EventQueue.invokeAndWait(() -> {51JSpinner spinner = new JSpinner();52JSpinner firstCopy = (JSpinner) createCopy(spinner);53JSpinner secondCopy = (JSpinner) createCopy(firstCopy);54});55}56}5758private static Object createCopy(Serializable objectToCopy) {59try {60ByteArrayOutputStream baos = new ByteArrayOutputStream();61ObjectOutputStream oos = new ObjectOutputStream(baos);62oos.writeObject(objectToCopy);63InputStream bais = new ByteArrayInputStream(baos.toByteArray());64ObjectInputStream ois = new ObjectInputStream(bais);65return ois.readObject();66} catch (Exception e) {67throw new RuntimeException(e);68}69}7071private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {72try {73UIManager.setLookAndFeel(laf.getClassName());74} catch (UnsupportedLookAndFeelException ignored) {75System.out.println("Unsupported LookAndFeel: " + laf.getClassName());76} catch (Exception e) {77throw new RuntimeException(e);78}79}80}818283