-
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 a currently-failing test for Java records and
@Ignore
it. (#2203)
* Add a currently-failing test for Java records and `@Ignore` it. Also do the Maven gymastics required to ensure that this test only runs on Java versions ≥17. (It would also work on Java 16, but 17 is all we have in the CI.) Fix some compilation problems I saw when running locally, which for some reason don't show up in the CI. * Suppress some new lint options that trigger `-Werror`. We may fix these later. (Every test will need an explicit constructor!) * Select Java version with maven.compiler.release and maven.compiler.testRelease. Use `assumeNotNull` rather than an if-statement. * Specify <release>11</release> for javadoc. * Restore the @see for AccessibleObject.
- Loading branch information
1 parent
2591ede
commit 441406c
Showing
5 changed files
with
119 additions
and
7 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
70 changes: 70 additions & 0 deletions
70
gson/src/test/java/com/google/gson/functional/Java17RecordTest.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,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) | ||
@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); | ||
} | ||
} | ||
} |
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