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
This commit is contained in:
Stéphane Nicoll
2023-12-11 10:59:55 +01:00
parent a43bd8b6f0
commit 29a7b01f75
8 changed files with 153 additions and 0 deletions

View File

@@ -0,0 +1 @@
Tests if @Resource is applied properly

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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