Skip to content

Commit

Permalink
Add plain native image tests (#72)
Browse files Browse the repository at this point in the history
 - Make sure Graal picks up service providers by itself.
 - Use more sensical packages.
  • Loading branch information
mizosoft committed May 7, 2024
1 parent 3c15027 commit 5171fe9
Show file tree
Hide file tree
Showing 19 changed files with 370 additions and 67 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ jobs:
with:
java: GraalVM
cmd: |
# For some reason, this never works with one command either by just running `build` or
# running `quarkusBuild` & `test` on one run.
# For some reason, testing quarkus never works with one command either by just running
# `build` or running `quarkusBuild` & `test` on one run.
./gradlew :quarkus-native-test:quarkusBuild -PincludeNativeTests
./gradlew :quarkus-native-test:test -PincludeNativeTests
./gradlew :native-test:build -PincludeNativeTests
- name: Upload test results
uses: actions/upload-artifact@v4
Expand Down
11 changes: 6 additions & 5 deletions buildSrc/src/main/kotlin/extensions/ProjectExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ val Project.isIncludedInCoverageReport
project(":methanol-samples:upload-progress"),
project(":methanol-blackbox"),
project(":spring-boot-test"),
projectOrNull(":methanol-brotli:brotli-jni") // Optionally included.
projectOrNull(":methanol-brotli:brotli-jni"), // Optionally included.
projectOrNull(":quarkus-native-test"), // Optionally included.
projectOrNull(":native-image-test") // Optionally included.
)

val Project.isIncludedInAggregateJavadoc
Expand All @@ -54,7 +56,9 @@ val Project.isIncludedInAggregateJavadoc
project(":methanol-samples:upload-progress"),
project(":methanol-blackbox"),
project(":spring-boot-test"),
projectOrNull(":methanol-brotli:brotli-jni") // Optionally included.
projectOrNull(":methanol-brotli:brotli-jni"), // Optionally included.
projectOrNull(":quarkus-native-test"), // Optionally included.
projectOrNull(":native-image-test") // Optionally included.
)

val Project.artifactId
Expand All @@ -69,9 +73,6 @@ val Project.javaVersion: String?
val Project.javaVendor: String?
get() = project.findProperty("javaVendor")?.toString()

val Project.enableNativeTests: Boolean
get() = project.hasProperty("enableNativeTests")

val Project.enableErrorprone: Boolean
get() = project.hasProperty("enableErrorprone")

Expand Down
40 changes: 40 additions & 0 deletions native-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
plugins {
id("conventions.java-library")
id("conventions.static-analysis")
id("conventions.testing")
id("conventions.coverage")
id("org.graalvm.buildtools.native") version "0.10.1"
}

dependencies {
implementation(project(":methanol"))
implementation(project(":methanol-jackson"))
implementation(project(":methanol-testing"))
implementation(libs.jackson.databind)
implementation(libs.mockwebserver)
implementation(libs.autoservice.annotations)
annotationProcessor(libs.autoservice.annprocess)
}

graalvmNative {
agent {
binaries {
named("main") {
// For debuggability.
buildArgs("-H:+ReportExceptionStackTraces")
}

named("test") {
// Okhttp accesses internal GraalVM APIs.
buildArgs(
"-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.configure=ALL-UNNAMED"
)
}
}
}
}

// Always run native tests.
tasks.named("check") {
dependsOn(tasks.named("nativeTest"))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2021 Moataz Abdelnasser
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,7 +20,7 @@
* SOFTWARE.
*/

package com.github.mizosoft.methanol.springboot;
package com.github.mizosoft.methanol.nativeimage.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mizosoft.methanol.BodyAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* SOFTWARE.
*/

package com.github.mizosoft.quarkus.graal.test;
package com.github.mizosoft.methanol.nativeimage.test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mizosoft.methanol.nativeimage.test;

import static com.github.mizosoft.methanol.testing.verifiers.Verifiers.verifyThat;
import static org.assertj.core.api.Assertions.assertThat;

import com.github.mizosoft.methanol.MediaType;
import com.github.mizosoft.methanol.Methanol;
import com.github.mizosoft.methanol.MutableRequest;
import com.github.mizosoft.methanol.testing.MockWebServerExtension;
import mockwebserver3.Dispatcher;
import mockwebserver3.MockResponse;
import mockwebserver3.MockWebServer;
import mockwebserver3.RecordedRequest;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(MockWebServerExtension.class)
class PointPingPongTest {
@Test
void pointPingPong(MockWebServer server) throws Exception {
server.setDispatcher(
new Dispatcher() {
@NotNull
@Override
public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) {
var contentType = recordedRequest.getHeader("Content-Type");
assertThat(contentType).isNotNull();
return new MockResponse()
.setHeader("Content-Type", contentType)
.setBody(recordedRequest.getBody());
}
});

var client = Methanol.create();
verifyThat(
client.send(
MutableRequest.POST(
server.url("/").uri(), new Point(1, 2), MediaType.APPLICATION_JSON),
Point.class))
.hasBody(new Point(1, 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mizosoft.methanol.nativeimage.test;

import static org.assertj.core.api.Assertions.assertThat;

import com.github.mizosoft.methanol.BodyAdapter;
import com.github.mizosoft.methanol.BodyDecoder;
import org.junit.jupiter.api.Test;

class ServiceProviderLookupTest {
@Test
void bodyDecoderServiceProvidersAreAccessible() {
assertThat(BodyDecoder.Factory.installedBindings().keySet()).contains("gzip", "deflate");
}

@Test
void bodyAdapterServiceProvidersAreAccessible() {
assertThat(BodyAdapter.Encoder.installed())
.anyMatch(encoder -> encoder.getClass().equals(JacksonAdapters.JacksonEncoder.class));
assertThat(BodyAdapter.Decoder.installed())
.anyMatch(encoder -> encoder.getClass().equals(JacksonAdapters.JacksonDecoder.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"name": "com.github.mizosoft.methanol.testing.MockWebServerExtension$ManagedServers",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"name": "com.github.mizosoft.methanol.nativeimage.test.Point",
"fields": [
{
"name": "x"
},
{
"name": "y"
}
],
"methods": [
{
"name": "<init>",
"parameterTypes": [
"int",
"int"
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mizosoft.methanol.quarkus.nativeimage.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mizosoft.methanol.BodyAdapter;
import com.github.mizosoft.methanol.adapter.ForwardingDecoder;
import com.github.mizosoft.methanol.adapter.ForwardingEncoder;
import com.github.mizosoft.methanol.adapter.jackson.JacksonAdapterFactory;
import com.google.auto.service.AutoService;

public class JacksonAdapters {
private static final ObjectMapper mapper = new ObjectMapper();

private JacksonAdapters() {}

@AutoService(BodyAdapter.Encoder.class)
public static class JacksonEncoder extends ForwardingEncoder {
public JacksonEncoder() {
super(JacksonAdapterFactory.createJsonEncoder(mapper));
}
}

@AutoService(BodyAdapter.Decoder.class)
public static class JacksonDecoder extends ForwardingDecoder {
public JacksonDecoder() {
super(JacksonAdapterFactory.createJsonDecoder(mapper));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Moataz Abdelnasser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.github.mizosoft.methanol.quarkus.nativeimage.test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

public final class Point {
public final int x;
public final int y;

@JsonCreator
public Point(@JsonProperty("x") int x, @JsonProperty("y") int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Point)) return false;

var that = (Point) obj;
return this.x == that.x && this.y == that.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}

@Override
public String toString() {
return "Point(" + "x=" + x + ", " + "y=" + y + ")";
}
}
Loading

0 comments on commit 5171fe9

Please sign in to comment.