-
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 JPMS module test Previously the build would not have detected if `module-info.class` was missing, or incorrectly configured. * Simplify disabling plugins for integration test modules Defines properties in the parent POM which determine whether the plugins should be skipped, and then sets the properties in the Maven modules.
- Loading branch information
1 parent
91d22b3
commit 6ebbc9d
Showing
16 changed files
with
494 additions
and
145 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
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.