Skip to content

Commit 11a49e5

Browse files
committed
Avoid unnecessarily parsing unique IDs
1 parent 2896f57 commit 11a49e5

File tree

22 files changed

+131
-80
lines changed

22 files changed

+131
-80
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.9.2.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ JUnit repository on GitHub.
2323

2424
==== New Features and Improvements
2525

26-
* ❓
26+
* Introduce `TestPlan.getTestIdentifier(UniqueId)` and `TestPlan.getChildren(UniqueId)` to
27+
avoid parsing unique IDs unnecessarily during test execution.
2728

2829

2930
[[release-notes-5.9.2-junit-jupiter]]

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/OverloadedTestMethodTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.junit.jupiter.api.Test;
2020
import org.junit.jupiter.api.TestInfo;
21+
import org.junit.platform.engine.UniqueId;
2122
import org.junit.platform.launcher.TestIdentifier;
2223
import org.junit.platform.testkit.engine.Event;
2324
import org.junit.platform.testkit.engine.Events;
@@ -40,7 +41,7 @@ void executeTestCaseWithOverloadedMethodsAndThenRerunOnlyOneOfTheMethodsSelected
4041
event -> event.getTestDescriptor().getUniqueId().toString().contains(TestInfo.class.getName())).findFirst();
4142
assertTrue(first.isPresent());
4243
TestIdentifier testIdentifier = TestIdentifier.from(first.get().getTestDescriptor());
43-
String uniqueId = testIdentifier.getUniqueId();
44+
UniqueId uniqueId = testIdentifier.getUniqueIdObject();
4445

4546
tests = executeTests(selectUniqueId(uniqueId)).testEvents();
4647

junit-platform-console/src/main/java/org/junit/platform/console/tasks/TreePrintingListener.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.platform.console.options.Theme;
1919
import org.junit.platform.engine.TestExecutionResult;
20+
import org.junit.platform.engine.UniqueId;
2021
import org.junit.platform.engine.reporting.ReportEntry;
2122
import org.junit.platform.launcher.TestExecutionListener;
2223
import org.junit.platform.launcher.TestIdentifier;
@@ -27,23 +28,22 @@
2728
*/
2829
class TreePrintingListener implements TestExecutionListener {
2930

30-
private final Map<String, TreeNode> nodesByUniqueId = new ConcurrentHashMap<>();
31+
private final Map<UniqueId, TreeNode> nodesByUniqueId = new ConcurrentHashMap<>();
3132
private TreeNode root;
3233
private final TreePrinter treePrinter;
3334

3435
TreePrintingListener(PrintWriter out, ColorPalette colorPalette, Theme theme) {
3536
this.treePrinter = new TreePrinter(out, theme, colorPalette);
3637
}
3738

38-
private TreeNode addNode(TestIdentifier testIdentifier, Supplier<TreeNode> nodeSupplier) {
39+
private void addNode(TestIdentifier testIdentifier, Supplier<TreeNode> nodeSupplier) {
3940
TreeNode node = nodeSupplier.get();
40-
nodesByUniqueId.put(testIdentifier.getUniqueId(), node);
41-
testIdentifier.getParentId().map(nodesByUniqueId::get).orElse(root).addChild(node);
42-
return node;
41+
nodesByUniqueId.put(testIdentifier.getUniqueIdObject(), node);
42+
testIdentifier.getParentIdObject().map(nodesByUniqueId::get).orElse(root).addChild(node);
4343
}
4444

4545
private TreeNode getNode(TestIdentifier testIdentifier) {
46-
return nodesByUniqueId.get(testIdentifier.getUniqueId());
46+
return nodesByUniqueId.get(testIdentifier.getUniqueIdObject());
4747
}
4848

4949
@Override

junit-platform-jfr/src/main/java/org/junit/platform/jfr/FlightRecordingExecutionListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public class FlightRecordingExecutionListener implements TestExecutionListener {
4343

4444
private final AtomicReference<TestPlanExecutionEvent> testPlanExecutionEvent = new AtomicReference<>();
45-
private final Map<String, TestExecutionEvent> testExecutionEvents = new ConcurrentHashMap<>();
45+
private final Map<org.junit.platform.engine.UniqueId, TestExecutionEvent> testExecutionEvents = new ConcurrentHashMap<>();
4646

4747
@Override
4848
public void testPlanExecutionStarted(TestPlan plan) {
@@ -70,15 +70,15 @@ public void executionSkipped(TestIdentifier test, String reason) {
7070
@Override
7171
public void executionStarted(TestIdentifier test) {
7272
TestExecutionEvent event = new TestExecutionEvent();
73-
testExecutionEvents.put(test.getUniqueId(), event);
73+
testExecutionEvents.put(test.getUniqueIdObject(), event);
7474
event.initialize(test);
7575
event.begin();
7676
}
7777

7878
@Override
7979
public void executionFinished(TestIdentifier test, TestExecutionResult result) {
8080
Optional<Throwable> throwable = result.getThrowable();
81-
TestExecutionEvent event = testExecutionEvents.remove(test.getUniqueId());
81+
TestExecutionEvent event = testExecutionEvents.remove(test.getUniqueIdObject());
8282
event.end();
8383
event.result = result.getStatus().toString();
8484
event.exceptionClass = throwable.map(Throwable::getClass).orElse(null);

junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestPlan.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,19 @@ public Set<TestIdentifier> getRoots() {
158158
*/
159159
public Optional<TestIdentifier> getParent(TestIdentifier child) {
160160
Preconditions.notNull(child, "child must not be null");
161-
return child.getParentId().map(this::getTestIdentifier);
161+
return child.getParentIdObject().map(this::getTestIdentifier);
162162
}
163163

164164
/**
165165
* Get the children of the supplied {@link TestIdentifier}.
166166
*
167167
* @param parent the identifier to look up the children for; never {@code null}
168168
* @return an unmodifiable set of the parent's children, potentially empty
169-
* @see #getChildren(String)
169+
* @see #getChildren(UniqueId)
170170
*/
171171
public Set<TestIdentifier> getChildren(TestIdentifier parent) {
172172
Preconditions.notNull(parent, "parent must not be null");
173-
return getChildren(parent.getUniqueId());
173+
return getChildren(parent.getUniqueIdObject());
174174
}
175175

176176
/**
@@ -180,11 +180,26 @@ public Set<TestIdentifier> getChildren(TestIdentifier parent) {
180180
* {@code null} or blank
181181
* @return an unmodifiable set of the parent's children, potentially empty
182182
* @see #getChildren(TestIdentifier)
183+
* @deprecated Use {@link #getChildren(UniqueId)}
183184
*/
185+
@API(status = DEPRECATED, since = "1.10")
186+
@Deprecated
184187
public Set<TestIdentifier> getChildren(String parentId) {
185188
Preconditions.notBlank(parentId, "parent ID must not be null or blank");
186-
UniqueId uniqueId = UniqueId.parse(parentId);
187-
return children.containsKey(uniqueId) ? unmodifiableSet(children.get(uniqueId)) : emptySet();
189+
return getChildren(UniqueId.parse(parentId));
190+
}
191+
192+
/**
193+
* Get the children of the supplied unique ID.
194+
*
195+
* @param parentId the unique ID to look up the children for; never
196+
* {@code null}
197+
* @return an unmodifiable set of the parent's children, potentially empty
198+
* @see #getChildren(TestIdentifier)
199+
*/
200+
@API(status = MAINTAINED, since = "1.10")
201+
public Set<TestIdentifier> getChildren(UniqueId parentId) {
202+
return children.containsKey(parentId) ? unmodifiableSet(children.get(parentId)) : emptySet();
188203
}
189204

190205
/**
@@ -195,13 +210,29 @@ public Set<TestIdentifier> getChildren(String parentId) {
195210
* @return the identifier with the supplied unique ID; never {@code null}
196211
* @throws PreconditionViolationException if no {@code TestIdentifier}
197212
* with the supplied unique ID is present in this test plan
213+
* @deprecated Use {@link #getTestIdentifier(UniqueId)}
198214
*/
215+
@API(status = DEPRECATED, since = "1.10")
216+
@Deprecated
199217
public TestIdentifier getTestIdentifier(String uniqueId) throws PreconditionViolationException {
200218
Preconditions.notBlank(uniqueId, "unique ID must not be null or blank");
201-
UniqueId uniqueIdObject = UniqueId.parse(uniqueId);
202-
Preconditions.condition(allIdentifiers.containsKey(uniqueIdObject),
219+
return getTestIdentifier(UniqueId.parse(uniqueId));
220+
}
221+
222+
/**
223+
* Get the {@link TestIdentifier} with the supplied unique ID.
224+
*
225+
* @param uniqueId the unique ID to look up the identifier for; never
226+
* {@code null}
227+
* @return the identifier with the supplied unique ID; never {@code null}
228+
* @throws PreconditionViolationException if no {@code TestIdentifier}
229+
* with the supplied unique ID is present in this test plan
230+
*/
231+
@API(status = MAINTAINED, since = "1.10")
232+
public TestIdentifier getTestIdentifier(UniqueId uniqueId) {
233+
Preconditions.notNull(uniqueId, () -> "uniqueId must not be null");
234+
return Preconditions.notNull(allIdentifiers.get(uniqueId),
203235
() -> "No TestIdentifier with unique ID [" + uniqueId + "] has been added to this TestPlan.");
204-
return allIdentifiers.get(uniqueIdObject);
205236
}
206237

207238
/**

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/ExecutionListenerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void reportingEntryPublished(TestDescriptor testDescriptor, ReportEntry e
6262
}
6363

6464
private TestIdentifier getTestIdentifier(TestDescriptor testDescriptor) {
65-
return this.testPlan.getTestIdentifier(testDescriptor.getUniqueId().toString());
65+
return this.testPlan.getTestIdentifier(testDescriptor.getUniqueId());
6666
}
6767

6868
}

junit-platform-launcher/src/main/java/org/junit/platform/launcher/core/InternalTestPlan.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.function.Predicate;
1717

1818
import org.junit.platform.commons.PreconditionViolationException;
19+
import org.junit.platform.engine.UniqueId;
1920
import org.junit.platform.launcher.TestIdentifier;
2021
import org.junit.platform.launcher.TestPlan;
2122

@@ -80,16 +81,28 @@ public Set<TestIdentifier> getChildren(TestIdentifier parent) {
8081
return delegate.getChildren(parent);
8182
}
8283

84+
@SuppressWarnings("deprecation")
8385
@Override
8486
public Set<TestIdentifier> getChildren(String parentId) {
8587
return delegate.getChildren(parentId);
8688
}
8789

90+
@Override
91+
public Set<TestIdentifier> getChildren(UniqueId parentId) {
92+
return delegate.getChildren(parentId);
93+
}
94+
95+
@SuppressWarnings("deprecation")
8896
@Override
8997
public TestIdentifier getTestIdentifier(String uniqueId) throws PreconditionViolationException {
9098
return delegate.getTestIdentifier(uniqueId);
9199
}
92100

101+
@Override
102+
public TestIdentifier getTestIdentifier(UniqueId uniqueId) {
103+
return delegate.getTestIdentifier(uniqueId);
104+
}
105+
93106
@Override
94107
public long countTestIdentifiers(Predicate<? super TestIdentifier> predicate) {
95108
return delegate.countTestIdentifiers(predicate);

junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/LegacyXmlReportGeneratingListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.apiguardian.api.API;
2626
import org.junit.platform.engine.TestExecutionResult;
27-
import org.junit.platform.engine.UniqueId;
2827
import org.junit.platform.engine.reporting.ReportEntry;
2928
import org.junit.platform.launcher.TestExecutionListener;
3029
import org.junit.platform.launcher.TestIdentifier;
@@ -107,7 +106,7 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult
107106

108107
private void writeXmlReportInCaseOfRoot(TestIdentifier testIdentifier) {
109108
if (isRoot(testIdentifier)) {
110-
String rootName = UniqueId.parse(testIdentifier.getUniqueId()).getSegments().get(0).getValue();
109+
String rootName = testIdentifier.getUniqueIdObject().getSegments().get(0).getValue();
111110
writeXmlReportSafely(testIdentifier, rootName);
112111
}
113112
}
@@ -123,7 +122,7 @@ private void writeXmlReportSafely(TestIdentifier testIdentifier, String rootName
123122
}
124123

125124
private boolean isRoot(TestIdentifier testIdentifier) {
126-
return !testIdentifier.getParentId().isPresent();
125+
return !testIdentifier.getParentIdObject().isPresent();
127126
}
128127

129128
private void printException(String message, Exception exception) {

junit-platform-runner/src/main/java/org/junit/platform/runner/JUnitPlatform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void filter(Filter filter) throws NoTestsRemainException {
191191
private LauncherDiscoveryRequest createDiscoveryRequestForUniqueIds(Set<TestIdentifier> testIdentifiers) {
192192
// @formatter:off
193193
List<DiscoverySelector> selectors = testIdentifiers.stream()
194-
.map(TestIdentifier::getUniqueId)
194+
.map(TestIdentifier::getUniqueIdObject)
195195
.map(DiscoverySelectors::selectUniqueId)
196196
.collect(toList());
197197
// @formatter:on

junit-platform-runner/src/main/java/org/junit/platform/runner/JUnitPlatformRunnerListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import org.junit.platform.engine.TestExecutionResult;
1717
import org.junit.platform.engine.TestExecutionResult.Status;
18+
import org.junit.platform.engine.UniqueId;
1819
import org.junit.platform.engine.reporting.ReportEntry;
1920
import org.junit.platform.launcher.TestExecutionListener;
2021
import org.junit.platform.launcher.TestIdentifier;
@@ -37,7 +38,7 @@ class JUnitPlatformRunnerListener implements TestExecutionListener {
3738

3839
@Override
3940
public void dynamicTestRegistered(TestIdentifier testIdentifier) {
40-
String parentId = testIdentifier.getParentId().get();
41+
UniqueId parentId = testIdentifier.getParentIdObject().get();
4142
testTree.addDynamicDescription(testIdentifier, parentId);
4243
}
4344

0 commit comments

Comments
 (0)