From 68acf0b0f51691102b25c557b8d8342719642a85 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 21 Oct 2024 18:16:56 +0200 Subject: [PATCH] call prerequisites, needs cleanup --- .../office/usecases/HsOfficeUseCasesTest.java | 2 ++ .../usecases/MethodReferenceExample.java | 34 +++++++++++++++++++ .../hs/office/usecases/Produces.java | 14 ++++++++ .../hs/office/usecases/Requires.java | 13 +++++++ .../hsadminng/hs/office/usecases/UseCase.java | 2 ++ .../hs/office/usecases/UseCaseTest.java | 24 ++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/usecases/MethodReferenceExample.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Produces.java create mode 100644 src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Requires.java diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/HsOfficeUseCasesTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/HsOfficeUseCasesTest.java index 97b76dc3..854e16ce 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/HsOfficeUseCasesTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/HsOfficeUseCasesTest.java @@ -25,6 +25,7 @@ class HsOfficeUseCasesTest extends UseCaseTest { @Test @Order(1010) + @Produces("Partner: Test AG") void shouldCreatePartner() { new CreatePartner(this, "Partner: Test AG") .given("partnerNumber", 30001) @@ -46,6 +47,7 @@ class HsOfficeUseCasesTest extends UseCaseTest { @Test @Order(2010) + @Requires("Partner: Test AG") void shouldCreateSelfDebitorForPartner() { new CreateSelfDebitorForPartner(this, "Debitor: Test AG - main debitor") .given("partnerPersonUuid", "%{Person: Test AG}") diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/MethodReferenceExample.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/MethodReferenceExample.java new file mode 100644 index 00000000..baafe9c2 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/MethodReferenceExample.java @@ -0,0 +1,34 @@ +package net.hostsharing.hsadminng.hs.office.usecases; + +import java.lang.invoke.SerializedLambda; +import java.lang.reflect.Method; +import java.util.function.Consumer; + +public class MethodReferenceExample { + + public static void myMethod(String input) { + System.out.println("Hello from myMethod, input: " + input); + } + + public static void main(String[] args) { + // Create a method reference + Consumer methodRef = MethodReferenceExample::myMethod; + + // Use reflection to retrieve method reference info + try { + // Step 1: Get the method 'writeReplace' via reflection + Method writeReplace = methodRef.getClass().getDeclaredMethod("writeReplace"); + writeReplace.setAccessible(true); + + // Step 2: Invoke 'writeReplace' to get a SerializedLambda + SerializedLambda serializedLambda = (SerializedLambda) writeReplace.invoke(methodRef); + + // Step 3: Get the method name using the SerializedLambda + String methodName = serializedLambda.getImplMethodName(); + + System.out.println("Original method name: " + methodName); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Produces.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Produces.java new file mode 100644 index 00000000..ba148cc7 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Produces.java @@ -0,0 +1,14 @@ +package net.hostsharing.hsadminng.hs.office.usecases; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.CLASS; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(METHOD) +@Retention(RUNTIME) +public @interface Produces { + String value(); +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Requires.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Requires.java new file mode 100644 index 00000000..0ca11334 --- /dev/null +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/Requires.java @@ -0,0 +1,13 @@ +package net.hostsharing.hsadminng.hs.office.usecases; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target(METHOD) +@Retention(RUNTIME) +public @interface Requires { + String value(); +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCase.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCase.java index 607cfcdf..d5b329d9 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCase.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCase.java @@ -60,6 +60,8 @@ public abstract class UseCase> { givenProperties.forEach((key, value) -> log("| " + key + " | " + value + " |")); log(""); requirements.forEach((alias, factory) -> factory.apply(alias).run().keep()); + + // final var testMethodProduct = testInfo.getTestMethod().map(m -> m.getAnnotation(Produces.class).value()).orElseThrow(); return run(); } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCaseTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCaseTest.java index 7f4541ac..f0283dce 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCaseTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/usecases/UseCaseTest.java @@ -16,13 +16,16 @@ import org.springframework.boot.test.web.server.LocalServerPort; import java.io.FileWriter; import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.UUID; import java.util.stream.Collectors; +import static java.util.Arrays.stream; import static org.assertj.core.api.Assertions.assertThat; public abstract class UseCaseTest extends ContextBasedTest { @@ -67,13 +70,30 @@ public abstract class UseCaseTest extends ContextBasedTest { } ); + callPrerequisites(testInfo); + final var testMethodName = testInfo.getTestMethod().map(Method::getName).orElseThrow(); final var testMethodOrder = testInfo.getTestMethod().map(m -> m.getAnnotation(Order.class).value()).orElseThrow(); markdownFile = new PrintWriter(new FileWriter( testMethodOrder + "-" + testMethodName + ".md")); log("## Testcase " + testMethodName.replaceAll("([a-z])([A-Z]+)", "$1 $2")); + currentTestInfo = testInfo; // FIXME: remove? } + private void callPrerequisites(final TestInfo testInfo) throws IllegalAccessException, InvocationTargetException { + final var testMethodRequired = testInfo.getTestMethod().map(m -> m.getAnnotation(Requires.class).value()).orElse(null); + if (testMethodRequired != null) { + for (Method testMethod : getClass().getDeclaredMethods()) { + final var producesAnnot = testMethod.getAnnotation(Produces.class); + if (producesAnnot != null) { + final var testMethodProduct = producesAnnot.value(); + if (testMethodProduct.equals(testMethodRequired)) + testMethod.invoke(this); + } + } + } + } + @AfterEach void cleanup() { properties.clear(); @@ -82,7 +102,9 @@ public abstract class UseCaseTest extends ContextBasedTest { @SneakyThrows void log(final String output) { - markdownFile.println(output); + if (markdownFile != null) { + markdownFile.println(output); + } } static boolean containsAlias(final String alias) {