Add unit tests for the DependencyInjectors

This commit is contained in:
James Seibel
2022-07-16 22:40:14 -05:00
parent 9a8ed301cb
commit c7e4781a95
11 changed files with 380 additions and 2 deletions
@@ -0,0 +1,25 @@
package testItems.dependencyInjection.implementations;
import testItems.dependencyInjection.objects.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.dependencyInjection.implementations;
import testItems.dependencyInjection.objects.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,16 @@
package testItems.dependencyInjection.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 ITestOne extends IBindable
{
public int getValue();
public int getDependentValue();
}
@@ -0,0 +1,16 @@
package testItems.dependencyInjection.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 ITestTwo extends IBindable
{
public int getValue();
public int getDependentValue();
}
@@ -0,0 +1,28 @@
package testItems.dependencyInjection.objects;
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
import testItems.dependencyInjection.interfaces.ITestOne;
import testItems.dependencyInjection.interfaces.ITestTwo;
/**
* Dummy test implementation object for dependency injection unit tests.
*
* @author James Seibel
* @version 2022-7-16
*/
public class ConcreteTestBoth implements ITestOne, ITestTwo, 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,34 @@
package testItems.dependencyInjection.objects;
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
import testItems.dependencyInjection.interfaces.ITestOne;
import testItems.dependencyInjection.interfaces.ITestTwo;
import tests.DependencyInjectorTest;
/**
* Dummy test implementation object for dependency injection unit tests.
*
* @author James Seibel
* @version 2022-7-16
*/
public class ConcreteTestOne implements ITestOne, IBindable
{
private ITestTwo testInterTwo;
public static int VALUE = 1;
@Override
public void finishDelayedSetup() { testInterTwo = DependencyInjectorTest.TEST_SINGLETON_HANDLER.get(ITestTwo.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,34 @@
package testItems.dependencyInjection.objects;
import com.seibel.lod.core.handlers.dependencyInjection.IBindable;
import testItems.dependencyInjection.interfaces.ITestOne;
import testItems.dependencyInjection.interfaces.ITestTwo;
import tests.DependencyInjectorTest;
/**
* Dummy test implementation object for dependency injection unit tests.
*
* @author James Seibel
* @version 2022-7-16
*/
public class ConcreteTestTwo implements ITestTwo, IBindable
{
private ITestOne testInterOne;
public static int VALUE = 2;
@Override
public void finishDelayedSetup() { testInterOne = DependencyInjectorTest.TEST_SINGLETON_HANDLER.get(ITestOne.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,40 @@
package testItems.dependencyInjection.objects;
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; }
}
@@ -1,4 +1,4 @@
/*
package tests;/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*
@@ -0,0 +1,160 @@
package tests;
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 org.junit.Assert;
import org.junit.Test;
import testItems.dependencyInjection.implementations.DhTestEvent;
import testItems.dependencyInjection.implementations.DhTestEventAlt;
import testItems.dependencyInjection.interfaces.ITestOne;
import testItems.dependencyInjection.interfaces.ITestTwo;
import testItems.dependencyInjection.objects.ConcreteTestBoth;
import testItems.dependencyInjection.objects.ConcreteTestOne;
import testItems.dependencyInjection.objects.ConcreteTestTwo;
import testItems.dependencyInjection.objects.DhApiTestEvent;
import java.util.ArrayList;
/**
* @author James Seibel
* @version 7-16-2022
*/
public class DependencyInjectorTest
{
public static DependencyInjector<IBindable> TEST_SINGLETON_HANDLER;
public static DhApiEventInjector TEST_EVENT_HANDLER;
@Test
public void testSingleImplementations()
{
// clear the previous dependencies and only allow single dependencies
TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false);
// pre-setup
Assert.assertNull(ITestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ITestOne.class));
// dependency setup
TEST_SINGLETON_HANDLER.bind(ITestOne.class, new ConcreteTestOne());
TEST_SINGLETON_HANDLER.bind(ITestTwo.class, new ConcreteTestTwo());
TEST_SINGLETON_HANDLER.runDelayedSetup();
// basic dependencies
ITestOne testInterOne = TEST_SINGLETON_HANDLER.get(ITestOne.class);
Assert.assertNotNull(ITestOne.class.getSimpleName() + " not bound.", testInterOne);
Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteTestOne.VALUE);
ITestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ITestTwo.class);
Assert.assertNotNull(ITestTwo.class.getSimpleName() + " not bound.", testInterTwo);
Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteTestTwo.VALUE);
// circular dependencies (if this throws an exception the dependency isn't set up)
Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getDependentValue(), ConcreteTestTwo.VALUE);
Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getDependentValue(), ConcreteTestOne.VALUE);
}
@Test
public void testMultipleImplementations()
{
// clear the previous dependencies and only allow single dependencies
TEST_SINGLETON_HANDLER = new DependencyInjector<IBindable>(IBindable.class, false);
// pre-setup
Assert.assertNull(ITestOne.class.getSimpleName() + " should not have been bound.", TEST_SINGLETON_HANDLER.get(ITestOne.class));
// dependency setup
ConcreteTestBoth concreteInstance = new ConcreteTestBoth();
TEST_SINGLETON_HANDLER.bind(ITestOne.class, concreteInstance);
TEST_SINGLETON_HANDLER.bind(ITestTwo.class, concreteInstance);
// basic dependencies
ITestOne testInterOne = TEST_SINGLETON_HANDLER.get(ITestOne.class);
Assert.assertNotNull(ITestOne.class.getSimpleName() + " not bound.", testInterOne);
Assert.assertEquals(ITestOne.class.getSimpleName() + " incorrect value.", testInterOne.getValue(), ConcreteTestBoth.VALUE);
ITestTwo testInterTwo = TEST_SINGLETON_HANDLER.get(ITestTwo.class);
Assert.assertNotNull(ITestTwo.class.getSimpleName() + " not bound.", testInterTwo);
Assert.assertEquals(ITestTwo.class.getSimpleName() + " incorrect value.", testInterTwo.getValue(), ConcreteTestBoth.VALUE);
}
@Test
public void testEventDependencies() // this also tests list dependencies since there can be more than one event handler bound per event
{
// clear the previous dependencies and only allow single dependencies
TEST_SINGLETON_HANDLER = new DependencyInjector<>(IBindable.class, false);
// setup the list (event) dependency handler
TEST_EVENT_HANDLER = new DhApiEventInjector();
// pre-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());
}
}
@@ -1,4 +1,4 @@
/*
package tests;/*
* This file is part of the Distant Horizons mod (formerly the LOD Mod),
* licensed under the GNU LGPL v3 License.
*