move core into a folder named "core"
This is so we can have multiple sub-projects in the core repo
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package testItems.eventInjection.abstractObjects;
|
||||
|
||||
import com.seibel.lod.core.api.implementation.interfaces.events.IDhApiEvent;
|
||||
|
||||
/**
|
||||
* A dummy event implementation used for unit testing.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public abstract class DhApiTestEvent implements IDhApiEvent<Boolean>
|
||||
{
|
||||
/**
|
||||
* Test event.
|
||||
*
|
||||
* @param input
|
||||
* @return whether the event should be canceled or not.
|
||||
*/
|
||||
public abstract boolean test(Boolean input);
|
||||
|
||||
/**
|
||||
* Normal DhApiEvent classes shouldn't have any other methods like this.
|
||||
* This is just for testing.
|
||||
*/
|
||||
public abstract Boolean getTestValue();
|
||||
|
||||
|
||||
//=========================//
|
||||
// internal DH API methods //
|
||||
//=========================//
|
||||
|
||||
@Override
|
||||
public final boolean onEvent(Boolean input)
|
||||
{
|
||||
return test(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean getCancelable() { return true; }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package testItems.eventInjection.objects;
|
||||
|
||||
import testItems.eventInjection.abstractObjects.DhApiTestEvent;
|
||||
|
||||
/**
|
||||
* Dummy test event for unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public class DhTestEvent extends DhApiTestEvent
|
||||
{
|
||||
public Boolean eventFiredValue = null;
|
||||
|
||||
@Override
|
||||
public boolean test(Boolean cancelEvent)
|
||||
{
|
||||
this.eventFiredValue = cancelEvent;
|
||||
return cancelEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getTestValue() { return this.eventFiredValue; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package testItems.eventInjection.objects;
|
||||
|
||||
import testItems.eventInjection.abstractObjects.DhApiTestEvent;
|
||||
|
||||
/**
|
||||
* Dummy test event for unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public class DhTestEventAlt extends DhApiTestEvent
|
||||
{
|
||||
public Boolean eventFiredValue = null;
|
||||
|
||||
@Override
|
||||
public boolean test(Boolean cancelEvent)
|
||||
{
|
||||
this.eventFiredValue = cancelEvent;
|
||||
return cancelEvent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getTestValue() { return this.eventFiredValue; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package testItems.overrideInjection.interfaces;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiOverrideable;
|
||||
|
||||
/**
|
||||
* Dummy override test interface for dependency unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-19
|
||||
*/
|
||||
public interface IOverrideTest extends IDhApiOverrideable
|
||||
{
|
||||
public int getValue();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package testItems.overrideInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.util.StringUtil;
|
||||
|
||||
/**
|
||||
* assembly classes are used to reference the package they are in.
|
||||
*
|
||||
* @author james seibel
|
||||
* @version 2022-7-19
|
||||
*/
|
||||
public class OverrideTestAssembly
|
||||
{
|
||||
|
||||
/** Returns the first N packages in this class' path. */
|
||||
public static String getPackagePath(int numberOfPackagesToReturn)
|
||||
{
|
||||
String thisPackageName = OverrideTestAssembly.class.getPackage().getName();
|
||||
int secondPackageEndingIndex = StringUtil.nthIndexOf(thisPackageName, ".", numberOfPackagesToReturn);
|
||||
return thisPackageName.substring(0, secondPackageEndingIndex);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package testItems.overrideInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import testItems.overrideInjection.interfaces.IOverrideTest;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-19
|
||||
*/
|
||||
public class OverrideTestCore implements IOverrideTest
|
||||
{
|
||||
public static int VALUE = 1;
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() { return VALUE; }
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.CORE; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package testItems.overrideInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import testItems.overrideInjection.interfaces.IOverrideTest;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-19
|
||||
*/
|
||||
public class OverrideTestPrimary implements IOverrideTest
|
||||
{
|
||||
public static int VALUE = 3;
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() { return VALUE; }
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.PRIMARY; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package testItems.overrideInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import testItems.overrideInjection.interfaces.IOverrideTest;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-19
|
||||
*/
|
||||
public class OverrideTestSecondary implements IOverrideTest
|
||||
{
|
||||
public static int VALUE = 2;
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() { return VALUE; }
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.SECONDARY; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package testItems.singletonInjection.interfaces;
|
||||
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
|
||||
|
||||
/**
|
||||
* Dummy test interface for dependency unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public interface ISingletonTestOne extends IBindable
|
||||
{
|
||||
public int getValue();
|
||||
|
||||
public int getDependentValue();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package testItems.singletonInjection.interfaces;
|
||||
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
|
||||
|
||||
/**
|
||||
* Dummy test interface for dependency unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public interface ISingletonTestTwo extends IBindable
|
||||
{
|
||||
public int getValue();
|
||||
|
||||
public int getDependentValue();
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package testItems.singletonInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestOne;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestTwo;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public class ConcreteSingletonTestBoth implements ISingletonTestOne, ISingletonTestTwo, IBindable
|
||||
{
|
||||
public static final int VALUE = 3;
|
||||
|
||||
@Override
|
||||
public void finishDelayedSetup() { }
|
||||
|
||||
@Override
|
||||
public int getValue()
|
||||
{
|
||||
return VALUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDependentValue() { return -1; }
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package testItems.singletonInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.DependencyInjector;
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestOne;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestTwo;
|
||||
import tests.DependencyInjectorTest;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public class ConcreteSingletonTestOne implements ISingletonTestOne, IBindable
|
||||
{
|
||||
private final DependencyInjector<IBindable> dependencyInjector;
|
||||
private ISingletonTestTwo testInterTwo;
|
||||
|
||||
public static int VALUE = 1;
|
||||
|
||||
|
||||
public ConcreteSingletonTestOne(DependencyInjector<IBindable> newDependencyInjector)
|
||||
{
|
||||
dependencyInjector = newDependencyInjector;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void finishDelayedSetup() { testInterTwo = dependencyInjector.get(ISingletonTestTwo.class, true); }
|
||||
@Override
|
||||
public boolean getDelayedSetupComplete() { return testInterTwo != null; }
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() { return VALUE; }
|
||||
|
||||
@Override
|
||||
public int getDependentValue() { return testInterTwo.getValue(); }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package testItems.singletonInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.DependencyInjector;
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestOne;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestTwo;
|
||||
import tests.DependencyInjectorTest;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for dependency injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-16
|
||||
*/
|
||||
public class ConcreteSingletonTestTwo implements ISingletonTestTwo, IBindable
|
||||
{
|
||||
private final DependencyInjector<IBindable> dependencyInjector;
|
||||
private ISingletonTestOne testInterOne;
|
||||
|
||||
public static int VALUE = 2;
|
||||
|
||||
|
||||
public ConcreteSingletonTestTwo(DependencyInjector<IBindable> newDependencyInjector)
|
||||
{
|
||||
dependencyInjector = newDependencyInjector;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void finishDelayedSetup() { testInterOne = dependencyInjector.get(ISingletonTestOne.class, true); }
|
||||
@Override
|
||||
public boolean getDelayedSetupComplete() { return testInterOne != null; }
|
||||
|
||||
|
||||
@Override
|
||||
public int getValue() { return VALUE; }
|
||||
|
||||
@Override
|
||||
public int getDependentValue() { return testInterOne.getValue(); }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package testItems.worldGeneratorInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiLevelType;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiDimensionTypeWrapper;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrapper;
|
||||
|
||||
/**
|
||||
* Stub implementation of a Level wrapper for basic unit testing.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-8-23
|
||||
*/
|
||||
public class LevelWrapperTest implements IDhApiLevelWrapper
|
||||
{
|
||||
@Override
|
||||
public Object getWrappedMcObject_UNSAFE() { return null; }
|
||||
|
||||
@Override
|
||||
public IDhApiDimensionTypeWrapper getDimensionType() { return null; }
|
||||
|
||||
@Override
|
||||
public EDhApiLevelType getLevelType() { return EDhApiLevelType.UNKNOWN; }
|
||||
|
||||
@Override
|
||||
public boolean hasCeiling() { return false; }
|
||||
|
||||
@Override
|
||||
public boolean hasSkyLight() { return false; }
|
||||
|
||||
@Override
|
||||
public int getHeight() { return 0; }
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package testItems.worldGeneratorInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.util.StringUtil;
|
||||
|
||||
/**
|
||||
* assembly classes are used to reference the package they are in.
|
||||
*
|
||||
* @author james seibel
|
||||
* @version 2022-7-26
|
||||
*/
|
||||
public class WorldGeneratorTestAssembly
|
||||
{
|
||||
|
||||
/** Returns the first N packages in this class' path. */
|
||||
public static String getPackagePath(int numberOfPackagesToReturn)
|
||||
{
|
||||
String thisPackageName = WorldGeneratorTestAssembly.class.getPackage().getName();
|
||||
int secondPackageEndingIndex = StringUtil.nthIndexOf(thisPackageName, ".", numberOfPackagesToReturn);
|
||||
return thisPackageName.substring(0, secondPackageEndingIndex);
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package testItems.worldGeneratorInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenThreadMode;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenerationStep;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiWorldGenerator;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiChunkWrapper;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrapper;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for world generator injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-26
|
||||
*/
|
||||
public class WorldGeneratorTestCore implements IDhApiWorldGenerator
|
||||
{
|
||||
public static EDhApiWorldGenThreadMode THREAD_MODE = EDhApiWorldGenThreadMode.SERVER_THREAD;
|
||||
|
||||
|
||||
//==============//
|
||||
// IOverridable //
|
||||
//==============//
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.CORE; }
|
||||
|
||||
|
||||
|
||||
//======================//
|
||||
// IDhApiWorldGenerator //
|
||||
//======================//
|
||||
|
||||
@Override
|
||||
public EDhApiWorldGenThreadMode getThreadingMode() { return THREAD_MODE; }
|
||||
|
||||
@Override
|
||||
public IDhApiChunkWrapper generateChunk(int chunkPosX, int chunkPosZ, IDhApiLevelWrapper serverLevelWrapper, EDhApiWorldGenerationStep maxStepToGenerate)
|
||||
{
|
||||
// not necessary for testing
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package testItems.worldGeneratorInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenThreadMode;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenerationStep;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiWorldGenerator;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiChunkWrapper;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrapper;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for world generator injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-26
|
||||
*/
|
||||
public class WorldGeneratorTestPrimary implements IDhApiWorldGenerator
|
||||
{
|
||||
public static EDhApiWorldGenThreadMode THREAD_MODE = EDhApiWorldGenThreadMode.MULTI_THREADED;
|
||||
|
||||
|
||||
//==============//
|
||||
// IOverridable //
|
||||
//==============//
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.PRIMARY; }
|
||||
|
||||
|
||||
|
||||
//======================//
|
||||
// IDhApiWorldGenerator //
|
||||
//======================//
|
||||
|
||||
@Override
|
||||
public EDhApiWorldGenThreadMode getThreadingMode() { return THREAD_MODE; }
|
||||
|
||||
@Override
|
||||
public IDhApiChunkWrapper generateChunk(int chunkPosX, int chunkPosZ, IDhApiLevelWrapper serverLevelWrapper, EDhApiWorldGenerationStep maxStepToGenerate)
|
||||
{
|
||||
// not necessary for testing
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package testItems.worldGeneratorInjection.objects;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenThreadMode;
|
||||
import com.seibel.lod.core.api.external.items.enums.worldGeneration.EDhApiWorldGenerationStep;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiWorldGenerator;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiChunkWrapper;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrapper;
|
||||
|
||||
/**
|
||||
* Dummy test implementation object for world generator injection unit tests.
|
||||
*
|
||||
* @author James Seibel
|
||||
* @version 2022-7-26
|
||||
*/
|
||||
public class WorldGeneratorTestSecondary implements IDhApiWorldGenerator
|
||||
{
|
||||
public static EDhApiWorldGenThreadMode THREAD_MODE = EDhApiWorldGenThreadMode.SINGLE_THREADED;
|
||||
|
||||
|
||||
//==============//
|
||||
// IOverridable //
|
||||
//==============//
|
||||
|
||||
@Override
|
||||
public EDhApiOverridePriority getOverrideType() { return EDhApiOverridePriority.SECONDARY; }
|
||||
|
||||
|
||||
|
||||
//======================//
|
||||
// IDhApiWorldGenerator //
|
||||
//======================//
|
||||
|
||||
@Override
|
||||
public EDhApiWorldGenThreadMode getThreadingMode() { return THREAD_MODE; }
|
||||
|
||||
@Override
|
||||
public IDhApiChunkWrapper generateChunk(int chunkPosX, int chunkPosZ, IDhApiLevelWrapper serverLevelWrapper, EDhApiWorldGenerationStep maxStepToGenerate)
|
||||
{
|
||||
// not necessary for testing
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,352 @@
|
||||
package tests;
|
||||
|
||||
import com.seibel.lod.core.api.external.items.enums.override.EDhApiOverridePriority;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiOverrideable;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.override.IDhApiWorldGenerator;
|
||||
import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrapper;
|
||||
import com.seibel.lod.core.enums.override.EOverridePriority;
|
||||
import com.seibel.lod.core.handlers.dependencyInjection.*;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import testItems.eventInjection.objects.DhTestEvent;
|
||||
import testItems.eventInjection.objects.DhTestEventAlt;
|
||||
import testItems.overrideInjection.objects.OverrideTestCore;
|
||||
import testItems.overrideInjection.objects.OverrideTestPrimary;
|
||||
import testItems.overrideInjection.objects.OverrideTestSecondary;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestOne;
|
||||
import testItems.singletonInjection.interfaces.ISingletonTestTwo;
|
||||
import testItems.singletonInjection.objects.ConcreteSingletonTestBoth;
|
||||
import testItems.singletonInjection.objects.ConcreteSingletonTestOne;
|
||||
import testItems.singletonInjection.objects.ConcreteSingletonTestTwo;
|
||||
import testItems.eventInjection.abstractObjects.DhApiTestEvent;
|
||||
import testItems.overrideInjection.interfaces.IOverrideTest;
|
||||
import testItems.overrideInjection.objects.OverrideTestAssembly;
|
||||
import testItems.worldGeneratorInjection.objects.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* @author James Seibel
|
||||
* @version 2022-7-27
|
||||
*/
|
||||
public class DependencyInjectorTest
|
||||
{
|
||||
|
||||
@Test
|
||||
public void testSingleImplementationSingleton()
|
||||
{
|
||||
// Injector setup
|
||||
DependencyInjector<IBindable> TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false);
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull(ISingletonTestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ISingletonTestOne.class));
|
||||
|
||||
|
||||
// dependency setup
|
||||
TEST_SINGLETON_HANDLER.bind(ISingletonTestOne.class, new ConcreteSingletonTestOne(TEST_SINGLETON_HANDLER));
|
||||
TEST_SINGLETON_HANDLER.bind(ISingletonTestTwo.class, new ConcreteSingletonTestTwo(TEST_SINGLETON_HANDLER));
|
||||
|
||||
TEST_SINGLETON_HANDLER.runDelayedSetup();
|
||||
|
||||
|
||||
// basic dependencies
|
||||
ISingletonTestOne testInterOne = TEST_SINGLETON_HANDLER.get(ISingletonTestOne.class);
|
||||
Assert.assertNotNull(ISingletonTestOne.class.getSimpleName() + " not bound.", testInterOne);
|
||||
Assert.assertEquals(ISingletonTestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteSingletonTestOne.VALUE);
|
||||
Assert.assertEquals(ISingletonTestOne.class.getSimpleName() + " incorrect value.", TEST_SINGLETON_HANDLER.get(ISingletonTestOne.class).getValue(), ConcreteSingletonTestOne.VALUE);
|
||||
|
||||
ISingletonTestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ISingletonTestTwo.class);
|
||||
Assert.assertNotNull(ISingletonTestTwo.class.getSimpleName() + " not bound.", testInterTwo);
|
||||
Assert.assertEquals(ISingletonTestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteSingletonTestTwo.VALUE);
|
||||
Assert.assertEquals(ISingletonTestTwo.class.getSimpleName() + " incorrect value.", TEST_SINGLETON_HANDLER.get(ISingletonTestTwo.class).getValue(), ConcreteSingletonTestTwo.VALUE);
|
||||
|
||||
|
||||
// circular dependencies (if this throws an exception the dependency isn't set up)
|
||||
Assert.assertEquals(ISingletonTestOne.class.getSimpleName() + " incorrect value.", testInterOne.getDependentValue(), ConcreteSingletonTestTwo.VALUE);
|
||||
Assert.assertEquals(ISingletonTestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getDependentValue(), ConcreteSingletonTestOne.VALUE);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleImplementationSingleton()
|
||||
{
|
||||
// Injector setup
|
||||
DependencyInjector<IBindable> TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false);
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull(ISingletonTestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ISingletonTestOne.class));
|
||||
|
||||
|
||||
// dependency setup
|
||||
ConcreteSingletonTestBoth concreteInstance = new ConcreteSingletonTestBoth();
|
||||
|
||||
TEST_SINGLETON_HANDLER.bind(ISingletonTestOne.class, concreteInstance);
|
||||
TEST_SINGLETON_HANDLER.bind(ISingletonTestTwo.class, concreteInstance);
|
||||
|
||||
|
||||
// basic dependencies
|
||||
ISingletonTestOne testInterOne = TEST_SINGLETON_HANDLER.get(ISingletonTestOne.class);
|
||||
Assert.assertNotNull(ISingletonTestOne.class.getSimpleName() + " not bound.", testInterOne);
|
||||
Assert.assertEquals(ISingletonTestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteSingletonTestBoth.VALUE);
|
||||
|
||||
ISingletonTestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ISingletonTestTwo.class);
|
||||
Assert.assertNotNull(ISingletonTestTwo.class.getSimpleName() + " not bound.", testInterTwo);
|
||||
Assert.assertEquals(ISingletonTestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteSingletonTestBoth.VALUE);
|
||||
|
||||
}
|
||||
|
||||
@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
|
||||
DhApiTestEvent afterRenderEvent = TEST_EVENT_HANDLER.get(DhApiTestEvent.class);
|
||||
Assert.assertNotNull("Event not bound.", afterRenderEvent);
|
||||
|
||||
|
||||
// get list
|
||||
ArrayList<DhApiTestEvent> afterRenderEventList = TEST_EVENT_HANDLER.getAll(DhApiTestEvent.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(DhApiTestEvent.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
|
||||
DhApiTestEvent unboundEvent = afterRenderEventList.get(0);
|
||||
Assert.assertTrue("Unbind should've removed item.", TEST_EVENT_HANDLER.unbind(DhApiTestEvent.class, DhTestEvent.class));
|
||||
Assert.assertFalse("Unbind should've already removed item.", TEST_EVENT_HANDLER.unbind(DhApiTestEvent.class, DhTestEvent.class));
|
||||
|
||||
// check unbinding
|
||||
afterRenderEventList = TEST_EVENT_HANDLER.getAll(DhApiTestEvent.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(DhApiTestEvent.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 testOverrideInjection()
|
||||
{
|
||||
OverrideInjector<IDhApiOverrideable> TEST_INJECTOR = new OverrideInjector<>(OverrideTestAssembly.getPackagePath(2));
|
||||
OverrideInjector<IDhApiOverrideable> CORE_INJECTOR = new OverrideInjector<>();
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull("Nothing should have been bound.", TEST_INJECTOR.get(IOverrideTest.class));
|
||||
Assert.assertNull("Nothing should have been bound.", CORE_INJECTOR.get(IOverrideTest.class));
|
||||
|
||||
|
||||
// variables to use later
|
||||
IOverrideTest override;
|
||||
OverrideTestCore coreOverride = new OverrideTestCore();
|
||||
OverrideTestSecondary secondaryOverride = new OverrideTestSecondary();
|
||||
OverrideTestPrimary primaryOverride = new OverrideTestPrimary();
|
||||
|
||||
|
||||
// core override binding
|
||||
try { TEST_INJECTOR.bind(IOverrideTest.class, coreOverride); } catch (IllegalArgumentException e) { Assert.fail("Core override should be bindable for test package injector."); }
|
||||
|
||||
try
|
||||
{
|
||||
CORE_INJECTOR.bind(IOverrideTest.class, coreOverride);
|
||||
Assert.fail("Core override should not be bindable for core package injector.");
|
||||
}
|
||||
catch (IllegalArgumentException e) { /* this exception should be thrown */ }
|
||||
|
||||
|
||||
// core override
|
||||
Assert.assertNotNull("Test injector should've bound core override.", TEST_INJECTOR.get(IOverrideTest.class));
|
||||
Assert.assertNull("Core injector should not have bound core override.", CORE_INJECTOR.get(IOverrideTest.class));
|
||||
// priority gets
|
||||
Assert.assertNotNull("Core override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
|
||||
Assert.assertNull("Secondary override should not be bound yet.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.SECONDARY));
|
||||
Assert.assertNull("Primary override should not be bound yet.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.PRIMARY));
|
||||
// standard get
|
||||
override = TEST_INJECTOR.get(IOverrideTest.class);
|
||||
Assert.assertEquals("Override returned incorrect override type.", override.getOverrideType(), EDhApiOverridePriority.CORE);
|
||||
Assert.assertEquals("Incorrect override object returned.", override.getValue(), OverrideTestCore.VALUE);
|
||||
|
||||
|
||||
// secondary override
|
||||
TEST_INJECTOR.bind(IOverrideTest.class, secondaryOverride);
|
||||
// priority gets
|
||||
Assert.assertNotNull("Test injector should've bound secondary override.", TEST_INJECTOR.get(IOverrideTest.class));
|
||||
Assert.assertNotNull("Core override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
|
||||
Assert.assertNotNull("Secondary override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.SECONDARY));
|
||||
Assert.assertNull("Primary override should not be bound yet.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.PRIMARY));
|
||||
// standard get
|
||||
override = TEST_INJECTOR.get(IOverrideTest.class);
|
||||
Assert.assertEquals("Override returned incorrect override type.", override.getOverrideType(), EDhApiOverridePriority.SECONDARY);
|
||||
Assert.assertEquals("Incorrect override object returned.", override.getValue(), OverrideTestSecondary.VALUE);
|
||||
|
||||
|
||||
// primary override
|
||||
TEST_INJECTOR.bind(IOverrideTest.class, primaryOverride);
|
||||
// priority gets
|
||||
Assert.assertNotNull("Test injector should've bound primary override.", TEST_INJECTOR.get(IOverrideTest.class));
|
||||
Assert.assertNotNull("Core override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
|
||||
Assert.assertNotNull("Secondary override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.SECONDARY));
|
||||
Assert.assertNotNull("Primary override should be bound.", TEST_INJECTOR.get(IOverrideTest.class, EOverridePriority.PRIMARY));
|
||||
// standard get
|
||||
override = TEST_INJECTOR.get(IOverrideTest.class);
|
||||
Assert.assertEquals("Override returned incorrect override type.", override.getOverrideType(), EDhApiOverridePriority.PRIMARY);
|
||||
Assert.assertEquals("Incorrect override object returned.", override.getValue(), OverrideTestPrimary.VALUE);
|
||||
|
||||
|
||||
// in-line get
|
||||
// (make sure the returned type is correct and compiles, the actual value doesn't matter)
|
||||
TEST_INJECTOR.get(IOverrideTest.class).getValue();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBackupWorldGeneratorInjection()
|
||||
{
|
||||
WorldGeneratorInjector TEST_INJECTOR = new WorldGeneratorInjector(WorldGeneratorTestAssembly.getPackagePath(2));
|
||||
WorldGeneratorInjector CORE_INJECTOR = new WorldGeneratorInjector();
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull("Nothing should have been bound.", TEST_INJECTOR.get());
|
||||
Assert.assertNull("Nothing should have been bound.", CORE_INJECTOR.get());
|
||||
|
||||
|
||||
// variables to use later
|
||||
IDhApiWorldGenerator generator;
|
||||
WorldGeneratorTestCore coreGenerator = new WorldGeneratorTestCore();
|
||||
WorldGeneratorTestSecondary secondaryGenerator = new WorldGeneratorTestSecondary();
|
||||
WorldGeneratorTestPrimary primaryGenerator = new WorldGeneratorTestPrimary();
|
||||
|
||||
|
||||
// core generator binding
|
||||
try { TEST_INJECTOR.bind(coreGenerator); } catch (IllegalArgumentException e) { Assert.fail("Core generator should be bindable for test package injector."); }
|
||||
|
||||
try
|
||||
{
|
||||
CORE_INJECTOR.bind(coreGenerator);
|
||||
Assert.fail("Core generator should not be bindable for core package injector.");
|
||||
}
|
||||
catch (IllegalArgumentException e) { /* this exception should be thrown */ }
|
||||
|
||||
|
||||
// core override
|
||||
Assert.assertNotNull("Test injector should've bound core override.", TEST_INJECTOR.get());
|
||||
Assert.assertNull("Core injector should not have bound core override.", CORE_INJECTOR.get());
|
||||
// standard get
|
||||
generator = TEST_INJECTOR.get();
|
||||
Assert.assertEquals("Override returned incorrect override type.", generator.getOverrideType(), EDhApiOverridePriority.CORE);
|
||||
Assert.assertEquals("Incorrect generator returned.", generator.getThreadingMode(), WorldGeneratorTestCore.THREAD_MODE);
|
||||
|
||||
|
||||
// secondary override
|
||||
TEST_INJECTOR.bind(secondaryGenerator);
|
||||
// priority gets
|
||||
generator = TEST_INJECTOR.get();
|
||||
Assert.assertEquals("Override returned incorrect override type.", generator.getOverrideType(), EDhApiOverridePriority.SECONDARY);
|
||||
Assert.assertEquals("Incorrect override object returned.", generator.getThreadingMode(), WorldGeneratorTestSecondary.THREAD_MODE);
|
||||
|
||||
|
||||
// primary override
|
||||
TEST_INJECTOR.bind(primaryGenerator);
|
||||
// priority gets
|
||||
generator = TEST_INJECTOR.get();
|
||||
Assert.assertEquals("Override returned incorrect override type.", generator.getOverrideType(), EDhApiOverridePriority.PRIMARY);
|
||||
Assert.assertEquals("Incorrect override object returned.", generator.getThreadingMode(), WorldGeneratorTestPrimary.THREAD_MODE);
|
||||
|
||||
|
||||
|
||||
// in-line get
|
||||
// (make sure the returned type is correct and compiles, the actual value doesn't matter)
|
||||
TEST_INJECTOR.get().getThreadingMode();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecificLevelWorldGeneratorInjection()
|
||||
{
|
||||
WorldGeneratorInjector TEST_INJECTOR = new WorldGeneratorInjector(WorldGeneratorTestAssembly.getPackagePath(2));
|
||||
|
||||
|
||||
// pre-dependency setup
|
||||
Assert.assertNull("Nothing should have been bound.", TEST_INJECTOR.get());
|
||||
|
||||
|
||||
// variables to use later
|
||||
IDhApiWorldGenerator generator;
|
||||
WorldGeneratorTestCore backupGenerator = new WorldGeneratorTestCore();
|
||||
WorldGeneratorTestPrimary levelGenerator = new WorldGeneratorTestPrimary();
|
||||
|
||||
IDhApiLevelWrapper boundLevel = new LevelWrapperTest();
|
||||
IDhApiLevelWrapper unboundLevel = new LevelWrapperTest();
|
||||
|
||||
|
||||
|
||||
// backup generator binding
|
||||
try { TEST_INJECTOR.bind(backupGenerator); } catch (IllegalArgumentException e) { Assert.fail("Core generator should be bindable for test package injector."); }
|
||||
|
||||
|
||||
// get backup generator
|
||||
generator = TEST_INJECTOR.get();
|
||||
Assert.assertNotNull("Backup generator not bound.", generator);
|
||||
Assert.assertEquals("Incorrect backup generator bound.", generator.getOverrideType(), EDhApiOverridePriority.CORE);
|
||||
Assert.assertEquals("Incorrect backup generator bound.", generator.getThreadingMode(), WorldGeneratorTestCore.THREAD_MODE);
|
||||
|
||||
|
||||
// bind level specific
|
||||
try { TEST_INJECTOR.bind(boundLevel, levelGenerator); } catch (IllegalArgumentException e) { Assert.fail("Core generator should be bindable for test package injector."); }
|
||||
|
||||
|
||||
// get bound level generator
|
||||
generator = TEST_INJECTOR.get(boundLevel);
|
||||
Assert.assertNotNull("Level generator not bound.", generator);
|
||||
Assert.assertEquals("Incorrect level generator bound.", generator.getOverrideType(), EDhApiOverridePriority.PRIMARY);
|
||||
Assert.assertEquals("Incorrect level generator bound.", generator.getThreadingMode(), WorldGeneratorTestPrimary.THREAD_MODE);
|
||||
|
||||
// get unbound level generator
|
||||
generator = TEST_INJECTOR.get(unboundLevel);
|
||||
Assert.assertNotNull("Backup level generator not bound.", generator);
|
||||
Assert.assertEquals("Incorrect level generator bound.", generator.getOverrideType(), EDhApiOverridePriority.CORE);
|
||||
Assert.assertEquals("Incorrect level generator bound.", generator.getThreadingMode(), WorldGeneratorTestCore.THREAD_MODE);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user