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