-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Terasology/fix/storeContext
fix: more consistent disposal of resources at the end of the test
- Loading branch information
Showing
6 changed files
with
237 additions
and
81 deletions.
There are no files selected for viewing
36 changes: 6 additions & 30 deletions
36
src/main/java/org/terasology/moduletestingenvironment/IsolatedMTEExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,19 @@ | ||
// Copyright 2020 The Terasology Foundation | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.moduletestingenvironment; | ||
|
||
import org.junit.jupiter.api.extension.AfterAllCallback; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.junit.jupiter.api.extension.TestInstancePostProcessor; | ||
|
||
/** | ||
* Subclass of {@link MTEExtension} which isolates all test cases by creating a new engine for each test. This is much | ||
* slower since it runs the startup and shutdown process for all tests. You should use {@link MTEExtension} unless | ||
* you're certain that you need to use this class. | ||
* <p> | ||
* Use this within {@link org.junit.jupiter.api.extension.ExtendWith} | ||
*/ | ||
public class IsolatedMTEExtension extends MTEExtension implements BeforeAllCallback, AfterAllCallback, | ||
AfterEachCallback, ParameterResolver, TestInstancePostProcessor { | ||
@Override | ||
public void afterAll(ExtensionContext context) throws Exception { | ||
// don't call super | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
// don't call super | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
super.afterAll(context); | ||
} | ||
|
||
@Override | ||
public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception { | ||
// beforeEach would be run after postProcess so postProcess would NPE, so we initialize the MTE here beforehand | ||
super.beforeAll(extensionContext); | ||
super.postProcessTestInstance(testInstance, extensionContext); | ||
public class IsolatedMTEExtension extends MTEExtension { | ||
{ | ||
// Resources are not shared between namespaces. We increase isolation by using a different | ||
// namespace for every test method. | ||
helperLifecycle = Scopes.PER_METHOD; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/terasology/moduletestingenvironment/Scopes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.moduletestingenvironment; | ||
|
||
import com.google.common.collect.ObjectArrays; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import java.util.function.Function; | ||
|
||
public final class Scopes { | ||
|
||
/** One per top-level test class. */ | ||
public static final Function<ExtensionContext, ExtensionContext.Namespace> PER_CLASS = context -> mteNamespace(getTopTestClass(context)); | ||
|
||
/** One per test method. */ | ||
public static final Function<ExtensionContext, ExtensionContext.Namespace> PER_METHOD = context -> mteNamespace(context.getTestMethod()); | ||
|
||
private Scopes() { }; | ||
|
||
static ExtensionContext.Namespace mteNamespace(Object... parts) { | ||
// Start with this Extension, so it's clear where this came from. | ||
return ExtensionContext.Namespace.create(ObjectArrays.concat(MTEExtension.class, parts)); | ||
} | ||
|
||
/** | ||
* The outermost class defining this test. | ||
* <p> | ||
* For <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested">nested tests</a>, this | ||
* returns the outermost class in which this test is nested. | ||
* <p> | ||
* Most tests are not nested, in which case this returns the class defining the test. | ||
* | ||
* @param context for the current test | ||
* @return a test class | ||
*/ | ||
public static Class<?> getTopTestClass(ExtensionContext context) { | ||
Class<?> testClass = context.getRequiredTestClass(); | ||
return testClass.isAnnotationPresent(Nested.class) ? testClass.getEnclosingClass() : testClass; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.