|
| 1 | +/* |
| 2 | + * Copyright 2016 FabricMC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.fabricmc.loader.impl.junit; |
| 18 | + |
| 19 | +import org.junit.platform.launcher.LauncherSession; |
| 20 | +import org.junit.platform.launcher.LauncherSessionListener; |
| 21 | + |
| 22 | +import net.fabricmc.api.EnvType; |
| 23 | +import net.fabricmc.loader.impl.launch.knot.Knot; |
| 24 | +import net.fabricmc.loader.impl.util.SystemProperties; |
| 25 | + |
| 26 | +public class FabricLoaderLauncherSessionListener implements LauncherSessionListener { |
| 27 | + static { |
| 28 | + System.setProperty(SystemProperties.DEVELOPMENT, "true"); |
| 29 | + System.setProperty(SystemProperties.UNIT_TEST, "true"); |
| 30 | + } |
| 31 | + |
| 32 | + private final Knot knot; |
| 33 | + private final ClassLoader classLoader; |
| 34 | + |
| 35 | + private ClassLoader launcherSessionClassLoader; |
| 36 | + |
| 37 | + public FabricLoaderLauncherSessionListener() { |
| 38 | + final Thread currentThread = Thread.currentThread(); |
| 39 | + final ClassLoader originalClassLoader = currentThread.getContextClassLoader(); |
| 40 | + |
| 41 | + try { |
| 42 | + knot = new Knot(EnvType.CLIENT); |
| 43 | + classLoader = knot.init(new String[]{}); |
| 44 | + } finally { |
| 45 | + // Knot.init sets the context class loader, revert it back for now. |
| 46 | + currentThread.setContextClassLoader(originalClassLoader); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void launcherSessionOpened(LauncherSession session) { |
| 52 | + final Thread currentThread = Thread.currentThread(); |
| 53 | + launcherSessionClassLoader = currentThread.getContextClassLoader(); |
| 54 | + currentThread.setContextClassLoader(classLoader); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void launcherSessionClosed(LauncherSession session) { |
| 59 | + final Thread currentThread = Thread.currentThread(); |
| 60 | + currentThread.setContextClassLoader(launcherSessionClassLoader); |
| 61 | + } |
| 62 | +} |
0 commit comments