Flip API -> Core dependency to Core -> API
Very rough, a decent amount of the API isn't hooked up to anything.
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
package testItems.events.abstractObjects;
|
||||
|
||||
import com.seibel.lod.api.items.interfaces.world.IDhApiLevelWrapper;
|
||||
import com.seibel.lod.api.items.objects.wrappers.DhApiLevelWrapper;
|
||||
import com.seibel.lod.api.methods.events.abstractEvents.DhApiLevelLoadEvent;
|
||||
import com.seibel.lod.api.methods.events.interfaces.IDhApiEvent;
|
||||
import com.seibel.lod.core.api.external.coreImplementations.objects.events.abstractEvents.CoreDhApiLevelLoadEvent;
|
||||
import com.seibel.lod.core.api.external.coreImplementations.objects.events.abstractEvents.CoreDhApiTestEvent;
|
||||
import com.seibel.lod.core.wrapperInterfaces.world.ILevelWrapper;
|
||||
|
||||
/**
|
||||
* A dummy event implementation used for unit testing.
|
||||
@@ -15,12 +9,15 @@ import com.seibel.lod.core.wrapperInterfaces.world.ILevelWrapper;
|
||||
* @version 2022-9-11
|
||||
*/
|
||||
public abstract class DhApiTestEvent
|
||||
extends CoreDhApiTestEvent
|
||||
implements IDhApiEvent<Boolean, Boolean>
|
||||
implements IDhApiEvent<Boolean>
|
||||
{
|
||||
|
||||
public abstract void onTestEvent(Boolean input);
|
||||
|
||||
/** just used for testing */
|
||||
public abstract boolean getTestValue();
|
||||
|
||||
|
||||
|
||||
//=========================//
|
||||
// internal DH API methods //
|
||||
|
||||
@@ -1,129 +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 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,184 +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.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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package tests;
|
||||
|
||||
import com.seibel.lod.core.api.external.coreImplementations.objects.events.abstractEvents.CoreDhApiTestEvent;
|
||||
import com.seibel.lod.core.dependencyInjection.DhApiEventInjector;
|
||||
import org.junit.Assert;
|
||||
import testItems.events.abstractObjects.DhApiTestEvent;
|
||||
import testItems.events.objects.DhTestEvent;
|
||||
@@ -17,66 +15,66 @@ import java.util.ArrayList;
|
||||
public class EventInjectorTest
|
||||
{
|
||||
|
||||
//@Test
|
||||
public void testEventDependencies() // this also tests list dependencies since there can be more than one event handler bound per event
|
||||
{
|
||||
// Injector setup
|
||||
DhApiEventInjector TEST_EVENT_HANDLER = new DhApiEventInjector();
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull("Nothing should have been bound.", TEST_EVENT_HANDLER.get(DhApiTestEvent.class));
|
||||
|
||||
|
||||
// dependency setup
|
||||
TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEvent());
|
||||
TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEventAlt());
|
||||
TEST_EVENT_HANDLER.runDelayedSetup();
|
||||
|
||||
|
||||
// get first
|
||||
CoreDhApiTestEvent afterRenderEvent = TEST_EVENT_HANDLER.get(CoreDhApiTestEvent.class);
|
||||
Assert.assertNotNull("Event not bound.", afterRenderEvent);
|
||||
|
||||
|
||||
// get list
|
||||
ArrayList<CoreDhApiTestEvent> afterRenderEventList = TEST_EVENT_HANDLER.getAll(CoreDhApiTestEvent.class);
|
||||
Assert.assertEquals("Bound list doesn't contain the correct number of items.", 2, afterRenderEventList.size());
|
||||
// object one
|
||||
Assert.assertNotNull("Event not bound.", afterRenderEventList.get(0));
|
||||
Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(0).getTestValue());
|
||||
// object two
|
||||
Assert.assertNotNull("Event not bound.", afterRenderEventList.get(1));
|
||||
Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(1).getTestValue());
|
||||
|
||||
|
||||
// event firing
|
||||
Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", true, TEST_EVENT_HANDLER.fireAllEvents(CoreDhApiTestEvent.class, true));
|
||||
// object one
|
||||
Assert.assertEquals("Event not fired for first object.", true, afterRenderEventList.get(0).getTestValue());
|
||||
// object two
|
||||
Assert.assertEquals("Event not fired for second object.", true, afterRenderEventList.get(1).getTestValue());
|
||||
|
||||
|
||||
// unbind
|
||||
CoreDhApiTestEvent unboundEvent = afterRenderEventList.get(0);
|
||||
Assert.assertTrue("Unbind should've removed item.", TEST_EVENT_HANDLER.unbind(CoreDhApiTestEvent.class, DhTestEvent.class));
|
||||
Assert.assertFalse("Unbind should've already removed item.", TEST_EVENT_HANDLER.unbind(CoreDhApiTestEvent.class, DhTestEvent.class));
|
||||
|
||||
// check unbinding
|
||||
afterRenderEventList = TEST_EVENT_HANDLER.getAll(CoreDhApiTestEvent.class);
|
||||
Assert.assertEquals("Unbound list doesn't contain the correct number of items.", 1, afterRenderEventList.size());
|
||||
Assert.assertNotNull("Unbinding removed all items.", afterRenderEventList.get(0));
|
||||
|
||||
|
||||
// check unbound event firing
|
||||
Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", false, TEST_EVENT_HANDLER.fireAllEvents(CoreDhApiTestEvent.class, false));
|
||||
// remaining event
|
||||
Assert.assertEquals("Event not fired for remaining object.", false, ((DhTestEventAlt) afterRenderEventList.get(0)).eventFiredValue);
|
||||
// unbound event
|
||||
Assert.assertEquals("Event fired for unbound object.", true, unboundEvent.getTestValue());
|
||||
|
||||
}
|
||||
// //@Test
|
||||
// public void testEventDependencies() // this also tests list dependencies since there can be more than one event handler bound per event
|
||||
// {
|
||||
// // Injector setup
|
||||
// DhApiEventInjector TEST_EVENT_HANDLER = new DhApiEventInjector();
|
||||
//
|
||||
//
|
||||
// // pre-dependency setup
|
||||
// Assert.assertNull("Nothing should have been bound.", TEST_EVENT_HANDLER.get(DhApiTestEvent.class));
|
||||
//
|
||||
//
|
||||
// // dependency setup
|
||||
// TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEvent());
|
||||
// TEST_EVENT_HANDLER.bind(DhApiTestEvent.class, new DhTestEventAlt());
|
||||
// TEST_EVENT_HANDLER.runDelayedSetup();
|
||||
//
|
||||
//
|
||||
// // get first
|
||||
// CoreDhApiTestEvent afterRenderEvent = TEST_EVENT_HANDLER.get(CoreDhApiTestEvent.class);
|
||||
// Assert.assertNotNull("Event not bound.", afterRenderEvent);
|
||||
//
|
||||
//
|
||||
// // get list
|
||||
// ArrayList<CoreDhApiTestEvent> afterRenderEventList = TEST_EVENT_HANDLER.getAll(CoreDhApiTestEvent.class);
|
||||
// Assert.assertEquals("Bound list doesn't contain the correct number of items.", 2, afterRenderEventList.size());
|
||||
// // object one
|
||||
// Assert.assertNotNull("Event not bound.", afterRenderEventList.get(0));
|
||||
// Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(0).getTestValue());
|
||||
// // object two
|
||||
// Assert.assertNotNull("Event not bound.", afterRenderEventList.get(1));
|
||||
// Assert.assertEquals("First event object setup incorrectly.", null, afterRenderEventList.get(1).getTestValue());
|
||||
//
|
||||
//
|
||||
// // event firing
|
||||
// Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", true, TEST_EVENT_HANDLER.fireAllEvents(CoreDhApiTestEvent.class, true));
|
||||
// // object one
|
||||
// Assert.assertEquals("Event not fired for first object.", true, afterRenderEventList.get(0).getTestValue());
|
||||
// // object two
|
||||
// Assert.assertEquals("Event not fired for second object.", true, afterRenderEventList.get(1).getTestValue());
|
||||
//
|
||||
//
|
||||
// // unbind
|
||||
// CoreDhApiTestEvent unboundEvent = afterRenderEventList.get(0);
|
||||
// Assert.assertTrue("Unbind should've removed item.", TEST_EVENT_HANDLER.unbind(CoreDhApiTestEvent.class, DhTestEvent.class));
|
||||
// Assert.assertFalse("Unbind should've already removed item.", TEST_EVENT_HANDLER.unbind(CoreDhApiTestEvent.class, DhTestEvent.class));
|
||||
//
|
||||
// // check unbinding
|
||||
// afterRenderEventList = TEST_EVENT_HANDLER.getAll(CoreDhApiTestEvent.class);
|
||||
// Assert.assertEquals("Unbound list doesn't contain the correct number of items.", 1, afterRenderEventList.size());
|
||||
// Assert.assertNotNull("Unbinding removed all items.", afterRenderEventList.get(0));
|
||||
//
|
||||
//
|
||||
// // check unbound event firing
|
||||
// Assert.assertEquals("fireAllEvents canceled returned canceled incorrectly.", false, TEST_EVENT_HANDLER.fireAllEvents(CoreDhApiTestEvent.class, false));
|
||||
// // remaining event
|
||||
// Assert.assertEquals("Event not fired for remaining object.", false, ((DhTestEventAlt) afterRenderEventList.get(0)).eventFiredValue);
|
||||
// // unbound event
|
||||
// Assert.assertEquals("Event fired for unbound object.", true, unboundEvent.getTestValue());
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user