diff --git a/src/main/java/junit/framework/JUnit4TestAdapter.java b/src/main/java/junit/framework/JUnit4TestAdapter.java index 88138ef9d9742..9d32031e4ebae 100644 --- a/src/main/java/junit/framework/JUnit4TestAdapter.java +++ b/src/main/java/junit/framework/JUnit4TestAdapter.java @@ -9,7 +9,7 @@ import org.junit.runner.Runner; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.GeneralOrdering; +import org.junit.runner.manipulation.Orderer; import org.junit.runner.manipulation.InvalidOrderingException; import org.junit.runner.manipulation.NoTestsRemainException; import org.junit.runner.manipulation.Orderable; @@ -101,7 +101,7 @@ public void sort(Sorter sorter) { * * @since 4.13 */ - public void order(GeneralOrdering ordering) throws InvalidOrderingException { - ordering.apply(fRunner); + public void order(Orderer orderer) throws InvalidOrderingException { + orderer.apply(fRunner); } } \ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java index a1332b44c9b31..0d51541adcf88 100644 --- a/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java +++ b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java @@ -15,7 +15,7 @@ import org.junit.runner.Runner; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.GeneralOrdering; +import org.junit.runner.manipulation.Orderer; import org.junit.runner.manipulation.InvalidOrderingException; import org.junit.runner.manipulation.NoTestsRemainException; import org.junit.runner.manipulation.Orderable; @@ -179,10 +179,10 @@ public void sort(Sorter sorter) { * * @since 4.13 */ - public void order(GeneralOrdering ordering) throws InvalidOrderingException { + public void order(Orderer orderer) throws InvalidOrderingException { if (getTest() instanceof Orderable) { Orderable adapter = (Orderable) getTest(); - adapter.order(ordering); + adapter.order(orderer); } } diff --git a/src/main/java/org/junit/runner/manipulation/GeneralOrdering.java b/src/main/java/org/junit/runner/manipulation/GeneralOrdering.java deleted file mode 100644 index e571f2446ae54..0000000000000 --- a/src/main/java/org/junit/runner/manipulation/GeneralOrdering.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.junit.runner.manipulation; - -import java.util.Collection; -import java.util.List; - -import org.junit.runner.Description; - -/** - * An {@link Ordering} that is not a {@link Sorter}. - * - * @since 4.13 - */ -public final class GeneralOrdering extends Ordering { - private final Ordering delegate; - - GeneralOrdering(Ordering delegate) { - this.delegate = delegate; - } - - @Override - protected List orderItems(Collection descriptions) { - return delegate.orderItems(descriptions); - } - - @Override - public void apply(Object target) throws InvalidOrderingException { - /* - * We overwrite apply() to avoid having a GeneralOrdering wrap another - * GeneralOrdering. - */ - if (target instanceof Orderable) { - Orderable orderable = (Orderable) target; - orderable.order(this); - } - } - - @Override - boolean validateOrderingIsCorrect() { - return delegate.validateOrderingIsCorrect(); - } -} diff --git a/src/main/java/org/junit/runner/manipulation/Orderable.java b/src/main/java/org/junit/runner/manipulation/Orderable.java index f6ce87b35dabd..f18f7e3405012 100644 --- a/src/main/java/org/junit/runner/manipulation/Orderable.java +++ b/src/main/java/org/junit/runner/manipulation/Orderable.java @@ -17,5 +17,5 @@ public interface Orderable extends Sortable { * @throws InvalidOrderingException if ordering does something invalid (like remove or add * children) */ - void order(GeneralOrdering ordering) throws InvalidOrderingException; + void order(Orderer ordering) throws InvalidOrderingException; } diff --git a/src/main/java/org/junit/runner/manipulation/Orderer.java b/src/main/java/org/junit/runner/manipulation/Orderer.java new file mode 100644 index 0000000000000..eb1305437076f --- /dev/null +++ b/src/main/java/org/junit/runner/manipulation/Orderer.java @@ -0,0 +1,62 @@ +package org.junit.runner.manipulation; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.runner.Description; + +/** + * Orders tests. + * + * @since 4.13 + */ +public final class Orderer { + private final Ordering ordering; + + Orderer(Ordering delegate) { + this.ordering = delegate; + } + + /** + * Orders the descriptions. + * + * @return descriptions in order + */ + public List order(Collection descriptions) + throws InvalidOrderingException { + List inOrder = ordering.orderItems( + Collections.unmodifiableCollection(descriptions)); + if (!ordering.validateOrderingIsCorrect()) { + return inOrder; + } + + Set uniqueDescriptions = new HashSet(descriptions); + if (!uniqueDescriptions.containsAll(inOrder)) { + throw new InvalidOrderingException("Ordering added items"); + } + Set resultAsSet = new HashSet(inOrder); + if (resultAsSet.size() != inOrder.size()) { + throw new InvalidOrderingException("Ordering duplicated items"); + } else if (!resultAsSet.containsAll(uniqueDescriptions)) { + throw new InvalidOrderingException("Ordering removed items"); + } + + return inOrder; + } + + /** + * Order the tests in target. + * + * @throws InvalidOrderingException if ordering does something invalid (like remove or add + * children) + */ + public void apply(Object target) throws InvalidOrderingException { + if (target instanceof Orderable) { + Orderable orderable = (Orderable) target; + orderable.order(this); + } + } +} diff --git a/src/main/java/org/junit/runner/manipulation/Ordering.java b/src/main/java/org/junit/runner/manipulation/Ordering.java index 9fc807b9390bc..0d0ce93780e6b 100644 --- a/src/main/java/org/junit/runner/manipulation/Ordering.java +++ b/src/main/java/org/junit/runner/manipulation/Ordering.java @@ -4,10 +4,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Random; -import java.util.Set; import org.junit.runner.Description; import org.junit.runner.OrderWith; @@ -118,46 +116,22 @@ public void apply(Object target) throws InvalidOrderingException { /* * Note that some subclasses of Ordering override apply(). The Sorter * subclass of Ordering overrides apply() to apply the sort (this is - * done because sorting is more efficient than ordering) the - * GeneralOrdering overrides apply() to avoid having a GenericOrdering - * wrap another GenericOrdering. + * done because sorting is more efficient than ordering). */ if (target instanceof Orderable) { Orderable orderable = (Orderable) target; - orderable.order(new GeneralOrdering(this)); + orderable.order(new Orderer(this)); } } + /** + * Returns {@code true} if this ordering could produce invalid results (i.e. + * if it could add or remove values). + */ boolean validateOrderingIsCorrect() { return true; } - /** - * Orders the descriptions. - * - * @return descriptions in order - */ - public final List order(Collection descriptions) - throws InvalidOrderingException { - List inOrder = orderItems(Collections.unmodifiableCollection(descriptions)); - if (!validateOrderingIsCorrect()) { - return inOrder; - } - - Set uniqueDescriptions = new HashSet(descriptions); - if (!uniqueDescriptions.containsAll(inOrder)) { - throw new InvalidOrderingException("Ordering added items"); - } - Set resultAsSet = new HashSet(inOrder); - if (resultAsSet.size() != inOrder.size()) { - throw new InvalidOrderingException("Ordering duplicated items"); - } else if (!resultAsSet.containsAll(uniqueDescriptions)) { - throw new InvalidOrderingException("Ordering removed items"); - } - - return inOrder; - } - /** * Implemented by sub-classes to order the descriptions. * diff --git a/src/main/java/org/junit/runner/manipulation/Sorter.java b/src/main/java/org/junit/runner/manipulation/Sorter.java index 5ef27ba8c56e6..4b5274c310125 100644 --- a/src/main/java/org/junit/runner/manipulation/Sorter.java +++ b/src/main/java/org/junit/runner/manipulation/Sorter.java @@ -32,6 +32,7 @@ public int compare(Description o1, Description o2) { * to sort tests * * @param comparator the {@link Comparator} to use when sorting tests + * @since 4.0 */ public Sorter(Comparator comparator) { this.comparator = comparator; @@ -39,6 +40,8 @@ public Sorter(Comparator comparator) { /** * Sorts the tests in target using comparator. + * + * @since 4.0 */ @Override public void apply(Object target) { diff --git a/src/main/java/org/junit/runners/ParentRunner.java b/src/main/java/org/junit/runners/ParentRunner.java index 2e50fce9d56d2..1f69e0567a990 100644 --- a/src/main/java/org/junit/runners/ParentRunner.java +++ b/src/main/java/org/junit/runners/ParentRunner.java @@ -32,7 +32,7 @@ import org.junit.runner.Runner; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.GeneralOrdering; +import org.junit.runner.manipulation.Orderer; import org.junit.runner.manipulation.InvalidOrderingException; import org.junit.runner.manipulation.NoTestsRemainException; import org.junit.runner.manipulation.Orderable; @@ -449,11 +449,11 @@ public void sort(Sorter sorter) { } /** - * Implementation of {@link Orderable#order(GeneralOrdering)}. + * Implementation of {@link Orderable#order(Orderer)}. * * @since 4.13 */ - public void order(GeneralOrdering ordering) throws InvalidOrderingException { + public void order(Orderer orderer) throws InvalidOrderingException { childrenLock.lock(); try { List children = getFilteredChildren(); @@ -469,10 +469,10 @@ public void order(GeneralOrdering ordering) throws InvalidOrderingException { childMap.put(description, childrenWithDescription); } childrenWithDescription.add(child); - ordering.apply(child); + orderer.apply(child); } - List inOrder = ordering.order(childMap.keySet()); + List inOrder = orderer.order(childMap.keySet()); children = new ArrayList(children.size()); for (Description description : inOrder) { diff --git a/src/test/java/org/junit/tests/manipulation/OrderableTest.java b/src/test/java/org/junit/tests/manipulation/OrderableTest.java index 087da1e39eb6a..3a787dcdd5354 100644 --- a/src/test/java/org/junit/tests/manipulation/OrderableTest.java +++ b/src/test/java/org/junit/tests/manipulation/OrderableTest.java @@ -10,7 +10,7 @@ import org.junit.runner.Request; import org.junit.runner.RunWith; import org.junit.runner.Runner; -import org.junit.runner.manipulation.GeneralOrdering; +import org.junit.runner.manipulation.Orderer; import org.junit.runner.manipulation.InvalidOrderingException; import org.junit.runner.manipulation.Orderable; import org.junit.runner.manipulation.Sorter; @@ -142,8 +142,8 @@ public Description getDescription() { return delegate.getDescription(); } - public void order(GeneralOrdering ordering) throws InvalidOrderingException { - delegate.order(ordering); + public void order(Orderer orderer) throws InvalidOrderingException { + delegate.order(orderer); } public void sort(Sorter sorter) {