Skip to content

Commit 34afa64

Browse files
committed
Add Current-Version Deserialization Test
We should test that serialized files from the current minor version can be deserialized. This ensures that serializations remain deserializable in patch releases. Issue gh-3737
1 parent b5e1c37 commit 34afa64

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

config/src/test/java/org/springframework/security/SpringSecurityCoreVersionSerializableTests.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,34 @@ void serializeCurrentVersionClasses(Class<?> clazz) throws Exception {
690690
}
691691

692692
@ParameterizedTest
693-
@MethodSource("getFilesToDeserialize")
693+
@MethodSource("getCurrentSerializedFiles")
694+
void shouldBeAbleToDeserializeClassFromCurrentVersion(Path filePath) {
695+
try (FileInputStream fileInputStream = new FileInputStream(filePath.toFile());
696+
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
697+
Object obj = objectInputStream.readObject();
698+
Class<?> clazz = Class.forName(filePath.getFileName().toString().replace(".serialized", ""));
699+
assertThat(obj).isInstanceOf(clazz);
700+
}
701+
catch (IOException | ClassNotFoundException ex) {
702+
fail("Could not deserialize " + filePath, ex);
703+
}
704+
}
705+
706+
static Stream<Path> getCurrentSerializedFiles() throws IOException {
707+
assertThat(currentVersionFolder.toFile().exists())
708+
.as("Make sure that the " + currentVersionFolder + " exists and is not empty")
709+
.isTrue();
710+
try (Stream<Path> files = Files.list(currentVersionFolder)) {
711+
if (files.findFirst().isEmpty()) {
712+
fail("Please make sure to run SpringSecurityCoreVersionSerializableTests#serializeCurrentVersionClasses for the "
713+
+ getPreviousVersion() + " version");
714+
}
715+
}
716+
return Files.list(currentVersionFolder);
717+
}
718+
719+
@ParameterizedTest
720+
@MethodSource("getPreviousSerializedFiles")
694721
void shouldBeAbleToDeserializeClassFromPreviousVersion(Path filePath) {
695722
try (FileInputStream fileInputStream = new FileInputStream(filePath.toFile());
696723
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream)) {
@@ -703,7 +730,7 @@ void shouldBeAbleToDeserializeClassFromPreviousVersion(Path filePath) {
703730
}
704731
}
705732

706-
static Stream<Path> getFilesToDeserialize() throws IOException {
733+
static Stream<Path> getPreviousSerializedFiles() throws IOException {
707734
assertThat(previousVersionFolder.toFile().exists())
708735
.as("Make sure that the " + previousVersionFolder + " exists and is not empty")
709736
.isTrue();

0 commit comments

Comments
 (0)