From 9b66d41a63b8302cc1e5b86b4d06d69c263f6ce6 Mon Sep 17 00:00:00 2001 From: Omega-Ariston <654815312@qq.com> Date: Fri, 3 Jan 2020 10:54:15 +0800 Subject: [PATCH 1/4] Json Primitive Tests --- gson/src/test/java/com/google/gson/JsonPrimitiveTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java index b5af6a8114..cdd4fdb613 100644 --- a/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/JsonPrimitiveTest.java @@ -108,6 +108,9 @@ public void testStringsAndChar() throws Exception { assertTrue(json.isString()); assertEquals('z', json.getAsCharacter()); assertEquals("z", json.getAsString()); + + json = new JsonPrimitive(true); + assertEquals("true", json.getAsString()); } public void testExponential() throws Exception { From 87997a797806528c2de4c51fe52a7ab281f7a55f Mon Sep 17 00:00:00 2001 From: Omega-Ariston <654815312@qq.com> Date: Fri, 3 Jan 2020 10:54:43 +0800 Subject: [PATCH 2/4] Json Tree Writer tests --- .../internal/bind/JsonTreeWriterTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java index cc04dbc324..094f04bd28 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/JsonTreeWriterTest.java @@ -120,6 +120,34 @@ public void testEmptyWriter() { assertEquals(JsonNull.INSTANCE, writer.get()); } + public void testBeginArray() throws Exception { + JsonTreeWriter writer = new JsonTreeWriter(); + assertEquals(writer, writer.beginArray()); + } + + public void testBeginObject() throws Exception { + JsonTreeWriter writer = new JsonTreeWriter(); + assertEquals(writer, writer.beginObject()); + } + + public void testValueString() throws Exception { + JsonTreeWriter writer = new JsonTreeWriter(); + String n = "as"; + assertEquals(writer, writer.value(n)); + } + + public void testBoolValue() throws Exception { + JsonTreeWriter writer = new JsonTreeWriter(); + boolean bool = true; + assertEquals(writer, writer.value(bool)); + } + + public void testBoolMaisValue() throws Exception { + JsonTreeWriter writer = new JsonTreeWriter(); + Boolean bool = true; + assertEquals(writer, writer.value(bool)); + } + public void testLenientNansAndInfinities() throws IOException { JsonTreeWriter writer = new JsonTreeWriter(); writer.setLenient(true); From e0a06d9c248772804de477ee51b6ab7e78fe8a15 Mon Sep 17 00:00:00 2001 From: Omega-Ariston <654815312@qq.com> Date: Fri, 3 Jan 2020 10:55:06 +0800 Subject: [PATCH 3/4] Add Tests for ISO8601Utils --- .../internal/bind/util/ISO8601UtilsTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java diff --git a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java new file mode 100644 index 0000000000..6d1912e548 --- /dev/null +++ b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java @@ -0,0 +1,101 @@ +package com.google.gson.internal.bind.util; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.text.ParseException; +import java.text.ParsePosition; +import java.util.*; + +import static org.junit.Assert.assertEquals; + +public class ISO8601UtilsTest { + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void testDateFormatString() { + Date date = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime(); + String dateStr = ISO8601Utils.format(date); + String expectedDate = "2018-06-25"; + assertEquals(expectedDate, dateStr.substring(0, expectedDate.length())); + } + + @Test + public void testDateFormatWithMilliseconds() { + long time = 1530209176870L; + Date date = new Date(time); + String dateStr = ISO8601Utils.format(date, true); + String expectedDate = "2018-06-28T18:06:16.870Z"; + assertEquals(expectedDate, dateStr); + } + + @Test + public void testDateFormatWithTimezone() { + long time = 1530209176870L; + Date date = new Date(time); + String dateStr = ISO8601Utils.format(date, true, TimeZone.getTimeZone("Brazil/East")); + String expectedDate = "2018-06-28T15:06:16.870-03:00"; + assertEquals(expectedDate, dateStr); + } + + @Test + public void testDateParseWithDefaultTimezone() throws ParseException { + String dateStr = "2018-06-25"; + Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0)); + Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25).getTime(); + assertEquals(expectedDate, date); + } + + @Test + public void testDateParseWithTimezone() throws ParseException { + TimeZone defaultTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Locale defaultLocale = Locale.getDefault(); + Locale.setDefault(Locale.US); + try { + String dateStr = "2018-06-25T00:00:00-03:00"; + Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0)); + Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime(); + assertEquals(expectedDate, date); + } finally { + TimeZone.setDefault(defaultTimeZone); + Locale.setDefault(defaultLocale); + } + } + + @Test + public void testDateParseSpecialTimezone() throws ParseException { + TimeZone defaultTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Locale defaultLocale = Locale.getDefault(); + Locale.setDefault(Locale.US); + try { + String dateStr = "2018-06-25T00:02:00-02:58"; + Date date = ISO8601Utils.parse(dateStr, new ParsePosition(0)); + Date expectedDate = new GregorianCalendar(2018, Calendar.JUNE, 25, 3, 0).getTime(); + assertEquals(expectedDate, date); + } finally { + TimeZone.setDefault(defaultTimeZone); + Locale.setDefault(defaultLocale); + } + } + + @Test + public void testDateParseInvalidTime() throws ParseException { + TimeZone defaultTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + Locale defaultLocale = Locale.getDefault(); + Locale.setDefault(Locale.US); + try { + String dateStr = "2018-06-25T61:60:62-03:00"; + exception.expect(ParseException.class); + ISO8601Utils.parse(dateStr, new ParsePosition(0)); + } finally { + TimeZone.setDefault(defaultTimeZone); + Locale.setDefault(defaultLocale); + } + } +} \ No newline at end of file From 69b334d2a4847210436831f6373b72799fa67e1c Mon Sep 17 00:00:00 2001 From: Omega-Ariston <654815312@qq.com> Date: Fri, 3 Jan 2020 10:57:58 +0800 Subject: [PATCH 4/4] Add Tests for ISO8601Utils --- .../com/google/gson/internal/bind/util/ISO8601UtilsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java index 6d1912e548..10b7b5e0dc 100644 --- a/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java +++ b/gson/src/test/java/com/google/gson/internal/bind/util/ISO8601UtilsTest.java @@ -98,4 +98,4 @@ public void testDateParseInvalidTime() throws ParseException { Locale.setDefault(defaultLocale); } } -} \ No newline at end of file +}