Skip to content

Commit 60e8687

Browse files
author
Jan Bobolz
committed
Fixed discovery of StandaloneRepresentable classes
1 parent 9f80155 commit 60e8687

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

src/test/java/org/cryptimeleon/math/serialization/standalone/StandaloneReprTest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
import org.junit.jupiter.api.DisplayName;
66
import org.junit.jupiter.api.Test;
77
import org.junit.jupiter.api.TestInstance;
8-
import org.junit.jupiter.api.extension.ExtensionContext;
98
import org.junit.jupiter.params.ParameterizedTest;
109
import org.junit.jupiter.params.provider.Arguments;
11-
import org.junit.jupiter.params.provider.ArgumentsProvider;
12-
import org.junit.jupiter.params.provider.ArgumentsSource;
1310
import org.junit.jupiter.params.provider.MethodSource;
1411
import org.reflections.Reflections;
1512

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

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

4453
@ParameterizedTest(name = "''{0}''")
@@ -55,6 +64,7 @@ public void testStandaloneRepresentablesWithParameterlessConstructors() {
5564
protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
5665
return reflection.getSubTypesOf(StandaloneReprSubTest.class).stream()
5766
.filter(clazz -> !clazz.equals(TestForParameterlessConstructorClasses.class))
67+
.filter(clazz -> clazz.getPackage().getName().startsWith(packageToTest)) //only use those that belong to the desired package
5868
.map(clazz -> {
5969
try {
6070
return clazz.getDeclaredConstructor().newInstance();
@@ -70,6 +80,7 @@ protected Stream<? extends Arguments> provideStandaloneReprSubTests() {
7080
protected class TestForParameterlessConstructorClasses extends StandaloneReprSubTest {
7181
public void testClassesWithTrivialConstructor() {
7282
reflection.getSubTypesOf(StandaloneRepresentable.class).stream()
83+
.filter(clazz -> clazz.getPackage().getName().startsWith(packageToTest)) //only use those that belong to the desired package
7384
.filter(clazz -> Arrays.stream(clazz.getConstructors()).anyMatch(constr -> constr.getParameterCount() == 0))
7485
.forEach(clazz -> {
7586
try {
@@ -87,8 +98,8 @@ public void checkForUntestedClasses() {
8798
Set<Class<? extends StandaloneRepresentable>> classesToTest = reflection.getSubTypesOf(StandaloneRepresentable.class);
8899
classesToTest.removeAll(testedClasses);
89100

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

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

0 commit comments

Comments
 (0)