Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.reflections.Reflections;

Expand All @@ -25,20 +22,32 @@
/**
* Tests that {@link StandaloneRepresentable} classes fulfill their contract.<br>
* This class mostly just gathers the classes that need testing and delegates to {@link StandaloneReprSubTest},
* whose task is essentially to instantiate classes so that they can be tested.
* whose task is essentially to instantiate classes so that they can be tested. <br>
*
* To use this test for your own package, extend this class and and set up a no-argument constructor like this: <br>
* {@code public YourOwnStandaloneReprTest() { super("com.mypackageprefix"); }}
*
* @see #StandaloneReprTest(String, String...).
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS) //required for the @MethodSource non-static method to work.
public abstract class StandaloneReprTest {
protected static HashSet<Class<? extends StandaloneRepresentable>> testedClasses = new HashSet<>();
private final String packageName;
private final String packageToTest;
private final Reflections reflection;

public StandaloneReprTest(String packageName) {
this.packageName = packageName;
this.reflection = new Reflections(packageName);
/**
* Instantiates the test
* @param packagePrefixToTest the package (-prefix), for example "com.yourpackageprefix".
* @param additionalPackagesToScan additional packages (/prefixes) that contain superclasses/interfaces of your StandaloneRepresentable classes.
* When in doubt, add all dependencies of your software that themselves depend on cryptimeleon.
* There is no need to add org.cryptimeleon packages to this list (as they are implicitly added).
*/
public StandaloneReprTest(String packagePrefixToTest, String... additionalPackagesToScan) {
this.packageToTest = packagePrefixToTest;
String[] allPackages = Arrays.copyOf(additionalPackagesToScan, additionalPackagesToScan.length + 2);
allPackages[additionalPackagesToScan.length] = packagePrefixToTest;
allPackages[additionalPackagesToScan.length + 1] = "org.cryptimeleon";
this.reflection = new Reflections((Object[]) allPackages);
}

@ParameterizedTest(name = "''{0}''")
Expand All @@ -55,6 +64,7 @@ public void testStandaloneRepresentablesWithParameterlessConstructors() {
protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
return reflection.getSubTypesOf(StandaloneReprSubTest.class).stream()
.filter(clazz -> !clazz.equals(TestForParameterlessConstructorClasses.class))
.filter(clazz -> clazz.getPackage().getName().startsWith(packageToTest)) //only use those that belong to the desired package
.map(clazz -> {
try {
return clazz.getDeclaredConstructor().newInstance();
Expand All @@ -70,6 +80,7 @@ protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
protected class TestForParameterlessConstructorClasses extends StandaloneReprSubTest {
public void testClassesWithTrivialConstructor() {
reflection.getSubTypesOf(StandaloneRepresentable.class).stream()
.filter(clazz -> clazz.getPackage().getName().startsWith(packageToTest)) //only use those that belong to the desired package
.filter(clazz -> Arrays.stream(clazz.getConstructors()).anyMatch(constr -> constr.getParameterCount() == 0))
.forEach(clazz -> {
try {
Expand All @@ -87,8 +98,8 @@ public void checkForUntestedClasses() {
Set<Class<? extends StandaloneRepresentable>> classesToTest = reflection.getSubTypesOf(StandaloneRepresentable.class);
classesToTest.removeAll(testedClasses);

//Remove interfaces and such
classesToTest.removeIf(c -> c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !c.getPackage().toString().startsWith("package "+packageName));
//Remove interfaces and stuff from other packages
classesToTest.removeIf(c -> c.isInterface() || Modifier.isAbstract(c.getModifiers()) || !c.getPackage().getName().startsWith(packageToTest));

for (Class<? extends StandaloneRepresentable> notTestedClass : classesToTest) {
System.err.println(notTestedClass.getName() + " implements StandaloneRepresentable was not tested by StandaloneTest. You need to define a StandaloneSubTest for it.");
Expand Down