diff --git a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java index b9a80092a1..0bfb4754cc 100644 --- a/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java +++ b/gson/src/test/java/com/google/gson/functional/PrimitiveTest.java @@ -17,6 +17,7 @@ package com.google.gson.functional; import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import com.google.gson.Gson; @@ -88,26 +89,14 @@ public void testByteDeserialization() { @Test public void testByteDeserializationLossy() { - try { - gson.fromJson("-129", byte.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from -129 to byte; at path $"); - } + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("-129", byte.class)); + assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from -129 to byte; at path $"); - try { - gson.fromJson("256", byte.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from 256 to byte; at path $"); - } + e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("256", byte.class)); + assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from 256 to byte; at path $"); - try { - gson.fromJson("2147483648", byte.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column 11 path $"); - } + e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("2147483648", byte.class)); + assertThat(e).hasMessageThat().isEqualTo("java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column 11 path $"); } @Test @@ -136,26 +125,14 @@ public void testShortDeserialization() { @Test public void testShortDeserializationLossy() { - try { - gson.fromJson("-32769", short.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from -32769 to short; at path $"); - } + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("-32769", short.class)); + assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from -32769 to short; at path $"); - try { - gson.fromJson("65536", short.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from 65536 to short; at path $"); - } + e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("65536", short.class)); + assertThat(e).hasMessageThat().isEqualTo("Lossy conversion from 65536 to short; at path $"); - try { - gson.fromJson("2147483648", short.class); - fail(); - } catch (JsonSyntaxException e) { - assertThat(e).hasMessageThat().isEqualTo("java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column 11 path $"); - } + e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("2147483648", short.class)); + assertThat(e).hasMessageThat().isEqualTo("java.lang.NumberFormatException: Expected an int but was 2147483648 at line 1 column 11 path $"); } @Test @@ -768,10 +745,7 @@ public void testUnquotedStringDeserializationFails() { assertThat(gson.fromJson("UnquotedSingleWord", String.class)).isEqualTo("UnquotedSingleWord"); String value = "String Blah Blah Blah...1, 2, 3"; - try { - gson.fromJson(value, String.class); - fail(); - } catch (JsonSyntaxException expected) { } + assertThrows(JsonSyntaxException.class, () -> gson.fromJson(value, String.class)); } @Test @@ -805,162 +779,102 @@ public void testPrimitiveClassLiteral() { @Test public void testDeserializeJsonObjectAsLongPrimitive() { - try { - gson.fromJson("{'abc':1}", long.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", long.class)); } @Test public void testDeserializeJsonArrayAsLongWrapper() { - try { - gson.fromJson("[1,2,3]", Long.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3]", Long.class)); } @Test public void testDeserializeJsonArrayAsInt() { - try { - gson.fromJson("[1, 2, 3, 4]", int.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1, 2, 3, 4]", int.class)); } @Test public void testDeserializeJsonObjectAsInteger() { - try { - gson.fromJson("{}", Integer.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{}", Integer.class)); } @Test public void testDeserializeJsonObjectAsShortPrimitive() { - try { - gson.fromJson("{'abc':1}", short.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", short.class)); } @Test public void testDeserializeJsonArrayAsShortWrapper() { - try { - gson.fromJson("['a','b']", Short.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("['a','b']", Short.class)); } @Test public void testDeserializeJsonArrayAsDoublePrimitive() { - try { - gson.fromJson("[1,2]", double.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2]", double.class)); } @Test public void testDeserializeJsonObjectAsDoubleWrapper() { - try { - gson.fromJson("{'abc':1}", Double.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", Double.class)); } @Test public void testDeserializeJsonObjectAsFloatPrimitive() { - try { - gson.fromJson("{'abc':1}", float.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", float.class)); } @Test public void testDeserializeJsonArrayAsFloatWrapper() { - try { - gson.fromJson("[1,2,3]", Float.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3]", Float.class)); } @Test public void testDeserializeJsonObjectAsBytePrimitive() { - try { - gson.fromJson("{'abc':1}", byte.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", byte.class)); } @Test public void testDeserializeJsonArrayAsByteWrapper() { - try { - gson.fromJson("[1,2,3,4]", Byte.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3,4]", Byte.class)); } @Test public void testDeserializeJsonObjectAsBooleanPrimitive() { - try { - gson.fromJson("{'abc':1}", boolean.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'abc':1}", boolean.class)); } @Test public void testDeserializeJsonArrayAsBooleanWrapper() { - try { - gson.fromJson("[1,2,3,4]", Boolean.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3,4]", Boolean.class)); } @Test public void testDeserializeJsonArrayAsBigDecimal() { - try { - gson.fromJson("[1,2,3,4]", BigDecimal.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3,4]", BigDecimal.class)); } @Test public void testDeserializeJsonObjectAsBigDecimal() { - try { - gson.fromJson("{'a':1}", BigDecimal.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'a':1}", BigDecimal.class)); } @Test public void testDeserializeJsonArrayAsBigInteger() { - try { - gson.fromJson("[1,2,3,4]", BigInteger.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3,4]", BigInteger.class)); } @Test public void testDeserializeJsonObjectAsBigInteger() { - try { - gson.fromJson("{'c':2}", BigInteger.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'c':2}", BigInteger.class)); } @Test public void testDeserializeJsonArrayAsNumber() { - try { - gson.fromJson("[1,2,3,4]", Number.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("[1,2,3,4]", Number.class)); } @Test public void testDeserializeJsonObjectAsNumber() { - try { - gson.fromJson("{'c':2}", Number.class); - fail(); - } catch (JsonSyntaxException expected) {} + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("{'c':2}", Number.class)); } @Test @@ -970,58 +884,30 @@ public void testDeserializingDecimalPointValueZeroSucceeds() { @Test public void testDeserializingNonZeroDecimalPointValuesAsIntegerFails() { - try { - gson.fromJson("1.02", Byte.class); - fail(); - } catch (JsonSyntaxException expected) { - } - try { - gson.fromJson("1.02", Short.class); - fail(); - } catch (JsonSyntaxException expected) { - } - try { - gson.fromJson("1.02", Integer.class); - fail(); - } catch (JsonSyntaxException expected) { - } - try { - gson.fromJson("1.02", Long.class); - fail(); - } catch (JsonSyntaxException expected) { - } + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("1.02", Byte.class)); + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("1.02", Short.class)); + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("1.02", Integer.class)); + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("1.02", Long.class)); } @Test public void testDeserializingBigDecimalAsIntegerFails() { - try { - gson.fromJson("-122.08e-213", Integer.class); - fail(); - } catch (JsonSyntaxException expected) { - assertThat(expected).hasCauseThat().hasMessageThat().isEqualTo("Expected an int but was -122.08e-213 at line 1 column 13 path $"); - } + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson("-122.08e-213", Integer.class)); + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Expected an int but was -122.08e-213 at line 1 column 13 path $"); } @Test public void testDeserializingBigIntegerAsInteger() { String number = "12121211243123245845384534687435634558945453489543985435"; - try { - gson.fromJson(number, Integer.class); - fail(); - } catch (JsonSyntaxException expected) { - assertThat(expected).hasCauseThat().hasMessageThat().isEqualTo("Expected an int but was " + number + " at line 1 column 57 path $"); - } + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson(number, Integer.class)); + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Expected an int but was " + number + " at line 1 column 57 path $"); } @Test public void testDeserializingBigIntegerAsLong() { String number = "12121211243123245845384534687435634558945453489543985435"; - try { - gson.fromJson(number, Long.class); - fail(); - } catch (JsonSyntaxException expected) { - assertThat(expected).hasCauseThat().hasMessageThat().isEqualTo("Expected a long but was " + number + " at line 1 column 57 path $"); - } + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, () -> gson.fromJson(number, Long.class)); + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Expected a long but was " + number + " at line 1 column 57 path $"); } @Test @@ -1038,11 +924,7 @@ public void testValueVeryCloseToZeroIsZero() { @Test public void testDeserializingBigDecimalAsBigIntegerFails() { - try { - gson.fromJson("-122.08e-213", BigInteger.class); - fail(); - } catch (JsonSyntaxException expected) { - } + assertThrows(JsonSyntaxException.class, () -> gson.fromJson("-122.08e-213", BigInteger.class)); } @Test