From f2621c299c7bf465ce60a97acc2794eee1ed5cae Mon Sep 17 00:00:00 2001 From: Graeme Rocher Date: Thu, 20 Apr 2023 06:37:16 +0200 Subject: [PATCH] Add GraalVM native testing and fix metadata (#307) --- buildSrc/build.gradle | 8 ++++ buildSrc/settings.gradle | 8 ++++ ...onaut.build.internal.cassandra-base.gradle | 4 ++ ...aut.build.internal.cassandra-module.gradle | 5 +-- ...ild.internal.cassandra-native-tests.gradle | 31 ++++++++++++++ .../cassandra/native-image.properties | 3 +- gradle/libs.versions.toml | 4 +- settings.gradle | 1 + test-suite-graal/build.gradle | 40 +++++++++++++++++++ .../src/main/java/example/CassandraInfo.java | 22 ++++++++++ .../java/example/CassandraRepository.java | 32 +++++++++++++++ .../src/main/resources/application.yml | 17 ++++++++ .../src/test/java/example/CassandraTest.java | 16 ++++++++ .../src/test/resources/logback.xml | 16 ++++++++ 14 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 buildSrc/settings.gradle create mode 100644 buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-base.gradle create mode 100644 buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-native-tests.gradle create mode 100644 test-suite-graal/build.gradle create mode 100644 test-suite-graal/src/main/java/example/CassandraInfo.java create mode 100644 test-suite-graal/src/main/java/example/CassandraRepository.java create mode 100644 test-suite-graal/src/main/resources/application.yml create mode 100644 test-suite-graal/src/test/java/example/CassandraTest.java create mode 100644 test-suite-graal/src/test/resources/logback.xml diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 67840524..f5e54d44 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -1,3 +1,11 @@ plugins { id 'groovy-gradle-plugin' } +repositories { + gradlePluginPortal() + mavenCentral() +} + +dependencies { + implementation libs.gradle.micronaut +} diff --git a/buildSrc/settings.gradle b/buildSrc/settings.gradle new file mode 100644 index 00000000..7efa3a0d --- /dev/null +++ b/buildSrc/settings.gradle @@ -0,0 +1,8 @@ +rootProject.name = 'cassandra-parent' +dependencyResolutionManagement { + versionCatalogs { + libs { + from(files("../gradle/libs.versions.toml")) + } + } +} diff --git a/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-base.gradle b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-base.gradle new file mode 100644 index 00000000..29ad2bbf --- /dev/null +++ b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-base.gradle @@ -0,0 +1,4 @@ +repositories { + mavenCentral() + maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" } +} diff --git a/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-module.gradle b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-module.gradle index beae5c47..fcc9d9f6 100644 --- a/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-module.gradle +++ b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-module.gradle @@ -1,8 +1,5 @@ plugins { id "io.micronaut.build.internal.module" + id "io.micronaut.build.internal.cassandra-base" } -repositories { - mavenCentral() - maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/" } -} diff --git a/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-native-tests.gradle b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-native-tests.gradle new file mode 100644 index 00000000..09f2ff39 --- /dev/null +++ b/buildSrc/src/main/groovy/io.micronaut.build.internal.cassandra-native-tests.gradle @@ -0,0 +1,31 @@ +plugins { + id "io.micronaut.build.internal.cassandra-base" + id "io.micronaut.application" + id "io.micronaut.test-resources" + id 'org.graalvm.buildtools.native' +} + +tasks.named("check") { task -> + def graal = ["jvmci.Compiler", "java.vendor.version", "java.vendor"].any { + System.getProperty(it)?.toLowerCase(Locale.ENGLISH)?.contains("graal") + } + if (graal) { + task.dependsOn("nativeTest") + } +} + +tasks.named("test") { + useJUnitPlatform() +} + +graalvmNative { + toolchainDetection = false + metadataRepository { + enabled = true + } + binaries { + all { + resources.autodetect() + } + } +} diff --git a/cassandra/src/main/resources/META-INF/native-image/io.micronaut.cassandra/cassandra/native-image.properties b/cassandra/src/main/resources/META-INF/native-image/io.micronaut.cassandra/cassandra/native-image.properties index 14d39af7..3f325789 100644 --- a/cassandra/src/main/resources/META-INF/native-image/io.micronaut.cassandra/cassandra/native-image.properties +++ b/cassandra/src/main/resources/META-INF/native-image/io.micronaut.cassandra/cassandra/native-image.properties @@ -14,5 +14,4 @@ # limitations under the License. # -Args = --initialize-at-run-time=io.micronaut.cassandra.health.$CassandraHealthIndicatorDefinition \ - --initialize-at-build-time=com.datastax.oss.driver.internal.core.util.Reflection,com.datastax.oss.driver.internal.core.util.DependencyCheck,com.datastax.oss.driver.internal.core.util.Dependency +Args = --initialize-at-build-time=com.datastax.oss.driver.internal.core.util.DependencyCheck,com.datastax.oss.driver.internal.core.util.Dependency diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 94978aad..57d49ba2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,10 +2,11 @@ managed-datastax-cassandra-driver = "4.15.0" micronaut = "4.0.0-M1" micronaut-docs = "2.0.0" -micronaut-test = "4.0.0-SNAPSHOT" +micronaut-test = "4.0.0-M1" groovy = "4.0.10" spock = "2.3-groovy-4.0" testcontainers = "1.18.0" +micronaut-gradle-plugin = "4.0.0-M1" [libraries] managed-datastax-cassandra-driver-core = { module = "com.datastax.oss:java-driver-core", version.ref = "managed-datastax-cassandra-driver" } @@ -13,3 +14,4 @@ managed-datastax-cassandra-driver-mapper-processor = { module = "com.datastax.os testcontainers-cassandra = { module = "org.testcontainers:cassandra", version.ref = "testcontainers" } testcontainers-spock = { module = "org.testcontainers:spock", version.ref = "testcontainers" } +gradle-micronaut = { module = "io.micronaut.gradle:micronaut-gradle-plugin", version.ref = "micronaut-gradle-plugin" } \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 0a67e073..b68c0219 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,6 +12,7 @@ plugins { rootProject.name = 'cassandra-parent' include 'cassandra' include 'cassandra-bom' +include 'test-suite-graal' micronautBuild { addSnapshotRepository() diff --git a/test-suite-graal/build.gradle b/test-suite-graal/build.gradle new file mode 100644 index 00000000..916209c7 --- /dev/null +++ b/test-suite-graal/build.gradle @@ -0,0 +1,40 @@ +plugins { + id "io.micronaut.application" + id "io.micronaut.build.internal.cassandra-native-tests" +} + +mainClassName = "helloworld.Application" +micronaut { + version "4.0.0-M1" + testRuntime "junit5" + enableNativeImage false + processing { + incremental(true) + annotations("helloworld.*") + } + testResources { + clientTimeout = 600 + } +} + +configurations.all { + resolutionStrategy.dependencySubstitution { + substitute module('io.micronaut.cassandra:micronaut-cassandra') using project(':micronaut-cassandra') + } +} + +dependencies { + implementation mn.micronaut.context + implementation mn.micronaut.jackson.databind + implementation project(':micronaut-cassandra') + implementation mn.snakeyaml + runtimeOnly mn.logback.classic +} + +graalvmNative { + binaries { + all { + buildArgs.add("--trace-class-initialization=com.datastax.oss.driver.internal.core.util.Dependency") + } + } +} diff --git a/test-suite-graal/src/main/java/example/CassandraInfo.java b/test-suite-graal/src/main/java/example/CassandraInfo.java new file mode 100644 index 00000000..65725aec --- /dev/null +++ b/test-suite-graal/src/main/java/example/CassandraInfo.java @@ -0,0 +1,22 @@ +package example; + +import io.micronaut.core.annotation.Introspected; + +@Introspected +public class CassandraInfo { + private final String clusterName; + private final String releaseVersion; + + public CassandraInfo(String clusterName, String releaseVersion) { + this.clusterName = clusterName; + this.releaseVersion = releaseVersion; + } + + public String getClusterName() { + return clusterName; + } + + public String getReleaseVersion() { + return releaseVersion; + } +} \ No newline at end of file diff --git a/test-suite-graal/src/main/java/example/CassandraRepository.java b/test-suite-graal/src/main/java/example/CassandraRepository.java new file mode 100644 index 00000000..68fc2e9d --- /dev/null +++ b/test-suite-graal/src/main/java/example/CassandraRepository.java @@ -0,0 +1,32 @@ +package example; + +import com.datastax.oss.driver.api.core.CqlSession; +import com.datastax.oss.driver.api.core.cql.ResultSet; +import com.datastax.oss.driver.api.core.cql.Row; +import jakarta.inject.Singleton; + +import java.util.Optional; + +@Singleton +public class CassandraRepository { + private final CqlSession cqlSession; + + public CassandraRepository(CqlSession cqlSession) { + this.cqlSession = cqlSession; + } + + public Optional getInfo() { + ResultSet resultSet = cqlSession.execute("select cluster_name, release_version from system.local"); + Row row = resultSet.one(); + + if (row != null) { + return Optional.of( + new CassandraInfo( + row.getString("cluster_name"), + row.getString("release_version") + ) + ); + } + return Optional.empty(); + } +} \ No newline at end of file diff --git a/test-suite-graal/src/main/resources/application.yml b/test-suite-graal/src/main/resources/application.yml new file mode 100644 index 00000000..9b267caf --- /dev/null +++ b/test-suite-graal/src/main/resources/application.yml @@ -0,0 +1,17 @@ +micronaut: + application: + name: cassandra +cassandra: + default: + basic: + contact-points: + - "127.0.0.1:${cassandra.port}" + load-balancing-policy: + local-datacenter: datacenter1 +test-resources: + containers: + cassandra: + startup-timeout: 600s + image-name: cassandra + exposed-ports: + - cassandra.port: 9042 diff --git a/test-suite-graal/src/test/java/example/CassandraTest.java b/test-suite-graal/src/test/java/example/CassandraTest.java new file mode 100644 index 00000000..88984d0c --- /dev/null +++ b/test-suite-graal/src/test/java/example/CassandraTest.java @@ -0,0 +1,16 @@ +package example; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.Test; + +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; + +@MicronautTest +class CassandraTest { + + @Test + void testCassandra(CassandraRepository repository) { + assertTrue(repository.getInfo().isPresent()); + } + +} \ No newline at end of file diff --git a/test-suite-graal/src/test/resources/logback.xml b/test-suite-graal/src/test/resources/logback.xml new file mode 100644 index 00000000..677b8faa --- /dev/null +++ b/test-suite-graal/src/test/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + + %cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n + + + + + + + + +