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