diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 80283ae..0579acb 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -37,6 +37,8 @@ import org.json.junit.data.Fraction; import org.json.junit.data.GenericBean; import org.json.junit.data.GenericBeanInt; import org.json.junit.data.MyBean; +import org.json.junit.data.MyBeanCustomName; +import org.json.junit.data.MyBeanCustomNameSubClass; import org.json.junit.data.MyBigNumberBean; import org.json.junit.data.MyEnum; import org.json.junit.data.MyEnumField; @@ -371,7 +373,7 @@ public class JSONObjectTest { /** - * Verifies that the put Map has backwards compatability with RAW types pre-java5. + * Verifies that the put Map has backwards compatibility with RAW types pre-java5. */ @Test public void verifyPutMap() { @@ -467,7 +469,7 @@ public class JSONObjectTest { */ @SuppressWarnings("boxing") @Test - public void jsonObjectByBean() { + public void jsonObjectByBean1() { /** * Default access classes have to be mocked since JSONObject, which is * not in the same package, cannot call MyBean methods by reflection. @@ -501,6 +503,73 @@ public class JSONObjectTest { assertTrue("expected 0 callbacks[1] items", ((Map)(JsonPath.read(doc, "$.callbacks[1]"))).size() == 0); } + /** + * JSONObject built from a bean that has custom field names. + */ + @Test + public void jsonObjectByBean2() { + JSONObject jsonObject = new JSONObject(new MyBeanCustomName()); + assertNotNull(jsonObject); + assertEquals("Wrong number of keys found:", + 5, + jsonObject.keySet().size()); + assertFalse("Normal field name (someString) processing did not work", + jsonObject.has("someString")); + assertFalse("Normal field name (myDouble) processing did not work", + jsonObject.has("myDouble")); + assertFalse("Normal field name (someFloat) found", + jsonObject.has("someFloat")); + assertFalse("Ignored field found!", + jsonObject.has("ignoredInt")); + assertTrue("Normal field name (someInt) processing did not work", + jsonObject.has("someInt")); + assertTrue("Normal field name (someLong) processing did not work", + jsonObject.has("someLong")); + assertTrue("Overridden String field name (myStringField) not found", + jsonObject.has("myStringField")); + assertTrue("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) not found", + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); + assertTrue("Overridden String field name (InterfaceField) not found", + jsonObject.has("InterfaceField")); + } + + /** + * JSONObject built from a bean that has custom field names inherited from a parent class. + */ + @Test + public void jsonObjectByBean3() { + JSONObject jsonObject = new JSONObject(new MyBeanCustomNameSubClass()); + assertNotNull(jsonObject); + assertEquals("Wrong number of keys found:", + 7, + jsonObject.keySet().size()); + assertFalse("Normal int field name (someInt) found, but was overridden", + jsonObject.has("someInt")); + assertFalse("Normal field name (myDouble) processing did not work", + jsonObject.has("myDouble")); + assertFalse("Overridden String field name (Some Weird NAme that Normally Wouldn't be possible!) FOUND!", + jsonObject.has("Some Weird NAme that Normally Wouldn't be possible!")); + assertFalse("Normal field name (someFloat) found", + jsonObject.has("someFloat")); + assertFalse("Ignored field found!", + jsonObject.has("ignoredInt")); + assertFalse("Ignored field at the same level as forced name found", + jsonObject.has("ShouldBeIgnored")); + assertTrue("Overridden int field name (newIntFieldName) not found", + jsonObject.has("newIntFieldName")); + assertTrue("Normal field name (someLong) processing did not work", + jsonObject.has("someLong")); + assertTrue("Overridden String field name (myStringField) not found", + jsonObject.has("myStringField")); + assertTrue(jsonObject.has("AMoreNormalName")); + assertTrue("Overridden String field name (InterfaceField) not found", + jsonObject.has("InterfaceField")); + assertTrue("Forced field not found!", + jsonObject.has("forcedInt")); + assertTrue("Normally ignored field (getable) with explicit property name not found", + jsonObject.has("Getable")); + } + /** * A bean is also an object. But in order to test the JSONObject * ctor that takes an object and a list of names, @@ -541,7 +610,7 @@ public class JSONObjectTest { assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later"))); assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator"))); } - + /** * Exercise the JSONObject.accumulate() method */ @@ -2857,7 +2926,7 @@ public class JSONObjectTest { public void testGenericIntBean() { GenericBeanInt bean = new GenericBeanInt(42); final JSONObject jo = new JSONObject(bean); - assertEquals(jo.keySet().toString(), 9, jo.length()); + assertEquals(jo.keySet().toString(), 10, jo.length()); assertEquals(42, jo.get("genericValue")); assertEquals("Expected the getter to only be called once", 1, bean.genericGetCounter); diff --git a/src/test/java/org/json/junit/data/GenericBean.java b/src/test/java/org/json/junit/data/GenericBean.java index 4740030..da6370d 100644 --- a/src/test/java/org/json/junit/data/GenericBean.java +++ b/src/test/java/org/json/junit/data/GenericBean.java @@ -20,7 +20,7 @@ public class GenericBean> implements MyBean { } /** */ - private T genericValue; + protected T genericValue; /** to be used by the calling test to see how often the getter is called */ public int genericGetCounter; /** to be used by the calling test to see how often the setter is called */ diff --git a/src/test/java/org/json/junit/data/GenericBeanInt.java b/src/test/java/org/json/junit/data/GenericBeanInt.java index 8f0248d..5056611 100644 --- a/src/test/java/org/json/junit/data/GenericBeanInt.java +++ b/src/test/java/org/json/junit/data/GenericBeanInt.java @@ -13,7 +13,7 @@ public class GenericBeanInt extends GenericBean { /** @return the a */ public char getA() { - return a; + return this.a; } /** @@ -25,6 +25,33 @@ public class GenericBeanInt extends GenericBean { return false; } + /** + * Should not be beanable + * + * @return false + */ + public boolean get() { + return false; + } + + /** + * Should not be beanable + * + * @return false + */ + public boolean is() { + return false; + } + + /** + * Should be beanable + * + * @return false + */ + public boolean isB() { + return this.genericValue.equals((Integer.valueOf(this.a+1))); + } + /** * @param genericValue * the value to initiate with. diff --git a/src/test/java/org/json/junit/data/MyBeanCustomName.java b/src/test/java/org/json/junit/data/MyBeanCustomName.java new file mode 100644 index 0000000..56756c2 --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomName.java @@ -0,0 +1,20 @@ +package org.json.junit.data; + +import org.json.JSONPropertyName; + +/** + * Test bean for the {@link JSONPropertyName} annotation. + */ +public class MyBeanCustomName implements MyBeanCustomNameInterface { + public int getSomeInt() { return 42; } + @JSONPropertyName("") + public long getSomeLong() { return 42L; } + @JSONPropertyName("myStringField") + public String getSomeString() { return "someStringValue"; } + @JSONPropertyName("Some Weird NAme that Normally Wouldn't be possible!") + public double getMyDouble() { return 0.0d; } + @Override + public float getSomeFloat() { return 2.0f; } + @Override + public int getIgnoredInt() { return 40; } +} diff --git a/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java b/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java new file mode 100644 index 0000000..b25b578 --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomNameInterface.java @@ -0,0 +1,11 @@ +package org.json.junit.data; + +import org.json.JSONPropertyIgnore; +import org.json.JSONPropertyName; + +public interface MyBeanCustomNameInterface { + @JSONPropertyName("InterfaceField") + float getSomeFloat(); + @JSONPropertyIgnore + int getIgnoredInt(); +} \ No newline at end of file diff --git a/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java b/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java new file mode 100644 index 0000000..8f0500c --- /dev/null +++ b/src/test/java/org/json/junit/data/MyBeanCustomNameSubClass.java @@ -0,0 +1,32 @@ +/** + * + */ +package org.json.junit.data; + +import org.json.JSONPropertyIgnore; +import org.json.JSONPropertyName; + +/** + * Test bean to verify that the {@link org.json.JSONPropertyName} annotation + * is inherited. + */ +public class MyBeanCustomNameSubClass extends MyBeanCustomName { + @Override + @JSONPropertyName("forcedInt") + public int getIgnoredInt() { return 42*42; } + @Override + @JSONPropertyName("newIntFieldName") + public int getSomeInt() { return 43; } + @Override + public String getSomeString() { return "subClassString"; } + @Override + @JSONPropertyName("AMoreNormalName") + public double getMyDouble() { return 1.0d; } + @Override + public float getSomeFloat() { return 3.0f; } + @JSONPropertyIgnore + @JSONPropertyName("ShouldBeIgnored") + public boolean getShouldNotBeJSON() { return true; } + @JSONPropertyName("Getable") + public boolean getable() { return true; } +}