From 4431515c7ff974b7eab8aeb09c6e80f4007c8115 Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 24 Oct 2023 21:17:01 +1300 Subject: [PATCH] Add native-image support and test --- .github/workflows/native-image.yml | 39 ++++++++++ test-native-image/pom.xml | 78 +++++++++++++++++++ .../src/main/java/org/example/Customer.java | 27 +++++++ .../src/main/java/org/example/Main.java | 32 ++++++++ .../test/java/org/example/CustomerTest.java | 22 ++++++ .../resource-config.json | 7 ++ 6 files changed, 205 insertions(+) create mode 100644 .github/workflows/native-image.yml create mode 100644 test-native-image/pom.xml create mode 100644 test-native-image/src/main/java/org/example/Customer.java create mode 100644 test-native-image/src/main/java/org/example/Main.java create mode 100644 test-native-image/src/test/java/org/example/CustomerTest.java create mode 100644 validator/src/main/resources/META-INF/native-image/io.avaje.validator.avaje-validator/resource-config.json diff --git a/.github/workflows/native-image.yml b/.github/workflows/native-image.yml new file mode 100644 index 00000000..6d8dcae3 --- /dev/null +++ b/.github/workflows/native-image.yml @@ -0,0 +1,39 @@ +name: native image build + +on: + workflow_dispatch: + schedule: + - cron: '40 1 1 1 6' + +jobs: + build: + runs-on: ${{ matrix.os }} + permissions: + contents: read + packages: write + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest] + + steps: + - uses: actions/checkout@v3 + - uses: graalvm/setup-graalvm@v1 + with: + java-version: '21' + distribution: 'graalvm' + cache: 'maven' + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: Versions + run: | + echo "GRAALVM_HOME: $GRAALVM_HOME" + echo "JAVA_HOME: $JAVA_HOME" + java --version + native-image --version + - name: Build with Maven + run: | + mvn clean install -DskipTests + cd test-native-image + mvn clean package -Pnative + ./target/test-native-image diff --git a/test-native-image/pom.xml b/test-native-image/pom.xml new file mode 100644 index 00000000..14765a5a --- /dev/null +++ b/test-native-image/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + io.avaje + avaje-validator-parent + 1.2-SNAPSHOT + + + org.example + test-native-image + + + 21 + UTF-8 + 0.9.27 + org.example.Main + + + + + io.avaje + avaje-validator + 1.2-SNAPSHOT + + + io.avaje + validator-constraints + 1.2-SNAPSHOT + + + io.avaje + avaje-validator-generator + ${project.version} + provided + + + io.avaje + junit + 1.1 + test + + + + + + + native + + + + org.graalvm.buildtools + native-maven-plugin + ${version.plugin.nativeimage} + + + build-native + + build + + package + + + --no-fallback + -H:IncludeLocales=de,en + + + + + + + + + + + diff --git a/test-native-image/src/main/java/org/example/Customer.java b/test-native-image/src/main/java/org/example/Customer.java new file mode 100644 index 00000000..ad63ee2c --- /dev/null +++ b/test-native-image/src/main/java/org/example/Customer.java @@ -0,0 +1,27 @@ +package org.example; + + +import io.avaje.validation.constraints.NotBlank; +import io.avaje.validation.constraints.Valid; + +@Valid +public class Customer { + + @NotBlank + final String name; + @NotBlank + final String email; + + public Customer(String name, String email) { + this.name = name; + this.email = email; + } + + public String name() { + return name; + } + + public String email() { + return email; + } +} diff --git a/test-native-image/src/main/java/org/example/Main.java b/test-native-image/src/main/java/org/example/Main.java new file mode 100644 index 00000000..95bc9c2d --- /dev/null +++ b/test-native-image/src/main/java/org/example/Main.java @@ -0,0 +1,32 @@ +package org.example; + +import io.avaje.validation.ConstraintViolation; +import io.avaje.validation.Validator; + +import java.util.ArrayList; +import java.util.Locale; +import java.util.Set; + +public class Main { + + public static void main(String[] args) { + Validator validator = Validator.builder() + .addLocales(Locale.GERMAN) + .build(); + + var customer = new Customer("hello", ""); + System.out.println("violations EN - " + validator.check(customer)); + System.out.println("violations DE - " + validator.check(customer, Locale.GERMAN)); + + if (!"must not be blank".equals(first(validator.check(customer)).message())) { + System.exit(1); + } + if (!"darf nicht leer sein".equals(first(validator.check(customer, Locale.GERMAN)).message())) { + System.exit(1); + } + } + + private static ConstraintViolation first(Set check) { + return new ArrayList<>(check).get(0); + } +} diff --git a/test-native-image/src/test/java/org/example/CustomerTest.java b/test-native-image/src/test/java/org/example/CustomerTest.java new file mode 100644 index 00000000..80f5f4de --- /dev/null +++ b/test-native-image/src/test/java/org/example/CustomerTest.java @@ -0,0 +1,22 @@ +package org.example; + +import io.avaje.validation.ConstraintViolation; +import io.avaje.validation.Validator; +import org.junit.jupiter.api.Test; + +import java.util.Locale; +import java.util.Set; + +class CustomerTest { + + @Test + void test() { + Validator validator = Validator.builder() + .addLocales(Locale.GERMAN) + .build(); + + var customer = new Customer("hello", ""); + System.out.println("violations EN - " + validator.check(customer)); + System.out.println("violations DE - " + validator.check(customer, Locale.GERMAN)); + } +} diff --git a/validator/src/main/resources/META-INF/native-image/io.avaje.validator.avaje-validator/resource-config.json b/validator/src/main/resources/META-INF/native-image/io.avaje.validator.avaje-validator/resource-config.json new file mode 100644 index 00000000..a105e19b --- /dev/null +++ b/validator/src/main/resources/META-INF/native-image/io.avaje.validator.avaje-validator/resource-config.json @@ -0,0 +1,7 @@ +{ + "bundles": [ + { + "name": "io.avaje.validation.Messages" + } + ] +}