Skip to content

Commit

Permalink
Ensure classes annotated with @OrderWith do not have @FixMethodOrder.
Browse files Browse the repository at this point in the history
This is needed because classes annotated with @FixMethodOrder will not
be reordered or sorted, so having both annotations is a contradiction.

- Add AnnotationValidator to fail if class annotated with @FixMethodOrder
- Annotate OrderWith with @ValidateWith(OrderWithValidator.class)
- Add tests for the new validator
  • Loading branch information
kcooney authored and marcphilipp committed Nov 27, 2019
1 parent 7c2f12c commit b51fa17
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/junit/runner/OrderWith.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.annotation.Target;

import org.junit.runner.manipulation.Ordering;
import org.junit.validator.ValidateWith;

/**
* When a test class is annotated with <code>&#064;OrderWith</code> or extends a class annotated
Expand All @@ -18,6 +19,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@ValidateWith(OrderWithValidator.class)
public @interface OrderWith {
/**
* Gets a class that extends {@link Ordering}. The class must have a public no-arg constructor.
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/junit/runner/OrderWithValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.junit.runner;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import java.util.List;

import org.junit.FixMethodOrder;
import org.junit.runners.model.TestClass;
import org.junit.validator.AnnotationValidator;

/**
* Validates that there are no errors in the use of the {@code OrderWith}
* annotation. If there is, a {@code Throwable} object will be added to the list
* of errors.
*
* @since 4.13
*/
public final class OrderWithValidator extends AnnotationValidator {

/**
* Adds to {@code errors} a throwable for each problem detected. Looks for
* {@code FixMethodOrder} annotations.
*
* @param testClass that is being validated
* @return A list of exceptions detected
*
* @since 4.13
*/
@Override
public List<Exception> validateAnnotatedClass(TestClass testClass) {
if (testClass.getAnnotation(FixMethodOrder.class) != null) {
return singletonList(
new Exception("@FixMethodOrder cannot be combined with @OrderWith"));
}
return emptyList();
}
}
1 change: 1 addition & 0 deletions src/test/java/org/junit/runner/AllRunnerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
AllNotificationTests.class,
FilterFactoriesTest.class,
FilterOptionIntegrationTest.class,
OrderWithValidatorTest.class,
JUnitCommandLineParseResultTest.class,
JUnitCoreTest.class, RequestTest.class
})
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/junit/runner/OrderWithValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.junit.runner;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.List;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.manipulation.Alphanumeric;
import org.junit.runners.JUnit4;
import org.junit.runners.MethodSorters;
import org.junit.runners.model.TestClass;

public class OrderWithValidatorTest {
private final OrderWithValidator validator = new OrderWithValidator();

@RunWith(JUnit4.class)
@OrderWith(Alphanumeric.class)
public static class TestWithNoValidationErrors {
@Test
public void passes() {}
}

@RunWith(JUnit4.class)
@OrderWith(Alphanumeric.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public static class TestAnnotatedWithFixMethodOrder {
@Test
public void passes() {}
}

@Test
public void noErrorIsAddedForTestWithoutValdationErrors() {
List<Exception> errors = validator.validateAnnotatedClass(
new TestClass(TestWithNoValidationErrors.class));

assertThat(errors.size(), is(0));
}

@Test
public void errorIsAddedWhenTestAnnotatedWithFixMethodOrder() {
List<Exception> errors = validator.validateAnnotatedClass(
new TestClass(TestAnnotatedWithFixMethodOrder.class));

assertThat(errors.size(), is(1));
Exception exception = errors.get(0);
assertThat(exception.getMessage(), is("@FixMethodOrder cannot be combined with @OrderWith"));
}
}

0 comments on commit b51fa17

Please sign in to comment.