Skip to content

Commit 1e357e9

Browse files
author
Roger Riggs
committed
8373623: Refactor Serialization tests for Records to JUnit
Reviewed-by: jlu
1 parent 817e3df commit 1e357e9

20 files changed

+367
-300
lines changed

test/jdk/java/io/Serializable/records/AbsentStreamValuesTest.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,7 +25,7 @@
2525
* @test
2626
* @bug 8246774
2727
* @summary Checks that the appropriate default value is given to the canonical ctr
28-
* @run testng AbsentStreamValuesTest
28+
* @run junit AbsentStreamValuesTest
2929
*/
3030

3131
import java.io.ByteArrayInputStream;
@@ -34,16 +34,20 @@
3434
import java.io.IOException;
3535
import java.io.ObjectInputStream;
3636
import java.io.Serializable;
37-
import org.testng.annotations.DataProvider;
38-
import org.testng.annotations.Test;
3937
import static java.io.ObjectStreamConstants.*;
4038
import static java.lang.System.out;
41-
import static org.testng.Assert.*;
39+
40+
import static org.junit.jupiter.api.Assertions.*;
41+
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.api.TestInstance;
43+
import org.junit.jupiter.params.ParameterizedTest;
44+
import org.junit.jupiter.params.provider.MethodSource;
4245

4346
/**
4447
* Basic test to check that default primitive / reference values are presented
4548
* to the record's canonical constructor, for fields not in the stream.
4649
*/
50+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
4751
public class AbsentStreamValuesTest {
4852

4953
record R01(boolean x) implements Serializable { }
@@ -61,7 +65,6 @@ record R12(Object[] x) implements Serializable { }
6165
record R13(R12 x) implements Serializable { }
6266
record R14(R13[] x) implements Serializable { }
6367

64-
@DataProvider(name = "recordTypeAndExpectedValue")
6568
public Object[][] recordTypeAndExpectedValue() {
6669
return new Object[][] {
6770
new Object[] { R01.class, false },
@@ -81,7 +84,8 @@ public Object[][] recordTypeAndExpectedValue() {
8184
};
8285
}
8386

84-
@Test(dataProvider = "recordTypeAndExpectedValue")
87+
@ParameterizedTest
88+
@MethodSource("recordTypeAndExpectedValue")
8589
public void testWithDifferentTypes(Class<?> clazz, Object expectedXValue)
8690
throws Exception
8791
{
@@ -92,7 +96,7 @@ public void testWithDifferentTypes(Class<?> clazz, Object expectedXValue)
9296
Object obj = deserialize(bytes);
9397
out.println("deserialized: " + obj);
9498
Object actualXValue = clazz.getDeclaredMethod("x").invoke(obj);
95-
assertEquals(actualXValue, expectedXValue);
99+
assertEquals(expectedXValue, actualXValue);
96100
}
97101

98102
// --- all together
@@ -107,18 +111,18 @@ public void testWithAllTogether() throws Exception {
107111

108112
R15 obj = (R15)deserialize(bytes);
109113
out.println("deserialized: " + obj);
110-
assertEquals(obj.a, false);
111-
assertEquals(obj.b, 0);
112-
assertEquals(obj.c, 0);
113-
assertEquals(obj.d, '\u0000');
114-
assertEquals(obj.e, 0);
115-
assertEquals(obj.f, 0l);
116-
assertEquals(obj.g, 0f);
117-
assertEquals(obj.h, 0d);
118-
assertEquals(obj.i, null);
119-
assertEquals(obj.j, null);
120-
assertEquals(obj.k, null);
121-
assertEquals(obj.l, null);
114+
assertEquals(false, obj.a);
115+
assertEquals(0, obj.b);
116+
assertEquals(0, obj.c);
117+
assertEquals('\u0000', obj.d);
118+
assertEquals(0, obj.e);
119+
assertEquals(0l, obj.f);
120+
assertEquals(0f, obj.g);
121+
assertEquals(0d, obj.h);
122+
assertEquals(null, obj.i);
123+
assertEquals(null, obj.j);
124+
assertEquals(null, obj.k);
125+
assertEquals(null, obj.l);
122126
}
123127

124128
// --- generic type
@@ -132,8 +136,8 @@ public void testGenericType() throws Exception {
132136

133137
R16 obj = (R16)deserialize(bytes);
134138
out.println("deserialized: " + obj);
135-
assertEquals(obj.t, null);
136-
assertEquals(obj.u, null);
139+
assertEquals(null, obj.t);
140+
assertEquals(null, obj.u);
137141
}
138142

139143
// --- infra

test/jdk/java/io/Serializable/records/BadCanonicalCtrTest.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,7 @@
2727
* @summary InvalidClassException is thrown when the canonical constructor
2828
* cannot be found during deserialization.
2929
* @library /test/lib
30-
* @run testng BadCanonicalCtrTest
30+
* @run junit BadCanonicalCtrTest
3131
*/
3232

3333
import java.io.ByteArrayInputStream;
@@ -44,22 +44,25 @@
4444

4545
import jdk.test.lib.compiler.InMemoryJavaCompiler;
4646
import jdk.test.lib.ByteCodeLoader;
47-
import org.testng.annotations.BeforeTest;
48-
import org.testng.annotations.DataProvider;
49-
import org.testng.annotations.Test;
5047
import static java.lang.System.out;
5148
import static java.lang.classfile.ClassFile.ACC_PUBLIC;
5249
import static java.lang.constant.ConstantDescs.CD_Object;
5350
import static java.lang.constant.ConstantDescs.CD_void;
5451
import static java.lang.constant.ConstantDescs.INIT_NAME;
5552
import static java.lang.constant.ConstantDescs.MTD_void;
56-
import static org.testng.Assert.assertTrue;
57-
import static org.testng.Assert.expectThrows;
53+
54+
import org.junit.jupiter.api.Assertions;
55+
import static org.junit.jupiter.api.Assertions.assertTrue;
56+
import org.junit.jupiter.api.BeforeAll;
57+
import org.junit.jupiter.api.TestInstance;
58+
import org.junit.jupiter.params.ParameterizedTest;
59+
import org.junit.jupiter.params.provider.MethodSource;
5860

5961
/**
6062
* Checks that an InvalidClassException is thrown when the canonical
6163
* constructor cannot be found during deserialization.
6264
*/
65+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
6366
public class BadCanonicalCtrTest {
6467

6568
// ClassLoader for creating instances of the records to test with.
@@ -76,7 +79,7 @@ public class BadCanonicalCtrTest {
7679
* the initial bytecode for the record classes using javac, then removes or
7780
* modifies the generated canonical constructor.
7881
*/
79-
@BeforeTest
82+
@BeforeAll
8083
public void setup() {
8184
{
8285
byte[] byteCode = InMemoryJavaCompiler.compile("R1",
@@ -133,7 +136,6 @@ Object newR3(long l) throws Exception {
133136
return c.getConstructor(long.class).newInstance(l);
134137
}
135138

136-
@DataProvider(name = "recordInstances")
137139
public Object[][] recordInstances() throws Exception {
138140
return new Object[][] {
139141
new Object[] { newR1() },
@@ -148,13 +150,14 @@ public Object[][] recordInstances() throws Exception {
148150
* Tests that InvalidClassException is thrown when no constructor is
149151
* present.
150152
*/
151-
@Test(dataProvider = "recordInstances")
153+
@ParameterizedTest
154+
@MethodSource("recordInstances")
152155
public void missingConstructorTest(Object objToSerialize) throws Exception {
153156
out.println("\n---");
154157
out.println("serializing : " + objToSerialize);
155158
byte[] bytes = serialize(objToSerialize);
156159
out.println("deserializing");
157-
InvalidClassException ice = expectThrows(ICE, () -> deserialize(bytes, missingCtrClassLoader));
160+
InvalidClassException ice = Assertions.assertThrows(ICE, () -> deserialize(bytes, missingCtrClassLoader));
158161
out.println("caught expected ICE: " + ice);
159162
assertTrue(ice.getMessage().contains("record canonical constructor not found"));
160163
}
@@ -164,13 +167,14 @@ public void missingConstructorTest(Object objToSerialize) throws Exception {
164167
* constructor is not present. ( a non-canonical constructor is
165168
* present ).
166169
*/
167-
@Test(dataProvider = "recordInstances")
170+
@ParameterizedTest
171+
@MethodSource("recordInstances")
168172
public void nonCanonicalConstructorTest(Object objToSerialize) throws Exception {
169173
out.println("\n---");
170174
out.println("serializing : " + objToSerialize);
171175
byte[] bytes = serialize(objToSerialize);
172176
out.println("deserializing");
173-
InvalidClassException ice = expectThrows(ICE, () -> deserialize(bytes, nonCanonicalCtrClassLoader));
177+
InvalidClassException ice = Assertions.assertThrows(ICE, () -> deserialize(bytes, nonCanonicalCtrClassLoader));
174178
out.println("caught expected ICE: " + ice);
175179
assertTrue(ice.getMessage().contains("record canonical constructor not found"));
176180
}

test/jdk/java/io/Serializable/records/BadValues.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,18 +24,19 @@
2424
/*
2525
* @test
2626
* @summary Basic test for ClassNotFoundException
27-
* @run testng BadValues
27+
* @run junit BadValues
2828
*/
2929

3030
import java.io.ByteArrayInputStream;
3131
import java.io.ByteArrayOutputStream;
3232
import java.io.DataOutputStream;
3333
import java.io.IOException;
3434
import java.io.ObjectInputStream;
35-
import org.testng.annotations.Test;
3635
import static java.io.ObjectStreamConstants.*;
3736
import static java.lang.System.out;
38-
import static org.testng.Assert.*;
37+
38+
import static org.junit.jupiter.api.Assertions.*;
39+
import org.junit.jupiter.api.Test;
3940

4041
/**
4142
* Not directly related to records but provokes surrounding code, and ensures
@@ -73,31 +74,31 @@ static byte[] byteStreamFor(String className, long uid, byte flags)
7374
public void testNotFoundSer() throws Exception {
7475
out.println("\n---");
7576
byte[] bytes = byteStreamFor("XxYyZz", 0L, (byte)SC_SERIALIZABLE);
76-
Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
77+
Throwable t = assertThrows(CNFE, () -> deserialize(bytes));
7778
out.println("caught expected CNFE: " + t);
7879
}
7980

8081
@Test
8182
public void testNotFoundSerWr() throws Exception {
8283
out.println("\n---");
8384
byte[] bytes = byteStreamFor("XxYyZz", 0L, (byte)(SC_SERIALIZABLE | SC_WRITE_METHOD));
84-
Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
85+
Throwable t = assertThrows(CNFE, () -> deserialize(bytes));
8586
out.println("caught expected CNFE: " + t);
8687
}
8788

8889
@Test
8990
public void testNotFoundExt() throws Exception {
9091
out.println("\n---");
9192
byte[] bytes = byteStreamFor("AaBbCc", 0L, (byte)SC_EXTERNALIZABLE);
92-
Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
93+
Throwable t = assertThrows(CNFE, () -> deserialize(bytes));
9394
out.println("caught expected CNFE: " + t);
9495
}
9596

9697
@Test
9798
public void testNotFoundExtWr() throws Exception {
9899
out.println("\n---");
99100
byte[] bytes = byteStreamFor("AaBbCc", 0L, (byte)(SC_SERIALIZABLE | SC_WRITE_METHOD));
100-
Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
101+
Throwable t = assertThrows(CNFE, () -> deserialize(bytes));
101102
out.println("caught expected CNFE: " + t);
102103
}
103104

0 commit comments

Comments
 (0)