|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.common.test; |
| 26 | + |
| 27 | +import net.minecraft.server.MinecraftServer; |
| 28 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 29 | +import org.junit.jupiter.api.extension.DynamicTestInvocationContext; |
| 30 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 31 | +import org.junit.jupiter.api.extension.InvocationInterceptor; |
| 32 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; |
| 33 | +import org.spongepowered.api.Sponge; |
| 34 | +import org.spongepowered.common.event.tracking.PhaseContext; |
| 35 | +import org.spongepowered.common.event.tracking.PhaseTracker; |
| 36 | +import org.spongepowered.common.event.tracking.phase.tick.TickPhase; |
| 37 | + |
| 38 | +import java.lang.reflect.Constructor; |
| 39 | +import java.lang.reflect.Method; |
| 40 | + |
| 41 | +public final class TestInvocationInterceptor implements InvocationInterceptor { |
| 42 | + |
| 43 | + @Override |
| 44 | + public <T> T interceptTestClassConstructor(final Invocation<T> invocation, final ReflectiveInvocationContext<Constructor<T>> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 45 | + return this.intercept(invocation); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void interceptBeforeAllMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 50 | + this.intercept(invocation); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void interceptBeforeEachMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 55 | + this.intercept(invocation); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void interceptTestMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 60 | + this.intercept(invocation); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public <T> T interceptTestFactoryMethod(final Invocation<T> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 65 | + return this.intercept(invocation); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void interceptTestTemplateMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 70 | + this.intercept(invocation); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void interceptDynamicTest(final Invocation<Void> invocation, final DynamicTestInvocationContext invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 75 | + this.intercept(invocation); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void interceptAfterAllMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 80 | + this.intercept(invocation); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void interceptAfterEachMethod(final Invocation<Void> invocation, final ReflectiveInvocationContext<Method> invocationContext, final ExtensionContext extensionContext) throws Throwable { |
| 85 | + this.intercept(invocation); |
| 86 | + } |
| 87 | + |
| 88 | + private <T> T intercept(final Invocation<T> invocation) throws Throwable { |
| 89 | + try ( |
| 90 | + final PhaseContext<@NonNull ?> context = TickPhase.Tick.SERVER_TICK |
| 91 | + .createPhaseContext(PhaseTracker.getServerInstanceExplicitly()) |
| 92 | + .server((MinecraftServer) Sponge.server()) |
| 93 | + ) { |
| 94 | + context.buildAndSwitch(); |
| 95 | + return invocation.proceed(); |
| 96 | + } finally { |
| 97 | + PhaseTracker.getServerInstanceExplicitly().ensureEmpty(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments