-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optimized version of orderChildren to AbstractTestDescriptor
- Loading branch information
1 parent
fc69751
commit 00310ee
Showing
4 changed files
with
238 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...st/java/org/junit/platform/engine/support/descriptor/TestDescriptorOrderChildrenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.engine.support.descriptor; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.UniqueId; | ||
|
||
public interface TestDescriptorOrderChildrenTest { | ||
|
||
/** | ||
* @return a test descriptor without any children. | ||
*/ | ||
TestDescriptor createEmptyTestDescriptor(); | ||
|
||
default TestDescriptor createTestDescriptorWithChildren() { | ||
var testDescriptor = createEmptyTestDescriptor(); | ||
testDescriptor.addChild(new StubTestDescriptor(UniqueId.root("child", "1"))); | ||
testDescriptor.addChild(new StubTestDescriptor(UniqueId.root("child", "2"))); | ||
testDescriptor.addChild(new StubTestDescriptor(UniqueId.root("child", "3"))); | ||
return testDescriptor; | ||
} | ||
|
||
@Test | ||
default void orderChildrenInReverseOrder() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var childrenInOriginalOrder = new ArrayList<>(testDescriptor.getChildren()); | ||
testDescriptor.orderChildren(children -> { | ||
children.sort(comparing((TestDescriptor o) -> childrenInOriginalOrder.indexOf(o)).reversed()); | ||
return children; | ||
}); | ||
List<TestDescriptor> children = new ArrayList<>(testDescriptor.getChildren()); | ||
assertThat(children).isEqualTo(childrenInOriginalOrder.reversed()); | ||
} | ||
|
||
@Test | ||
default void orderChildrenInSameOrder() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var childrenInOriginalOrder = new ArrayList<>(testDescriptor.getChildren()); | ||
testDescriptor.orderChildren(children -> { | ||
children.sort(comparing(childrenInOriginalOrder::indexOf)); | ||
return children; | ||
}); | ||
List<TestDescriptor> children = new ArrayList<>(testDescriptor.getChildren()); | ||
assertThat(children).isEqualTo(childrenInOriginalOrder); | ||
} | ||
|
||
@Test | ||
default void orderChildrenRemovesDescriptor() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var exception = assertThrows(JUnitException.class, () -> { | ||
testDescriptor.orderChildren(children -> { | ||
children.removeFirst(); | ||
return children; | ||
}); | ||
}); | ||
assertThat(exception).hasMessage("orderer may not add or remove test descriptors"); | ||
} | ||
|
||
@Test | ||
default void orderChildrenAddsDescriptor() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var exception = assertThrows(JUnitException.class, () -> { | ||
testDescriptor.orderChildren(children -> { | ||
children.add(new StubTestDescriptor(UniqueId.root("extra", "extra1"))); | ||
return children; | ||
}); | ||
}); | ||
assertThat(exception).hasMessage("orderer may not add or remove test descriptors"); | ||
} | ||
|
||
@Test | ||
default void orderChildrenReplacesDescriptor() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var exception = assertThrows(JUnitException.class, () -> { | ||
testDescriptor.orderChildren(children -> { | ||
children.set(0, new StubTestDescriptor(UniqueId.root("replaced", "replaced1"))); | ||
return children; | ||
}); | ||
}); | ||
assertThat(exception).hasMessage("orderer may not add or remove test descriptors"); | ||
} | ||
|
||
@Test | ||
default void orderChildrenDuplicatesDescriptor() { | ||
var testDescriptor = createTestDescriptorWithChildren(); | ||
var exception = assertThrows(JUnitException.class, () -> { | ||
testDescriptor.orderChildren(children -> { | ||
children.set(1, children.getFirst()); | ||
return children; | ||
}); | ||
}); | ||
assertThat(exception).hasMessage("orderer may not add or remove test descriptors"); | ||
} | ||
} | ||
|
||
class StubTestDescriptor extends AbstractTestDescriptor { | ||
|
||
StubTestDescriptor(UniqueId uniqueId) { | ||
super(uniqueId, "stub: " + uniqueId); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.TEST; | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...-tests/src/test/java/org/junit/platform/engine/support/descriptor/TestDescriptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2015-2025 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.engine.support.descriptor; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import org.junit.platform.engine.TestDescriptor; | ||
import org.junit.platform.engine.TestSource; | ||
import org.junit.platform.engine.TestTag; | ||
import org.junit.platform.engine.UniqueId; | ||
|
||
class TestDescriptorTest implements TestDescriptorOrderChildrenTest { | ||
|
||
@Override | ||
public TestDescriptor createEmptyTestDescriptor() { | ||
return new MinimalTestDescriptorImplementation(); | ||
} | ||
|
||
private static class MinimalTestDescriptorImplementation implements TestDescriptor { | ||
|
||
private final LinkedHashSet<TestDescriptor> children = new LinkedHashSet<>(); | ||
|
||
@Override | ||
public UniqueId getUniqueId() { | ||
return UniqueId.root("root", "value"); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "TestDescriptorImplementation"; | ||
} | ||
|
||
@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) { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public Set<? extends TestDescriptor> getChildren() { | ||
return children; | ||
} | ||
|
||
@Override | ||
public void addChild(TestDescriptor descriptor) { | ||
children.add(descriptor); | ||
} | ||
|
||
@Override | ||
public void removeChild(TestDescriptor descriptor) { | ||
children.remove(descriptor); | ||
} | ||
|
||
@Override | ||
public void removeFromHierarchy() { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public Type getType() { | ||
return Type.CONTAINER; | ||
} | ||
|
||
@Override | ||
public Optional<? extends TestDescriptor> findByUniqueId(UniqueId uniqueId) { | ||
throw new UnsupportedOperationException("Not implemented"); | ||
} | ||
} | ||
} |