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 a currently-failing test for Java records and @Ignore it. #2203

Merged
merged 7 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
</license>
</licenses>

<properties>
<excludeTestCompilation>**/Java17*</excludeTestCompilation>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -60,6 +64,18 @@
</excludes>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<testExcludes>
<exclude>${excludeTestCompilation}</exclude>
</testExcludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down Expand Up @@ -222,4 +238,17 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>JDK17</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties>
<javaVersion>17</javaVersion>
<excludeTestCompilation></excludeTestCompilation>
<extraLintSuppressions>,-exports,-missing-explicit-ctor,-removal</extraLintSuppressions>
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (C) 2022 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.functional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import java.util.Objects;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
@Ignore
public final class Java17RecordTest {
private final Gson gson = new Gson();

@Test
public void testFirstNameIsChosenForSerialization() {
MyRecord target = new MyRecord("v1", "v2");
// Ensure name1 occurs exactly once, and name2 and name3 don't appear
assertEquals("{\"name\":\"v1\",\"name1\":\"v2\"}", gson.toJson(target));
}

@Test
public void testMultipleNamesDeserializedCorrectly() {
assertEquals("v1", gson.fromJson("{'name':'v1'}", MyRecord.class).a);

// Both name1 and name2 gets deserialized to b
assertEquals("v11", gson.fromJson("{'name1':'v11'}", MyRecord.class).b);
assertEquals("v2", gson.fromJson("{'name2':'v2'}", MyRecord.class).b);
assertEquals("v3", gson.fromJson("{'name3':'v3'}", MyRecord.class).b);
}

@Test
public void testMultipleNamesInTheSameString() {
// The last value takes precedence
assertEquals("v3", gson.fromJson("{'name1':'v1','name2':'v2','name3':'v3'}", MyRecord.class).b);
}

@Test
public void testConstructorRuns() {
assertThrows(NullPointerException.class,
() -> gson.fromJson("{'name1': null, 'name2': null", MyRecord.class));
}

private static record MyRecord(
@SerializedName("name") String a,
@SerializedName(value = "name1", alternate = {"name2", "name3"}) String b) {
MyRecord {
Objects.requireNonNull(a);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.awt.Point;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -40,7 +40,7 @@ public void close() throws IOException {
}

@Test
public void testBlockInaccessibleJava() {
public void testBlockInaccessibleJava() throws ReflectiveOperationException {
Gson gson = new GsonBuilder()
.addReflectionAccessFilter(ReflectionAccessFilter.BLOCK_INACCESSIBLE_JAVA)
.create();
Expand All @@ -58,9 +58,20 @@ public void testBlockInaccessibleJava() {
);
}

// But serialization should succeed for classes with only public fields
String json = gson.toJson(new Point(1, 2));
assertEquals("{\"x\":1,\"y\":2}", json);

// But serialization should succeed for classes with only public fields.
// Not many JDK classes have mutable public fields, thank goodness, but java.awt.Point does.
Class<?> pointClass = null;
try {
pointClass = Class.forName("java.awt.Point");
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
} catch (ClassNotFoundException e) {
}
if (pointClass != null) {
eamonnmcmanus marked this conversation as resolved.
Show resolved Hide resolved
Constructor<?> pointConstructor = pointClass.getConstructor(int.class, int.class);
Object point = pointConstructor.newInstance(1, 2);
String json = gson.toJson(point);
assertEquals("{\"x\":1,\"y\":2}", json);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private static Class<?> loadClassWithDifferentClassLoader(Class<?> c) throws Exc
}

@Test
@SuppressWarnings("removal") // java.lang.SecurityManager
public void testRestrictiveSecurityManager() throws Exception {
// Must use separate class loader, otherwise permission is not checked, see Class.getDeclaredFields()
Class<?> clazz = loadClassWithDifferentClassLoader(ClassWithPrivateMembers.class);
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaVersion>7</javaVersion>
<extraLintSuppressions></extraLintSuppressions>
</properties>

<scm>
Expand Down Expand Up @@ -77,7 +78,7 @@
<compilerArgs>
<!-- Enable all warnings, except for ones which cause issues when building with newer JDKs, see also
https://docs.oracle.com/en/java/javase/11/tools/javac.html -->
<compilerArg>-Xlint:all,-options</compilerArg>
<compilerArg>-Xlint:all,-options${extraLintSuppressions}</compilerArg>
</compilerArgs>
<jdkToolchain>
<version>[11,)</version>
Expand Down