Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
Annnnd more test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Mar 1, 2017
1 parent f750d0e commit fc27da7
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 23 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ AsonArray<Person> 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<Person> people = array.getList("people", Person.class);
```

---

# Annotations
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/com/afollestad/ason/Ason.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public <T> T get(@NotNull String key, @NotNull Class<T> cls) {
@NotNull String key,
@NotNull Class<T> 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 ||
Expand All @@ -285,12 +285,8 @@ public <T> T get(@NotNull String key, @NotNull Class<T> cls) {
AsonArray<T> array = (AsonArray<T>) 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<T> array = (AsonArray<T>) 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 " +
Expand All @@ -301,6 +297,20 @@ public <T> T get(@NotNull String key, @NotNull Class<T> cls) {
}
}

@Nullable public <T> List<T> getList(@NotNull String key,
Class<T> 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<T> array = (AsonArray<T>) value;
return AsonSerializer.get().deserializeList(array, itemCls);
}

public boolean has(@NotNull String key) {
return get(key) != null;
}
Expand Down
18 changes: 15 additions & 3 deletions src/test/java/com/afollestad/ason/AsonPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class AsonPathTest {
Expand Down Expand Up @@ -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) {
}
}

Expand All @@ -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) {
}
}
}
77 changes: 65 additions & 12 deletions src/test/java/com/afollestad/ason/AsonSerializeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Expand Down Expand Up @@ -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) {
}
Expand All @@ -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) {
}
Expand All @@ -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<Integer> 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
//
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/afollestad/ason/SimpleTestDataOne.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.afollestad.ason;

/**
* @author Aidan Follestad (afollestad)
*/
class SimpleTestDataOne {
String hi = "hello";
}
7 changes: 7 additions & 0 deletions src/test/java/com/afollestad/ason/SimpleTestDataTwo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.afollestad.ason;

/**
* @author Aidan Follestad (afollestad)
*/
class SimpleTestDataTwo {
}

0 comments on commit fc27da7

Please sign in to comment.