WIP json refactor - initial tidy

This commit is contained in:
Rob Bygrave
2014-11-05 23:15:09 +13:00
parent 0ceda6406e
commit b313809894
29 changed files with 1301 additions and 1001 deletions
@@ -0,0 +1,104 @@
package com.avaje.ebean.json;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
public class EJsonTests {
@Test
public void test_map_simple() {
String jsonInput = "{\"name\":\"rob\",\"age\":12}";
Object result = EJson.parse(jsonInput);
Assert.assertTrue(result instanceof Map);
Map<?,?> map = (Map<?,?>)result;
Assert.assertEquals("rob", map.get("name"));
Assert.assertEquals(12L, map.get("age"));
String jsonOutput = EJson.write(result);
Assert.assertEquals(jsonInput, jsonOutput);
}
@Test
public void test_map_nested() {
String jsonInput = "{\"name\":\"rob\",\"age\":12,\"org\":{\"name\":\"superorg\",\"rating\":4},\"nums\":[1,2,3]}";
Object result = EJson.parse(jsonInput);
Assert.assertTrue(result instanceof Map);
Map<?,?> map = (Map<?,?>)result;
Assert.assertEquals(4, map.size());
Assert.assertEquals("rob", map.get("name"));
Assert.assertEquals(12L, map.get("age"));
Map<?,?> org = (Map<?,?>)map.get("org");
Assert.assertEquals("superorg", org.get("name"));
Assert.assertEquals(4L, org.get("rating"));
List<?> nums = (List<?>)map.get("nums");
Assert.assertEquals(3, nums.size());
Assert.assertEquals(1L, nums.get(0));
Assert.assertEquals(2L, nums.get(1));
Assert.assertEquals(3L, nums.get(2));
String jsonOutput = EJson.write(result);
Assert.assertEquals(jsonInput, jsonOutput);
}
@Test
public void test_map_withNull() {
String jsonInput = "{\"name\":\"rob\",\"age\":null}";
Object result = EJson.parse(jsonInput);
Assert.assertTrue(result instanceof Map);
Map<?,?> map = (Map<?,?>)result;
Assert.assertEquals("rob", map.get("name"));
Assert.assertNull(map.get("age"));
String jsonOutput = EJson.write(result);
Assert.assertEquals(jsonInput, jsonOutput);
}
@Test
public void test_list_simple() {
String jsonInput = "[\"name\",\"rob\",12,13]";
Object result = EJson.parse(jsonInput);
Assert.assertTrue(result instanceof List);
List<?> list = (List<?>)result;
Assert.assertEquals(4, list.size());
Assert.assertEquals("name", list.get(0));
Assert.assertEquals("rob", list.get(1));
Assert.assertEquals(12L, list.get(2));
Assert.assertEquals(13L, list.get(3));
String jsonOutput = EJson.write(result);
Assert.assertEquals(jsonInput, jsonOutput);
}
@SuppressWarnings("unchecked")
@Test
public void test_list_ofMaps() {
String jsonInput = "[{\"name\":\"rob\",\"age\":12},{\"name\":\"mike\",\"age\":13}]";
Object result = EJson.parse(jsonInput);
Assert.assertTrue(result instanceof List);
List<Map<?,?>> list = (List<Map<?,?>>)result;
Assert.assertEquals(2, list.size());
Assert.assertEquals("rob", list.get(0).get("name"));
Assert.assertEquals(12L, list.get(0).get("age"));
Assert.assertEquals("mike", list.get(1).get("name"));
Assert.assertEquals(13L, list.get(1).get("age"));
String jsonOutput = EJson.write(result);
Assert.assertEquals(jsonInput, jsonOutput);
}
}
@@ -0,0 +1,81 @@
package com.avaje.ebean.text;
import org.junit.Assert;
import org.junit.Test;
public class PathPropertiesTests {
@Test
public void test_noParentheses() {
PathProperties s0 = PathProperties.parse("id,name");
Assert.assertEquals(1, s0.getPaths().size());
Assert.assertTrue(s0.get(null).contains("id"));
Assert.assertTrue(s0.get(null).contains("name"));
Assert.assertFalse(s0.get(null).contains("status"));
}
@Test
public void test_noParentheses_needTrim() {
PathProperties s0 = PathProperties.parse(" id, name ");
Assert.assertEquals(1, s0.getPaths().size());
Assert.assertTrue(s0.get(null).contains("id"));
Assert.assertTrue(s0.get(null).contains("name"));
Assert.assertFalse(s0.get(null).contains("status"));
}
@Test
public void test_withParentheses() {
PathProperties s0 = PathProperties.parse("(id,name)");
Assert.assertEquals(1, s0.getPaths().size());
Assert.assertTrue(s0.get(null).contains("id"));
Assert.assertTrue(s0.get(null).contains("name"));
Assert.assertFalse(s0.get(null).contains("status"));
}
@Test
public void test_withColon() {
PathProperties s0 = PathProperties.parse(":(id,name)");
Assert.assertEquals(1, s0.getPaths().size());
Assert.assertTrue(s0.get(null).contains("id"));
Assert.assertTrue(s0.get(null).contains("name"));
Assert.assertFalse(s0.get(null).contains("status"));
}
@Test
public void test_nested() {
PathProperties s1 = PathProperties.parse("id,name,shipAddr(*)");
Assert.assertEquals(2, s1.getPaths().size());
Assert.assertEquals(3, s1.get(null).size());
Assert.assertTrue(s1.get(null).contains("id"));
Assert.assertTrue(s1.get(null).contains("name"));
Assert.assertTrue(s1.get(null).contains("shipAddr"));
Assert.assertTrue(s1.get("shipAddr").contains("*"));
Assert.assertEquals(1, s1.get("shipAddr").size());
}
@Test
public void test_withParenthesesColonNested() {
PathProperties s1 = PathProperties.parse(":(id,name,shipAddr(*))");
Assert.assertEquals(2, s1.getPaths().size());
Assert.assertEquals(3, s1.get(null).size());
Assert.assertTrue(s1.get(null).contains("id"));
Assert.assertTrue(s1.get(null).contains("name"));
Assert.assertTrue(s1.get(null).contains("shipAddr"));
Assert.assertTrue(s1.get("shipAddr").contains("*"));
Assert.assertEquals(1, s1.get("shipAddr").size());
}
}
@@ -1,31 +0,0 @@
package com.avaje.ebean.text;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
public class TestPathPropertiesParse extends BaseTestCase {
@Test
public void test() {
PathProperties s0 = PathProperties.parse("(id,name)");
Assert.assertEquals(1, s0.getPaths().size());
Assert.assertTrue(s0.get(null).contains("id"));
Assert.assertTrue(s0.get(null).contains("name"));
Assert.assertFalse(s0.get(null).contains("status"));
PathProperties s1 = PathProperties.parse(":(id,name,shipAddr(*))");
Assert.assertEquals(2, s1.getPaths().size());
Assert.assertEquals(3, s1.get(null).size());
Assert.assertTrue(s1.get(null).contains("id"));
Assert.assertTrue(s1.get(null).contains("name"));
Assert.assertTrue(s1.get(null).contains("shipAddr"));
Assert.assertTrue(s1.get("shipAddr").contains("*"));
Assert.assertEquals(1, s1.get("shipAddr").size());
}
}
@@ -0,0 +1,24 @@
package com.avaje.ebean.text.json;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.text.PathProperties;
import com.avaje.ebean.text.json.JsonWriteOptions;
public class JsonWriteOptionsTests {
@Test
public void test_parse() {
JsonWriteOptions options = JsonWriteOptions.parsePath("id,status,name");
PathProperties pathProps = options.getPathProperties();
Assert.assertEquals(1, pathProps.getPaths().size());
Assert.assertTrue(pathProps.get(null).contains("id"));
Assert.assertTrue(pathProps.get(null).contains("name"));
Assert.assertTrue(pathProps.get(null).contains("status"));
Assert.assertFalse(pathProps.get(null).contains("foo"));
}
}
@@ -4,8 +4,7 @@ import java.io.StringReader;
import java.util.List;
import java.util.Map;
import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;
import com.avaje.ebean.BaseTestCase;
@@ -26,9 +25,12 @@ public class TestTextJsonBeanReadVisitor extends BaseTestCase {
ResetBasicData.reset();
List<Customer> list = Ebean.find(Customer.class).select("id, name, status, shippingAddress")
.fetch("billingAddress", "line1, city").fetch("billingAddress.country", "*")
.fetch("contacts", "firstName,email").order().desc("id").findList();
List<Customer> list = Ebean.find(Customer.class)
.select("id, name, status, shippingAddress")
.fetch("billingAddress", "line1, city")
.fetch("billingAddress.country", "*")
.fetch("contacts", "firstName,email")
.order().desc("id").findList();
JsonContext json = Ebean.createJsonContext();