mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
Add utility class tests (#871)
Checks for classes that only have static methods.
This commit is contained in:
committed by
Peter Johnson
parent
863cfde394
commit
2e5fece594
@@ -18,6 +18,7 @@ import edu.wpi.first.wpilibj.hal.PowerJNI;
|
||||
*/
|
||||
public final class RobotController {
|
||||
private RobotController() {
|
||||
throw new UnsupportedOperationException("This is a utility class!");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -45,6 +45,10 @@ public class LiveWindow {
|
||||
private static boolean liveWindowEnabled = false;
|
||||
private static boolean telemetryEnabled = true;
|
||||
|
||||
private LiveWindow() {
|
||||
throw new UnsupportedOperationException("This is a utility class!");
|
||||
}
|
||||
|
||||
public static synchronized boolean isEnabled() {
|
||||
return liveWindowEnabled;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ public class SmartDashboard {
|
||||
HLUsageReporting.reportSmartDashboard();
|
||||
}
|
||||
|
||||
private SmartDashboard() {
|
||||
throw new UnsupportedOperationException("This is a utility class!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the specified key to the specified value in this table. The key can not be null. The value
|
||||
* can be retrieved by calling the get method with a key that is equal to the original key.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user