Add Api Enum Unit tests

This commit is contained in:
James Seibel
2022-06-09 20:54:18 -05:00
parent 6df38f9c7c
commit 1743aad851
7 changed files with 361 additions and 34 deletions
+88 -26
View File
@@ -1,22 +1,43 @@
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiFogDistance;
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiVerticalQuality;
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiEnumAssembly;
import com.seibel.lod.core.api.external.apiObjects.enums.DhApiFogDrawMode;
import com.seibel.lod.core.enums.CoreEnumAssembly;
import com.seibel.lod.core.enums.config.VerticalQuality;
import com.seibel.lod.core.util.EnumUtil;
import org.junit.Test;
import org.junit.Assert;
import java.util.ArrayList;
/**
* These tests were primary created to confirm that the
* API enums are properly synced with their Core variants.
*
*
* @author James Seibel
* @version 2022-6-6
* @version 2022-6-9
*/
public class ApiEnumSyncTests
{
/** This is just a quick example to confirm the testing system is set up correctly. */
/** This is just a quick demo to confirm the testing system is set up correctly. */
@Test
public void ExampleTests()
{
@@ -28,38 +49,79 @@ public class ApiEnumSyncTests
@Test
public void ConfirmEnumsAreSynced()
{
//================================//
// base case tests to make sure //
// the tests are set up correctly //
//================================//
//=================//
// test validation //
//=================//
// this should always succeed (comparing the same enum to itself)
AssertEnumsValuesAreEqual(VerticalQuality.class, VerticalQuality.class, true);
// this should always succeed (comparing an enum to itself)
AssertEnumsValuesAreEqual(EnumUtil.compareEnumClassesByValues(VerticalQuality.class, VerticalQuality.class), true);
// this should always fail (two completely different enums)
AssertEnumsValuesAreEqual(VerticalQuality.class, DhApiFogDistance.class, false);
AssertEnumsValuesAreEqual(EnumUtil.compareEnumClassesByValues(VerticalQuality.class, DhApiFogDrawMode.class), false);
//=========================//
// actual enum comparisons //
//=========================//
// TODO using reflection I should be able to automatically find and compare each enum in the Api to its corresponding Core object
AssertEnumsValuesAreEqual(VerticalQuality.class, DhApiVerticalQuality.class);
//================//
// Api enum Setup //
//================//
// make sure the enum packages are loaded
new DhApiEnumAssembly();
new CoreEnumAssembly();
// get the list of API enums
ArrayList<Class<? extends Enum<?>>> apiEnumClassList = new ArrayList<>();
ArrayList<String> apiEnumPackageNames = EnumTestHelper.findPackageNamesStartingWith(DhApiEnumAssembly.class.getPackage().getName());
for (String apiEnumPackageName : apiEnumPackageNames)
{
apiEnumClassList.addAll(EnumTestHelper.getAllEnumsFromPackage(apiEnumPackageName));
}
// get the list of core enums
ArrayList<Class<? extends Enum<?>>> coreEnumClassList = new ArrayList<>();
ArrayList<String> coreEnumPackageNames = EnumTestHelper.findPackageNamesStartingWith(CoreEnumAssembly.class.getPackage().getName());
for (String coreEnumPackageName : coreEnumPackageNames)
{
coreEnumClassList.addAll(EnumTestHelper.getAllEnumsFromPackage(coreEnumPackageName));
}
//======================//
// Api enum comparisons //
//======================//
// compare each API enum to its corresponding Core enum
for (Class<? extends Enum<?>> apiEnumClass : apiEnumClassList)
{
String coreEnumName = apiEnumClass.getSimpleName().substring(DhApiEnumAssembly.API_ENUM_PREFIX.length());
boolean coreEnumFound = false;
// find the core enum to compare against
for (Class<? extends Enum<?>> coreEnumClass : coreEnumClassList)
{
if (coreEnumClass.getSimpleName().equals(coreEnumName))
{
AssertEnumsValuesAreEqual(EnumUtil.compareEnumClassesByValues(coreEnumClass, apiEnumClass), true);
coreEnumFound = true;
break;
}
}
if (!coreEnumFound)
{
Assert.fail("API enum [" + coreEnumName + "] not found in Core.");
}
}
}
private <Ta extends Enum<Ta>, Tb extends Enum<Tb>> void AssertEnumsValuesAreEqual(Class<Ta> alphaEnum, Class<Tb> betaEnum)
/** Helper method to make enum comparisons a little cleaner */
private void AssertEnumsValuesAreEqual(EnumUtil.EnumComparisonResult comparisonResult, boolean assertEqual)
{
AssertEnumsValuesAreEqual(alphaEnum, betaEnum, true);
}
private <Ta extends Enum<Ta>, Tb extends Enum<Tb>> void AssertEnumsValuesAreEqual(Class<Ta> alphaEnum, Class<Tb> betaEnum, boolean shouldBeEqual)
{
EnumUtil.EnumComparisonResult comparisonResult = EnumUtil.compareEnumsByValues(alphaEnum, betaEnum);
if (shouldBeEqual)
if (assertEqual)
{
Assert.assertTrue(comparisonResult.failMessage, comparisonResult.success);
}
+116
View File
@@ -0,0 +1,116 @@
/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
* Copyright (C) 2020-2022 James Seibel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import org.junit.Assert;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.util.ArrayList;
/**
* A list of methods related to the Enum unit tests.
*
* @author James Seibel
* @version 6-9-2022
*/
public class EnumTestHelper
{
/**
* Returns a list of every Enum in the package with the given full name.
*
* @param packageFullName includes the package path
*/
@SuppressWarnings("unchecked")
public static ArrayList<Class<? extends Enum<?>>> getAllEnumsFromPackage(String packageFullName)
{
ArrayList<Class<? extends Enum<?>>> enumList = new ArrayList<>();
URL packageRoot = Thread.currentThread().getContextClassLoader().getResource(packageFullName.replace(".", "/"));
// find all .class files in the package folder
Assert.assertNotNull("No enums found in the package [" + packageFullName + "].", packageRoot); // asserts can be used since this method should only ever be used in the context of unit tests
File[] files = new File(packageRoot.getFile()).listFiles(new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".class");
}
});
// get the enums from each file
Assert.assertNotNull("No files found in the package [" + packageFullName + "].", files);
for (File file : files)
{
String className = file.getName().replaceAll(".class$", "");
// ignore internal classes
if (!className.contains("$"))
{
String fullClassName = packageFullName + "." + className;
try
{
// attempt to parse the file's class into an enum
Class<?> enumClass = Class.forName(fullClassName);
if (Enum.class.isAssignableFrom(enumClass))
{
enumList.add((Class<? extends Enum<?>>) enumClass);
}
else
{
System.out.println("The Class [" + fullClassName + "] isn't an enum.");
}
}
catch (ClassNotFoundException e)
{
System.out.println("No enum found with the full name [" + fullClassName + "].");
}
}
}
return enumList;
}
/**
* Returns every loaded package that begins with the given string.
*
* Note: this will only search packages that have been loaded
* at some point during the JVM's lifetime.
* To Make sure the package(s) you want to find are loaded you can
* initialize an object from that package to load it.
*/
public static ArrayList<String> findPackageNamesStartingWith(String packagePrefix)
{
ArrayList<String> nestedPackages = new ArrayList<>();
// search all the loaded packages
Package[] packageArray = Package.getPackages();
for (Package pack : packageArray)
{
String packageName = pack.getName();
if (packageName.startsWith(packagePrefix))
{
nestedPackages.add(packageName);
}
}
return nestedPackages;
}
}