-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for GraalVM Native Image
- Loading branch information
1 parent
c5c1fbf
commit 8258892
Showing
8 changed files
with
731 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# graal-native-image-test | ||
|
||
This Maven module contains integration tests for using Gson in a GraalVM Native Image. | ||
|
||
Execution requires using GraalVM as JDK, and can be quite resource intensive. Native Image tests are therefore not enabled by default and the tests are only executed as regular unit tests. To run Native Image tests, make sure your `PATH` and `JAVA_HOME` environment variables point to GraalVM and then run: | ||
|
||
``` | ||
mvn clean test --activate-profiles native-image-test | ||
``` | ||
|
||
Technically it would also be possible to directly configure Native Image test execution for the `gson` module instead of having this separate Maven module. However, maintaining the reflection metadata for the unit tests would be quite cumbersome and would hinder future changes to the `gson` unit tests because many of them just happen to use reflection, without all of them being relevant for Native Image testing. | ||
|
||
## Reflection metadata | ||
|
||
Native Image creation requires configuring which class members are accessed using reflection, see the [GraalVM documentation](https://www.graalvm.org/22.3/reference-manual/native-image/metadata/#specifying-reflection-metadata-in-json). | ||
|
||
The file [`reflect-config.json`](./src/test/resources/META-INF/native-image/reflect-config.json) contains this reflection metadata. | ||
|
||
You can also run with `-Dagent=true` to let the Maven plugin automatically generate a metadata file, see the [plugin documentation](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html#agent-support-running-tests). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<!-- | ||
Copyright 2023 Google Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson-parent</artifactId> | ||
<version>2.10.2-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>graal-native-image-test</artifactId> | ||
|
||
<properties> | ||
<!-- GraalVM is JDK >= 17, however for build with regular JDK these tests | ||
are also executed with JDK 11, so for them exclude JDK 17 specific tests --> | ||
<maven.compiler.testRelease>11</maven.compiler.testRelease> | ||
<excludeTestCompilation>**/Java17*</excludeTestCompilation> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
|
||
<!-- Graal Native Maven Plugin requires using JUnit Platform (JUnit 5), see | ||
https://graalvm.github.io/native-build-tools/latest/maven-plugin.html#testing-support | ||
This also supports using JUnit Vintage to run JUnit 4 tests, but for simplicity | ||
completely use JUnit 5 here and no JUnit 4 at all --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.10.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<executions> | ||
<!-- Adjust standard `default-testCompile` execution --> | ||
<execution> | ||
<id>default-testCompile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>testCompile</goal> | ||
</goals> | ||
<configuration> | ||
<testExcludes> | ||
<exclude>${excludeTestCompilation}</exclude> | ||
</testExcludes> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<configuration> | ||
<!-- This Maven module has no 'main' source code; skip creating JAR and avoid | ||
warning on console --> | ||
<skipIfEmpty>true</skipIfEmpty> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>JDK17</id> | ||
<activation> | ||
<jdk>[17,)</jdk> | ||
</activation> | ||
<properties> | ||
<maven.compiler.testRelease>17</maven.compiler.testRelease> | ||
<excludeTestCompilation /> | ||
</properties> | ||
</profile> | ||
|
||
<profile> | ||
<id>native-image-test</id> | ||
<activation> | ||
<activeByDefault>false</activeByDefault> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.graalvm.buildtools</groupId> | ||
<artifactId>native-maven-plugin</artifactId> | ||
<version>0.9.24</version> | ||
<extensions>true</extensions> | ||
<executions> | ||
<execution> | ||
<id>test-native</id> | ||
<goals> | ||
<goal>test</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<dependencies> | ||
<!-- Workaround for https://github.com/graalvm/native-build-tools/issues/418 --> | ||
<dependency> | ||
<groupId>org.codehaus.plexus</groupId> | ||
<artifactId>plexus-utils</artifactId> | ||
<!-- Don't upgrade version to >= 4 because that seems to contain potentially backward | ||
incompatible changes --> | ||
<version>3.5.1</version> | ||
</dependency> | ||
</dependencies> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
186 changes: 186 additions & 0 deletions
186
...tive-image-test/src/test/java/com/google/gson/native_test/Java17RecordReflectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Copyright (C) 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.gson.native_test; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.annotations.JsonAdapter; | ||
import com.google.gson.annotations.SerializedName; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@SuppressWarnings("UnusedVariable") // workaround for https://github.com/google/error-prone/issues/2713 | ||
class Java17RecordReflectionTest { | ||
public record PublicRecord(int i) { | ||
} | ||
|
||
@Test | ||
void testPublicRecord() { | ||
Gson gson = new Gson(); | ||
PublicRecord r = gson.fromJson("{\"i\":1}", PublicRecord.class); | ||
assertThat(r.i).isEqualTo(1); | ||
} | ||
|
||
// Private record has implicit private canonical constructor | ||
private record PrivateRecord(int i) { | ||
} | ||
|
||
@Test | ||
void testPrivateRecord() { | ||
Gson gson = new Gson(); | ||
PrivateRecord r = gson.fromJson("{\"i\":1}", PrivateRecord.class); | ||
assertThat(r.i).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void testLocalRecord() { | ||
record LocalRecordDeserialization(int i) { | ||
} | ||
|
||
Gson gson = new Gson(); | ||
LocalRecordDeserialization r = gson.fromJson("{\"i\":1}", LocalRecordDeserialization.class); | ||
assertThat(r.i).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void testLocalRecordSerialization() { | ||
record LocalRecordSerialization(int i) { | ||
} | ||
|
||
Gson gson = new Gson(); | ||
assertThat(gson.toJson(new LocalRecordSerialization(1))).isEqualTo("{\"i\":1}"); | ||
} | ||
|
||
private record RecordWithSerializedName(@SerializedName("custom-name") int i) { | ||
} | ||
|
||
@Test | ||
void testSerializedName() { | ||
Gson gson = new Gson(); | ||
RecordWithSerializedName r = gson.fromJson("{\"custom-name\":1}", RecordWithSerializedName.class); | ||
assertThat(r.i).isEqualTo(1); | ||
|
||
assertThat(gson.toJson(new RecordWithSerializedName(2))).isEqualTo("{\"custom-name\":2}"); | ||
} | ||
|
||
private record RecordWithCustomConstructor(int i) { | ||
@SuppressWarnings("unused") | ||
RecordWithCustomConstructor { | ||
i += 5; | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomConstructor() { | ||
Gson gson = new Gson(); | ||
RecordWithCustomConstructor r = gson.fromJson("{\"i\":1}", RecordWithCustomConstructor.class); | ||
assertThat(r.i).isEqualTo(6); | ||
} | ||
|
||
private record RecordWithCustomAccessor(int i) { | ||
@SuppressWarnings("UnusedMethod") | ||
@Override | ||
public int i() { | ||
return i + 5; | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomAccessor() { | ||
Gson gson = new Gson(); | ||
assertThat(gson.toJson(new RecordWithCustomAccessor(2))).isEqualTo("{\"i\":7}"); | ||
} | ||
|
||
@JsonAdapter(RecordWithCustomClassAdapter.CustomAdapter.class) | ||
private record RecordWithCustomClassAdapter(int i) { | ||
private static class CustomAdapter extends TypeAdapter<RecordWithCustomClassAdapter> { | ||
@Override | ||
public RecordWithCustomClassAdapter read(JsonReader in) throws IOException { | ||
return new RecordWithCustomClassAdapter(in.nextInt() + 5); | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, RecordWithCustomClassAdapter value) throws IOException { | ||
out.value(value.i + 6); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomClassAdapter() { | ||
Gson gson = new Gson(); | ||
RecordWithCustomClassAdapter r = gson.fromJson("1", RecordWithCustomClassAdapter.class); | ||
assertThat(r.i).isEqualTo(6); | ||
|
||
assertThat(gson.toJson(new RecordWithCustomClassAdapter(1))).isEqualTo("7"); | ||
} | ||
|
||
private record RecordWithCustomFieldAdapter( | ||
@JsonAdapter(RecordWithCustomFieldAdapter.CustomAdapter.class) | ||
int i | ||
) { | ||
private static class CustomAdapter extends TypeAdapter<Integer> { | ||
@Override | ||
public Integer read(JsonReader in) throws IOException { | ||
return in.nextInt() + 5; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Integer value) throws IOException { | ||
out.value(value + 6); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
void testCustomFieldAdapter() { | ||
Gson gson = new Gson(); | ||
RecordWithCustomFieldAdapter r = gson.fromJson("{\"i\":1}", RecordWithCustomFieldAdapter.class); | ||
assertThat(r.i).isEqualTo(6); | ||
|
||
assertThat(gson.toJson(new RecordWithCustomFieldAdapter(1))).isEqualTo("{\"i\":7}"); | ||
} | ||
|
||
private record RecordWithRegisteredAdapter(int i) { | ||
} | ||
|
||
@Test | ||
void testCustomAdapter() { | ||
Gson gson = new GsonBuilder() | ||
.registerTypeAdapter(RecordWithRegisteredAdapter.class, new TypeAdapter<RecordWithRegisteredAdapter>() { | ||
@Override | ||
public RecordWithRegisteredAdapter read(JsonReader in) throws IOException { | ||
return new RecordWithRegisteredAdapter(in.nextInt() + 5); | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, RecordWithRegisteredAdapter value) throws IOException { | ||
out.value(value.i + 6); | ||
} | ||
}) | ||
.create(); | ||
|
||
RecordWithRegisteredAdapter r = gson.fromJson("1", RecordWithRegisteredAdapter.class); | ||
assertThat(r.i).isEqualTo(6); | ||
|
||
assertThat(gson.toJson(new RecordWithRegisteredAdapter(1))).isEqualTo("7"); | ||
} | ||
} |
Oops, something went wrong.