diff --git a/JSONObjectTest.java b/JSONObjectTest.java new file mode 100644 index 0000000..301f149 --- /dev/null +++ b/JSONObjectTest.java @@ -0,0 +1,161 @@ +package org.json.junit; + +import java.util.*; + +import org.json.*; +import org.junit.*; + + +public class JSONObjectTest { + + + @Test + public void jsonObjectByNames() { + String str = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + String[] keys = {"falseKey", "stringKey", "nullKey", "doubleKey"}; + String expectedStr = + "{"+ + "\"falseKey\":false,"+ + "\"nullKey\":null,"+ + "\"stringKey\":\"hello world!\","+ + "\"doubleKey\":-23.45e67"+ + "}"; + JSONObject jsonObject = new JSONObject(str); + JSONObject copyJsonObject = new JSONObject(jsonObject, keys); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(copyJsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByMap() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e67"+ + "}"; + Map jsonMap = new HashMap(); + jsonMap.put("trueKey", new Boolean(true)); + jsonMap.put("falseKey", new Boolean(false)); + jsonMap.put("stringKey", "hello world!"); + jsonMap.put("complexStringKey", "h\be\tllo w\u1234orld!"); + jsonMap.put("intKey", new Long(42)); + jsonMap.put("doubleKey", new Double(-23.45e67)); + + JSONObject jsonObject = new JSONObject(jsonMap); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByBean() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"falseKey\":false,"+ + "\"stringKey\":\"hello world!\","+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"intKey\":42,"+ + "\"doubleKey\":-23.45e7"+ + "}"; + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByBeanAndNames() { + String expectedStr = + "{"+ + "\"trueKey\":true,"+ + "\"complexStringKey\":\"h\be\tllo w\u1234orld!\","+ + "\"doubleKey\":-23.45e7"+ + "}"; + String[] keys = {"trueKey", "complexStringKey", "doubleKey"}; + MyBean myBean = new MyBean(); + JSONObject jsonObject = new JSONObject(myBean, keys); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectByResourceBundle() { + String expectedStr = + "{"+ + "\"greetings\": {"+ + "\"hello\":\"Hello, \","+ + "\"world\":\"World!\""+ + "},"+ + "\"farewells\": {"+ + "\"later\":\"Later, \","+ + "\"gator\":\"Alligator!\""+ + "}"+ + "}"; + JSONObject jsonObject = new + JSONObject("org.json.junit.StringsResourceBundle", + Locale.getDefault()); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAccumulate() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.accumulate("myArray", true); + jsonObject.accumulate("myArray", false); + jsonObject.accumulate("myArray", "hello world!"); + jsonObject.accumulate("myArray", "h\be\tllo w\u1234orld!"); + jsonObject.accumulate("myArray", 42); + jsonObject.accumulate("myArray", -23.45e7); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } + + @Test + public void jsonObjectAppend() { + String expectedStr = + "{"+ + "\"myArray\": ["+ + "true,"+ + "false,"+ + "\"hello world!\","+ + "\"h\be\tllo w\u1234orld!\","+ + "42,"+ + "-23.45e7"+ + "]"+ + "}"; + JSONObject jsonObject = new JSONObject(); + jsonObject.append("myArray", true); + jsonObject.append("myArray", false); + jsonObject.append("myArray", "hello world!"); + jsonObject.append("myArray", "h\be\tllo w\u1234orld!"); + jsonObject.append("myArray", 42); + jsonObject.append("myArray", -23.45e7); + JSONObject expectedJsonObject = new JSONObject(expectedStr); + Util.compareActualVsExpectedJsonObjects(jsonObject, expectedJsonObject); + } +} diff --git a/JunitTestSuite.java b/JunitTestSuite.java index 2519e81..63bde2f 100644 --- a/JunitTestSuite.java +++ b/JunitTestSuite.java @@ -11,7 +11,8 @@ import org.junit.runners.Suite; XMLTest.class, JSONMLTest.class, HTTPTest.class, - JSONStringerTest.class + JSONStringerTest.class, + JSONObjectTest.class }) public class JunitTestSuite { } \ No newline at end of file diff --git a/MyBean.java b/MyBean.java new file mode 100644 index 0000000..6477fde --- /dev/null +++ b/MyBean.java @@ -0,0 +1,37 @@ +package org.json.junit; + +public class MyBean { + public int intKey; + public double doubleKey; + public String stringKey; + public String complexStringKey; + public boolean trueKey; + public boolean falseKey; + + public MyBean() { + intKey = 42; + doubleKey = -23.45e7; + stringKey = "hello world!"; + complexStringKey = "h\be\tllo w\u1234orld!"; + trueKey = true; + falseKey = false; + } + public int getIntKey() { + return intKey; + } + public double getDoubleKey() { + return doubleKey; + } + public String getStringKey() { + return stringKey; + } + public String getComplexStringKey() { + return complexStringKey; + } + public boolean isTrueKey() { + return trueKey; + } + public boolean isFalseKey() { + return falseKey; + } +} diff --git a/StringsResourceBundle.java b/StringsResourceBundle.java new file mode 100644 index 0000000..b1b9df4 --- /dev/null +++ b/StringsResourceBundle.java @@ -0,0 +1,15 @@ +package org.json.junit; + +import java.util.*; + +public class StringsResourceBundle extends ListResourceBundle { + public Object[][] getContents() { + return contents; + } + static final Object[][] contents = { + {"greetings.hello", "Hello, "}, + {"greetings.world", "World!"}, + {"farewells.later", "Later, "}, + {"farewells.gator", "Alligator!"} + }; +} \ No newline at end of file