-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
eamonnmcmanus
merged 2 commits into
google:main
from
Marcono1234:marcono1234/jpms-module-test
May 29, 2024
Merged
Add JPMS module test #2685
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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> |
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 test 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.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()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.