Add basic unit tests for WorldGenInjector

This commit is contained in:
James Seibel
2022-07-27 07:53:19 -05:00
parent 30aba99c27
commit b9cd99cb6c
2 changed files with 74 additions and 55 deletions
@@ -21,6 +21,7 @@ package com.seibel.lod.core.handlers.dependencyInjection;
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.util.StringUtil;
import java.util.HashMap;
@@ -29,7 +30,7 @@ import java.util.HashMap;
* This is done so other mods can override our world generator(s) to improve or replace them.
*
* @author James Seibel
* @version 2022-7-26
* @version 2022-7-27
*/
public class WorldGeneratorInjector
{
@@ -37,7 +38,33 @@ public class WorldGeneratorInjector
private final HashMap<IDhApiLevelWrapper, OverrideInjector<IDhApiWorldGenerator>> worldGeneratorByLevelWrapper = new HashMap<>();
/** World generators that aren't bound to a specific level and are used if no other world generators are bound. */
private final OverrideInjector<IDhApiWorldGenerator> backupUniversalWorldGenerators = new OverrideInjector<>();
private final OverrideInjector<IDhApiWorldGenerator> backupUniversalWorldGenerators;
/**
* This is used to determine if an override is part of Distant Horizons'
* Core or not.
* This probably isn't the best way of going about this, but it works for now.
*/
private final String corePackagePath;
public WorldGeneratorInjector()
{
String thisPackageName = this.getClass().getPackage().getName();
int secondPackageEndingIndex = StringUtil.nthIndexOf(thisPackageName, ".", 3);
this.corePackagePath = thisPackageName.substring(0, secondPackageEndingIndex); // this should be "com.seibel.lod"
this.backupUniversalWorldGenerators = new OverrideInjector<>(this.corePackagePath);
}
/** This constructor should only be used for testing different corePackagePaths. */
public WorldGeneratorInjector(String newCorePackagePath)
{
this.corePackagePath = newCorePackagePath;
this.backupUniversalWorldGenerators = new OverrideInjector<>(this.corePackagePath);
}
@@ -67,7 +94,7 @@ public class WorldGeneratorInjector
// bind this generator to a specific level
if (!worldGeneratorByLevelWrapper.containsKey(levelForWorldGenerator))
{
worldGeneratorByLevelWrapper.put(levelForWorldGenerator, new OverrideInjector<>());
worldGeneratorByLevelWrapper.put(levelForWorldGenerator, new OverrideInjector<>(this.corePackagePath));
}
worldGeneratorByLevelWrapper.get(levelForWorldGenerator).bind(IDhApiWorldGenerator.class, worldGeneratorImplementation);
+44 -52
View File
@@ -21,6 +21,7 @@ import testItems.singletonInjection.objects.ConcreteSingletonTestTwo;
import testItems.eventInjection.abstractObjects.DhApiTestEvent;
import testItems.overrideInjection.interfaces.IOverrideTest;
import testItems.overrideInjection.objects.OverrideTestAssembly;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestAssembly;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestCore;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestPrimary;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestSecondary;
@@ -30,7 +31,7 @@ import java.util.ArrayList;
/**
* @author James Seibel
* @version 2022-7-26
* @version 2022-7-27
*/
public class DependencyInjectorTest
{
@@ -230,15 +231,16 @@ public class DependencyInjectorTest
Assert.assertEquals("Incorrect override object returned.", override.getValue(), OverrideTestPrimary.VALUE);
// in-line get (make sure the gotten type is correct, the actual value doesn't matter)
Assert.assertNotEquals("Inline get incorrect value.", -1, TEST_INJECTOR.get(IOverrideTest.class).getValue());
// 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 testWorldGeneratorInjection()
{
WorldGeneratorInjector TEST_INJECTOR = new WorldGeneratorInjector();
WorldGeneratorInjector TEST_INJECTOR = new WorldGeneratorInjector(WorldGeneratorTestAssembly.getPackagePath(2));
WorldGeneratorInjector CORE_INJECTOR = new WorldGeneratorInjector();
@@ -254,58 +256,48 @@ public class DependencyInjectorTest
WorldGeneratorTestPrimary primaryGenerator = new WorldGeneratorTestPrimary();
// TODO need core package overriding
// // core generator binding
// try { TEST_GENERATOR_INJECTOR.bind(coreGenerator); } catch (IllegalArgumentException e) { Assert.fail("Core generator should be bindable for test package injector."); }
//
// try
// {
// CORE_GENERATOR_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_GENERATOR_INJECTOR.get());
// Assert.assertNull("Core injector should not have bound core override.", CORE_GENERATOR_INJECTOR.get());
// // standard get
// generator = TEST_GENERATOR_INJECTOR.get();
// Assert.assertEquals("Override returned incorrect override type.", generator.getOverrideType(), EDhApiOverridePriority.CORE);
// Assert.assertEquals("Incorrect generator returned.", generator.getThreadingMode(), WorldGeneratorTestCore.THREAD_MODE);
// 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);
// TODO not started
// // secondary override
// TEST_OVERRIDE_INJECTOR.bind(IOverrideTest.class, secondaryOverride);
// // priority gets
// Assert.assertNotNull("Test injector should've bound secondary override.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class));
// Assert.assertNotNull("Core override should be bound.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
// Assert.assertNotNull("Secondary override should be bound.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.SECONDARY));
// Assert.assertNull("Primary override should not be bound yet.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.PRIMARY));
// // standard get
// override = TEST_OVERRIDE_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_OVERRIDE_INJECTOR.bind(IOverrideTest.class, primaryOverride);
// // priority gets
// Assert.assertNotNull("Test injector should've bound primary override.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class));
// Assert.assertNotNull("Core override should be bound.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
// Assert.assertNotNull("Secondary override should be bound.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.SECONDARY));
// Assert.assertNotNull("Primary override should be bound.", TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.PRIMARY));
// // standard get
// override = TEST_OVERRIDE_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 gotten type is correct, the actual value doesn't matter)
// Assert.assertNotEquals("Inline get incorrect value.", -1, TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class).getValue());
// 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();
}