Add utility class tests (#871)

Checks for classes that only have static methods.
This commit is contained in:
Austin Shalit
2018-05-24 20:39:15 -04:00
committed by Peter Johnson
parent 863cfde394
commit 2e5fece594
7 changed files with 128 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
public class RobotControllerTest extends UtilityClassTest {
public RobotControllerTest() {
super(RobotController.class);
}
}

View File

@@ -0,0 +1,73 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
@SuppressWarnings("PMD.AbstractClassWithoutAbstractMethod")
public abstract class UtilityClassTest {
@Before
public void setup() {
UnitTestUtility.setupMockBase();
}
private final Class m_clazz;
protected UtilityClassTest(Class clazz) {
m_clazz = clazz;
}
@Test
public void testSingleConstructor() {
assertEquals("More than one constructor defined", 1,
m_clazz.getDeclaredConstructors().length);
}
@Test
public void testConstructorPrivate() {
Constructor constructor = m_clazz.getDeclaredConstructors()[0];
assertFalse("Constructor is not private", constructor.isAccessible());
}
@Test(expected = InvocationTargetException.class)
public void testConstructorReflection() throws Throwable {
Constructor constructor = m_clazz.getDeclaredConstructors()[0];
constructor.setAccessible(true);
constructor.newInstance();
}
@Test
public void testPublicMethodsStatic() {
List<String> failures = new ArrayList<>();
for (Method method : m_clazz.getDeclaredMethods()) {
int modifiers = method.getModifiers();
if (Modifier.isPublic(modifiers) && !Modifier.isStatic(modifiers)) {
failures.add(method.toString());
}
}
if (!failures.isEmpty()) {
fail("Found public methods that are not static: "
+ Arrays.toString(failures.toArray()));
}
}
}

View File

@@ -0,0 +1,16 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.livewindow;
import edu.wpi.first.wpilibj.UtilityClassTest;
public class LiveWindowTest extends UtilityClassTest {
public LiveWindowTest() {
super(LiveWindow.class);
}
}

View File

@@ -0,0 +1,16 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package edu.wpi.first.wpilibj.smartdashboard;
import edu.wpi.first.wpilibj.UtilityClassTest;
public class SmartDashboardTest extends UtilityClassTest {
public SmartDashboardTest() {
super(SmartDashboard.class);
}
}