-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify that test classes are public.
Test classes should be public in order to avoid problems with reflection caused by the security manager. Public classes are always accesible.
- Loading branch information
1 parent
63743d5
commit 1d97da7
Showing
9 changed files
with
152 additions
and
6 deletions.
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
33 changes: 33 additions & 0 deletions
33
src/main/java/org/junit/validator/PublicClassValidator.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,33 @@ | ||
package org.junit.validator; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.runners.model.TestClass; | ||
|
||
/** | ||
* Validates that a {@link TestClass} is public. | ||
* | ||
* @since 4.12 | ||
*/ | ||
public class PublicClassValidator implements TestClassValidator { | ||
private static final List<Exception> NO_VALIDATION_ERRORS = emptyList(); | ||
|
||
/** | ||
* Validate that the specified {@link TestClass} is public. | ||
* | ||
* @param testClass the {@link TestClass} that is validated. | ||
* @return an empty list if the class is public or a list with a single | ||
* exception otherwise. | ||
*/ | ||
public List<Exception> validateTestClass(TestClass testClass) { | ||
if (testClass.isPublic()) { | ||
return NO_VALIDATION_ERRORS; | ||
} else { | ||
return singletonList(new Exception("The class " | ||
+ testClass.getName() + " is not public.")); | ||
} | ||
} | ||
} |
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,21 @@ | ||
package org.junit.validator; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.runners.model.TestClass; | ||
|
||
/** | ||
* Validates a single facet of a test class. | ||
* | ||
* @since 4.12 | ||
*/ | ||
public interface TestClassValidator { | ||
/** | ||
* Validate a single facet of a test class. | ||
* | ||
* @param testClass | ||
* the {@link TestClass} that is validated. | ||
* @return the validation errors found by the validator. | ||
*/ | ||
public List<Exception> validateTestClass(TestClass testClass); | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/test/java/org/junit/validator/PublicClassValidatorTest.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,41 @@ | ||
package org.junit.validator; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
import org.junit.runners.model.TestClass; | ||
|
||
public class PublicClassValidatorTest { | ||
private final PublicClassValidator validator = new PublicClassValidator(); | ||
|
||
public static class PublicClass { | ||
|
||
} | ||
|
||
@Test | ||
public void acceptsPublicClass() { | ||
TestClass testClass = new TestClass(PublicClass.class); | ||
List<Exception> validationErrors = validator | ||
.validateTestClass(testClass); | ||
assertThat(validationErrors, | ||
is(equalTo(Collections.<Exception> emptyList()))); | ||
} | ||
|
||
static class NonPublicClass { | ||
|
||
} | ||
|
||
@Test | ||
public void rejectsNonPublicClass() { | ||
TestClass testClass = new TestClass(NonPublicClass.class); | ||
List<Exception> validationErrors = validator | ||
.validateTestClass(testClass); | ||
assertThat("Wrong number of errors.", validationErrors.size(), | ||
is(equalTo(1))); | ||
} | ||
} |