Skip to content

Commit

Permalink
Improve UtilityClassTest
Browse files Browse the repository at this point in the history
  • Loading branch information
AustinShalit committed Aug 24, 2017
1 parent 0ae0530 commit 3977868
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

public class EqualityUtilsTest extends UtilityClassTest<EqualityUtils> {

public EqualityUtilsTest() {
super(EqualityUtils.class);
}

@Test
public void isEqualInvalidPrimativeTest() {
assertThrows(UnsupportedOperationException.class, () -> EqualityUtils.isEqual(new long[0], new long[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@

public class FxUtilsTest extends UtilityClassTest<FxUtils> {

public FxUtilsTest() {
super(FxUtils.class);
}

@Nested
public class FxTests extends ApplicationTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

public class MapsTest extends UtilityClassTest<Maps> {

public MapsTest() {
super(Maps.class);
}

@Test
public void builderEmptyTest() {
Map<Object, Object> map = Maps.builder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@

public class NetworkTableUtilsTest extends UtilityClassTest<NetworkTableUtils> {

public NetworkTableUtilsTest() {
super(NetworkTableUtils.class);
}

@ParameterizedTest
@CsvSource({"simple, simple",
"simple, one/two/many/simple",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

public class PropertyUtilsTest extends UtilityClassTest<PropertyUtils> {

public PropertyUtilsTest() {
super(PropertyUtils.class);
}

@Test
@SuppressWarnings("LocalVariableName")
public void testCombineLists() {
Expand Down
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())));
}
}
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ subprojects {
}

dependencies {
fun junitJupiter(name: String, version: String = "5.0.0-M5") =
fun junitJupiter(name: String, version: String = "5.0.0-RC2") =
create(group = "org.junit.jupiter", name = name, version = version)
"testCompile"(junitJupiter(name = "junit-jupiter-api"))
"testCompile"(junitJupiter(name = "junit-jupiter-engine"))
"testCompile"(junitJupiter(name = "junit-jupiter-params"))
"testRuntime"(create(group = "org.junit.platform", name = "junit-platform-launcher", version = "1.0.0-M5"))
"testRuntime"(create(group = "org.junit.platform", name = "junit-platform-launcher", version = "1.0.0-RC2"))
fun testFx(name: String, version: String = "4.0.+") =
create(group = "org.testfx", name = name, version = version)
"testCompile"(testFx(name = "testfx-core"))
Expand Down

0 comments on commit 3977868

Please sign in to comment.