2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-05-24 20:39:15 -04:00
|
|
|
|
|
|
|
|
package edu.wpi.first.wpilibj;
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
|
|
|
|
|
2018-05-24 20:39:15 -04:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
|
import java.lang.reflect.Modifier;
|
|
|
|
|
import java.util.Arrays;
|
2018-06-11 18:01:49 -04:00
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
import org.junit.jupiter.api.DynamicTest;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.junit.jupiter.api.TestFactory;
|
2018-05-24 20:39:15 -04:00
|
|
|
|
2021-06-09 07:01:00 -07:00
|
|
|
// Declaring this class abstract prevents UtilityClassTest from running on itself and throwing the
|
|
|
|
|
// following exception:
|
|
|
|
|
//
|
|
|
|
|
// org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered
|
|
|
|
|
// for parameter [java.lang.Class<T> arg0] in constructor [protected
|
|
|
|
|
// edu.wpi.first.wpilibj.UtilityClassTest(java.lang.Class<T>)].
|
2018-05-24 20:39:15 -04:00
|
|
|
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
|
2019-09-03 19:44:24 -07:00
|
|
|
public abstract class UtilityClassTest<T> {
|
|
|
|
|
private final Class<T> m_clazz;
|
2018-05-24 20:39:15 -04:00
|
|
|
|
2019-09-03 19:44:24 -07:00
|
|
|
protected UtilityClassTest(Class<T> clazz) {
|
2018-05-24 20:39:15 -04:00
|
|
|
m_clazz = clazz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2021-12-09 12:20:08 -08:00
|
|
|
void singleConstructorTest() {
|
2020-12-29 22:45:16 -08:00
|
|
|
assertEquals(1, m_clazz.getDeclaredConstructors().length, "More than one constructor defined");
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2021-12-09 12:20:08 -08:00
|
|
|
void constructorPrivateTest() {
|
2019-09-03 19:44:24 -07:00
|
|
|
Constructor<?> constructor = m_clazz.getDeclaredConstructors()[0];
|
2018-05-24 20:39:15 -04:00
|
|
|
|
2019-09-03 19:44:24 -07:00
|
|
|
assertFalse(constructor.canAccess(null), "Constructor is not private");
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 18:01:49 -04:00
|
|
|
@Test
|
2021-12-09 12:20:08 -08:00
|
|
|
@SuppressWarnings("PMD.AvoidAccessibilityAlteration")
|
|
|
|
|
void constructorReflectionTest() {
|
2019-09-03 19:44:24 -07:00
|
|
|
Constructor<?> constructor = m_clazz.getDeclaredConstructors()[0];
|
2018-05-24 20:39:15 -04:00
|
|
|
constructor.setAccessible(true);
|
2019-08-01 01:19:48 -04:00
|
|
|
assertThrows(InvocationTargetException.class, constructor::newInstance);
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 18:01:49 -04:00
|
|
|
@TestFactory
|
|
|
|
|
Stream<DynamicTest> publicMethodsStaticTestFactory() {
|
|
|
|
|
return Arrays.stream(m_clazz.getDeclaredMethods())
|
|
|
|
|
.filter(method -> Modifier.isPublic(method.getModifiers()))
|
2020-12-29 22:45:16 -08:00
|
|
|
.map(
|
|
|
|
|
method ->
|
|
|
|
|
dynamicTest(
|
|
|
|
|
method.getName(), () -> assertTrue(Modifier.isStatic(method.getModifiers()))));
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
}
|