mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
* Update link in the README to the main JSON-Java repo
* Cleans up some warnings * Adds new test for bug https://github.com/stleary/JSON-java/issues/332 * Adds some resource handling for string writers using pre-java1.7 support. I know StringWriters don't need a close method called, but the tests should still handle their resources properly.
This commit is contained in:
parent
f6ab6d7b27
commit
9df5d34bbe
10 changed files with 2878 additions and 2703 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Unit tests to validate the JSON-Java GitHub project code<br>
|
Unit tests to validate the JSON-Java GitHub project code<br>
|
||||||
|
|
||||||
https://github.com/douglascrockford/JSON-java<br>
|
https://github.com/stleary/JSON-java<br>
|
||||||
|
|
||||||
Gradle and Eclipse is the recommended build tool and IDE.<br>
|
Gradle and Eclipse is the recommended build tool and IDE.<br>
|
||||||
Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or execute the **TestRunner** application directly.<br>
|
Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or execute the **TestRunner** application directly.<br>
|
||||||
|
|
|
@ -5,7 +5,6 @@ import static org.junit.Assert.assertTrue;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
@ -92,7 +91,7 @@ public class EnumTest {
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
||||||
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
||||||
assertTrue("expected VAL3", myEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -61,7 +61,7 @@ public class JSONArrayTest {
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected=NullPointerException.class)
|
||||||
public void nullException() {
|
public void nullException() {
|
||||||
String str = null;
|
String str = null;
|
||||||
new JSONArray(str);
|
assertNull("Should throw an exception", new JSONArray(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,8 +72,7 @@ public class JSONArrayTest {
|
||||||
public void emptStr() {
|
public void emptStr() {
|
||||||
String str = "";
|
String str = "";
|
||||||
try {
|
try {
|
||||||
new JSONArray(str);
|
assertNull("Should throw an exception", new JSONArray(str));
|
||||||
assertTrue("Should throw an exception", false);
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertTrue("Expected an exception message",
|
assertTrue("Expected an exception message",
|
||||||
"A JSONArray text must start with '[' at 1 [character 2 line 1]".
|
"A JSONArray text must start with '[' at 1 [character 2 line 1]".
|
||||||
|
@ -90,8 +89,7 @@ public class JSONArrayTest {
|
||||||
public void badObject() {
|
public void badObject() {
|
||||||
String str = "abc";
|
String str = "abc";
|
||||||
try {
|
try {
|
||||||
new JSONArray((Object)str);
|
assertNull("Should throw an exception", new JSONArray((Object)str));
|
||||||
assertTrue("Should throw an exception", false);
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertTrue("Expected an exception message",
|
assertTrue("Expected an exception message",
|
||||||
"JSONArray initial value should be a string or collection or array.".
|
"JSONArray initial value should be a string or collection or array.".
|
||||||
|
@ -100,7 +98,7 @@ public class JSONArrayTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the constructor has backwards compatability with RAW types pre-java5.
|
* Verifies that the constructor has backwards compatibility with RAW types pre-java5.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifyConstructor() {
|
public void verifyConstructor() {
|
||||||
|
@ -130,7 +128,7 @@ public class JSONArrayTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the put Collection has backwards compatability with RAW types pre-java5.
|
* Verifies that the put Collection has backwards compatibility with RAW types pre-java5.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifyPutCollection() {
|
public void verifyPutCollection() {
|
||||||
|
@ -164,7 +162,7 @@ public class JSONArrayTest {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
@Test
|
||||||
public void verifyPutMap() {
|
public void verifyPutMap() {
|
||||||
|
@ -209,9 +207,10 @@ public class JSONArrayTest {
|
||||||
* Create a JSONArray doc with a variety of different elements.
|
* Create a JSONArray doc with a variety of different elements.
|
||||||
* Confirm that the values can be accessed via the get[type]() API methods
|
* Confirm that the values can be accessed via the get[type]() API methods
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void getArrayValues() {
|
public void getArrayValues() {
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
// booleans
|
// booleans
|
||||||
assertTrue("Array true",
|
assertTrue("Array true",
|
||||||
true == jsonArray.getBoolean(0));
|
true == jsonArray.getBoolean(0));
|
||||||
|
@ -255,7 +254,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void failedGetArrayValues() {
|
public void failedGetArrayValues() {
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
try {
|
try {
|
||||||
jsonArray.getBoolean(4);
|
jsonArray.getBoolean(4);
|
||||||
assertTrue("expected getBoolean to fail", false);
|
assertTrue("expected getBoolean to fail", false);
|
||||||
|
@ -321,7 +320,7 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void join() {
|
public void join() {
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
String joinStr = jsonArray.join(",");
|
String joinStr = jsonArray.join(",");
|
||||||
|
|
||||||
// validate JSON
|
// validate JSON
|
||||||
|
@ -357,7 +356,7 @@ public class JSONArrayTest {
|
||||||
public void length() {
|
public void length() {
|
||||||
assertTrue("expected empty JSONArray length 0",
|
assertTrue("expected empty JSONArray length 0",
|
||||||
new JSONArray().length() == 0);
|
new JSONArray().length() == 0);
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
assertTrue("expected JSONArray length 13", jsonArray.length() == 13);
|
assertTrue("expected JSONArray length 13", jsonArray.length() == 13);
|
||||||
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
|
JSONArray nestedJsonArray = jsonArray.getJSONArray(9);
|
||||||
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
|
assertTrue("expected JSONArray length 1", nestedJsonArray.length() == 1);
|
||||||
|
@ -368,9 +367,10 @@ public class JSONArrayTest {
|
||||||
* Confirm that the values can be accessed via the opt[type](index)
|
* Confirm that the values can be accessed via the opt[type](index)
|
||||||
* and opt[type](index, default) API methods.
|
* and opt[type](index, default) API methods.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void opt() {
|
public void opt() {
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
assertTrue("Array opt value true",
|
assertTrue("Array opt value true",
|
||||||
Boolean.TRUE == jsonArray.opt(0));
|
Boolean.TRUE == jsonArray.opt(0));
|
||||||
assertTrue("Array opt value out of range",
|
assertTrue("Array opt value out of range",
|
||||||
|
@ -441,6 +441,7 @@ public class JSONArrayTest {
|
||||||
* Exercise the JSONArray.put(value) method with various parameters
|
* Exercise the JSONArray.put(value) method with various parameters
|
||||||
* and confirm the resulting JSONArray.
|
* and confirm the resulting JSONArray.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void put() {
|
public void put() {
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
|
@ -516,6 +517,7 @@ public class JSONArrayTest {
|
||||||
* Exercise the JSONArray.put(index, value) method with various parameters
|
* Exercise the JSONArray.put(index, value) method with various parameters
|
||||||
* and confirm the resulting JSONArray.
|
* and confirm the resulting JSONArray.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void putIndex() {
|
public void putIndex() {
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
|
@ -596,11 +598,11 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void remove() {
|
public void remove() {
|
||||||
String arrayStr =
|
String arrayStr1 =
|
||||||
"["+
|
"["+
|
||||||
"1"+
|
"1"+
|
||||||
"]";
|
"]";
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(arrayStr1);
|
||||||
jsonArray.remove(0);
|
jsonArray.remove(0);
|
||||||
assertTrue("array should be empty", null == jsonArray.remove(5));
|
assertTrue("array should be empty", null == jsonArray.remove(5));
|
||||||
assertTrue("jsonArray should be empty", jsonArray.length() == 0);
|
assertTrue("jsonArray should be empty", jsonArray.length() == 0);
|
||||||
|
@ -612,11 +614,11 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void notSimilar() {
|
public void notSimilar() {
|
||||||
String arrayStr =
|
String arrayStr1 =
|
||||||
"["+
|
"["+
|
||||||
"1"+
|
"1"+
|
||||||
"]";
|
"]";
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(arrayStr1);
|
||||||
JSONArray otherJsonArray = new JSONArray();
|
JSONArray otherJsonArray = new JSONArray();
|
||||||
assertTrue("arrays lengths differ", !jsonArray.similar(otherJsonArray));
|
assertTrue("arrays lengths differ", !jsonArray.similar(otherJsonArray));
|
||||||
|
|
||||||
|
@ -745,9 +747,10 @@ public class JSONArrayTest {
|
||||||
/**
|
/**
|
||||||
* Exercise the JSONArray iterator.
|
* Exercise the JSONArray iterator.
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void iterator() {
|
public void iterator() {
|
||||||
JSONArray jsonArray = new JSONArray(arrayStr);
|
JSONArray jsonArray = new JSONArray(this.arrayStr);
|
||||||
Iterator<Object> it = jsonArray.iterator();
|
Iterator<Object> it = jsonArray.iterator();
|
||||||
assertTrue("Array true",
|
assertTrue("Array true",
|
||||||
Boolean.TRUE.equals(it.next()));
|
Boolean.TRUE.equals(it.next()));
|
||||||
|
@ -803,16 +806,20 @@ public class JSONArrayTest {
|
||||||
* Exercise the JSONArray write() method
|
* Exercise the JSONArray write() method
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void write() {
|
public void write() throws IOException {
|
||||||
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
|
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
|
||||||
JSONArray jsonArray = new JSONArray(str);
|
JSONArray jsonArray = new JSONArray(str);
|
||||||
String expectedStr = str;
|
String expectedStr = str;
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
Writer writer = jsonArray.write(stringWriter);
|
try {
|
||||||
String actualStr = writer.toString();
|
jsonArray.write(stringWriter);
|
||||||
assertTrue("write() expected " + expectedStr +
|
String actualStr = stringWriter.toString();
|
||||||
" but found " + actualStr,
|
assertTrue("write() expected " + expectedStr +
|
||||||
expectedStr.equals(actualStr));
|
" but found " + actualStr,
|
||||||
|
expectedStr.equals(actualStr));
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -837,7 +844,7 @@ public class JSONArrayTest {
|
||||||
* Exercise the JSONArray write(Writer, int, int) method
|
* Exercise the JSONArray write(Writer, int, int) method
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void write3Param() {
|
public void write3Param() throws IOException {
|
||||||
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
|
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
|
||||||
String str2 =
|
String str2 =
|
||||||
"[\n" +
|
"[\n" +
|
||||||
|
@ -852,15 +859,20 @@ public class JSONArrayTest {
|
||||||
JSONArray jsonArray = new JSONArray(str0);
|
JSONArray jsonArray = new JSONArray(str0);
|
||||||
String expectedStr = str0;
|
String expectedStr = str0;
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
Writer writer = jsonArray.write(stringWriter, 0, 0);
|
try {
|
||||||
String actualStr = writer.toString();
|
String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
|
||||||
assertEquals(expectedStr, actualStr);
|
assertEquals(expectedStr, actualStr);
|
||||||
|
} finally {
|
||||||
expectedStr = str2;
|
stringWriter.close();
|
||||||
|
}
|
||||||
stringWriter = new StringWriter();
|
stringWriter = new StringWriter();
|
||||||
writer = jsonArray.write(stringWriter, 2, 1);
|
try {
|
||||||
actualStr = writer.toString();
|
expectedStr = str2;
|
||||||
assertEquals(expectedStr, actualStr);
|
String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
|
||||||
|
assertEquals(expectedStr, actualStr);
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -917,49 +929,49 @@ public class JSONArrayTest {
|
||||||
"]";
|
"]";
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
JSONArray jsonArray = new JSONArray(jsonArrayStr);
|
||||||
List list = jsonArray.toList();
|
List<?> list = jsonArray.toList();
|
||||||
|
|
||||||
assertTrue("List should not be null", list != null);
|
assertTrue("List should not be null", list != null);
|
||||||
assertTrue("List should have 3 elements", list.size() == 3);
|
assertTrue("List should have 3 elements", list.size() == 3);
|
||||||
|
|
||||||
List val1List = (List) list.get(0);
|
List<?> val1List = (List<?>) list.get(0);
|
||||||
assertTrue("val1 should not be null", val1List != null);
|
assertTrue("val1 should not be null", val1List != null);
|
||||||
assertTrue("val1 should have 3 elements", val1List.size() == 3);
|
assertTrue("val1 should have 3 elements", val1List.size() == 3);
|
||||||
|
|
||||||
assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1)));
|
assertTrue("val1 value 1 should be 1", val1List.get(0).equals(Integer.valueOf(1)));
|
||||||
assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2)));
|
assertTrue("val1 value 2 should be 2", val1List.get(1).equals(Integer.valueOf(2)));
|
||||||
|
|
||||||
Map key1Value3Map = (Map)val1List.get(2);
|
Map<?,?> key1Value3Map = (Map<?,?>)val1List.get(2);
|
||||||
assertTrue("Map should not be null", key1Value3Map != null);
|
assertTrue("Map should not be null", key1Value3Map != null);
|
||||||
assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
|
assertTrue("Map should have 1 element", key1Value3Map.size() == 1);
|
||||||
assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));
|
assertTrue("Map key3 should be true", key1Value3Map.get("key3").equals(Boolean.TRUE));
|
||||||
|
|
||||||
Map val2Map = (Map) list.get(1);
|
Map<?,?> val2Map = (Map<?,?>) list.get(1);
|
||||||
assertTrue("val2 should not be null", val2Map != null);
|
assertTrue("val2 should not be null", val2Map != null);
|
||||||
assertTrue("val2 should have 4 elements", val2Map.size() == 4);
|
assertTrue("val2 should have 4 elements", val2Map.size() == 4);
|
||||||
assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));
|
assertTrue("val2 map key 1 should be val1", val2Map.get("key1").equals("val1"));
|
||||||
assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42)));
|
assertTrue("val2 map key 3 should be 42", val2Map.get("key3").equals(Integer.valueOf(42)));
|
||||||
|
|
||||||
Map val2Key2Map = (Map)val2Map.get("key2");
|
Map<?,?> val2Key2Map = (Map<?,?>)val2Map.get("key2");
|
||||||
assertTrue("val2 map key 2 should not be null", val2Key2Map != null);
|
assertTrue("val2 map key 2 should not be null", val2Key2Map != null);
|
||||||
assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2"));
|
assertTrue("val2 map key 2 should have an entry", val2Key2Map.containsKey("key2"));
|
||||||
assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null);
|
assertTrue("val2 map key 2 value should be null", val2Key2Map.get("key2") == null);
|
||||||
|
|
||||||
List val2Key4List = (List)val2Map.get("key4");
|
List<?> val2Key4List = (List<?>)val2Map.get("key4");
|
||||||
assertTrue("val2 map key 4 should not be null", val2Key4List != null);
|
assertTrue("val2 map key 4 should not be null", val2Key4List != null);
|
||||||
assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty());
|
assertTrue("val2 map key 4 should be empty", val2Key4List.isEmpty());
|
||||||
|
|
||||||
List val3List = (List) list.get(2);
|
List<?> val3List = (List<?>) list.get(2);
|
||||||
assertTrue("val3 should not be null", val3List != null);
|
assertTrue("val3 should not be null", val3List != null);
|
||||||
assertTrue("val3 should have 2 elements", val3List.size() == 2);
|
assertTrue("val3 should have 2 elements", val3List.size() == 2);
|
||||||
|
|
||||||
List val3Val1List = (List)val3List.get(0);
|
List<?> val3Val1List = (List<?>)val3List.get(0);
|
||||||
assertTrue("val3 list val 1 should not be null", val3Val1List != null);
|
assertTrue("val3 list val 1 should not be null", val3Val1List != null);
|
||||||
assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
|
assertTrue("val3 list val 1 should have 2 elements", val3Val1List.size() == 2);
|
||||||
assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));
|
assertTrue("val3 list val 1 list element 1 should be value1", val3Val1List.get(0).equals("value1"));
|
||||||
assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1")));
|
assertTrue("val3 list val 1 list element 2 should be 2.1", val3Val1List.get(1).equals(Double.valueOf("2.1")));
|
||||||
|
|
||||||
List val3Val2List = (List)val3List.get(1);
|
List<?> val3Val2List = (List<?>)val3List.get(1);
|
||||||
assertTrue("val3 list val 2 should not be null", val3Val2List != null);
|
assertTrue("val3 list val 2 should not be null", val3Val2List != null);
|
||||||
assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1);
|
assertTrue("val3 list val 2 should have 1 element", val3Val2List.size() == 1);
|
||||||
assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null);
|
assertTrue("val3 list val 2 list element 1 should be null", val3Val2List.get(0) == null);
|
||||||
|
|
|
@ -711,8 +711,7 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON string cannot be reverted to original xml. See test result in
|
* JSON string cannot be reverted to original xml when type guessing is used.
|
||||||
* comment below.
|
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testToJSONArray_reversibility() {
|
public void testToJSONArray_reversibility() {
|
||||||
|
@ -722,10 +721,11 @@ public class JSONMLTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* test passes when using the new method toJsonML.
|
* JSON string cannot be reverted to original xml when type guessing is used.
|
||||||
|
* When we force all the values as string, the original text comes back.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testToJsonML() {
|
public void testToJSONArray_reversibility2() {
|
||||||
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
|
||||||
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
|
final String expectedJsonString = "[\"root\",[\"id\",\"01\"],[\"id\",\"1\"],[\"id\",\"00\"],[\"id\",\"0\"],[\"item\",{\"id\":\"01\"}],[\"title\",\"True\"]]";
|
||||||
final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
||||||
|
@ -735,4 +735,71 @@ public class JSONMLTest {
|
||||||
assertEquals(originalXml, reverseXml);
|
assertEquals(originalXml, reverseXml);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON can be reverted to original xml.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONArray_reversibility3() {
|
||||||
|
final String originalXml = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
|
||||||
|
final JSONArray jsonArray = JSONML.toJSONArray(originalXml, false);
|
||||||
|
final String revertedXml = JSONML.toString(jsonArray);
|
||||||
|
assertEquals(revertedXml, originalXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON string cannot be reverted to original xml. See test result in
|
||||||
|
* comment below.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testToJSONObject_reversibility() {
|
||||||
|
final String originalXml = "<readResult><errors someAttr=\"arrtValue\"><code>400</code></errors><errors><code>402</code></errors></readResult>";
|
||||||
|
final JSONObject originalObject=JSONML.toJSONObject(originalXml,false);
|
||||||
|
final String originalJson = originalObject.toString();
|
||||||
|
final String xml = JSONML.toString(originalObject);
|
||||||
|
final JSONObject revertedObject = JSONML.toJSONObject(xml, false);
|
||||||
|
final String newJson = revertedObject.toString();
|
||||||
|
assertTrue("JSON Objects are not similar",originalObject.similar(revertedObject));
|
||||||
|
assertEquals("original JSON does not equal the new JSON",originalJson, newJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test texts taken from jsonml.org. Currently our implementation FAILS this conversion but shouldn't.
|
||||||
|
* Technically JsonML should be able to transform any valid xhtml document, but ours only supports
|
||||||
|
* standard XML entities, not HTML entities.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testAttributeConversionReversabilityHTML() {
|
||||||
|
final String originalXml = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\"> <span style=\"background-color:maroon\">©</span> </td></tr></table>";
|
||||||
|
final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"\u00A0\",[\"span\",{ \"style\" : \"background-color:maroon\" },\"\u00A9\"],\"\u00A0\"]]]";
|
||||||
|
final JSONArray json = JSONML.toJSONArray(originalXml,true);
|
||||||
|
final String actualJsonString = json.toString();
|
||||||
|
|
||||||
|
final String reverseXml = JSONML.toString(json);
|
||||||
|
assertNotEquals(originalXml, reverseXml);
|
||||||
|
|
||||||
|
assertNotEquals(expectedJsonString, actualJsonString);
|
||||||
|
}
|
||||||
|
|
||||||
|
// this test does not pass for the following reasons:
|
||||||
|
// 1. Our XML parser does not handle generic HTML entities, only valid XML entities. Hence
|
||||||
|
// or other HTML specific entites would fail on reversability
|
||||||
|
// 2. Our JSON implementation for storing the XML attributes uses the standard unordered map.
|
||||||
|
// This means that <tag attr1="v1" attr2="v2" /> can not be reversed reliably.
|
||||||
|
// /**
|
||||||
|
// * Test texts taken from jsonml.org but modified to have XML entities only.
|
||||||
|
// */
|
||||||
|
// @Test
|
||||||
|
// public void testAttributeConversionReversabilityXML() {
|
||||||
|
// final String originalXml = "<table class=\"MyTable\" style=\"background-color:yellow\"><tr><td class=\"MyTD\" style=\"border:1px solid black\">#5D28D1</td><td class=\"MyTD\" style=\"background-color:red\">Example text here</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AF44EF</td><td class=\"MyTD\" style=\"background-color:green\">127310656</td></tr><tr><td class=\"MyTD\" style=\"border:1px solid black\">#AAD034</td><td class=\"MyTD\" style=\"background-color:blue\">&<span style=\"background-color:maroon\">></span><</td></tr></table>";
|
||||||
|
// final String expectedJsonString = "[\"table\",{\"class\" : \"MyTable\",\"style\" : \"background-color:yellow\"},[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#550758\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:red\"},\"Example text here\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#993101\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:green\"},\"127624015\"]],[\"tr\",[\"td\",{\"class\" : \"MyTD\",\"style\" : \"border:1px solid black\"},\"#E33D87\"],[\"td\",{\"class\" : \"MyTD\",\"style\" : \"background-color:blue\"},\"&\",[\"span\",{ \"style\" : \"background-color:maroon\" },\">\"],\"<\"]]]";
|
||||||
|
// final JSONArray jsonML = JSONML.toJSONArray(originalXml,true);
|
||||||
|
// final String actualJsonString = jsonML.toString();
|
||||||
|
//
|
||||||
|
// final String reverseXml = JSONML.toString(jsonML);
|
||||||
|
// // currently not equal because the hashing of the attribute objects makes the attribute
|
||||||
|
// // order not happen the same way twice
|
||||||
|
// assertEquals(originalXml, reverseXml);
|
||||||
|
//
|
||||||
|
// assertEquals(expectedJsonString, actualJsonString);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,310 +1,371 @@
|
||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.io.StringWriter;
|
||||||
|
import java.util.*;
|
||||||
import org.json.*;
|
|
||||||
import org.junit.Test;
|
import org.json.*;
|
||||||
|
import org.junit.Test;
|
||||||
/**
|
|
||||||
* Tests for JSONString implementations, and the difference between
|
/**
|
||||||
* {@link JSONObject#valueToString} and {@link JSONObject#writeValue}.
|
* Tests for JSONString implementations, and the difference between
|
||||||
*/
|
* {@link JSONObject#valueToString} and {@link JSONObject#writeValue}.
|
||||||
public class JSONStringTest {
|
*/
|
||||||
|
public class JSONStringTest {
|
||||||
/**
|
|
||||||
* This tests the JSONObject.writeValue() method. We can't test directly
|
/**
|
||||||
* due to it being a package-protected method. Instead, we can call
|
* This tests the JSONObject.writeValue() method. We can't test directly
|
||||||
* JSONArray.write(), which delegates the writing of each entry to
|
* due to it being a package-protected method. Instead, we can call
|
||||||
* writeValue().
|
* JSONArray.write(), which delegates the writing of each entry to
|
||||||
*/
|
* writeValue().
|
||||||
@Test
|
*/
|
||||||
public void writeValues() throws Exception {
|
@Test
|
||||||
JSONArray jsonArray = new JSONArray();
|
public void writeValues() throws Exception {
|
||||||
jsonArray.put((Object)null);
|
JSONArray jsonArray = new JSONArray();
|
||||||
|
jsonArray.put((Object)null);
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
String output = jsonArray.write(writer).toString();
|
StringWriter writer = new StringWriter();
|
||||||
assertTrue("String values should be equal", "[null]".equals(output));
|
try {
|
||||||
|
String output = jsonArray.write(writer).toString();
|
||||||
jsonArray = new JSONArray();
|
assertTrue("String values should be equal", "[null]".equals(output));
|
||||||
jsonArray.put(JSONObject.NULL);
|
|
||||||
writer = new StringWriter();
|
jsonArray = new JSONArray();
|
||||||
output = jsonArray.write(writer).toString();
|
jsonArray.put(JSONObject.NULL);
|
||||||
assertTrue("String values should be equal", "[null]".equals(output));
|
} finally {
|
||||||
|
writer.close();
|
||||||
jsonArray = new JSONArray();
|
}
|
||||||
jsonArray.put(new JSONObject());
|
writer = new StringWriter();
|
||||||
writer = new StringWriter();
|
try {
|
||||||
output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[{}]".equals(output));
|
assertTrue("String values should be equal", "[null]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(new JSONArray());
|
jsonArray.put(new JSONObject());
|
||||||
writer = new StringWriter();
|
} finally {
|
||||||
output = jsonArray.write(writer).toString();
|
writer.close();
|
||||||
assertTrue("String values should be equal", "[[]]".equals(output));
|
}
|
||||||
|
writer = new StringWriter();
|
||||||
jsonArray = new JSONArray();
|
try {
|
||||||
Map singleMap = Collections.singletonMap("key1", "value1");
|
String output = jsonArray.write(writer).toString();
|
||||||
jsonArray.put((Object)singleMap);
|
assertTrue("String values should be equal", "[{}]".equals(output));
|
||||||
writer = new StringWriter();
|
|
||||||
output = jsonArray.write(writer).toString();
|
jsonArray = new JSONArray();
|
||||||
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
|
jsonArray.put(new JSONArray());
|
||||||
|
} finally {
|
||||||
jsonArray = new JSONArray();
|
writer.close();
|
||||||
List singleList = Collections.singletonList("entry1");
|
}
|
||||||
jsonArray.put((Object)singleList);
|
writer = new StringWriter();
|
||||||
writer = new StringWriter();
|
try {
|
||||||
output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output));
|
assertTrue("String values should be equal", "[[]]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
int[] intArray = new int[] { 1, 2, 3 };
|
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
||||||
jsonArray.put(intArray);
|
jsonArray.put((Object)singleMap);
|
||||||
writer = new StringWriter();
|
} finally {
|
||||||
output = jsonArray.write(writer).toString();
|
writer.close();
|
||||||
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
|
}
|
||||||
|
writer = new StringWriter();
|
||||||
jsonArray = new JSONArray();
|
try {
|
||||||
jsonArray.put(24);
|
String output = jsonArray.write(writer).toString();
|
||||||
writer = new StringWriter();
|
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
|
||||||
output = jsonArray.write(writer).toString();
|
|
||||||
assertTrue("String values should be equal", "[24]".equals(output));
|
jsonArray = new JSONArray();
|
||||||
|
List<?> singleList = Collections.singletonList("entry1");
|
||||||
jsonArray = new JSONArray();
|
jsonArray.put((Object)singleList);
|
||||||
jsonArray.put("string value");
|
} finally {
|
||||||
writer = new StringWriter();
|
writer.close();
|
||||||
output = jsonArray.write(writer).toString();
|
}
|
||||||
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
jsonArray = new JSONArray();
|
String output = jsonArray.write(writer).toString();
|
||||||
jsonArray.put(true);
|
assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output));
|
||||||
writer = new StringWriter();
|
|
||||||
output = jsonArray.write(writer).toString();
|
jsonArray = new JSONArray();
|
||||||
assertTrue("String values should be equal", "[true]".equals(output));
|
int[] intArray = new int[] { 1, 2, 3 };
|
||||||
|
jsonArray.put(intArray);
|
||||||
}
|
} finally {
|
||||||
|
writer.close();
|
||||||
/**
|
}
|
||||||
* This tests the JSONObject.valueToString() method. These should be
|
writer = new StringWriter();
|
||||||
* identical to the values above, except for the enclosing [ and ].
|
try {
|
||||||
*/
|
String output = jsonArray.write(writer).toString();
|
||||||
@Test
|
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
|
||||||
public void valuesToString() throws Exception {
|
|
||||||
|
jsonArray = new JSONArray();
|
||||||
String output = JSONObject.valueToString(null);
|
jsonArray.put(24);
|
||||||
assertTrue("String values should be equal", "null".equals(output));
|
} finally {
|
||||||
|
writer.close();
|
||||||
output = JSONObject.valueToString(JSONObject.NULL);
|
}
|
||||||
assertTrue("String values should be equal", "null".equals(output));
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
output = JSONObject.valueToString(new JSONObject());
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "{}".equals(output));
|
assertTrue("String values should be equal", "[24]".equals(output));
|
||||||
|
|
||||||
output = JSONObject.valueToString(new JSONArray());
|
jsonArray = new JSONArray();
|
||||||
assertTrue("String values should be equal", "[]".equals(output));
|
jsonArray.put("string value");
|
||||||
|
} finally {
|
||||||
Map singleMap = Collections.singletonMap("key1", "value1");
|
writer.close();
|
||||||
output = JSONObject.valueToString(singleMap);
|
}
|
||||||
assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output));
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
List singleList = Collections.singletonList("entry1");
|
String output = jsonArray.write(writer).toString();
|
||||||
output = JSONObject.valueToString(singleList);
|
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
|
||||||
assertTrue("String values should be equal", "[\"entry1\"]".equals(output));
|
|
||||||
|
jsonArray = new JSONArray();
|
||||||
int[] intArray = new int[] { 1, 2, 3 };
|
jsonArray.put(true);
|
||||||
output = JSONObject.valueToString(intArray);
|
} finally {
|
||||||
assertTrue("String values should be equal", "[1,2,3]".equals(output));
|
writer.close();
|
||||||
|
}
|
||||||
output = JSONObject.valueToString(24);
|
writer = new StringWriter();
|
||||||
assertTrue("String values should be equal", "24".equals(output));
|
try {
|
||||||
|
String output = jsonArray.write(writer).toString();
|
||||||
output = JSONObject.valueToString("string value");
|
assertTrue("String values should be equal", "[true]".equals(output));
|
||||||
assertTrue("String values should be equal", "\"string value\"".equals(output));
|
} finally {
|
||||||
|
writer.close();
|
||||||
output = JSONObject.valueToString(true);
|
}
|
||||||
assertTrue("String values should be equal", "true".equals(output));
|
|
||||||
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* This tests the JSONObject.valueToString() method. These should be
|
||||||
* Test what happens when toJSONString() returns a well-formed JSON value.
|
* identical to the values above, except for the enclosing [ and ].
|
||||||
* This is the usual case.
|
*/
|
||||||
*/
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void testJSONStringValue() throws Exception {
|
public void valuesToString() throws Exception {
|
||||||
JSONStringValue jsonString = new JSONStringValue();
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
String output = JSONObject.valueToString(null);
|
||||||
|
assertTrue("String values should be equal", "null".equals(output));
|
||||||
jsonArray.put(jsonString);
|
|
||||||
|
output = JSONObject.valueToString(JSONObject.NULL);
|
||||||
StringWriter writer = new StringWriter();
|
assertTrue("String values should be equal", "null".equals(output));
|
||||||
String output = jsonArray.write(writer).toString();
|
|
||||||
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
|
output = JSONObject.valueToString(new JSONObject());
|
||||||
|
assertTrue("String values should be equal", "{}".equals(output));
|
||||||
output = JSONObject.valueToString(jsonString);
|
|
||||||
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
|
output = JSONObject.valueToString(new JSONArray());
|
||||||
}
|
assertTrue("String values should be equal", "[]".equals(output));
|
||||||
|
|
||||||
/**
|
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
||||||
* Test what happens when toJSONString() returns null. In one case,
|
output = JSONObject.valueToString(singleMap);
|
||||||
* use the object's toString() method. In the other, throw a JSONException.
|
assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output));
|
||||||
*/
|
|
||||||
@Test
|
List<?> singleList = Collections.singletonList("entry1");
|
||||||
public void testJSONNullStringValue() throws Exception {
|
output = JSONObject.valueToString(singleList);
|
||||||
JSONNullStringValue jsonString = new JSONNullStringValue();
|
assertTrue("String values should be equal", "[\"entry1\"]".equals(output));
|
||||||
JSONArray jsonArray = new JSONArray();
|
|
||||||
|
int[] intArray = new int[] { 1, 2, 3 };
|
||||||
jsonArray.put(jsonString);
|
output = JSONObject.valueToString(intArray);
|
||||||
|
assertTrue("String values should be equal", "[1,2,3]".equals(output));
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
String output = jsonArray.write(writer).toString();
|
output = JSONObject.valueToString(24);
|
||||||
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
|
assertTrue("String values should be equal", "24".equals(output));
|
||||||
|
|
||||||
// The only different between writeValue() and valueToString():
|
output = JSONObject.valueToString("string value");
|
||||||
// in this case, valueToString throws a JSONException
|
assertTrue("String values should be equal", "\"string value\"".equals(output));
|
||||||
try {
|
|
||||||
output = JSONObject.valueToString(jsonString);
|
output = JSONObject.valueToString(true);
|
||||||
fail("Expected an exception, got a String value");
|
assertTrue("String values should be equal", "true".equals(output));
|
||||||
} catch (Exception e) {
|
|
||||||
assertTrue("Expected JSONException", e instanceof JSONException);
|
}
|
||||||
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
|
|
||||||
}
|
/**
|
||||||
}
|
* Test what happens when toJSONString() returns a well-formed JSON value.
|
||||||
|
* This is the usual case.
|
||||||
/**
|
*/
|
||||||
* Test what happens when toJSONString() returns an exception. In both
|
@Test
|
||||||
* cases, a JSONException is thrown, with the cause and message set from
|
public void testJSONStringValue() throws Exception {
|
||||||
* the original exception.
|
JSONStringValue jsonString = new JSONStringValue();
|
||||||
*/
|
JSONArray jsonArray = new JSONArray();
|
||||||
@Test
|
|
||||||
public void testJSONStringExceptionValue() throws Exception {
|
jsonArray.put(jsonString);
|
||||||
JSONStringExceptionValue jsonString = new JSONStringExceptionValue();
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
jsonArray.put(jsonString);
|
String output = jsonArray.write(writer).toString();
|
||||||
|
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
String output = null;
|
output = JSONObject.valueToString(jsonString);
|
||||||
try {
|
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
|
||||||
output = jsonArray.write(writer).toString();
|
} finally {
|
||||||
fail("Expected an exception, got a String value");
|
writer.close();
|
||||||
} catch (Exception e) {
|
}
|
||||||
assertTrue("Expected JSONException", e instanceof JSONException);
|
}
|
||||||
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
|
|
||||||
}
|
/**
|
||||||
|
* Test what happens when toJSONString() returns null. In one case,
|
||||||
try {
|
* use the object's toString() method. In the other, throw a JSONException.
|
||||||
output = JSONObject.valueToString(jsonString);
|
*/
|
||||||
fail("Expected an exception, got a String value");
|
@Test
|
||||||
} catch (Exception e) {
|
public void testJSONNullStringValue() throws Exception {
|
||||||
assertTrue("Expected JSONException", e instanceof JSONException);
|
JSONNullStringValue jsonString = new JSONNullStringValue();
|
||||||
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
|
JSONArray jsonArray = new JSONArray();
|
||||||
}
|
|
||||||
}
|
jsonArray.put(jsonString);
|
||||||
|
|
||||||
/**
|
StringWriter writer = new StringWriter();
|
||||||
* Test what happens when a Java object's toString() returns a String value.
|
try {
|
||||||
* This is the usual case.
|
String output = jsonArray.write(writer).toString();
|
||||||
*/
|
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
|
||||||
@Test
|
|
||||||
public void testStringValue() throws Exception {
|
// The only different between writeValue() and valueToString():
|
||||||
StringValue nonJsonString = new StringValue();
|
// in this case, valueToString throws a JSONException
|
||||||
JSONArray jsonArray = new JSONArray();
|
try {
|
||||||
|
output = JSONObject.valueToString(jsonString);
|
||||||
jsonArray.put(nonJsonString);
|
fail("Expected an exception, got a String value");
|
||||||
|
} catch (Exception e) {
|
||||||
StringWriter writer = new StringWriter();
|
assertTrue("Expected JSONException", e instanceof JSONException);
|
||||||
String output = jsonArray.write(writer).toString();
|
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
|
||||||
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
|
}
|
||||||
|
} finally {
|
||||||
output = JSONObject.valueToString(nonJsonString);
|
writer.close();
|
||||||
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test what happens when a Java object's toString() returns null.
|
* Test what happens when toJSONString() returns an exception. In both
|
||||||
* Defaults to empty string.
|
* cases, a JSONException is thrown, with the cause and message set from
|
||||||
*/
|
* the original exception.
|
||||||
@Test
|
*/
|
||||||
public void testNullStringValue() throws Exception {
|
@Test
|
||||||
NullStringValue nonJsonString = new NullStringValue();
|
public void testJSONStringExceptionValue() throws IOException {
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONStringExceptionValue jsonString = new JSONStringExceptionValue();
|
||||||
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put(nonJsonString);
|
|
||||||
|
jsonArray.put(jsonString);
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
String output = jsonArray.write(writer).toString();
|
StringWriter writer = new StringWriter();
|
||||||
assertTrue("String values should be equal", "[\"\"]".equals(output));
|
try {
|
||||||
|
jsonArray.write(writer).toString();
|
||||||
output = JSONObject.valueToString(nonJsonString);
|
fail("Expected an exception, got a String value");
|
||||||
assertTrue("String values should be equal", "\"\"".equals(output));
|
} catch (JSONException e) {
|
||||||
}
|
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
|
||||||
|
} catch(Exception e) {
|
||||||
/**
|
fail("Expected JSONException");
|
||||||
* A JSONString that returns a valid JSON string value.
|
} finally {
|
||||||
*/
|
writer.close();
|
||||||
private static final class JSONStringValue implements JSONString {
|
}
|
||||||
|
|
||||||
@Override
|
try {
|
||||||
public String toJSONString() {
|
JSONObject.valueToString(jsonString);
|
||||||
return "\"the JSON string value\"";
|
fail("Expected an exception, got a String value");
|
||||||
}
|
} catch (JSONException e) {
|
||||||
|
assertTrue("Exception message does not match", "the exception value".equals(e.getMessage()));
|
||||||
@Override
|
} catch(Exception e) {
|
||||||
public String toString() {
|
fail("Expected JSONException");
|
||||||
return "the toString value for JSONStringValue";
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* Test what happens when a Java object's toString() returns a String value.
|
||||||
* A JSONString that returns null when calling toJSONString().
|
* This is the usual case.
|
||||||
*/
|
*/
|
||||||
private static final class JSONNullStringValue implements JSONString {
|
@Test
|
||||||
|
public void testStringValue() throws Exception {
|
||||||
@Override
|
StringValue nonJsonString = new StringValue();
|
||||||
public String toJSONString() {
|
JSONArray jsonArray = new JSONArray();
|
||||||
return null;
|
|
||||||
}
|
jsonArray.put(nonJsonString);
|
||||||
|
|
||||||
@Override
|
StringWriter writer = new StringWriter();
|
||||||
public String toString() {
|
try {
|
||||||
return "the toString value";
|
String output = jsonArray.write(writer).toString();
|
||||||
}
|
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
|
||||||
}
|
|
||||||
|
output = JSONObject.valueToString(nonJsonString);
|
||||||
/**
|
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
|
||||||
* A JSONString that throw an exception when calling toJSONString().
|
} finally {
|
||||||
*/
|
writer.close();
|
||||||
private static final class JSONStringExceptionValue implements JSONString {
|
}
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String toJSONString() {
|
/**
|
||||||
throw new IllegalStateException("the exception value");
|
* Test what happens when a Java object's toString() returns null.
|
||||||
}
|
* Defaults to empty string.
|
||||||
|
*/
|
||||||
@Override
|
@Test
|
||||||
public String toString() {
|
public void testNullStringValue() throws Exception {
|
||||||
return "the toString value for JSONStringExceptionValue";
|
NullStringValue nonJsonString = new NullStringValue();
|
||||||
}
|
JSONArray jsonArray = new JSONArray();
|
||||||
}
|
|
||||||
|
jsonArray.put(nonJsonString);
|
||||||
public static final class StringValue {
|
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
@Override
|
try {
|
||||||
public String toString() {
|
String output = jsonArray.write(writer).toString();
|
||||||
return "the toString value for StringValue";
|
assertTrue("String values should be equal", "[\"\"]".equals(output));
|
||||||
}
|
|
||||||
}
|
output = JSONObject.valueToString(nonJsonString);
|
||||||
|
assertTrue("String values should be equal", "\"\"".equals(output));
|
||||||
public static final class NullStringValue {
|
} finally {
|
||||||
|
writer.close();
|
||||||
@Override
|
}
|
||||||
public String toString() {
|
}
|
||||||
return null;
|
|
||||||
}
|
/**
|
||||||
}
|
* A JSONString that returns a valid JSON string value.
|
||||||
}
|
*/
|
||||||
|
private static final class JSONStringValue implements JSONString {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toJSONString() {
|
||||||
|
return "\"the JSON string value\"";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "the toString value for JSONStringValue";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JSONString that returns null when calling toJSONString().
|
||||||
|
*/
|
||||||
|
private static final class JSONNullStringValue implements JSONString {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toJSONString() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "the toString value";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A JSONString that throw an exception when calling toJSONString().
|
||||||
|
*/
|
||||||
|
private static final class JSONStringExceptionValue implements JSONString {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toJSONString() {
|
||||||
|
throw new IllegalStateException("the exception value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "the toString value for JSONStringExceptionValue";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class StringValue {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "the toString value for StringValue";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class NullStringValue {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.json.junit;
|
||||||
/**
|
/**
|
||||||
* An enum that contains getters and some internal fields
|
* An enum that contains getters and some internal fields
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
public enum MyEnumField {
|
public enum MyEnumField {
|
||||||
VAL1(1, "val 1"),
|
VAL1(1, "val 1"),
|
||||||
VAL2(2, "val 2"),
|
VAL2(2, "val 2"),
|
||||||
|
@ -15,12 +16,13 @@ public enum MyEnumField {
|
||||||
this.intVal = intVal;
|
this.intVal = intVal;
|
||||||
}
|
}
|
||||||
public String getValue() {
|
public String getValue() {
|
||||||
return value;
|
return this.value;
|
||||||
}
|
}
|
||||||
public Integer getIntVal() {
|
public Integer getIntVal() {
|
||||||
return intVal;
|
return this.intVal;
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return value;
|
return this.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.json.junit;
|
||||||
/**
|
/**
|
||||||
* Need a class with some public data members for testing
|
* Need a class with some public data members for testing
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("boxing")
|
||||||
public class MyPublicClass {
|
public class MyPublicClass {
|
||||||
public Integer publicInt = 42;
|
public Integer publicInt = 42;
|
||||||
public String publicString = "abc";
|
public String publicString = "abc";
|
||||||
|
|
|
@ -6,6 +6,7 @@ import java.util.*;
|
||||||
* A resource bundle class
|
* A resource bundle class
|
||||||
*/
|
*/
|
||||||
public class StringsResourceBundle extends ListResourceBundle {
|
public class StringsResourceBundle extends ListResourceBundle {
|
||||||
|
@Override
|
||||||
public Object[][] getContents() {
|
public Object[][] getContents() {
|
||||||
return contents;
|
return contents;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,8 @@ import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONML;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.XML;
|
import org.json.XML;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue