blob: 7595136db4307468c0258477bac58c9fa1095e95 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 5004188
27 * @run main TestSerialization 1.5.0_10.ser 1.6.0.ser
28 * @summary Tests serialization of VetoableChangeSupport
29 * @author Sergey Malenkov
30 */
31
32import java.beans.PropertyChangeEvent;
33import java.beans.VetoableChangeListener;
34import java.beans.VetoableChangeListenerProxy;
35import java.beans.VetoableChangeSupport;
36
37import java.io.ByteArrayOutputStream;
38import java.io.File;
39import java.io.FileInputStream;
40import java.io.FileOutputStream;
41import java.io.ObjectInputStream;
42import java.io.ObjectOutputStream;
43import java.io.Serializable;
44
45public final class TestSerialization implements VetoableChangeListener, Serializable {
46 private static final String NAME = "property";
47
48 public static void main(String[] args) throws Exception {
49 if (args.length == 0) {
50 String file = System.getProperty("java.version") + ".ser";
51 serialize(file, create());
52 }
53 else {
54 byte[] array = serialize(create());
55 for (String file : args) {
56 check(deserialize(file));
57 check(array, read(file));
58 }
59 }
60 }
61
62 private static VetoableChangeSupport create() {
63 VetoableChangeSupport vcs = new VetoableChangeSupport(TestSerialization.class);
64 vcs.addVetoableChangeListener(new TestSerialization(0));
65 vcs.addVetoableChangeListener(NAME, new TestSerialization(1));
66 return vcs;
67 }
68
69 private static void check(VetoableChangeSupport vcs) {
70 VetoableChangeListener[] namedListeners = vcs.getVetoableChangeListeners(NAME);
71 check(namedListeners, 1);
72 check(namedListeners[0], 1);
73
74 VetoableChangeListener[] allListeners = vcs.getVetoableChangeListeners();
75 check(allListeners, 2);
76 check(allListeners[0], 0);
77 check(allListeners[1], 1, NAME);
78 }
79
80 private static void check(byte[] a1, byte[] a2) {
81 int length = a1.length;
82 if (length != a2.length)
83 throw new Error("Different file sizes: " + length + " != " + a2.length);
84
85 for (int i = 0; i < length; i++)
86 if (a1[i] != a2[i])
87 throw new Error("Different bytes at " + i + " position");
88 }
89
90 private static void check(VetoableChangeListener[] array, int length) {
91 if (length != array.length)
92 throw new Error("Unexpected amount of listeners: " + array.length);
93 }
94
95 private static void check(VetoableChangeListener listener, int index) {
96 if (!(listener instanceof TestSerialization))
97 throw new Error("Unexpected listener: " + listener);
98
99 TestSerialization object = (TestSerialization)listener;
100 if (index != object.index)
101 throw new Error("Unexpected index: " + index + " != " + object.index);
102 }
103
104 private static void check(VetoableChangeListener listener, int index, String name) {
105 if (!(listener instanceof VetoableChangeListenerProxy))
106 throw new Error("Unexpected listener: " + listener);
107
108 VetoableChangeListenerProxy object = (VetoableChangeListenerProxy)listener;
109 if (!name.equals(object.getPropertyName()))
110 throw new Error("Unexpected name: " + name + " != " + object.getPropertyName());
111
112 check((VetoableChangeListener)object.getListener(), index);
113 }
114
115 private static byte[] read(String file) throws Exception {
116 FileInputStream stream = null;
117 try {
118 stream = new FileInputStream(new File(System.getProperty("test.src", "."), file));
119 ByteArrayOutputStream out = new ByteArrayOutputStream();
120 for (int i = stream.read(); i != -1; i = stream.read())
121 out.write(i);
122
123 return out.toByteArray();
124 }
125 finally {
126 if (stream != null)
127 stream.close();
128 }
129 }
130
131 private static VetoableChangeSupport deserialize(String file) throws Exception {
132 ObjectInputStream stream = null;
133 try {
134 stream = new ObjectInputStream(new FileInputStream(new File(System.getProperty("test.src", "."), file)));
135 return (VetoableChangeSupport)stream.readObject();
136 }
137 finally {
138 if (stream != null)
139 stream.close();
140 }
141 }
142
143 private static void serialize(String file, VetoableChangeSupport vcs) throws Exception {
144 ObjectOutputStream stream = null;
145 try {
146 stream = new ObjectOutputStream(new FileOutputStream(new File(System.getProperty("test.src", "."), file)));
147 stream.writeObject(vcs);
148 }
149 finally {
150 if (stream != null)
151 stream.close();
152 }
153 }
154
155 private static byte[] serialize(VetoableChangeSupport vcs) throws Exception {
156 ObjectOutputStream stream = null;
157 try {
158 ByteArrayOutputStream out = new ByteArrayOutputStream();
159 stream = new ObjectOutputStream(out);
160 stream.writeObject(vcs);
161 return out.toByteArray();
162 }
163 finally {
164 if (stream != null)
165 stream.close();
166 }
167 }
168
169
170 private int index;
171
172 public TestSerialization(int index) {
173 this.index = index;
174 }
175
176 public int getIndex() {
177 return this.index;
178 }
179
180 public void vetoableChange(PropertyChangeEvent event) {
181 }
182}