Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JPMS module test #2685

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/check-android-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ jobs:
- name: Check Android compatibility
run: |
# Run 'test' phase because plugin normally expects to be executed after tests have been compiled
mvn --batch-mode --no-transfer-progress test animal-sniffer:check@check-android-compatibility -DskipTests
# Have to skip 'jpms-test' module because it requires that full Gson JAR has been built
mvn --batch-mode --no-transfer-progress test animal-sniffer:check@check-android-compatibility -DskipTests --projects '!jpms-test'
18 changes: 3 additions & 15 deletions extras/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<!-- Make the build reproducible, see root `pom.xml` -->
<!-- This is duplicated here because that is recommended by `artifact:check-buildplan` -->
<project.build.outputTimestamp>2024-05-19T18:54:10Z</project.build.outputTimestamp>

<!-- Overwrite property from parent; this module is currently not deployed -->
<gson.isInternalModule>true</gson.isInternalModule>
</properties>

<licenses>
Expand Down Expand Up @@ -64,21 +67,6 @@
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- Currently not deployed -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<developers>
<developer>
<name>Inderjeet Singh</name>
Expand Down
50 changes: 4 additions & 46 deletions graal-native-image-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
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>
Expand All @@ -23,16 +22,16 @@
<version>2.11.1-SNAPSHOT</version>
</parent>
<artifactId>graal-native-image-test</artifactId>
<name>Test: GraalVM Native Image</name>

<properties>
<!-- Make the build reproducible, see root `pom.xml` -->
<!-- This is duplicated here because that is recommended by `artifact:check-buildplan` -->
<project.build.outputTimestamp>2024-05-19T18:54:10Z</project.build.outputTimestamp>

<!-- 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>

<!-- Overwrite property from parent -->
<gson.isTestModule>true</gson.isTestModule>
</properties>

<dependencies>
Expand Down Expand Up @@ -63,22 +62,6 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-maven-plugin</artifactId>
<configuration>
<!-- This module is not supposed to be consumed as library, so no need to check API -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<configuration>
<!-- This module is not supposed to be consumed as library, so no need to check used classes -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand All @@ -87,31 +70,6 @@
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<!-- This module has no 'main' source code, so no JAR is created which could be installed;
see maven-jar-plugin configuration above -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- Not deployed -->
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<configuration>
<!-- Not deployed -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Expand Down
2 changes: 1 addition & 1 deletion gson/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
exports com.google.gson.reflect;
exports com.google.gson.stream;

// Dependency on Error Prone Annotations
// Optional dependency on Error Prone Annotations
requires static com.google.errorprone.annotations;

// Optional dependency on java.sql
Expand Down
5 changes: 5 additions & 0 deletions jpms-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# jpms-test

This Maven module contains tests to verify that Gson's `module-info.class` which is used by the Java Platform Module System (JPMS) works properly and can be used by other projects. The module declaration file `src/test/java/module-info.java` uses Gson's module.

This is a separate Maven module to test Gson's final JAR instead of just the compiled classes.
53 changes: 53 additions & 0 deletions jpms-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!--
Copyright 2024 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.11.1-SNAPSHOT</version>
</parent>
<artifactId>jpms-test</artifactId>
<name>Test: Java Platform Module System (JPMS)</name>

<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.testRelease>${maven.compiler.release}</maven.compiler.testRelease>

<!-- Overwrite property from parent -->
<gson.isTestModule>true</gson.isTestModule>
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
27 changes: 27 additions & 0 deletions jpms-test/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2024 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.
*/

/**
* Dummy module to prevent Maven Compiler Plugin from failing if {@code module-info.java} exists
* only in test sources:
*
* <blockquote>
*
* Can't compile test sources when main sources are missing a module descriptor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is another way to work around this; the Maven Compiler Plugin checks if the main output directory exists, but maybe that check is a bit brittle?

https://lists.apache.org/thread/c3f71v8tf3643dto6bwf3ctcos4gt5cq suggests it might even depend on the JUnit version being used (?).

So for now having this dummy module declaration might be the most reliable workaround.

*
* </blockquote>
*/
module com.google.gson.dummy {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2024 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.jpms_test;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import org.junit.Test;

/**
* Verifies that Gson's {@code module-info.class} properly 'exports' all its packages containing
* public API.
*/
public class ExportedPackagesTest {
/** Tests package {@code com.google.gson} */
@Test
public void testMainPackage() {
Gson gson = new Gson();
assertThat(gson.toJson(1)).isEqualTo("1");
}

/** Tests package {@code com.google.gson.annotations} */
@Test
public void testAnnotationsPackage() throws Exception {
class Annotated {
@SerializedName("custom-name")
int i;
}

Field field = Annotated.class.getDeclaredField("i");
SerializedName annotation = field.getAnnotation(SerializedName.class);
assertThat(annotation.value()).isEqualTo("custom-name");
}

/** Tests package {@code com.google.gson.reflect} */
@Test
public void testReflectPackage() {
var typeToken = TypeToken.get(String.class);
assertThat(typeToken.getRawType()).isEqualTo(String.class);
}

/** Tests package {@code com.google.gson.stream} */
@Test
public void testStreamPackage() throws IOException {
JsonReader jsonReader = new JsonReader(new StringReader("2"));
assertThat(jsonReader.nextInt()).isEqualTo(2);
}

/** Verifies that Gson packages are only 'exported' but not 'opened' for reflection. */
@Test
public void testReflectionInternalField() throws Exception {
Gson gson = new Gson();

// Get an arbitrary non-public instance field
Field field =
Arrays.stream(Gson.class.getDeclaredFields())
.filter(
f -> !Modifier.isStatic(f.getModifiers()) && !Modifier.isPublic(f.getModifiers()))
.findFirst()
.get();
assertThrows(InaccessibleObjectException.class, () -> field.setAccessible(true));
assertThrows(IllegalAccessException.class, () -> field.get(gson));
}

@Test
public void testInaccessiblePackage() throws Exception {
// Note: In case this class is renamed / removed, can change this to any other internal class
Class<?> internalClass = Class.forName("com.google.gson.internal.LinkedTreeMap");
assertThat(Modifier.isPublic(internalClass.getModifiers())).isTrue();
// Get the public constructor
Constructor<?> constructor = internalClass.getConstructor();
assertThrows(InaccessibleObjectException.class, () -> constructor.setAccessible(true));
assertThrows(IllegalAccessException.class, () -> constructor.newInstance());
}
}
Loading