Skip to content

Commit

Permalink
Fix NPE in TestRunSession#toString
Browse files Browse the repository at this point in the history
currently if one calls TestRunSession#toString (e.g. in a debugger) a
NPE can occur if the session is not yet started and the fStartTime is
null.

This checks for this case and adjust the message accordingly and also
replace the volatile boolean with an AtomicBoolean.
  • Loading branch information
laeubi committed Feb 27, 2024
1 parent a17ae73 commit 20a536b
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.unittest.internal.UnitTestPlugin;
import org.eclipse.unittest.internal.launcher.TestViewSupportRegistry;
Expand Down Expand Up @@ -78,7 +79,7 @@ public class TestRunSession extends TestSuiteElement implements ITestRunSession,
*/
private HashMap<String, TestElement> fIdToTest;

volatile Instant fStartTime;
private final AtomicReference<Instant> fStartTime = new AtomicReference<>();
volatile Integer fPredefinedTestCount;

volatile boolean fIsAborted;
Expand All @@ -105,7 +106,7 @@ public TestRunSession(String testRunName, Instant startTime, ILaunchConfiguratio
fIdToTest = new HashMap<>();

fTestRunnerClient = null;
fStartTime = startTime;
fStartTime.set(startTime);

fSessionListeners = new ListenerList<>();
}
Expand Down Expand Up @@ -254,7 +255,7 @@ public int getCurrentIgnoredCount() {
* @return an {@link Instant} object indicating a test run session start time
*/
public Instant getStartTime() {
return fStartTime;
return fStartTime.get();
}

/**
Expand Down Expand Up @@ -381,7 +382,8 @@ private class TestSessionNotifier {
* @param testCount number of tests in this run
*/
public void testRunStarted(Integer testCount) {
fStartTime = Instant.now();
// only update if not already set!
fStartTime.compareAndSet(null, Instant.now());
fPredefinedTestCount = testCount;

for (ITestSessionListener listener : fSessionListeners) {
Expand Down Expand Up @@ -590,7 +592,12 @@ private void addFailures(Collection<TestElement> failures, TestElement testEleme

@Override
public String toString() {
return fTestRunName + " " + DateFormat.getDateTimeInstance().format(new Date(fStartTime.toEpochMilli())); //$NON-NLS-1$
Instant startTime = getStartTime();
if (startTime == null) {
return fTestRunName + " (not started)"; //$NON-NLS-1$
} else {
return fTestRunName + " " + DateFormat.getDateTimeInstance().format(new Date(startTime.toEpochMilli())); //$NON-NLS-1$
}
}

@Override
Expand Down

0 comments on commit 20a536b

Please sign in to comment.