Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Dec 15, 2023
1 parent 5300ad8 commit 507d426
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions junit/src/main/java/net/neoforged/fml/junit/JUnitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.LauncherSessionListener;

/**
* A session listener for JUnit environments that will bootstrap a Minecraft (FML) environment.
*/
public class JUnitService implements LauncherSessionListener {
private ClassLoader oldLoader;
public JUnitService() {
Expand All @@ -16,12 +19,16 @@ public JUnitService() {

@Override
public void launcherSessionOpened(LauncherSession session) {
// When the tests are started we want to make sure that they run on the transforming class loader which is set up by
// bootstrapping BSL which will then load the launch target
oldLoader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(LaunchWrapper.getTransformingLoader());
}

@Override
public void launcherSessionClosed(LauncherSession session) {
// Reset the loader in case JUnit wants to execute some pre-shutdown commands
// and our custom class loader might throw it off
Thread.currentThread().setContextClassLoader(oldLoader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
public class LaunchWrapper {
private static ClassLoader transformingCL;

/**
* Lazily get the transforming module class loader used by the game at runtime by loading the game when needed.
* <p>
* Since in a JUnit environment we can't easily get the launch arguments, we will load them from the file specified via the {@code fml.junit.argsfile} system property.
*/
public static synchronized ClassLoader getTransformingLoader() {
if (transformingCL != null) return transformingCL;
final var oldLoader = Thread.currentThread().getContextClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import net.neoforged.api.distmarker.Dist;

/**
* A launch target for bootstrapping a slim Minecraft environment in forgedev, to be used in JUnit tests.
*/
public class JUnitDevLaunchTarget extends CommonDevLaunchHandler {
@Override
public Dist getDist() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import net.neoforged.api.distmarker.Dist;

/**
* A launch target for bootstrapping a slim Minecraft environment in userdev, to be used in JUnit tests.
*/
public class JUnitUserDevLaunchTarget extends ForgeUserdevLaunchHandler {
@Override
public Dist getDist() {
Expand Down

0 comments on commit 507d426

Please sign in to comment.