1 /*
2 * Copyright (c) 2019, 2025, 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 /*
25 * @test
26 * @bug 8246774
27 * @summary Basic tests for serializing and deserializing record classes
28 * @run junit RecordClassTest
29 */
30
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.Externalizable;
34 import java.io.IOException;
35 import java.io.ObjectInput;
36 import java.io.ObjectInputStream;
37 import java.io.ObjectOutput;
38 import java.io.ObjectOutputStream;
39 import java.io.ObjectStreamClass;
40 import java.io.Serializable;
41 import static java.lang.System.out;
42
43 import static org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.fail;
45 import org.junit.jupiter.api.TestInstance;
46 import org.junit.jupiter.params.ParameterizedTest;
47 import org.junit.jupiter.params.provider.MethodSource;
48
49 /**
50 * Serializes and deserializes record classes. Ensures that the SUID is 0.
51 */
52 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
53 public class RecordClassTest {
54
55 record Foo () implements Serializable { }
56
57 record Bar (int x) implements Serializable {
58 private static final long serialVersionUID = 987654321L;
59 }
60
61 record Baz (Foo foo, Bar bar, int i) implements Serializable { }
62
63 interface ThrowingExternalizable extends Externalizable {
64 default void writeExternal(ObjectOutput out) {
65 fail("should not reach here");
66 }
67 default void readExternal(ObjectInput in) {
68 fail("should not reach here");
69 }
70 }
71
72 record Wibble () implements ThrowingExternalizable {
73 private static final long serialVersionUID = 12345678L;
74 }
75
76 record Wobble (long l) implements ThrowingExternalizable { }
77
78 record Wubble (Wobble wobble, Wibble wibble, String s) implements ThrowingExternalizable { }
79
80 public Object[][] recordClasses() {
81 return new Object[][] {
82 new Object[] { Foo.class , 0L },
83 new Object[] { Bar.class , 987654321L },
84 new Object[] { Baz.class , 0L },
85 new Object[] { Wibble.class , 12345678L },
86 new Object[] { Wobble.class , 0L },
87 new Object[] { Wubble.class , 0L },
88 };
89 }
90
91 /** Tests that the serialized and deserialized instances are equal. */
92 @ParameterizedTest
93 @MethodSource("recordClasses")
94 public void testClassSerialization(Class<?> recordClass, long unused)
95 throws Exception
96 {
97 out.println("\n---");
98 out.println("serializing : " + recordClass);
99 var deserializedClass = serializeDeserialize(recordClass);
100 out.println("deserialized: " + deserializedClass);
101 assertEquals(deserializedClass, recordClass);
102 assertEquals(recordClass, deserializedClass);
103 }
104
105 /** Tests that the SUID is always 0 unless explicitly declared. */
106 @ParameterizedTest
107 @MethodSource("recordClasses")
108 public void testSerialVersionUID(Class<?> recordClass, long expectedUID) {
109 out.println("\n---");
110 ObjectStreamClass osc = ObjectStreamClass.lookup(recordClass);
111 out.println("ObjectStreamClass::lookup : " + osc);
112 assertEquals(expectedUID, osc.getSerialVersionUID());
113
114 osc = ObjectStreamClass.lookupAny(recordClass);
115 out.println("ObjectStreamClass::lookupAny: " + osc);
116 assertEquals(expectedUID, osc.getSerialVersionUID());
117 }
118
119 // --- not Serializable
120
121 record NotSerializable1() { }
122
123 record NotSerializable2(int x) { }
124
125 record NotSerializable3<T>(T t) { }
126
127 public Object[][] notSerRecordClasses() {
128 return new Object[][] {
129 new Object[] { NotSerializable1.class },
130 new Object[] { NotSerializable2.class },
131 new Object[] { NotSerializable3.class },
132 };
133 }
134
135 /** Tests that the generated SUID is always 0 for all non-Serializable record classes. */
136 @ParameterizedTest
137 @MethodSource("notSerRecordClasses")
138 public void testSerialVersionUIDNonSer(Class<?> recordClass) {
139 out.println("\n---");
140 ObjectStreamClass osc = ObjectStreamClass.lookup(recordClass);
141 out.println("ObjectStreamClass::lookup : " + osc);
142 assertEquals(null, osc);
143
144 osc = ObjectStreamClass.lookupAny(recordClass);
145 out.println("ObjectStreamClass::lookupAny: " + osc);
146 assertEquals(0L, osc.getSerialVersionUID());
147 }
148
149 // --- infra
150
151 static <T> byte[] serialize(T obj) throws IOException {
152 ByteArrayOutputStream baos = new ByteArrayOutputStream();
153 ObjectOutputStream oos = new ObjectOutputStream(baos);
154 oos.writeObject(obj);
155 oos.close();
156 return baos.toByteArray();
157 }
158
159 @SuppressWarnings("unchecked")
160 static <T> T deserialize(byte[] streamBytes)
161 throws IOException, ClassNotFoundException
162 {
163 ByteArrayInputStream bais = new ByteArrayInputStream(streamBytes);
164 ObjectInputStream ois = new ObjectInputStream(bais);
165 return (T) ois.readObject();
166 }
167
168 static <T> T serializeDeserialize(T obj)
169 throws IOException, ClassNotFoundException
170 {
171 return deserialize(serialize(obj));
172 }
173 }