forked from google/gson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit test for type variable inference test.
See google#2563
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
gson/src/test/java/com/google/gson/functional/InferenceFromTypeVariableTest.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,54 @@ | ||
package com.google.gson.functional; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.gson.Gson; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test deserialization of generic wrapper with type bound. | ||
* | ||
* @author sevcenko | ||
*/ | ||
public class InferenceFromTypeVariableTest { | ||
private Gson gson; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
gson = new Gson(); | ||
} | ||
|
||
public static class Foo { | ||
private final String text; | ||
|
||
public Foo(String text) { | ||
this.text = text; | ||
} | ||
|
||
public String getText() { | ||
return text; | ||
} | ||
} | ||
|
||
public static class BarDynamic<T extends Foo> { | ||
private final T foo; | ||
|
||
public BarDynamic(T foo) { | ||
this.foo = foo; | ||
} | ||
|
||
public T getFoo() { | ||
return foo; | ||
} | ||
} | ||
|
||
@Test | ||
public void testSubClassSerialization() { | ||
BarDynamic<Foo> bar = new BarDynamic<>(new Foo("foo!")); | ||
assertThat(gson.toJson(bar)).isEqualTo("{\"foo\":{\"text\":\"foo!\"}}"); | ||
// without #2563 fix, this would deserialize foo as Object and fails to assign it to foo field | ||
BarDynamic<?> deserialized = gson.fromJson(gson.toJson(bar), BarDynamic.class); | ||
assertThat(deserialized.getFoo().getText()).isEqualTo("foo!"); | ||
} | ||
} |