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
@@ -1,42 +0,0 @@
/*
* 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 com.seibel.lod.core.api.external.items.enums;
import com.seibel.lod.core.api.external.items.enums.override.DhApiOverrideEnumAssembly;
import com.seibel.lod.core.api.external.items.enums.config.DhApiConfigEnumAssembly;
import com.seibel.lod.core.api.external.items.enums.worldGeneration.DhApiWorldGenerationEnumAssembly;
/**
* Assembly classes are used to reference the package they are in.
*
* @author James Seibel
* @version 2022-7-18
*/
public class DhApiEnumAssembly
{
// These variables are added in order to load each package into the JVM's class loader.
// This is done so they can be found via reflection.
private static final DhApiWorldGenerationEnumAssembly worldGenerationAssembly = new DhApiWorldGenerationEnumAssembly();
private static final DhApiConfigEnumAssembly configAssembly = new DhApiConfigEnumAssembly();
private static final DhApiOverrideEnumAssembly overrideAssembly = new DhApiOverrideEnumAssembly();
/** All DH API enums should have this prefix */
public static final String API_ENUM_PREFIX = "EDhApi";
}
@@ -1,135 +0,0 @@
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 com.seibel.lod.core.api.external.items.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
{
/** This is just a quick demo to confirm the testing system is set up correctly. */
@Test
public void ExampleTests()
{
Assert.assertTrue("Example test 1", true);
Assert.assertFalse("Example test 2", false);
}
/** 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);
}
}
}
@@ -1,116 +0,0 @@
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.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;
}
}
+41
View File
@@ -0,0 +1,41 @@
/*
* 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 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 ExampleTest
{
@Test
public void DemoTest()
{
Assert.assertTrue("Example test 1", true);
Assert.assertFalse("Example test 2", false);
}
}