mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
in progress
This commit is contained in:
parent
2219b5919b
commit
22d5fd3aed
4 changed files with 215 additions and 1 deletions
161
JSONObjectTest.java
Normal file
161
JSONObjectTest.java
Normal file
|
@ -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<String, Object> jsonMap = new HashMap<String, Object>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,8 @@ import org.junit.runners.Suite;
|
||||||
XMLTest.class,
|
XMLTest.class,
|
||||||
JSONMLTest.class,
|
JSONMLTest.class,
|
||||||
HTTPTest.class,
|
HTTPTest.class,
|
||||||
JSONStringerTest.class
|
JSONStringerTest.class,
|
||||||
|
JSONObjectTest.class
|
||||||
})
|
})
|
||||||
public class JunitTestSuite {
|
public class JunitTestSuite {
|
||||||
}
|
}
|
37
MyBean.java
Normal file
37
MyBean.java
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
15
StringsResourceBundle.java
Normal file
15
StringsResourceBundle.java
Normal file
|
@ -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!"}
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue