Skip to content

Commit

Permalink
Fix NPE when deserializing TestIdentifier
Browse files Browse the repository at this point in the history
Issue: #3819
  • Loading branch information
dmlloyd committed May 16, 2024
1 parent 5b13209 commit 4056f66
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ repository on GitHub.
[[release-notes-5.11.0-M2-junit-platform-bug-fixes]]
==== Bug Fixes

* Fixed a bug where `TestIdentifier` could cause a `NullPointerException` on deserialize when there is no parent identifier. See link:https://github.com/junit-team/junit5/issues/3819[issue 3819].
* ❓

[[release-notes-5.11.0-M2-junit-platform-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 @@ -16,10 +16,12 @@
import static org.junit.platform.commons.util.SerializationUtils.deserialize;
import static org.junit.platform.commons.util.SerializationUtils.serialize;

import java.util.Optional;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.TestTag;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
Expand Down Expand Up @@ -72,6 +74,74 @@ void initialVersionCanBeDeserialized() throws Exception {
}
}

@Test
void identifierWithNoParentCanBeSerializedAndDeserialized() throws Exception {
TestIdentifier ti = TestIdentifier.from(new TestDescriptor() {
@Override
public UniqueId getUniqueId() {
return UniqueId.root("example", "id");
}

@Override
public String getDisplayName() {
return "displayName";
}

@Override
public Set<TestTag> getTags() {
return Set.of();
}

@Override
public Optional<TestSource> getSource() {
return Optional.empty();
}

@Override
public Optional<TestDescriptor> getParent() {
return Optional.empty();
}

@Override
public void setParent(TestDescriptor parent) {
// ignore
}

@Override
public Set<? extends TestDescriptor> getChildren() {
return Set.of();
}

@Override
public void addChild(TestDescriptor descriptor) {
// ignore
}

@Override
public void removeChild(TestDescriptor descriptor) {
// ignore
}

@Override
public void removeFromHierarchy() {
// ignore
}

@Override
public Type getType() {
return Type.TEST;
}

@Override
public Optional<? extends TestDescriptor> findByUniqueId(UniqueId uniqueId) {
return Optional.empty();
}
});
byte[] bytes = serialize(ti);
TestIdentifier dti = (TestIdentifier) deserialize(bytes);
assertEquals(ti, dti);
}

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

0 comments on commit 4056f66

Please sign in to comment.