Improve `Calendar` unit tests (#2592)
This commit is contained in:
parent
3e020e7236
commit
2bcaaed875
|
@ -16,7 +16,7 @@
|
||||||
package com.google.gson.functional;
|
package com.google.gson.functional;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.assertThrows;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
@ -90,17 +90,15 @@ public class DefaultTypeAdaptersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClassSerialization() {
|
public void testClassSerialization() {
|
||||||
try {
|
var exception =
|
||||||
gson.toJson(String.class);
|
assertThrows(UnsupportedOperationException.class, () -> gson.toJson(String.class));
|
||||||
fail();
|
assertThat(exception)
|
||||||
} catch (UnsupportedOperationException expected) {
|
.hasMessageThat()
|
||||||
assertThat(expected)
|
.isEqualTo(
|
||||||
.hasMessageThat()
|
"Attempted to serialize java.lang.Class: java.lang.String. Forgot to register a type"
|
||||||
.isEqualTo(
|
+ " adapter?\n"
|
||||||
"Attempted to serialize java.lang.Class: java.lang.String. Forgot to register a type"
|
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
||||||
+ " adapter?\n"
|
|
||||||
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
|
||||||
}
|
|
||||||
// Override with a custom type adapter for class.
|
// Override with a custom type adapter for class.
|
||||||
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
||||||
assertThat(gson.toJson(String.class)).isEqualTo("\"java.lang.String\"");
|
assertThat(gson.toJson(String.class)).isEqualTo("\"java.lang.String\"");
|
||||||
|
@ -108,16 +106,15 @@ public class DefaultTypeAdaptersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClassDeserialization() {
|
public void testClassDeserialization() {
|
||||||
try {
|
var exception =
|
||||||
gson.fromJson("String.class", Class.class);
|
assertThrows(
|
||||||
fail();
|
UnsupportedOperationException.class, () -> gson.fromJson("String.class", Class.class));
|
||||||
} catch (UnsupportedOperationException expected) {
|
assertThat(exception)
|
||||||
assertThat(expected)
|
.hasMessageThat()
|
||||||
.hasMessageThat()
|
.isEqualTo(
|
||||||
.isEqualTo(
|
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?\n"
|
||||||
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?\n"
|
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
||||||
+ "See https://github.com/google/gson/blob/main/Troubleshooting.md#java-lang-class-unsupported");
|
|
||||||
}
|
|
||||||
// Override with a custom type adapter for class.
|
// Override with a custom type adapter for class.
|
||||||
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
gson = new GsonBuilder().registerTypeAdapter(Class.class, new MyClassTypeAdapter()).create();
|
||||||
assertThat(gson.fromJson("java.lang.String", Class.class)).isAssignableTo(String.class);
|
assertThat(gson.fromJson("java.lang.String", Class.class)).isAssignableTo(String.class);
|
||||||
|
@ -289,11 +286,10 @@ public class DefaultTypeAdaptersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBadValueForBigDecimalDeserialization() {
|
public void testBadValueForBigDecimalDeserialization() {
|
||||||
try {
|
// Exponent of a BigDecimal must be an integer value
|
||||||
gson.fromJson("{\"value\"=1.5e-1.0031}", ClassWithBigDecimal.class);
|
assertThrows(
|
||||||
fail("Exponent of a BigDecimal must be an integer value.");
|
JsonParseException.class,
|
||||||
} catch (JsonParseException expected) {
|
() -> gson.fromJson("{\"value\": 1.5e-1.0031}", ClassWithBigDecimal.class));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -374,23 +370,17 @@ public class DefaultTypeAdaptersTest {
|
||||||
json = "[true,false,true,true,true,true,false,false,true,false,false]";
|
json = "[true,false,true,true,true,true,false,false,true,false,false]";
|
||||||
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
assertThat(gson.fromJson(json, BitSet.class)).isEqualTo(expected);
|
||||||
|
|
||||||
try {
|
var exception =
|
||||||
gson.fromJson("[1, []]", BitSet.class);
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1, []]", BitSet.class));
|
||||||
fail();
|
assertThat(exception)
|
||||||
} catch (JsonSyntaxException e) {
|
.hasMessageThat()
|
||||||
assertThat(e)
|
.isEqualTo("Invalid bitset value type: BEGIN_ARRAY; at path $[1]");
|
||||||
.hasMessageThat()
|
|
||||||
.isEqualTo("Invalid bitset value type: BEGIN_ARRAY; at path $[1]");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
exception =
|
||||||
gson.fromJson("[1, 2]", BitSet.class);
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1, 2]", BitSet.class));
|
||||||
fail();
|
assertThat(exception)
|
||||||
} catch (JsonSyntaxException e) {
|
.hasMessageThat()
|
||||||
assertThat(e)
|
.isEqualTo("Invalid bitset value 2, expected 0 or 1; at path $[1]");
|
||||||
.hasMessageThat()
|
|
||||||
.isEqualTo("Invalid bitset value 2, expected 0 or 1; at path $[1]");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -468,28 +458,47 @@ public class DefaultTypeAdaptersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultGregorianCalendarSerialization() {
|
public void testDefaultGregorianCalendarSerialization() {
|
||||||
|
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"), Locale.US);
|
||||||
|
// Calendar was created with current time, must clear it
|
||||||
|
cal.clear();
|
||||||
|
cal.set(2018, Calendar.JUNE, 25, 10, 20, 30);
|
||||||
|
|
||||||
Gson gson = new GsonBuilder().create();
|
Gson gson = new GsonBuilder().create();
|
||||||
GregorianCalendar cal = new GregorianCalendar();
|
|
||||||
String json = gson.toJson(cal);
|
String json = gson.toJson(cal);
|
||||||
assertThat(json).contains("year");
|
assertThat(json)
|
||||||
assertThat(json).contains("month");
|
.isEqualTo(
|
||||||
assertThat(json).contains("dayOfMonth");
|
"{\"year\":2018,\"month\":5,\"dayOfMonth\":25,\"hourOfDay\":10,\"minute\":20,\"second\":30}");
|
||||||
assertThat(json).contains("hourOfDay");
|
|
||||||
assertThat(json).contains("minute");
|
|
||||||
assertThat(json).contains("second");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultGregorianCalendarDeserialization() {
|
public void testDefaultGregorianCalendarDeserialization() {
|
||||||
Gson gson = new GsonBuilder().create();
|
TimeZone defaultTimeZone = TimeZone.getDefault();
|
||||||
String json = "{year:2009,month:2,dayOfMonth:11,hourOfDay:14,minute:29,second:23}";
|
Locale defaultLocale = Locale.getDefault();
|
||||||
GregorianCalendar cal = gson.fromJson(json, GregorianCalendar.class);
|
|
||||||
assertThat(cal.get(Calendar.YEAR)).isEqualTo(2009);
|
try {
|
||||||
assertThat(cal.get(Calendar.MONTH)).isEqualTo(2);
|
// Calendar deserialization uses default TimeZone and Locale; set them here to make the test
|
||||||
assertThat(cal.get(Calendar.DAY_OF_MONTH)).isEqualTo(11);
|
// deterministic
|
||||||
assertThat(cal.get(Calendar.HOUR_OF_DAY)).isEqualTo(14);
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
|
||||||
assertThat(cal.get(Calendar.MINUTE)).isEqualTo(29);
|
Locale.setDefault(Locale.US);
|
||||||
assertThat(cal.get(Calendar.SECOND)).isEqualTo(23);
|
|
||||||
|
Gson gson = new GsonBuilder().create();
|
||||||
|
String json =
|
||||||
|
"{\"year\":2009,\"month\":2,\"dayOfMonth\":11,\"hourOfDay\":14,\"minute\":29,\"second\":23}";
|
||||||
|
GregorianCalendar cal = gson.fromJson(json, GregorianCalendar.class);
|
||||||
|
assertThat(cal.get(Calendar.YEAR)).isEqualTo(2009);
|
||||||
|
assertThat(cal.get(Calendar.MONTH)).isEqualTo(2);
|
||||||
|
assertThat(cal.get(Calendar.DAY_OF_MONTH)).isEqualTo(11);
|
||||||
|
assertThat(cal.get(Calendar.HOUR_OF_DAY)).isEqualTo(14);
|
||||||
|
assertThat(cal.get(Calendar.MINUTE)).isEqualTo(29);
|
||||||
|
assertThat(cal.get(Calendar.SECOND)).isEqualTo(23);
|
||||||
|
assertThat(cal.getTimeInMillis()).isEqualTo(1236781763000L);
|
||||||
|
|
||||||
|
// Serializing value again should be equivalent to original JSON
|
||||||
|
assertThat(gson.toJson(cal)).isEqualTo(json);
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(defaultTimeZone);
|
||||||
|
Locale.setDefault(defaultLocale);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -654,16 +663,13 @@ public class DefaultTypeAdaptersTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJsonElementTypeMismatch() {
|
public void testJsonElementTypeMismatch() {
|
||||||
try {
|
var exception =
|
||||||
gson.fromJson("\"abc\"", JsonObject.class);
|
assertThrows(JsonSyntaxException.class, () -> gson.fromJson("\"abc\"", JsonObject.class));
|
||||||
fail();
|
assertThat(exception)
|
||||||
} catch (JsonSyntaxException expected) {
|
.hasMessageThat()
|
||||||
assertThat(expected)
|
.isEqualTo(
|
||||||
.hasMessageThat()
|
"Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive;"
|
||||||
.isEqualTo(
|
+ " at path $");
|
||||||
"Expected a com.google.gson.JsonObject but was com.google.gson.JsonPrimitive;"
|
|
||||||
+ " at path $");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ClassWithBigDecimal {
|
private static class ClassWithBigDecimal {
|
||||||
|
|
|
@ -18,7 +18,6 @@ package com.google.gson.internal.bind.util;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.assertThrows;
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
|
@ -52,7 +51,7 @@ public class ISO8601UtilsTest {
|
||||||
Date date = calendar.getTime();
|
Date date = calendar.getTime();
|
||||||
String dateStr = ISO8601Utils.format(date);
|
String dateStr = ISO8601Utils.format(date);
|
||||||
String expectedDate = "2018-06-25";
|
String expectedDate = "2018-06-25";
|
||||||
assertThat(dateStr.substring(0, expectedDate.length())).isEqualTo(expectedDate);
|
assertThat(dateStr).startsWith(expectedDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -87,21 +86,13 @@ public class ISO8601UtilsTest {
|
||||||
@Test
|
@Test
|
||||||
public void testDateParseInvalidDay() {
|
public void testDateParseInvalidDay() {
|
||||||
String dateStr = "2022-12-33";
|
String dateStr = "2022-12-33";
|
||||||
try {
|
assertThrows(ParseException.class, () -> ISO8601Utils.parse(dateStr, new ParsePosition(0)));
|
||||||
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
|
||||||
fail("Expected parsing to fail");
|
|
||||||
} catch (ParseException expected) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDateParseInvalidMonth() {
|
public void testDateParseInvalidMonth() {
|
||||||
String dateStr = "2022-14-30";
|
String dateStr = "2022-14-30";
|
||||||
try {
|
assertThrows(ParseException.class, () -> ISO8601Utils.parse(dateStr, new ParsePosition(0)));
|
||||||
ISO8601Utils.parse(dateStr, new ParsePosition(0));
|
|
||||||
fail("Expected parsing to fail");
|
|
||||||
} catch (ParseException expected) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue