-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ae0530
commit 3977868
Showing
7 changed files
with
24 additions
and
39 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
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
39 changes: 22 additions & 17 deletions
39
api/src/test/java/edu/wpi/first/shuffleboard/api/util/UtilityClassTest.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 |
---|---|---|
@@ -1,35 +1,40 @@ | ||
package edu.wpi.first.shuffleboard.api.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import com.google.common.reflect.TypeToken; | ||
import org.junit.jupiter.api.DynamicTest; | ||
import org.junit.jupiter.api.TestFactory; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Arrays; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod") | ||
public abstract class UtilityClassTest<T> { | ||
|
||
private final Class<T> clazz; | ||
private final Class<? super T> clazz; | ||
|
||
public UtilityClassTest(Class<T> clazz) { | ||
this.clazz = clazz; | ||
public UtilityClassTest() { | ||
this.clazz = (new TypeToken<T>(getClass()) {}).getRawType(); | ||
} | ||
|
||
@Test | ||
public void testConstructorPrivate() throws NoSuchMethodException { | ||
Constructor<T> constructor = clazz.getDeclaredConstructor(); | ||
|
||
assertFalse(constructor.isAccessible()); | ||
@TestFactory | ||
public Stream<DynamicTest> constructorsPrivateDynamicTests() { | ||
return DynamicTest.stream(Arrays.asList(clazz.getDeclaredConstructors()).iterator(), Constructor::getName, | ||
constructor -> assertTrue(Modifier.isPrivate(constructor.getModifiers()), | ||
String.format("The Utility class %s has a non-private constructor.", clazz.getName()))); | ||
} | ||
|
||
@Test | ||
public void testConstructorReflection() { | ||
assertThrows(InvocationTargetException.class, () -> { | ||
Constructor<T> constructor = clazz.getDeclaredConstructor(); | ||
constructor.setAccessible(true); | ||
constructor.newInstance(); | ||
}); | ||
@TestFactory | ||
public Stream<DynamicTest> constructorsReflectionDynamicTests() { | ||
return DynamicTest.stream(Arrays.asList(clazz.getDeclaredConstructors()).iterator(), Constructor::getName, | ||
constructor -> assertThrows(InvocationTargetException.class, () -> { | ||
constructor.setAccessible(true); | ||
constructor.newInstance(); | ||
}, String.format("The Utility class %s has a non-private constructor.", clazz.getName()))); | ||
} | ||
} |
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