From 9d2fb3ba09c052fe9a43a2d492fb3d48955141b6 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Tue, 14 Feb 2023 15:50:31 -0300 Subject: [PATCH] Include test case for #31153 Fix the test case for #31153 --- .../recording/BytecodeRecorderTestCase.java | 25 ++++++ .../deployment/recording/JobDetails.java | 81 +++++++++++++++++++ .../deployment/recording/JobParameter.java | 78 ++++++++++++++++++ .../deployment/recording/TestRecorder.java | 4 + 4 files changed, 188 insertions(+) create mode 100644 core/deployment/src/test/java/io/quarkus/deployment/recording/JobDetails.java create mode 100644 core/deployment/src/test/java/io/quarkus/deployment/recording/JobParameter.java diff --git a/core/deployment/src/test/java/io/quarkus/deployment/recording/BytecodeRecorderTestCase.java b/core/deployment/src/test/java/io/quarkus/deployment/recording/BytecodeRecorderTestCase.java index b4d5e01aac009e..146ba6e44e51ea 100644 --- a/core/deployment/src/test/java/io/quarkus/deployment/recording/BytecodeRecorderTestCase.java +++ b/core/deployment/src/test/java/io/quarkus/deployment/recording/BytecodeRecorderTestCase.java @@ -1,5 +1,6 @@ package io.quarkus.deployment.recording; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -137,6 +138,30 @@ public void testJavaBean() throws Exception { }, new TestJavaBeanSubclass("A string", 99, "PUT")); } + @Test + public void testJobDetails() throws Exception { + runTest(generator -> { + assertThatCode(() ->{ + generator.registerNonDefaultConstructor( + JobParameter.class.getDeclaredConstructor(String.class, String.class, Object.class), + jobParameter -> Arrays.asList( + jobParameter.getClassName(), + jobParameter.getActualClassName(), + jobParameter.getObject())); + generator.registerNonDefaultConstructor( + JobDetails.class.getDeclaredConstructor(String.class, String.class, String.class, List.class), + jobDetails -> Arrays.asList( + jobDetails.getClassName(), + jobDetails.getStaticFieldName(), + jobDetails.getMethodName(), + jobDetails.getJobParameters())); + }).doesNotThrowAnyException(); + TestRecorder recorder = generator.getRecordingProxy(TestRecorder.class); + recorder.bean(new JobDetails("A string", null, "methodName", List.of(JobParameter.JobContext))); + }, new JobDetails("A string", null, "methodName", List.of(JobParameter.JobContext))); + + } + @Test public void testValidationFails() throws Exception { Assertions.assertThrows(RuntimeException.class, () -> { diff --git a/core/deployment/src/test/java/io/quarkus/deployment/recording/JobDetails.java b/core/deployment/src/test/java/io/quarkus/deployment/recording/JobDetails.java new file mode 100644 index 00000000000000..c5007e2cc4ada8 --- /dev/null +++ b/core/deployment/src/test/java/io/quarkus/deployment/recording/JobDetails.java @@ -0,0 +1,81 @@ +package io.quarkus.deployment.recording; + +import static java.util.Collections.unmodifiableList; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class JobDetails { + + private final String className; + private final String staticFieldName; + private final String methodName; + private final ArrayList jobParameters; + private Boolean cacheable; + + private JobDetails() { + this(null, null, null, null); + // used for deserialization + } + + public JobDetails(String className, String staticFieldName, String methodName, List jobParameters) { + this.className = className; + this.staticFieldName = staticFieldName; + this.methodName = methodName; + this.jobParameters = new ArrayList<>(jobParameters); + this.cacheable = false; + } + + public String getClassName() { + return className; + } + + public String getStaticFieldName() { + return staticFieldName; + } + + public boolean hasStaticFieldName() { + return staticFieldName != null; + } + + public String getMethodName() { + return methodName; + } + + public List getJobParameters() { + return unmodifiableList(jobParameters); + } + + public Class[] getJobParameterTypes() { + return jobParameters.stream() + .map(JobParameter::getClassName) + .toArray(Class[]::new); + } + + public Object[] getJobParameterValues() { + return jobParameters.stream() + .map(JobParameter::getObject) + .toArray(); + } + + public Boolean getCacheable() { + return cacheable; + } + + public void setCacheable(boolean cacheable) { + this.cacheable = cacheable; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof JobDetails)) return false; + JobDetails that = (JobDetails) o; + return Objects.equals(className, that.className) + && Objects.equals(staticFieldName, that.staticFieldName) + && Objects.equals(methodName, that.methodName) + && Objects.equals(jobParameters, that.jobParameters) + && Objects.equals(cacheable, that.cacheable); + } +} diff --git a/core/deployment/src/test/java/io/quarkus/deployment/recording/JobParameter.java b/core/deployment/src/test/java/io/quarkus/deployment/recording/JobParameter.java new file mode 100644 index 00000000000000..22f53f97ffa4ed --- /dev/null +++ b/core/deployment/src/test/java/io/quarkus/deployment/recording/JobParameter.java @@ -0,0 +1,78 @@ +package io.quarkus.deployment.recording; + +import java.util.Objects; + +public class JobParameter { + public static final JobParameter JobContext = new JobParameter(JobParameter.class); + + private String className; + private String actualClassName; + private Object object; + + private JobParameter() { + // used for deserialization + } + + private JobParameter(Class clazz) { + this(clazz.getName(), null); + } + + public JobParameter(Class clazz, Object object) { + this(clazz.getName(), object); + } + + public JobParameter(Object object) { + this(object.getClass().getName(), object); + } + + public JobParameter(String className, Object object) { + this(className, isNotNullNorAnEnum(object) ? object.getClass().getName() : className, object); + } + + public JobParameter(String className, String actualClassName, Object object) { + this.className = className; + this.actualClassName = actualClassName; + this.object = object; + } + + /** + * Represents the class name expected by the job method (e.g. an object or an interface) + * + * @return the class name expected by the job method (e.g. an object or an interface) + */ + public String getClassName() { + return className; + } + + /** + * Represents the actual class name of the job parameter (e.g. an object), this will never be an interface + * + * @return the actual class name of the job parameter (e.g. an object), this will never be an interface + */ + public String getActualClassName() { + return actualClassName; + } + + /** + * The actual job parameter + * + * @return the actual job parameter + */ + public Object getObject() { + return object; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof JobParameter)) return false; + JobParameter that = (JobParameter) o; + return Objects.equals(className, that.className) + && Objects.equals(actualClassName, that.actualClassName) + && Objects.equals(object, that.object); + } + + protected static boolean isNotNullNorAnEnum(Object object) { + return object != null && !(object instanceof Enum); + } +} diff --git a/core/deployment/src/test/java/io/quarkus/deployment/recording/TestRecorder.java b/core/deployment/src/test/java/io/quarkus/deployment/recording/TestRecorder.java index a4238e45f4ebe7..cc89e569a87066 100644 --- a/core/deployment/src/test/java/io/quarkus/deployment/recording/TestRecorder.java +++ b/core/deployment/src/test/java/io/quarkus/deployment/recording/TestRecorder.java @@ -59,6 +59,10 @@ public void bean(TestJavaBean bean) { RESULT.add(bean); } + public void bean(JobDetails jobDetails) { + RESULT.add(jobDetails); + } + public void bean(TestJavaBeanWithBoolean bean) { RESULT.add(bean); }