Add WorldGeneratorInjector and start unit tests

This commit is contained in:
James Seibel
2022-07-26 22:09:49 -05:00
parent a49512f750
commit 30aba99c27
7 changed files with 373 additions and 31 deletions
@@ -7,11 +7,11 @@ import com.seibel.lod.core.api.external.items.interfaces.world.IDhApiLevelWrappe
/**
* @author James Seibel
* @version 2022-7-14
* @version 2022-7-26
*/
public interface IDhApiWorldGenerator extends IDhApiOverrideable
{
/** Returns where chunk generation requests can be generated. */
/** Returns which thread chunk generation requests can be created on. */
EDhApiWorldGenThreadMode getThreadingMode();
IDhApiChunkWrapper generateChunk(int chunkPosX, int chunkPosZ, IDhApiLevelWrapper serverLevelWrapper, EDhApiWorldGenerationStep maxStepToGenerate);
@@ -0,0 +1,116 @@
/*
* 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.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 java.util.HashMap;
/**
* This class takes care of dependency injection for world generators. <Br>
* This is done so other mods can override our world generator(s) to improve or replace them.
*
* @author James Seibel
* @version 2022-7-26
*/
public class WorldGeneratorInjector
{
public static final WorldGeneratorInjector INSTANCE = new 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<>();
/**
* Binds the backup world generator. <Br>
* See {@link DependencyInjector#bind(Class, IBindable) bind(Class, IBindable)} for full documentation.
*
* @throws IllegalArgumentException if a non-Distant Horizons world generator with the priority CORE is passed in
* @see DependencyInjector#bind(Class, IBindable)
*/
public void bind(IDhApiWorldGenerator worldGeneratorImplementation) throws IllegalStateException, IllegalArgumentException
{
bind(null, worldGeneratorImplementation);
}
/**
* Binds the world generator to the given level. <Br>
* See {@link DependencyInjector#bind(Class, IBindable) bind(Class, IBindable)} for full documentation.
*
* @throws IllegalArgumentException if a non-Distant Horizons world generator with the priority CORE is passed in
* @see DependencyInjector#bind(Class, IBindable)
*/
public void bind(IDhApiLevelWrapper levelForWorldGenerator, IDhApiWorldGenerator worldGeneratorImplementation) throws IllegalStateException, IllegalArgumentException
{
if (levelForWorldGenerator != null)
{
// bind this generator to a specific level
if (!worldGeneratorByLevelWrapper.containsKey(levelForWorldGenerator))
{
worldGeneratorByLevelWrapper.put(levelForWorldGenerator, new OverrideInjector<>());
}
worldGeneratorByLevelWrapper.get(levelForWorldGenerator).bind(IDhApiWorldGenerator.class, worldGeneratorImplementation);
}
else
{
// a null level wrapper binds the generator to all levels
backupUniversalWorldGenerators.bind(IDhApiWorldGenerator.class, worldGeneratorImplementation);
}
}
/**
* Returns the backup world generator with the highest priority. <br>
* See {@link OverrideInjector#get(Class) get(Class)} for more documentation.
*
* @see OverrideInjector#get(Class)
*/
public IDhApiWorldGenerator get() throws ClassCastException
{
return backupUniversalWorldGenerators.get(IDhApiWorldGenerator.class);
}
/**
* Returns the bound world generator with the highest priority. <br>
* (Returns a backup world generator if no world generators have been bound for this specific level.) <br>
* See {@link OverrideInjector#get(Class) get(Class)} for more documentation.
*
* @see OverrideInjector#get(Class)
*/
public IDhApiWorldGenerator get(IDhApiLevelWrapper levelForWorldGenerator) throws ClassCastException
{
if (!worldGeneratorByLevelWrapper.containsKey(levelForWorldGenerator))
{
// no generator exists for this specific level.
// check for a backup universal world generator
return backupUniversalWorldGenerators.get(IDhApiWorldGenerator.class);
}
// use the existing world generator
return worldGeneratorByLevelWrapper.get(levelForWorldGenerator).get(IDhApiWorldGenerator.class);
}
}
@@ -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);
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
+104 -29
View File
@@ -2,12 +2,10 @@ 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.enums.override.EOverridePriority;
import com.seibel.lod.core.handlers.dependencyInjection.DependencyInjector;
import com.seibel.lod.core.handlers.dependencyInjection.DhApiEventInjector;
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
import com.seibel.lod.core.handlers.dependencyInjection.*;
import com.seibel.lod.core.handlers.dependencyInjection.OverrideInjector;
import org.junit.Assert;
import org.junit.Test;
import testItems.eventInjection.objects.DhTestEvent;
@@ -23,6 +21,9 @@ import testItems.singletonInjection.objects.ConcreteSingletonTestTwo;
import testItems.eventInjection.abstractObjects.DhApiTestEvent;
import testItems.overrideInjection.interfaces.IOverrideTest;
import testItems.overrideInjection.objects.OverrideTestAssembly;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestCore;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestPrimary;
import testItems.worldGeneratorInjection.objects.WorldGeneratorTestSecondary;
import java.util.ArrayList;
@@ -163,13 +164,13 @@ public class DependencyInjectorTest
@Test
public void testOverrideInjection()
{
OverrideInjector<IDhApiOverrideable> TEST_OVERRIDE_INJECTOR = new OverrideInjector<>(OverrideTestAssembly.getPackagePath(2));
OverrideInjector<IDhApiOverrideable> CORE_OVERRIDE_INJECTOR = new OverrideInjector<>();
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_OVERRIDE_INJECTOR.get(IOverrideTest.class));
Assert.assertNull("Nothing should have been bound.", CORE_OVERRIDE_INJECTOR.get(IOverrideTest.class));
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
@@ -180,57 +181,131 @@ public class DependencyInjectorTest
// core override binding
try { TEST_OVERRIDE_INJECTOR.bind(IOverrideTest.class, coreOverride); } catch (IllegalArgumentException e) { Assert.fail("Core override should be bindable for test package injector."); }
try { TEST_INJECTOR.bind(IOverrideTest.class, coreOverride); } catch (IllegalArgumentException e) { Assert.fail("Core override should be bindable for test package injector."); }
try
{
CORE_OVERRIDE_INJECTOR.bind(IOverrideTest.class, coreOverride);
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_OVERRIDE_INJECTOR.get(IOverrideTest.class));
Assert.assertNull("Core injector should not have bound core override.", CORE_OVERRIDE_INJECTOR.get(IOverrideTest.class));
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_OVERRIDE_INJECTOR.get(IOverrideTest.class, EOverridePriority.CORE));
Assert.assertNull("Secondary override should not be bound yet.", 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));
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_OVERRIDE_INJECTOR.get(IOverrideTest.class);
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_OVERRIDE_INJECTOR.bind(IOverrideTest.class, secondaryOverride);
TEST_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));
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_OVERRIDE_INJECTOR.get(IOverrideTest.class);
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_OVERRIDE_INJECTOR.bind(IOverrideTest.class, primaryOverride);
TEST_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));
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_OVERRIDE_INJECTOR.get(IOverrideTest.class);
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 gotten type is correct, the actual value doesn't matter)
Assert.assertNotEquals("Inline get incorrect value.", -1, TEST_OVERRIDE_INJECTOR.get(IOverrideTest.class).getValue());
Assert.assertNotEquals("Inline get incorrect value.", -1, TEST_INJECTOR.get(IOverrideTest.class).getValue());
}
@Test
public void testWorldGeneratorInjection()
{
WorldGeneratorInjector TEST_INJECTOR = new WorldGeneratorInjector();
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();
// 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);
// 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());
}