Skip to content

Commit

Permalink
refactor(MTEExtension): use Engines instead of broad Helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
keturn committed Nov 2, 2021
1 parent b891e14 commit 3e89662
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public List<TerasologyEngine> getEngines() {
/**
* Get the host context for this module testing environment.
* <p>
* The host context will be null if the testing environment has not been set up via {@link ModuleTestingHelper#setup()}
* The host context will be null if the testing environment has not been set up via {@link #setup()}
* beforehand.
*
* @return the engine's host context, or null if not set up yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,44 @@ public void beforeAll(ExtensionContext context) {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
Class<?> type = parameterContext.getParameter().getType();
ModuleTestingHelper helper = getHelper(extensionContext);
return helper.getHostContext().get(type) != null
Engines engines = getEngines(extensionContext);
return engines.getHostContext().get(type) != null
|| type.isAssignableFrom(Engines.class)
|| type.isAssignableFrom(MainLoop.class)
|| type.isAssignableFrom(ModuleTestingHelper.class);
}

@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
ModuleTestingHelper helper = getHelper(extensionContext);
Engines engines = getEngines(extensionContext);
Class<?> type = parameterContext.getParameter().getType();

return getDIInstance(helper, type);
return getDIInstance(engines, type);
}

private Object getDIInstance(ModuleTestingHelper helper, Class<?> type) {
private Object getDIInstance(Engines engines, Class<?> type) {
if (type.isAssignableFrom(Engines.class)) {
return helper.engines;
return engines;
} else if (type.isAssignableFrom(MainLoop.class)) {
return helper.mainLoop;
return new MainLoop(engines);
} else if (type.isAssignableFrom(ModuleTestingHelper.class)) {
return helper;
return new ModuleTestingHelper(engines);
} else {
return helper.getHostContext().get(type);
return engines.getHostContext().get(type);
}
}

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) {
ModuleTestingHelper helper = getHelper(extensionContext);
Engines engines = getEngines(extensionContext);
List<IllegalAccessException> exceptionList = new LinkedList<>();
Class<?> type = testInstance.getClass();
while (type != null) {
Arrays.stream(type.getDeclaredFields())
.filter((field) -> field.getAnnotation(In.class) != null)
.peek((field) -> field.setAccessible(true))
.forEach((field) -> {
Object candidateObject = getDIInstance(helper, field.getType());
Object candidateObject = getDIInstance(engines, field.getType());
try {
field.set(testInstance, candidateObject);
} catch (IllegalAccessException e) {
Expand All @@ -128,9 +128,9 @@ public Set<String> getDependencyNames(ExtensionContext context) {
}

/**
* Get the ModuleTestingHelper for this test.
* Get the Engines for this test.
* <p>
* The new ModuleTestingHelper instance is configured using the {@link Dependencies} and {@link UseWorldGenerator}
* The new Engines instance is configured using the {@link Dependencies} and {@link UseWorldGenerator}
* annotations for the test class.
* <p>
* This will create a new instance when necessary. It will be stored in the
Expand All @@ -140,12 +140,12 @@ public Set<String> getDependencyNames(ExtensionContext context) {
* @param context for the current test
* @return configured for this test
*/
protected ModuleTestingHelper getHelper(ExtensionContext context) {
protected Engines getEngines(ExtensionContext context) {
ExtensionContext.Store store = context.getStore(helperLifecycle.apply(context));
HelperCleaner autoCleaner = store.getOrComputeIfAbsent(
HelperCleaner.class, k -> new HelperCleaner(getDependencyNames(context), getWorldGeneratorUri(context)),
HelperCleaner.class);
return autoCleaner.helper;
EnginesCleaner autoCleaner = store.getOrComputeIfAbsent(
EnginesCleaner.class, k -> new EnginesCleaner(getDependencyNames(context), getWorldGeneratorUri(context)),
EnginesCleaner.class);
return autoCleaner.engines;
}

/**
Expand Down Expand Up @@ -184,23 +184,23 @@ void setupLogging() {
}

/**
* Manages a ModuleTestingHelper for storage in an ExtensionContext.
* Manages Engines for storage in an ExtensionContext.
* <p>
* Implements {@link ExtensionContext.Store.CloseableResource CloseableResource} to dispose of
* the {@link ModuleTestingHelper} when the context is closed.
* the {@link Engines} when the context is closed.
*/
static class HelperCleaner implements ExtensionContext.Store.CloseableResource {
protected ModuleTestingHelper helper;
static class EnginesCleaner implements ExtensionContext.Store.CloseableResource {
protected Engines engines;

HelperCleaner(Set<String> dependencyNames, String worldGeneratorUri) {
helper = new ModuleTestingHelper(dependencyNames, worldGeneratorUri);
helper.setup();
EnginesCleaner(Set<String> dependencyNames, String worldGeneratorUri) {
engines = new Engines(dependencyNames, worldGeneratorUri);
engines.setup();
}

@Override
public void close() {
helper.tearDown();
helper = null;
engines.tearDown();
engines = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public interface ModuleTestingEnvironment {
/**
* Get the host context for this module testing environment.
* <p>
* The host context will be null if the testing environment has not been set up via {@link ModuleTestingHelper#setup()}
* The host context will be null if the testing environment has not been set up via {@link Engines#setup()}
* beforehand.
*
* @return the engine's host context, or null if not set up yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -82,27 +81,9 @@ public class ModuleTestingHelper implements ModuleTestingEnvironment {
final Engines engines;
final MainLoop mainLoop;

protected ModuleTestingHelper(Set<String> dependencies, String worldGeneratorUri) {
engines = new Engines(dependencies, worldGeneratorUri);
mainLoop = new MainLoop(engines);
}

/**
* Set up and start the engine as configured via this environment.
* <p>
* Every instance should be shut down properly by calling {@link #tearDown()}.
*/
protected void setup() {
engines.setup();
}

/**
* Shut down a previously started testing environment.
* <p>
* Used to properly shut down and clean up a testing environment set up and started with {@link #setup()}.
*/
protected void tearDown() {
engines.tearDown();
ModuleTestingHelper(Engines engines) {
this.engines = engines;
this.mainLoop = new MainLoop(engines);
}

@Override
Expand Down

0 comments on commit 3e89662

Please sign in to comment.