Skip to content

Commit

Permalink
Fix NPE when deserializing TestIdentifier (#3820)
Browse files Browse the repository at this point in the history
An NPE was thrown when (de)serializing `TestIdentifiers` without a parent.

Fixes #3819.

Co-authored-by: Marc Philipp <[email protected]>

(cherry picked from commit bc55d0e)
  • Loading branch information
dmlloyd authored and marcphilipp committed Jun 17, 2024
1 parent f936c01 commit d29e3eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ on GitHub.
`@ConfigurationParameter` when selecting tests by `UniqueId`.
* In order to support using `@EnabledInNativeImage` on test classes,
`UniqueIdTrackingListener` now tracks descendants of skipped test containers.
* Attempting to deserialize a `TestIdentifier` no longer causes a `NullPointerException`
when there is no parent identifier. See
link:https://github.com/junit-team/junit5/issues/3819[issue 3819].

==== Deprecations and Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOEx
source = serializedForm.source;
tags = serializedForm.tags;
type = serializedForm.type;
parentId = UniqueId.parse(serializedForm.parentId);
String parentId = serializedForm.parentId;
this.parentId = parentId == null ? null : UniqueId.parse(parentId);
legacyReportingName = serializedForm.legacyReportingName;
}

Expand All @@ -307,7 +308,8 @@ private static class SerializedForm implements Serializable {

SerializedForm(TestIdentifier testIdentifier) {
this.uniqueId = testIdentifier.uniqueId.toString();
this.parentId = testIdentifier.parentId.toString();
UniqueId parentId = testIdentifier.parentId;
this.parentId = parentId == null ? null : parentId.toString();
this.displayName = testIdentifier.displayName;
this.legacyReportingName = testIdentifier.legacyReportingName;
this.source = testIdentifier.source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ void initialVersionCanBeDeserialized() throws Exception {
}
}

@Test
void identifierWithNoParentCanBeSerializedAndDeserialized() throws Exception {
TestIdentifier originalIdentifier = TestIdentifier.from(
new AbstractTestDescriptor(UniqueId.root("example", "id"), "Example") {
@Override
public Type getType() {
return Type.CONTAINER;
}
});

var deserializedIdentifier = (TestIdentifier) deserialize(serialize(originalIdentifier));

assertDeepEquals(originalIdentifier, deserializedIdentifier);
}

private static void assertDeepEquals(TestIdentifier first, TestIdentifier second) {
assertEquals(first, second);
assertEquals(first.getUniqueId(), second.getUniqueId());
Expand Down

0 comments on commit d29e3eb

Please sign in to comment.