Move API tests to the API sub-project

This commit is contained in:
James Seibel
2022-09-05 18:53:12 -05:00
parent 9eefd53fdc
commit 7a00ba09a2
6 changed files with 234 additions and 130 deletions
@@ -0,0 +1,129 @@
/*
* 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/>.
*/
package tests;
import com.seibel.lod.api.enums.DhApiEnumAssembly;
import com.seibel.lod.core.enums.rendering.EFogDrawMode;
import com.seibel.lod.core.enums.CoreEnumAssembly;
import com.seibel.lod.core.enums.config.EVerticalQuality;
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-9
*/
public class ApiEnumSyncTests
{
/** Make sure each DhApi enum has the same values as its corresponding core enum. */
@Test
public void ConfirmEnumsAreSynced()
{
//=================//
// test validation //
//=================//
// this should always succeed (comparing an enum to itself)
AssertEnumsValuesAreEqual(EnumUtil.compareEnumClassesByValues(EVerticalQuality.class, EVerticalQuality.class), true);
// this should always fail (two completely different enums)
AssertEnumsValuesAreEqual(EnumUtil.compareEnumClassesByValues(EVerticalQuality.class, EFogDrawMode.class), false);
//================//
// 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> apiConfigEnumPackageNames = EnumTestHelper.findPackageNamesStartingWith(DhApiEnumAssembly.class.getPackage().getName());
for (String apiEnumPackageName : apiConfigEnumPackageNames)
{
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 = CoreEnumAssembly.ENUM_PREFIX + 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.");
}
}
}
/** Helper method to make enum comparisons a little cleaner */
private void AssertEnumsValuesAreEqual(EnumUtil.EnumComparisonResult comparisonResult, boolean assertEqual)
{
if (assertEqual)
{
Assert.assertTrue(comparisonResult.failMessage, comparisonResult.success);
}
else
{
Assert.assertFalse(comparisonResult.failMessage, comparisonResult.success);
}
}
}
+184
View File
@@ -0,0 +1,184 @@
package tests;/*
* 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.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* A list of methods related to the Enum unit tests.
*
* @author James Seibel
* @version 2022-9-5
*/
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<>();
List<Class<?>> classesInPackage = getClassesInPackage(packageFullName, true, "DistantHorizons");
// get the enums from each file
Assert.assertTrue("No files found in the package [" + packageFullName + "].", classesInPackage.size() != 0);
for (Class<?> clazz : classesInPackage)
{
// ignore internal classes
if (!clazz.getName().contains("$"))
{
// attempt to parse the file's class into an enum
if (Enum.class.isAssignableFrom(clazz))
{
enumList.add((Class<? extends Enum<?>>) clazz);
}
else
{
System.out.println("The Class [" + clazz + "] isn't an enum.");
}
}
}
return enumList;
}
/**
* Returns every class in the given package. <br><br>
*
* Originally from:
* https://stackoverflow.com/questions/28678026/how-can-i-get-all-class-files-in-a-specific-package-in-java
*
* @param packageName
* @param onlySearchJars if true only jar files will be searched, otherwise jars and loose files will be searched
* @param expectedJarPathString Only search jars that contain this string
*/
public static List<Class<?>> getClassesInPackage(String packageName, boolean onlySearchJars, String expectedJarPathString)
{
String path = packageName.replace('.', '/');
List<Class<?>> classes = new ArrayList<>();
String[] classPathEntries = System.getProperty("java.class.path").split(
System.getProperty("path.separator")
);
String name;
for (String classpathEntry : classPathEntries)
{
if (classpathEntry.endsWith(".jar") && classpathEntry.toLowerCase().contains(expectedJarPathString.toLowerCase()))
{
File jar = new File(classpathEntry);
try
{
JarInputStream is = new JarInputStream(new FileInputStream(jar));
JarEntry entry;
while ((entry = is.getNextJarEntry()) != null)
{
name = entry.getName();
if (name.endsWith(".class"))
{
if (name.contains(path) && name.endsWith(".class"))
{
try
{
String classPath = name.substring(0, entry.getName().length() - 6);
classPath = classPath.replaceAll("[\\|/]", ".");
classes.add(Class.forName(classPath));
}
catch (ClassNotFoundException ex)
{
// the class wasn't found
System.err.println("The Class [" + packageName + "." + name + "] failed to load.");
}
}
}
}
}
catch (IOException e)
{
System.err.println("Error reading the jar [" + jar.getPath() + "].");
}
}
else if (!onlySearchJars)
{
File base = new File(classpathEntry + File.separatorChar + path);
File[] files = base.listFiles();
if (files != null)
{
for (File file : files)
{
try
{
name = file.getName();
if (name.endsWith(".class"))
{
name = name.substring(0, name.length() - 6);
classes.add(Class.forName(packageName + "." + name));
}
}
catch (ClassNotFoundException ex)
{
// the class wasn't found
System.err.println("The Class [" + packageName + "." + file.getName() + "] failed to load.");
}
}
}
}
}
return classes;
}
/**
* Returns every loaded package that begins with the given string. <br>
*
* 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;
}
}
@@ -23,15 +23,16 @@ import org.junit.Assert;
import org.junit.Test;
/**
* This is just a quick demo to confirm the testing system is set up correctly.
*
* @author James Seibel
* @version 2022-9-5
*/
public class BasicTest
public class ExampleTest
{
/** This is just a quick demo to confirm the testing system is set up correctly. */
@Test
public void ExampleTests()
public void DemoTest()
{
Assert.assertTrue("Example test 1", true);
Assert.assertFalse("Example test 2", false);