Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ordering, Orderable and @OrderWith #1130

Merged
merged 29 commits into from
Jul 30, 2018
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
44b64df
Add Ordering, Orderable and @OrderWith.
kcooney Mar 28, 2015
e68c015
Remove unreachable code
kcooney Apr 25, 2015
4770fff
Handle @RunWith in RunnerBuilder
kcooney Apr 30, 2015
a19a3ab
Change Sorter to no longer extend Ordering.
kcooney Apr 30, 2015
82e019f
Revert "Change Sorter to no longer extend Ordering."
kcooney May 1, 2015
f19f035
Replace Ordering.order(List) with Ordering.orderDescription(Description)
kcooney May 1, 2015
8390bc7
Rename GenericOrdering to GeneralOrdering
kcooney Jan 7, 2017
6e38776
Merge branch 'master' into ordering
kcooney Jan 7, 2017
1ba37d2
Add Ordering.Context so Orderings can use the Description to get
kcooney Jan 7, 2017
62ca6a0
Rename parameters in applyOrdering() and Sorter.apply() from "runner"…
kcooney Jan 7, 2017
753842d
Check ordering correctness in Ordering.
kcooney Jan 7, 2017
ea71fa4
Pass Ordering.Context in constructor when reflectively creating insta…
kcooney Jan 7, 2017
121744f
Remove unnecessary call to unmodifableCollection()
kcooney Jan 7, 2017
9d71b2f
Remove use of ReflectiveOperationException
kcooney Jan 8, 2017
5a7186b
Merge branch 'master' into ordering
kcooney May 15, 2017
2f9d21a
Fix javadoc for orderWith()
kcooney May 15, 2017
c6de86d
Minor formatting fixes
kcooney May 15, 2017
5a3f954
Add Ordering.Factory.
kcooney May 18, 2017
f2f6131
Merge branch 'master' into ordering
kcooney May 18, 2017
4ce52c1
Add missing @since Javadoc for Ordering methods
kcooney May 18, 2017
d8a1ee6
Merge branch 'master' into ordering
kcooney May 26, 2017
bfbad94
Minor formatting fix.
kcooney Jun 27, 2017
9fb4772
Merge branch 'master' into ordering
kcooney Aug 7, 2017
78ee8c6
Rename ComparsionBasedOrdering to ComparatorBasedOrdering; fix Javadoc
kcooney Jun 1, 2018
9d1e2aa
Rename GeneralOrdering to Orderer and make it no longer implement Ord…
kcooney Jun 1, 2018
550654a
Add OrderingRequest, to ensure orderWith() orders once
kcooney Jun 2, 2018
b2ce86a
Introduce MemoizingRequest
kcooney Jun 2, 2018
ca3e040
Sort tests in AllManipulationTests
kcooney Jun 2, 2018
3e6d464
Removed Comparators.reverse(); it's broken and the JDK provides a bet…
kcooney Jun 2, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check ordering correctness in Ordering.
This allows us to optimize Ordering.shuffledBy, and makes it easier
for custom runners to be orderable.
kcooney committed Jan 7, 2017
commit 753842d6bc429b11e57d33a6b7a9a67abff559bc
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ public final class GeneralOrdering extends Ordering {
}

@Override
public List<Description> order(Ordering.Context context, Collection<Description> descriptions) {
return delegate.order(context, descriptions);
protected List<Description> orderItems(Ordering.Context context, Collection<Description> descriptions) {
return delegate.orderItems(context, descriptions);
}

@Override
@@ -34,4 +34,9 @@ public void apply(Object target, Ordering.Context context)
orderable.order(this, context);
}
}

@Override
boolean validateOrderingIsCorrect() {
return delegate.validateOrderingIsCorrect();
}
}
46 changes: 43 additions & 3 deletions src/main/java/org/junit/runner/manipulation/Ordering.java
Original file line number Diff line number Diff line change
@@ -3,8 +3,10 @@
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;

@@ -26,7 +28,12 @@ public abstract class Ordering {
public static Ordering shuffledBy(final Random random) {
return new Ordering() {
@Override
public List<Description> order(Ordering.Context context, Collection<Description> descriptions) {
boolean validateOrderingIsCorrect() {
return false;
}

@Override
protected List<Description> orderItems(Ordering.Context context, Collection<Description> descriptions) {
List<Description> shuffled = new ArrayList<Description>(descriptions);
Collections.shuffle(shuffled, random);
return shuffled;
@@ -73,14 +80,47 @@ public void apply(Object target, Ordering.Context context)
}
}

boolean validateOrderingIsCorrect() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment to document what this method is used for?

return true;
}

/**
* Orders the descriptions.
*
* @param context context for the ordering operation
* @param descriptions items to sort
* @return descriptions in order
*/
public final List<Description> order(Ordering.Context context, Collection<Description> descriptions)
throws InvalidOrderingException {
List<Description> inOrder = orderItems(
context, Collections.unmodifiableCollection(descriptions));
if (!validateOrderingIsCorrect()) {
return inOrder;
}

Set<Description> uniqueDescriptions = new HashSet<Description>(descriptions);
if (!uniqueDescriptions.containsAll(inOrder)) {
throw new InvalidOrderingException("Ordering added items");
}
Set<Description> resultAsSet = new HashSet<Description>(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;
}

/**
* Orders the children of the given {@link Description}.
* Implemented by sub-classes to order the descriptions.
*
* @param context context for the ordering operation
* @param descriptions items to sort
* @return descriptions in order
*/
public abstract List<Description> order(Ordering.Context context, Collection<Description> descriptions);
protected abstract List<Description> orderItems(Ordering.Context context, Collection<Description> descriptions);

/** Context about the ordering being applied. */
public final static class Context {
7 changes: 6 additions & 1 deletion src/main/java/org/junit/runner/manipulation/Sorter.java
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ public int compare(Description o1, Description o2) {
}

@Override
public final List<Description> order(Ordering.Context context, Collection<Description> descriptions) {
protected final List<Description> orderItems(Ordering.Context context, Collection<Description> descriptions) {
/*
* In practice, we will never get here--Sorters do their work in the
* compare() method--but the Liskov substitution principle demands that
@@ -73,4 +73,9 @@ public final List<Description> order(Ordering.Context context, Collection<Descri
Collections.sort(sorted, this); // Note: it would be incorrect to pass in "comparator"
return sorted;
}

@Override
boolean validateOrderingIsCorrect() {
return false;
}
}
15 changes: 2 additions & 13 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
@@ -9,12 +9,10 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -434,6 +432,8 @@ public void order(GeneralOrdering ordering, Ordering.Context context)
throws InvalidOrderingException {
synchronized (childrenLock) {
List<T> children = getFilteredChildren();
// In theory, we could have duplicate Descriptions. De-dup them before ordering,
// and add them back at the end.
Map<Description, List<T>> childMap = new LinkedHashMap<Description, List<T>>(
children.size());
for (T child : children) {
@@ -447,20 +447,9 @@ public void order(GeneralOrdering ordering, Ordering.Context context)
ordering.apply(child, context);
}

Set<Description> uniqueDescriptions = childMap.keySet();
List<Description> inOrder = ordering.order(
context, Collections.unmodifiableCollection(childMap.keySet()));

if (!uniqueDescriptions.containsAll(inOrder)) {
throw new InvalidOrderingException("Ordering added items");
}
Set<Description> resultAsSet = new HashSet<Description>(inOrder);
if (resultAsSet.size() != inOrder.size()) {
throw new InvalidOrderingException("Ordering duplicated items");
} else if (!resultAsSet.containsAll(uniqueDescriptions)) {
throw new InvalidOrderingException("Ordering removed items");
}

children = new ArrayList<T>(children.size());
for (Description description : inOrder) {
children.addAll(childMap.get(description));
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ public ComparsionBasedOrdering(Comparator<Description> comparator) {
}

@Override
public List<Description> order(Ordering.Context context, Collection<Description> descriptions) {
protected List<Description> orderItems(Ordering.Context context, Collection<Description> descriptions) {
List<Description> ordered = new ArrayList<Description>(descriptions);
Collections.sort(ordered, comparator);
return ordered;