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;
|
|
|
|
|
|
|
|
|
|
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;
|
2018-05-24 20:39:15 -04:00
|
|
|
|
2018-06-11 18:01:49 -04:00
|
|
|
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
|
|
|
|
2018-06-11 18:01:49 -04: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
|
|
|
|
|
|
|
|
@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
|
2018-06-11 18:01:49 -04:00
|
|
|
public void singleConstructorTest() {
|
|
|
|
|
assertEquals(1, m_clazz.getDeclaredConstructors().length,
|
|
|
|
|
"More than one constructor defined");
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-06-11 18:01:49 -04:00
|
|
|
public 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
|
|
|
|
|
public 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()))
|
|
|
|
|
.map(method -> dynamicTest(method.getName(),
|
|
|
|
|
() -> assertTrue(Modifier.isStatic(method.getModifiers()))));
|
2018-05-24 20:39:15 -04:00
|
|
|
}
|
|
|
|
|
}
|