-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
@Rule(order=N) #1445
Closed
Closed
@Rule(order=N) #1445
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,113 @@ | ||
package org.junit.runners; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.IdentityHashMap; | ||
import java.util.List; | ||
|
||
import org.junit.Rule; | ||
import org.junit.rules.MethodRule; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.Statement; | ||
|
||
/** | ||
* Data structure for ordering of {@link TestRule}/{@link MethodRule} instances. | ||
* | ||
* @since 4.13 | ||
*/ | ||
class RuleContainer { | ||
private final IdentityHashMap<Object, Integer> orderValues = new IdentityHashMap<Object, Integer>(); | ||
private final List<TestRule> testRules = new ArrayList<TestRule>(); | ||
private final List<MethodRule> methodRules = new ArrayList<MethodRule>(); | ||
|
||
/** | ||
* Sets order value for the specified rule. | ||
*/ | ||
public void setOrder(Object rule, int order) { | ||
orderValues.put(rule, order); | ||
} | ||
|
||
public void add(MethodRule methodRule) { | ||
methodRules.add(methodRule); | ||
} | ||
|
||
public void add(TestRule testRule) { | ||
testRules.add(testRule); | ||
} | ||
|
||
static final Comparator<RuleEntry> ENTRY_COMPARATOR = new Comparator<RuleEntry>() { | ||
public int compare(RuleEntry o1, RuleEntry o2) { | ||
int result = compareInt(o1.order, o2.order); | ||
return result != 0 ? result : o1.type - o2.type; | ||
} | ||
|
||
private int compareInt(int a, int b) { | ||
return (a < b) ? 1 : (a == b ? 0 : -1); | ||
} | ||
}; | ||
|
||
/** | ||
* Returns entries in the order how they should be applied, i.e. inner-to-outer. | ||
*/ | ||
private List<RuleEntry> getSortedEntries() { | ||
List<RuleEntry> ruleEntries = new ArrayList<RuleEntry>( | ||
methodRules.size() + testRules.size()); | ||
for (MethodRule rule : methodRules) { | ||
ruleEntries.add(new RuleEntry(rule, RuleEntry.TYPE_METHOD_RULE, orderValues.get(rule))); | ||
} | ||
for (TestRule rule : testRules) { | ||
ruleEntries.add(new RuleEntry(rule, RuleEntry.TYPE_TEST_RULE, orderValues.get(rule))); | ||
} | ||
Collections.sort(ruleEntries, ENTRY_COMPARATOR); | ||
return ruleEntries; | ||
} | ||
|
||
/** | ||
* Applies all the rules ordered accordingly to the specified {@code statement}. | ||
*/ | ||
public Statement apply(FrameworkMethod method, Description description, Object target, | ||
Statement statement) { | ||
if (methodRules.isEmpty() && testRules.isEmpty()) { | ||
return statement; | ||
} | ||
Statement result = statement; | ||
for (RuleEntry ruleEntry : getSortedEntries()) { | ||
if (ruleEntry.type == RuleEntry.TYPE_TEST_RULE) { | ||
result = ((TestRule) ruleEntry.rule).apply(result, description); | ||
} else { | ||
result = ((MethodRule) ruleEntry.rule).apply(result, method, target); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Returns rule instances in the order how they should be applied, i.e. inner-to-outer. | ||
* VisibleForTesting | ||
*/ | ||
List<Object> getSortedRules() { | ||
List<Object> result = new ArrayList<Object>(); | ||
for (RuleEntry entry : getSortedEntries()) { | ||
result.add(entry.rule); | ||
} | ||
return result; | ||
} | ||
|
||
static class RuleEntry { | ||
static final int TYPE_TEST_RULE = 1; | ||
static final int TYPE_METHOD_RULE = 0; | ||
|
||
final Object rule; | ||
final int type; | ||
final int order; | ||
|
||
RuleEntry(Object rule, int type, Integer order) { | ||
this.rule = rule; | ||
this.type = type; | ||
this.order = order != null ? order.intValue() : Rule.DEFAULT_ORDER; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/junit/runners/model/MemberValueConsumer.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,18 @@ | ||
package org.junit.runners.model; | ||
|
||
/** | ||
* Represents a receiver for values of annotated fields/methods together with the declaring member. | ||
* | ||
* @see TestClass#collectAnnotatedFieldValues(Object, Class, Class, MemberValueConsumer) | ||
* @see TestClass#collectAnnotatedMethodValues(Object, Class, Class, MemberValueConsumer) | ||
* @since 4.13 | ||
*/ | ||
public interface MemberValueConsumer<T> { | ||
/** | ||
* Receives the next value and its declaring member. | ||
* | ||
* @param member declaring member ({@link FrameworkMethod or {@link FrameworkField}} | ||
* @param value the value of the next member | ||
*/ | ||
void accept(FrameworkMember member, T value); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this have to be a
ThreadLocal
? Can't we use an instance variable and pass it to theRuleCollector
constructor?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This complexity comes from different methods returning
MethodRule
andTestRule
instances - both are merged and ordered together.