Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Dec 15, 2023
1 parent 1f72e56 commit 0846f6a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
5 changes: 3 additions & 2 deletions junit/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
plugins {
id 'java'
id 'java-library'
}

dependencies {
implementation('org.junit.platform:junit-platform-launcher:1.9.3')
implementation('cpw.mods:bootstraplauncher:1.1.8-test')
// BSL should not be exposed and the actual version should be provided by the neo dep
compileOnly("cpw.mods:bootstraplauncher:${project.bootstraplauncher_version}")
}
5 changes: 5 additions & 0 deletions junit/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

import net.neoforged.fml.junit.JUnitService;
import org.junit.platform.launcher.LauncherSessionListener;

Expand Down
19 changes: 7 additions & 12 deletions junit/src/main/java/net/neoforged/fml/junit/JUnitService.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.junit;

import cpw.mods.bootstraplauncher.BootstrapLauncher;
import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.LauncherSessionListener;

import java.nio.file.Files;
import java.nio.file.Path;

public class JUnitService implements LauncherSessionListener {
private ClassLoader oldLoader;
public JUnitService() {

}

@Override
public void launcherSessionOpened(LauncherSession session) {
oldLoader = Thread.currentThread().getContextClassLoader();

try {
final String[] args = Files.readAllLines(Path.of(System.getProperty("fml.junit.argsfile", "mainargs.txt"))).toArray(String[]::new);
BootstrapLauncher.main(args);
} catch (Exception exception) {
System.err.println("Failed to start Minecraft: " + exception);
throw new RuntimeException(exception);
}
Thread.currentThread().setContextClassLoader(LaunchWrapper.getTransformingLoader());
}

@Override
Expand Down
34 changes: 34 additions & 0 deletions junit/src/main/java/net/neoforged/fml/junit/LaunchWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.junit;

import cpw.mods.bootstraplauncher.BootstrapLauncher;

import java.nio.file.Files;
import java.nio.file.Path;

public class LaunchWrapper {
private static ClassLoader transformingCL;

public static synchronized ClassLoader getTransformingLoader() {
if (transformingCL != null) return transformingCL;
final var oldLoader = Thread.currentThread().getContextClassLoader();

try {
final String[] args = Files.readAllLines(Path.of(System.getProperty("fml.junit.argsfile", "mainargs.txt"))).toArray(String[]::new);
BootstrapLauncher.main(args);

transformingCL = Thread.currentThread().getContextClassLoader();
} catch (Exception exception) {
System.err.println("Failed to start Minecraft: " + exception);
throw new RuntimeException(exception);
} finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}

return transformingCL;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.loading.targets;

import net.neoforged.api.distmarker.Dist;

public class JUnitDevLaunchTarget extends CommonDevLaunchHandler {
@Override
public Dist getDist() {
return Dist.CLIENT;
return Dist.DEDICATED_SERVER;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.fml.loading.targets;

import net.neoforged.api.distmarker.Dist;

public class JUnitUserDevLaunchTarget extends ForgeUserdevLaunchHandler {
@Override
public Dist getDist() {
return Dist.CLIENT;
return Dist.DEDICATED_SERVER;
}

@Override
Expand Down

0 comments on commit 0846f6a

Please sign in to comment.