Skip to content

Commit 35ea95d

Browse files
authored
Explicitly set serialVersionUID and add serialization tests (#1187)
1 parent 7bb5212 commit 35ea95d

11 files changed

Lines changed: 122 additions & 0 deletions

File tree

sls-versions/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
implementation 'com.palantir.safe-logging:logger'
1010

1111
annotationProcessor 'org.immutables:value'
12+
compileOnly 'org.immutables:serial'
1213
compileOnly 'org.immutables:value::annotations'
1314

1415
testImplementation 'com.fasterxml.jackson.core:jackson-databind'

sls-versions/src/main/java/com/palantir/sls/versions/NonOrderableSlsVersion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
import com.fasterxml.jackson.annotation.JsonCreator;
2222
import com.palantir.logsafe.UnsafeArg;
2323
import java.util.Optional;
24+
import org.immutables.serial.Serial;
2425
import org.immutables.value.Value;
2526

2627
@Value.Immutable
2728
@ImmutablesStyle
29+
@Serial.Version(value = 8200475591920425177L)
2830
public abstract class NonOrderableSlsVersion extends SlsVersion {
2931

32+
private static final long serialVersionUID = -5283676181284606311L;
33+
3034
@JsonCreator
3135
public static NonOrderableSlsVersion valueOf(String value) {
3236
Optional<NonOrderableSlsVersion> optional = safeValueOf(value);

sls-versions/src/main/java/com/palantir/sls/versions/OrderableSlsVersion.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.annotation.JsonCreator;
2222
import com.palantir.logsafe.UnsafeArg;
2323
import java.util.Optional;
24+
import org.immutables.serial.Serial;
2425
import org.immutables.value.Value;
2526

2627
/**
@@ -29,8 +30,11 @@
2930
*/
3031
@Value.Immutable
3132
@ImmutablesStyle
33+
@Serial.Version(value = 1396122010215857210L)
3234
public abstract class OrderableSlsVersion extends SlsVersion implements Comparable<OrderableSlsVersion> {
3335

36+
private static final long serialVersionUID = -8422496404637895757L;
37+
3438
private static final SlsVersionType[] ORDERED_VERSION_TYPES = {
3539
SlsVersionType.RELEASE,
3640
SlsVersionType.RELEASE_CANDIDATE,

sls-versions/src/main/java/com/palantir/sls/versions/SlsVersion.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
public abstract class SlsVersion implements Serializable {
2929

30+
private static final long serialVersionUID = -4387436969618893669L;
31+
3032
@JsonCreator
3133
public static SlsVersion valueOf(String value) {
3234
Optional<OrderableSlsVersion> optionalOrderableVersion = OrderableSlsVersion.safeValueOf(value);

sls-versions/src/test/java/com/palantir/sls/versions/SlsVersionTests.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,20 @@
2222
import com.fasterxml.jackson.databind.ObjectReader;
2323
import com.fasterxml.jackson.databind.ObjectWriter;
2424
import java.io.IOException;
25+
import java.io.ObjectInputStream;
26+
import java.io.ObjectOutputStream;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.util.List;
2530
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.params.ParameterizedTest;
32+
import org.junit.jupiter.params.provider.Arguments;
33+
import org.junit.jupiter.params.provider.MethodSource;
2634

2735
public class SlsVersionTests {
2836

37+
private static final Path SERIALIZABLE_CASES_PATH = Path.of("src/test/serializableCases");
38+
2939
private static final ObjectMapper MAPPER = new ObjectMapper();
3040
private static final ObjectReader READER = MAPPER.readerFor(SlsVersion.class);
3141
private static final ObjectWriter WRITER = MAPPER.writerFor(SlsVersion.class);
@@ -76,4 +86,103 @@ public void testCheckWithNonOrderableVersion() {
7686
public void testCheckWithGarbage() {
7787
assertThat(SlsVersion.check("foo")).isFalse();
7888
}
89+
90+
@ParameterizedTest
91+
@MethodSource("serializable_arguments")
92+
public void serializable(int version, SlsVersion input, SlsVersion expected) throws Exception {
93+
Path path =
94+
SERIALIZABLE_CASES_PATH.resolve(String.format("v" + version)).resolve(input.getValue() + ".ser");
95+
96+
if (!Files.exists(path) && Boolean.getBoolean("recreate")) {
97+
writeObject(path, input);
98+
}
99+
100+
SlsVersion actual = readObject(path, SlsVersion.class);
101+
102+
assertThat(actual).isEqualTo(expected);
103+
}
104+
105+
// The version number here is intended to represent a specific version of this library. The
106+
// version number is used in the expected file path in order to be able to test the serialization behavior of the
107+
// same value in different versions of this library.
108+
//
109+
// The version number and expected file for existing test cases should NEVER be changed. The expected value for
110+
// existing test cases can be changed if the SlsVersion API changes (but the existing serialized values still need
111+
// to deserialize "correctly").
112+
//
113+
// When changing to the serialization behehavior and adding new test cases, the new test cases should always use a
114+
// new version number.
115+
private static List<Arguments> serializable_arguments() {
116+
return List.of(
117+
Arguments.of(
118+
0,
119+
SlsVersion.valueOf("1.2.3-4-gabcdef"),
120+
new OrderableSlsVersion.Builder()
121+
.value("1.2.3-4-gabcdef")
122+
.majorVersionNumber(1)
123+
.minorVersionNumber(2)
124+
.patchVersionNumber(3)
125+
.firstSequenceVersionNumber(4)
126+
.type(SlsVersionType.RELEASE_SNAPSHOT)
127+
.build()),
128+
Arguments.of(
129+
0,
130+
SlsVersion.valueOf("1.2.3"),
131+
new OrderableSlsVersion.Builder()
132+
.value("1.2.3")
133+
.majorVersionNumber(1)
134+
.minorVersionNumber(2)
135+
.patchVersionNumber(3)
136+
.type(SlsVersionType.RELEASE)
137+
.build()),
138+
Arguments.of(
139+
0,
140+
SlsVersion.valueOf("1.2.3-rc4-5-gabcdef"),
141+
new OrderableSlsVersion.Builder()
142+
.value("1.2.3-rc4-5-gabcdef")
143+
.majorVersionNumber(1)
144+
.minorVersionNumber(2)
145+
.patchVersionNumber(3)
146+
.firstSequenceVersionNumber(4)
147+
.secondSequenceVersionNumber(5)
148+
.type(SlsVersionType.RELEASE_CANDIDATE_SNAPSHOT)
149+
.build()),
150+
Arguments.of(
151+
0,
152+
SlsVersion.valueOf("1.2.3-rc4"),
153+
new OrderableSlsVersion.Builder()
154+
.value("1.2.3-rc1")
155+
.majorVersionNumber(1)
156+
.minorVersionNumber(2)
157+
.patchVersionNumber(3)
158+
.firstSequenceVersionNumber(4)
159+
.type(SlsVersionType.RELEASE_CANDIDATE)
160+
.build()),
161+
Arguments.of(
162+
0,
163+
SlsVersion.valueOf("1.2.3-abcdef"),
164+
new NonOrderableSlsVersion.Builder()
165+
.value("1.2.3-abcdef")
166+
.majorVersionNumber(1)
167+
.minorVersionNumber(2)
168+
.patchVersionNumber(3)
169+
.type(SlsVersionType.NON_ORDERABLE)
170+
.build()));
171+
}
172+
173+
private static void writeObject(Path path, Object object) throws Exception {
174+
Files.createDirectories(path.getParent());
175+
176+
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(path))) {
177+
objectOutputStream.writeObject(object);
178+
}
179+
}
180+
181+
private static <T> T readObject(Path path, Class<T> objectClass) throws Exception {
182+
try (ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(path))) {
183+
Object object = objectInputStream.readObject();
184+
assertThat(object).isInstanceOf(objectClass);
185+
return objectClass.cast(object);
186+
}
187+
}
79188
}
629 Bytes
Binary file not shown.
553 Bytes
Binary file not shown.
652 Bytes
Binary file not shown.
624 Bytes
Binary file not shown.
534 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)