From fc27da7075485b2659f837918880c4cf17a6d477 Mon Sep 17 00:00:00 2001 From: Aidan Follestad Date: Tue, 28 Feb 2017 21:02:57 -0600 Subject: [PATCH] Annnnd more test coverage. --- README.md | 8 ++ src/main/java/com/afollestad/ason/Ason.java | 26 +++++-- .../com/afollestad/ason/AsonPathTest.java | 18 ++++- .../afollestad/ason/AsonSerializeTest.java | 77 ++++++++++++++++--- .../afollestad/ason/SimpleTestDataOne.java | 8 ++ .../afollestad/ason/SimpleTestDataTwo.java | 7 ++ 6 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 src/test/java/com/afollestad/ason/SimpleTestDataOne.java create mode 100644 src/test/java/com/afollestad/ason/SimpleTestDataTwo.java diff --git a/README.md b/README.md index adc1dab..58e955c 100644 --- a/README.md +++ b/README.md @@ -698,6 +698,14 @@ AsonArray array = // ... Person person = array.get(0, Person.class); ``` +For Lists, you need to use a separate method since it requires that you specific the type held by the list: + +```java +Ason ason = // ... +// The JSON object needs to contain a child array with the key "people" representing an array of Person classes. +List people = array.getList("people", Person.class); +``` + --- # Annotations diff --git a/src/main/java/com/afollestad/ason/Ason.java b/src/main/java/com/afollestad/ason/Ason.java index 71194bb..beab582 100644 --- a/src/main/java/com/afollestad/ason/Ason.java +++ b/src/main/java/com/afollestad/ason/Ason.java @@ -268,8 +268,8 @@ public T get(@NotNull String key, @NotNull Class cls) { @NotNull String key, @NotNull Class cls, @Nullable T defaultValue) { - final Object value = get(key, defaultValue); - if (value == null) { + final Object value = get(key, (T) null); + if (Util.isNull(value)) { return defaultValue; } else if (isPrimitive(cls) || cls == JSONObject.class || @@ -285,12 +285,8 @@ public T get(@NotNull String key, @NotNull Class cls) { AsonArray array = (AsonArray) value; return AsonSerializer.get().deserializeArray(array, cls); } else if (isList(cls)) { - if (!(value instanceof AsonArray)) { - throw new IllegalStateException("Expected a AsonArray to convert to " + - cls.getName() + ", found " + value.getClass().getName() + "."); - } - AsonArray array = (AsonArray) value; - return (T) AsonSerializer.get().deserializeList(array, cls.getComponentType()); + throw new IllegalStateException("Use getList(String, Class) instead of " + + "get(String, Class) for deserializing arrays to Lists."); } else { if (!(value instanceof Ason)) { throw new IllegalStateException("Expected a Ason to convert to " + @@ -301,6 +297,20 @@ public T get(@NotNull String key, @NotNull Class cls) { } } + @Nullable public List getList(@NotNull String key, + Class itemCls) { + final Object value = get(key, (T) null); + if (Util.isNull(value)) { + return null; + } + if (!(value instanceof AsonArray)) { + throw new IllegalStateException("Expected a AsonArray to convert to List, " + + "found " + value.getClass().getName() + "."); + } + AsonArray array = (AsonArray) value; + return AsonSerializer.get().deserializeList(array, itemCls); + } + public boolean has(@NotNull String key) { return get(key) != null; } diff --git a/src/test/java/com/afollestad/ason/AsonPathTest.java b/src/test/java/com/afollestad/ason/AsonPathTest.java index 4f19906..455073f 100644 --- a/src/test/java/com/afollestad/ason/AsonPathTest.java +++ b/src/test/java/com/afollestad/ason/AsonPathTest.java @@ -2,6 +2,8 @@ import org.junit.Test; +import java.util.List; + import static org.junit.Assert.*; public class AsonPathTest { @@ -276,12 +278,22 @@ public class AsonPathTest { try { ason.get("id.name"); assertFalse("No exception was thrown for using a path on a primitive entry!", false); - } catch(InvalidPathException ignored) { + } catch (InvalidPathException ignored) { } try { ason.get("test.id.hello"); assertFalse("No exception was thrown for using a path on a primitive entry!", false); - } catch(InvalidPathException ignored) { + } catch (InvalidPathException ignored) { + } + try { + ason.get("test.id", SimpleTestDataOne[].class); + assertFalse("No exception thrown when AsonArray was expected, but not found!", false); + } catch (IllegalStateException ignored) { + } + try { + ason.get("test.id", List.class); + assertFalse("No exception thrown when AsonArray was expected, but not found!", false); + } catch (IllegalStateException ignored) { } } @@ -292,7 +304,7 @@ public class AsonPathTest { try { ason.get("person.$0.test"); assertFalse("No exception thrown for using index notation on a parent object!", false); - } catch(InvalidPathException ignored) { + } catch (InvalidPathException ignored) { } } } diff --git a/src/test/java/com/afollestad/ason/AsonSerializeTest.java b/src/test/java/com/afollestad/ason/AsonSerializeTest.java index 36a7552..48fded5 100644 --- a/src/test/java/com/afollestad/ason/AsonSerializeTest.java +++ b/src/test/java/com/afollestad/ason/AsonSerializeTest.java @@ -345,17 +345,10 @@ static class Person3 { assertTrue(array.isEmpty()); } - @SuppressWarnings("unused") class One { - String hi = "hello"; - } - - @SuppressWarnings({"unused", "WeakerAccess"}) class Two { - } - @Test public void test_serialize_inaccessible_field() throws Exception { - Field hi = One.class.getDeclaredField("hi"); + Field hi = SimpleTestDataOne.class.getDeclaredField("hi"); try { - AsonSerializer.get().serializeField(hi, new Two()); + AsonSerializer.get().serializeField(hi, new SimpleTestDataTwo()); assertFalse("No exception thrown when trying to serialize inaccessible field!", false); } catch (RuntimeException ignored) { } @@ -402,14 +395,14 @@ static class Person3 { } @Test public void test_deserialize_null_json() { - One one = AsonSerializer.get().deserialize(null, One.class); + SimpleTestDataOne one = AsonSerializer.get().deserialize(null, SimpleTestDataOne.class); assertNull(one); } @Test public void test_deserialize_wrong_object_target() { Ason ason = new Ason("{\"obj\":{\"hi\":\"hello\"}}"); try { - ason.get("obj.hi", One.class); + ason.get("obj.hi", SimpleTestDataOne.class); assertFalse("No exception thrown for wrong get() cast.", false); } catch (IllegalStateException ignored) { } @@ -418,7 +411,7 @@ static class Person3 { @Test public void test_deserialize_wrong_array_target() { Ason ason = new Ason("{\"obj\":[\"hi\",\"hello\"]}"); try { - ason.get("obj", One[].class); + ason.get("obj", SimpleTestDataOne[].class); assertFalse("No exception thrown for wrong get() cast.", false); } catch (IllegalStateException ignored) { } @@ -433,6 +426,66 @@ static class Person3 { assertNotNull(array.get(2)); } + @Test public void test_auto_deserialize_default() { + Ason ason = new Ason() + .put("obj", new SimpleTestDataOne()); + SimpleTestDataOne fallback = new SimpleTestDataOne(); + fallback.hi = "fallback"; + SimpleTestDataOne result = ason.get("obj", SimpleTestDataOne.class, fallback); + assertNotNull(result); + assertEquals("hello", result.hi); + result = ason.get("obj2", SimpleTestDataOne.class, fallback); + assertNotNull(result); + assertEquals("fallback", result.hi); + } + + @Test public void test_auto_deserialize_primitive() { + // This happens if you use the variation of get() that takes a class even though it's unnecessary + Ason ason = new Ason() + .put("id", 6); + assertEquals(6, ason.get("id", + Integer.class, 0).intValue()); + } + + @Test public void test_auto_deserialize_list() { + Ason ason = new Ason() + .put("array", 1, 2, 3, 4); + List list = ason.getList("array", Integer.class); + assertEquals(1, list.get(0).intValue()); + assertEquals(2, list.get(1).intValue()); + assertEquals(3, list.get(2).intValue()); + assertEquals(4, list.get(3).intValue()); + } + + @Test public void test_auto_deserialize_list_wrong_method() { + Ason ason = new Ason() + .put("array", 1, 2, 3, 4); + try { + ason.get("array", List.class); + assertFalse("Exception not thrown for using the wrong " + + "list get() method.", false); + } catch (IllegalStateException ignored) { + } + } + + @Test public void test_get_list_on_object() { + Ason ason = new Ason() + .put("value", 1); + try { + ason.getList("value", Integer.class); + assertFalse("No exception thrown when using getList() for a " + + "key that represents a non-array!", false); + } catch(IllegalStateException ignored) { + } + } + + @Test public void test_get_list_null() { + Ason ason = new Ason() + .putNull("value"); + assertNull(ason.getList("value", Integer.class)); + assertNull(ason.getList("value2", Integer.class)); + } + // ////// TEST FOR ISSUE #10 // diff --git a/src/test/java/com/afollestad/ason/SimpleTestDataOne.java b/src/test/java/com/afollestad/ason/SimpleTestDataOne.java new file mode 100644 index 0000000..97f0491 --- /dev/null +++ b/src/test/java/com/afollestad/ason/SimpleTestDataOne.java @@ -0,0 +1,8 @@ +package com.afollestad.ason; + +/** + * @author Aidan Follestad (afollestad) + */ +class SimpleTestDataOne { + String hi = "hello"; +} diff --git a/src/test/java/com/afollestad/ason/SimpleTestDataTwo.java b/src/test/java/com/afollestad/ason/SimpleTestDataTwo.java new file mode 100644 index 0000000..5b208e5 --- /dev/null +++ b/src/test/java/com/afollestad/ason/SimpleTestDataTwo.java @@ -0,0 +1,7 @@ +package com.afollestad.ason; + +/** + * @author Aidan Follestad (afollestad) + */ +class SimpleTestDataTwo { +}