Skip to content

Commit

Permalink
Merge pull request #313 from dsaff/categorized_parameters_fix
Browse files Browse the repository at this point in the history
Fixes gh-291: Categories + Parameterized still does not work
  • Loading branch information
dsaff committed Sep 16, 2011
2 parents c82eef0 + 13885b9 commit 2d13004
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void addMultiPointMethods(List<PotentialAssignment> list) {
}
}

@SuppressWarnings("deprecation")
private void addSinglePointMethods(ParameterSignature sig,
List<PotentialAssignment> list) {
for (FrameworkMethod dataPointMethod : fClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.rules.TestRule;
import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.TestClass;
Expand Down Expand Up @@ -80,7 +79,8 @@ private boolean isTestRule(FrameworkField target) {

@SuppressWarnings("deprecation")
private boolean isMethodRule(FrameworkField target) {
return MethodRule.class.isAssignableFrom(target.getType());
return org.junit.rules.MethodRule.class.isAssignableFrom(target
.getType());
}

private void addError(List<Throwable> errors, FrameworkField field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.junit.internal.runners.statements.InvokeMethod;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.rules.MethodRule;
import org.junit.rules.RunRules;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/junit/runners/Parameterized.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.junit.runners;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -117,6 +118,11 @@ protected void validateConstructor(List<Throwable> errors) {
protected Statement classBlock(RunNotifier notifier) {
return childrenInvoker(notifier);
}

@Override
protected Annotation[] getRunnerAnnotations() {
return new Annotation[0];
}
}

private final ArrayList<Runner> runners= new ArrayList<Runner>();
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,22 @@ protected final void runLeaf(Statement statement, Description description,
}
}

/**
* @return the annotations that should be attached to this runner's
* description.
*/
protected Annotation[] getRunnerAnnotations() {
return fTestClass.getAnnotations();
}

//
// Implementation of Runner
//

@Override
public Description getDescription() {
Description description= Description.createSuiteDescription(getName(),
fTestClass.getAnnotations());
getRunnerAnnotations());
for (T child : getFilteredChildren())
description.addChild(describeChild(child));
return description;
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/junit/samples/SimpleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ public void testAdd() {
// forced failure result == 5
assertTrue(result == 6);
}

public int unused;
public void testDivideByZero() {
int zero= 0;
int result= 8/zero;
result++; // avoid warning for not using result
unused= result; // avoid warning for not using result
}
public void testEquals() {
assertEquals(12, 12);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/junit/samples/SimpleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public static junit.framework.Test suite() {
return new JUnit4TestAdapter(SimpleTest.class);
}

public int unused;
@Test public void divideByZero() {
int zero= 0;
int result= 8/zero;
result++; // avoid warning for not using result
unused= result; // avoid warning for not using result
}

@Test public void testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.junit.tests.experimental.categories;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.hasFailureContaining;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

import java.util.Collection;
import java.util.Collections;
Expand All @@ -10,10 +13,6 @@
import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.experimental.categories.Category;
import org.junit.experimental.results.PrintableResult;
import org.junit.experimental.results.ResultMatchers;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
Expand Down Expand Up @@ -57,6 +56,23 @@ public void testSomething() {
}
}

@RunWith(Parameterized.class)
@Category(Token.class)
public static class ParameterizedTestWithClassCategory {
public ParameterizedTestWithClassCategory(String a) {
}

@Parameters
public static Collection<Object[]> getParameters() {
return Collections.singletonList(new Object[] { "a" });
}

@Test
public void testSomething() {
Assert.assertTrue(true);
}
}

@Category(Token.class)
public static class VanillaCategorizedJUnitTest {
@Test
Expand All @@ -67,7 +83,9 @@ public void testSomething() {

@RunWith(Categories.class)
@IncludeCategory(Token.class)
@SuiteClasses({ VanillaCategorizedJUnitTest.class, WellBehavedParameterizedTest.class })
@SuiteClasses({ VanillaCategorizedJUnitTest.class,
WellBehavedParameterizedTest.class,
ParameterizedTestWithClassCategory.class })
public static class ParameterTokenSuiteWellFormed {
}

Expand All @@ -85,23 +103,21 @@ public static class ParameterTokenSuiteMalformedAndSwapped {

@Test
public void shouldSucceedWithAParameterizedClassSomewhere() {
Result result= new JUnitCore().run(ParameterTokenSuiteWellFormed.class);
assertTrue(result.wasSuccessful());
assertThat(testResult(ParameterTokenSuiteWellFormed.class),
isSuccessful());
}

@Test
public void shouldFailWithMethodLevelCategoryAnnotation() {
Assert.assertThat(
PrintableResult.testResult(ParameterTokenSuiteMalformed.class),
ResultMatchers
.hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
assertThat(
testResult(ParameterTokenSuiteMalformed.class),
hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
}

@Test
public void shouldFailWithMethodLevelCategoryAnnotationSwapped() {
Assert.assertThat(
PrintableResult.testResult(ParameterTokenSuiteMalformedAndSwapped.class),
ResultMatchers
.hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
assertThat(
testResult(ParameterTokenSuiteMalformedAndSwapped.class),
hasFailureContaining("Category annotations on Parameterized classes are not supported on individual methods."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void testNewFile() throws IOException {
}

@Test
public void testNewFolder() throws IOException {
public void testNewFolder() {
folder.newFolder(NEW_FOLDER_DUMMY);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;

@SuppressWarnings("deprecation")
public class TestWatchmanTest {
public static class ViolatedAssumptionTest {
@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ protected void verify() {
}

@Test public void verifierRunsAfterTest() {
sequence = "";
assertThat(testResult(UsesVerifier.class), isSuccessful());
assertEquals("test verify ", sequence);
}
}

0 comments on commit 2d13004

Please sign in to comment.