Skip to content

Commit

Permalink
ParentRunner.classRules() returns modifiable List (junit-team#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
panchenko authored and kcooney committed Dec 17, 2018
1 parent 9b34c4b commit 61c3a03
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
4 changes: 0 additions & 4 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
Expand Down Expand Up @@ -541,9 +540,6 @@ public void accept(FrameworkMember member, TestRule value) {
}

public List<TestRule> getOrderedRules() {
if (entries.isEmpty()) {
return Collections.emptyList();
}
Collections.sort(entries, RuleContainer.ENTRY_COMPARATOR);
List<TestRule> result = new ArrayList<TestRule>(entries.size());
for (RuleContainer.RuleEntry entry : entries) {
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/junit/rules/ClassRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.MethodSorters;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;

/**
Expand Down Expand Up @@ -310,4 +313,54 @@ public void classRuleOrderingDefault() {
assertTrue(result.wasSuccessful());
assertEquals(" inner.begin outer.begin bar foo outer.end inner.end", log.toString());
}

public static class RunnerWithClassRuleAddedProgrammatically extends BlockJUnit4ClassRunner {
public RunnerWithClassRuleAddedProgrammatically(Class testClass) throws InitializationError {
super(testClass);
}

@Override
protected List<TestRule> classRules() {
final List<TestRule> rules = super.classRules();
rules.add(new LoggingTestRule(log, "fromCode"));
return rules;
}
}

@RunWith(RunnerWithClassRuleAddedProgrammatically.class)
public static class ClassRulesModifiableListEmpty {
@Test
public void test() {
log.append(" test");
}
}

@Test
public void classRulesModifiableListEmpty() {
log.setLength(0);
Result result = JUnitCore.runClasses(ClassRulesModifiableListEmpty.class);
assertTrue(result.wasSuccessful());
assertEquals(" fromCode.begin test fromCode.end", log.toString());
}

@RunWith(RunnerWithClassRuleAddedProgrammatically.class)
public static class ClassRulesModifiableList {
@ClassRule
public static TestRule classRule() {
return new LoggingTestRule(log, "classRule");
}

@Test
public void test() {
log.append(" test");
}
}

@Test
public void classRulesModifiableList() {
log.setLength(0);
Result result = JUnitCore.runClasses(ClassRulesModifiableList.class);
assertTrue(result.wasSuccessful());
assertEquals(" fromCode.begin classRule.begin test classRule.end fromCode.end", log.toString());
}
}

0 comments on commit 61c3a03

Please sign in to comment.