From 29a7b01f7567fdc6dcfdd8e0deacac99c88abdec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Mon, 11 Dec 2023 10:59:55 +0100 Subject: [PATCH] Add smoke test for @Resource processing This commit adds a smoke test for `@Resource` handing now that is supported by Spring Framework. Note that this does not include processing of @Resource on test classes as, unfortunately, this behavior is not aligned at the moment. Closes gh-197 --- framework/resource/README.adoc | 1 + framework/resource/build.gradle | 16 +++++++++ .../resource/ResourceApplicationAotTests.java | 25 +++++++++++++ .../example/resource/ResourceApplication.java | 29 +++++++++++++++ .../resource/ResourceConfiguration.java | 24 +++++++++++++ .../java/com/example/resource/TestFields.java | 23 ++++++++++++ .../com/example/resource/TestMethods.java | 35 +++++++++++++++++++ .../src/main/resources/application.properties | 0 8 files changed, 153 insertions(+) create mode 100644 framework/resource/README.adoc create mode 100644 framework/resource/build.gradle create mode 100644 framework/resource/src/appTest/java/com/example/resource/ResourceApplicationAotTests.java create mode 100644 framework/resource/src/main/java/com/example/resource/ResourceApplication.java create mode 100644 framework/resource/src/main/java/com/example/resource/ResourceConfiguration.java create mode 100644 framework/resource/src/main/java/com/example/resource/TestFields.java create mode 100644 framework/resource/src/main/java/com/example/resource/TestMethods.java create mode 100644 framework/resource/src/main/resources/application.properties diff --git a/framework/resource/README.adoc b/framework/resource/README.adoc new file mode 100644 index 00000000..69af5995 --- /dev/null +++ b/framework/resource/README.adoc @@ -0,0 +1 @@ +Tests if @Resource is applied properly diff --git a/framework/resource/build.gradle b/framework/resource/build.gradle new file mode 100644 index 00000000..72d6b947 --- /dev/null +++ b/framework/resource/build.gradle @@ -0,0 +1,16 @@ +plugins { + id "java" + id "org.springframework.boot" + id "org.springframework.aot.smoke-test" + id "org.graalvm.buildtools.native" +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter") + + testImplementation("org.springframework.boot:spring-boot-starter-test") + + appTestImplementation(project(":aot-smoke-test-support")) + appTestImplementation("org.awaitility:awaitility:4.2.0") +} diff --git a/framework/resource/src/appTest/java/com/example/resource/ResourceApplicationAotTests.java b/framework/resource/src/appTest/java/com/example/resource/ResourceApplicationAotTests.java new file mode 100644 index 00000000..9a60779f --- /dev/null +++ b/framework/resource/src/appTest/java/com/example/resource/ResourceApplicationAotTests.java @@ -0,0 +1,25 @@ +package com.example.resource; + +import java.time.Duration; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; + +import org.springframework.aot.smoketest.support.assertj.AssertableOutput; +import org.springframework.aot.smoketest.support.junit.ApplicationTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@ApplicationTest +class ResourceApplicationAotTests { + + @Test + void expectedResourcesAreLogged(AssertableOutput output) { + Awaitility.await() + .atMost(Duration.ofSeconds(10)) + .untilAsserted( + () -> assertThat(output).hasSingleLineContaining("Methods: hello='Hello',test='Test',number=42") + .hasSingleLineContaining("Fields: hello='Hello',test='Test',number=42")); + } + +} diff --git a/framework/resource/src/main/java/com/example/resource/ResourceApplication.java b/framework/resource/src/main/java/com/example/resource/ResourceApplication.java new file mode 100644 index 00000000..e998e6fb --- /dev/null +++ b/framework/resource/src/main/java/com/example/resource/ResourceApplication.java @@ -0,0 +1,29 @@ +package com.example.resource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class ResourceApplication { + + private static final Log logger = LogFactory.getLog(ResourceApplication.class); + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(ResourceApplication.class, args); + Thread.currentThread().join(); // To be able to measure memory consumption + } + + @Bean + public ApplicationRunner run(TestMethods testMethods, TestFields testFields) { + return (args) -> { + logger.info("Methods: " + testMethods.describe()); + logger.info("Fields: " + testFields.describe()); + }; + } + +} diff --git a/framework/resource/src/main/java/com/example/resource/ResourceConfiguration.java b/framework/resource/src/main/java/com/example/resource/ResourceConfiguration.java new file mode 100644 index 00000000..3e570498 --- /dev/null +++ b/framework/resource/src/main/java/com/example/resource/ResourceConfiguration.java @@ -0,0 +1,24 @@ +package com.example.resource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +class ResourceConfiguration { + + @Bean + String hello() { + return "Hello"; + } + + @Bean + String test() { + return "Test"; + } + + @Bean + Integer counter() { + return 42; + } + +} diff --git a/framework/resource/src/main/java/com/example/resource/TestFields.java b/framework/resource/src/main/java/com/example/resource/TestFields.java new file mode 100644 index 00000000..10ac3d5b --- /dev/null +++ b/framework/resource/src/main/java/com/example/resource/TestFields.java @@ -0,0 +1,23 @@ +package com.example.resource; + +import jakarta.annotation.Resource; + +import org.springframework.stereotype.Component; + +@Component +class TestFields { + + @Resource + private String hello; + + @Resource + private String test; + + @Resource + private Integer number; + + public String describe() { + return "hello='%s',test='%s',number=%s".formatted(this.hello, this.test, this.number); + } + +} diff --git a/framework/resource/src/main/java/com/example/resource/TestMethods.java b/framework/resource/src/main/java/com/example/resource/TestMethods.java new file mode 100644 index 00000000..9417515f --- /dev/null +++ b/framework/resource/src/main/java/com/example/resource/TestMethods.java @@ -0,0 +1,35 @@ +package com.example.resource; + +import jakarta.annotation.Resource; + +import org.springframework.stereotype.Component; + +@Component +class TestMethods { + + private String hello; + + private String test; + + private Integer number; + + @Resource + public void setHello(String hello) { + this.hello = hello; + } + + @Resource + public void setTest(String test) { + this.test = test; + } + + @Resource + public void setNumber(Integer number) { + this.number = number; + } + + public String describe() { + return "hello='%s',test='%s',number=%s".formatted(this.hello, this.test, this.number); + } + +} diff --git a/framework/resource/src/main/resources/application.properties b/framework/resource/src/main/resources/application.properties new file mode 100644 index 00000000..e69de29b