|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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.InputStream; |
| 28 | +import java.io.ObjectInputStream; |
| 29 | +import java.io.ObjectOutputStream; |
| 30 | +import java.io.Serializable; |
| 31 | + |
| 32 | +import javax.swing.JSpinner; |
| 33 | +import javax.swing.UIManager; |
| 34 | +import javax.swing.UnsupportedLookAndFeelException; |
| 35 | + |
| 36 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 37 | + |
| 38 | +/** |
| 39 | + * @test |
| 40 | + * @bug 7124397 |
| 41 | + * @summary Verifies that JSpinner can be serialized/deserialized correctly. |
| 42 | + */ |
| 43 | +public final class SerializationTest { |
| 44 | + |
| 45 | + public static void main(String[] argv) throws Exception { |
| 46 | + for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) { |
| 47 | + EventQueue.invokeAndWait(() -> setLookAndFeel(laf)); |
| 48 | + EventQueue.invokeAndWait(() -> { |
| 49 | + JSpinner spinner = new JSpinner(); |
| 50 | + JSpinner firstCopy = (JSpinner) createCopy(spinner); |
| 51 | + JSpinner secondCopy = (JSpinner) createCopy(firstCopy); |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static Object createCopy(Serializable objectToCopy) { |
| 57 | + try { |
| 58 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 59 | + ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 60 | + oos.writeObject(objectToCopy); |
| 61 | + InputStream bais = new ByteArrayInputStream(baos.toByteArray()); |
| 62 | + ObjectInputStream ois = new ObjectInputStream(bais); |
| 63 | + return ois.readObject(); |
| 64 | + } catch (Exception e) { |
| 65 | + throw new RuntimeException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 70 | + try { |
| 71 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 72 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 73 | + System.out.println("Unsupported LookAndFeel: " + laf.getClassName()); |
| 74 | + } catch (Exception e) { |
| 75 | + throw new RuntimeException(e); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments