599N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
599N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
599N/A *
599N/A * This code is free software; you can redistribute it and/or modify it
599N/A * under the terms of the GNU General Public License version 2 only, as
599N/A * published by the Free Software Foundation.
599N/A *
599N/A * This code is distributed in the hope that it will be useful, but WITHOUT
599N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
599N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
599N/A * version 2 for more details (a copy is included in the LICENSE file that
599N/A * accompanied this code).
599N/A *
599N/A * You should have received a copy of the GNU General Public License version
599N/A * 2 along with this work; if not, write to the Free Software Foundation,
599N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
599N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
599N/A */
599N/A
599N/A/*
599N/A * @test
599N/A * @bug 4165217
599N/A * @summary Tests JColorChooser serialization
599N/A * @author Ilya Boyandin
599N/A */
599N/A
599N/Aimport java.awt.Color;
599N/Aimport java.io.ByteArrayInputStream;
599N/Aimport java.io.ByteArrayOutputStream;
599N/Aimport java.io.IOException;
599N/Aimport java.io.ObjectInputStream;
599N/Aimport java.io.ObjectOutputStream;
599N/Aimport java.util.Random;
599N/Aimport javax.swing.JColorChooser;
599N/A
599N/Apublic class Test4165217 {
599N/A public static void main(String[] args) {
599N/A JColorChooser chooser = new JColorChooser();
599N/A chooser.setColor(new Color(new Random().nextInt()));
599N/A
599N/A Color before = chooser.getColor();
599N/A Color after = copy(chooser).getColor();
599N/A
599N/A if (!after.equals(before)) {
599N/A throw new Error("color is changed after serialization");
599N/A }
599N/A }
599N/A
599N/A private static JColorChooser copy(JColorChooser chooser) {
599N/A try {
599N/A return (JColorChooser) deserialize(serialize(chooser));
599N/A }
599N/A catch (ClassNotFoundException exception) {
599N/A throw new Error("unexpected exception during class creation", exception);
599N/A }
599N/A catch (IOException exception) {
599N/A throw new Error("unexpected exception during serialization", exception);
599N/A }
599N/A }
599N/A
599N/A private static byte[] serialize(Object object) throws IOException {
599N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
599N/A ObjectOutputStream oos = new ObjectOutputStream(baos);
599N/A oos.writeObject(object);
599N/A oos.flush();
599N/A return baos.toByteArray();
599N/A }
599N/A
599N/A private static Object deserialize(byte[] array) throws IOException, ClassNotFoundException {
599N/A ByteArrayInputStream bais = new ByteArrayInputStream(array);
599N/A ObjectInputStream ois = new ObjectInputStream(bais);
599N/A return ois.readObject();
599N/A }
599N/A}