forked from google/gson
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently the build would not detected if `module-info.class` was missing, or incorrectly configured.
- Loading branch information
1 parent
91d22b3
commit a20ead2
Showing
12 changed files
with
506 additions
and
2 deletions.
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
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,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. |
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,110 @@ | ||
<!-- | ||
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> | ||
|
||
<!-- 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> | ||
</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> | ||
|
||
<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> | ||
<configuration> | ||
<!-- This module has no 'main' source code; skip creating JAR and avoid warning on console --> | ||
<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> | ||
</build> | ||
</project> |
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,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 tests sources: | ||
* | ||
* <blockquote> | ||
* | ||
* Can't compile test sources when main sources are missing a module descriptor | ||
* | ||
* </blockquote> | ||
*/ | ||
module com.google.gson.dummy {} |
100 changes: 100 additions & 0 deletions
100
jpms-test/src/test/java/com/google/gson/jpms_test/ExportedPackagesTest.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,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.java} 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 testReflectionGson() 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 { | ||
// 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()); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
jpms-test/src/test/java/com/google/gson/jpms_test/ModuleTest.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,114 @@ | ||
/* | ||
* 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 com.google.gson.Gson; | ||
import java.lang.module.ModuleDescriptor; | ||
import java.lang.module.ModuleDescriptor.Exports; | ||
import java.lang.module.ModuleDescriptor.Requires; | ||
import java.net.URL; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Verifies that this test project is properly set up, and has a module descriptor, and checks | ||
* Gson's module descriptor. | ||
*/ | ||
public class ModuleTest { | ||
@Test | ||
public void testOwnModule() { | ||
Module module = getClass().getModule(); | ||
assertThat(module.getName()).isEqualTo("com.google.gson.jpms_test"); | ||
} | ||
|
||
@Test | ||
public void testGsonModule() { | ||
// Verify that this test actually loads the final Gson JAR, and not only compiled classes | ||
// Note: This might fail when run from the IDE, but should succeed when run with Maven from | ||
// command line | ||
URL gsonLocation = Gson.class.getProtectionDomain().getCodeSource().getLocation(); | ||
assertThat(gsonLocation.getPath()).containsMatch("gson/target/gson-[^/]+\\.jar"); | ||
|
||
Module module = Gson.class.getModule(); | ||
ModuleDescriptor moduleDescriptor = module.getDescriptor(); | ||
|
||
assertThat(moduleDescriptor.name()).isEqualTo("com.google.gson"); | ||
// Permit `Modifier.SYNTHETIC`; current versions of Moditect seem to set that | ||
assertThat(moduleDescriptor.modifiers()) | ||
.containsNoneOf( | ||
ModuleDescriptor.Modifier.AUTOMATIC, | ||
ModuleDescriptor.Modifier.MANDATED, | ||
ModuleDescriptor.Modifier.OPEN); | ||
// Should have implicitly included the Maven project version | ||
assertThat(moduleDescriptor.rawVersion()).isPresent(); | ||
|
||
Set<Requires> moduleRequires = moduleDescriptor.requires(); | ||
assertThat(getModuleDependencies(moduleRequires)) | ||
.containsExactly("com.google.errorprone.annotations", "java.sql", "jdk.unsupported"); | ||
assertThat(getTransitiveModuleDependencies(moduleRequires)).isEmpty(); | ||
assertThat(getOptionalModuleDependencies(moduleRequires)) | ||
.containsExactly("com.google.errorprone.annotations", "java.sql", "jdk.unsupported"); | ||
|
||
Set<Exports> packageExports = moduleDescriptor.exports(); | ||
assertThat(packageExports.stream().map(Exports::source)) | ||
.containsExactly( | ||
"com.google.gson", | ||
"com.google.gson.annotations", | ||
"com.google.gson.reflect", | ||
"com.google.gson.stream"); | ||
// Gson currently does not export packages to specific modules only | ||
assertThat(packageExports.stream().filter(Exports::isQualified)).isEmpty(); | ||
|
||
// Gson does not allow access to its implementation details using reflection | ||
assertThat(moduleDescriptor.opens()).isEmpty(); | ||
// Gson currently does not use or provide any services | ||
assertThat(moduleDescriptor.uses()).isEmpty(); | ||
assertThat(moduleDescriptor.provides()).isEmpty(); | ||
// Gson has no main class | ||
assertThat(moduleDescriptor.mainClass()).isEmpty(); | ||
} | ||
|
||
private static Stream<Requires> filterImplicitRequires(Set<Requires> requires) { | ||
return requires.stream() | ||
.filter( | ||
r -> | ||
!r.modifiers().contains(Requires.Modifier.MANDATED) | ||
&& !r.modifiers().contains(Requires.Modifier.SYNTHETIC)); | ||
} | ||
|
||
private static Set<String> getModuleDependencies(Set<Requires> requires) { | ||
return filterImplicitRequires(requires).map(Requires::name).collect(Collectors.toSet()); | ||
} | ||
|
||
private static Set<String> getTransitiveModuleDependencies(Set<Requires> requires) { | ||
return filterImplicitRequires(requires) | ||
.filter(r -> r.modifiers().contains(Requires.Modifier.TRANSITIVE)) | ||
.map(Requires::name) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private static Set<String> getOptionalModuleDependencies(Set<Requires> requires) { | ||
return filterImplicitRequires(requires) | ||
.filter(r -> r.modifiers().contains(Requires.Modifier.STATIC)) | ||
.map(Requires::name) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
Oops, something went wrong.