call prerequisites, needs cleanup

This commit is contained in:
Michael Hoennig 2024-10-21 18:16:56 +02:00
parent a643b86295
commit 68acf0b0f5
6 changed files with 88 additions and 1 deletions

View File

@ -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}")

View File

@ -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<String> 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();
}
}
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -60,6 +60,8 @@ public abstract class UseCase<T extends 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();
}

View File

@ -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,8 +102,10 @@ public abstract class UseCaseTest extends ContextBasedTest {
@SneakyThrows
void log(final String output) {
if (markdownFile != null) {
markdownFile.println(output);
}
}
static boolean containsAlias(final String alias) {
return aliases.containsKey(alias);