Skip to content

Commit 9403dd5

Browse files
committed
Use a more robust approach to isTab()
1 parent e01073d commit 9403dd5

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/dodona/junit/JSONListener.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.platform.launcher.TestPlan;
2222
import org.junit.runners.model.TestTimedOutException;
2323

24+
import javax.swing.text.html.Option;
2425
import java.io.PrintStream;
2526
import java.lang.annotation.Annotation;
2627
import java.util.ArrayList;
@@ -179,23 +180,27 @@ private void handleUnknownError() {
179180
write(new StartTestcase(Message.plain("Unknown Error")));
180181
}
181182

182-
// Helper to determine if an ID is a "Tab" (Test Class)
183-
private boolean isTab(TestIdentifier testIdentifier) {
184-
if (!testIdentifier.isContainer() || testPlan == null || testIdentifier.getSource().isEmpty()) {
185-
return false;
183+
private Optional<TestSource> getParentSource(TestIdentifier testIdentifier) {
184+
Optional<TestIdentifier> parent = testPlan.getParent(testIdentifier);
185+
// Sometimes there are intermediate parents that have an empty source,
186+
// so the direct parent of an identifier is not always a "real" parent
187+
while (parent.isPresent() && parent.get().getSource().isEmpty()) {
188+
parent = testPlan.getParent(parent.get());
186189
}
190+
return parent.flatMap(TestIdentifier::getSource);
191+
}
187192

188-
String parent = testIdentifier.getParentId().orElse("");
189-
if (!parent.contains("TestSuite")) {
193+
// Helper to determine if an ID is a "Tab" (Test Class)
194+
private boolean isTab(TestIdentifier testIdentifier) {
195+
// Filter out top-level and "empty" identifiers (such as the empty Vintage runner)
196+
if (testPlan == null || testIdentifier.getSource().isEmpty()) {
190197
return false;
191198
}
192199

193-
// Identify tabs more robustly by checking the uniqueId segment type:
194-
// - regular JUnit 5 classes use "[class:...]"
195-
// - JUnit 4 (vintage) runners use "[runner:...]"
196-
// Suites use "[suite:...]" and must NOT be tabs.
197-
String uid = testIdentifier.getUniqueId();
198-
return (uid.contains("[class:") || uid.contains("[runner:"));
200+
// Direct children of the "TestSuite" class are our test classes which should become tabs
201+
return getParentSource(testIdentifier)
202+
.map(ps -> ps instanceof ClassSource && ((ClassSource) ps).getClassName().equals("TestSuite"))
203+
.orElse(false);
199204
}
200205

201206
private String getDescription(TestIdentifier testIdentifier) {

0 commit comments

Comments
 (0)