mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
Merge pull request #539 from stleary/unit-tests-1.6
Bring Junit tests to Java 1.6 compatibility
This commit is contained in:
commit
0e34d8d383
4
pom.xml
4
pom.xml
@ -118,8 +118,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>2.3.2</version>
|
<version>2.3.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.7</source>
|
<source>1.6</source>
|
||||||
<target>1.7</target>
|
<target>1.6</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -940,9 +940,10 @@ public class JSONArrayTest {
|
|||||||
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;
|
||||||
try (StringWriter stringWriter = new StringWriter();) {
|
StringWriter stringWriter = new StringWriter();
|
||||||
jsonArray.write(stringWriter);
|
jsonArray.write(stringWriter);
|
||||||
String actualStr = stringWriter.toString();
|
String actualStr = stringWriter.toString();
|
||||||
|
try {
|
||||||
JSONArray finalArray = new JSONArray(actualStr);
|
JSONArray finalArray = new JSONArray(actualStr);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
||||||
assertTrue("write() expected " + expectedStr +
|
assertTrue("write() expected " + expectedStr +
|
||||||
@ -952,6 +953,8 @@ public class JSONArrayTest {
|
|||||||
&& actualStr.contains("\"key2\":2")
|
&& actualStr.contains("\"key2\":2")
|
||||||
&& actualStr.contains("\"key3\":3")
|
&& actualStr.contains("\"key3\":3")
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -981,7 +984,8 @@ public class JSONArrayTest {
|
|||||||
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
|
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
|
||||||
JSONArray jsonArray = new JSONArray(str0);
|
JSONArray jsonArray = new JSONArray(str0);
|
||||||
String expectedStr = str0;
|
String expectedStr = str0;
|
||||||
try (StringWriter stringWriter = new StringWriter();) {
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
try {
|
||||||
String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
|
String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
|
||||||
JSONArray finalArray = new JSONArray(actualStr);
|
JSONArray finalArray = new JSONArray(actualStr);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
||||||
@ -992,9 +996,12 @@ public class JSONArrayTest {
|
|||||||
&& actualStr.contains("\"key2\":false")
|
&& actualStr.contains("\"key2\":false")
|
||||||
&& actualStr.contains("\"key3\":3.14")
|
&& actualStr.contains("\"key3\":3.14")
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter stringWriter = new StringWriter();) {
|
stringWriter = new StringWriter();
|
||||||
|
try {
|
||||||
String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
|
String actualStr = jsonArray.write(stringWriter, 2, 1).toString();
|
||||||
JSONArray finalArray = new JSONArray(actualStr);
|
JSONArray finalArray = new JSONArray(actualStr);
|
||||||
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
|
||||||
@ -1008,6 +1015,8 @@ public class JSONArrayTest {
|
|||||||
&& actualStr.contains("\"key2\": false")
|
&& actualStr.contains("\"key2\": false")
|
||||||
&& actualStr.contains("\"key3\": 3.14")
|
&& actualStr.contains("\"key3\": 3.14")
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1879,7 +1879,7 @@ public class JSONObjectTest {
|
|||||||
@Test
|
@Test
|
||||||
public void jsonObjectToStringSuppressWarningOnCastToMap() {
|
public void jsonObjectToStringSuppressWarningOnCastToMap() {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap();
|
||||||
map.put("abc", "def");
|
map.put("abc", "def");
|
||||||
jsonObject.put("key", map);
|
jsonObject.put("key", map);
|
||||||
|
|
||||||
@ -2632,12 +2632,15 @@ public class JSONObjectTest {
|
|||||||
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
|
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
|
||||||
String expectedStr = str;
|
String expectedStr = str;
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
try (StringWriter stringWriter = new StringWriter()) {
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
try {
|
||||||
String actualStr = jsonObject.write(stringWriter).toString();
|
String actualStr = jsonObject.write(stringWriter).toString();
|
||||||
// key order may change. verify length and individual key content
|
// key order may change. verify length and individual key content
|
||||||
assertEquals("length", expectedStr.length(), actualStr.length());
|
assertEquals("length", expectedStr.length(), actualStr.length());
|
||||||
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
|
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
|
||||||
assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
|
assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
|
||||||
|
} finally {
|
||||||
|
stringWriter.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2651,29 +2654,40 @@ public class JSONObjectTest {
|
|||||||
jsonObject.put("someKey",new BrokenToString());
|
jsonObject.put("someKey",new BrokenToString());
|
||||||
|
|
||||||
// test single element JSONObject
|
// test single element JSONObject
|
||||||
try(StringWriter writer = new StringWriter();) {
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
jsonObject.write(writer).toString();
|
jsonObject.write(writer).toString();
|
||||||
fail("Expected an exception, got a String value");
|
fail("Expected an exception, got a String value");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
fail("Expected JSONException");
|
fail("Expected JSONException");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
//test multiElement
|
//test multiElement
|
||||||
jsonObject.put("somethingElse", "a value");
|
jsonObject.put("somethingElse", "a value");
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter()) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
jsonObject.write(writer).toString();
|
jsonObject.write(writer).toString();
|
||||||
fail("Expected an exception, got a String value");
|
fail("Expected an exception, got a String value");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
fail("Expected JSONException");
|
fail("Expected JSONException");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test a more complex object
|
// test a more complex object
|
||||||
try (StringWriter writer = new StringWriter()) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
new JSONObject()
|
new JSONObject()
|
||||||
.put("somethingElse", "a value")
|
.put("somethingElse", "a value")
|
||||||
.put("someKey", new JSONArray()
|
.put("someKey", new JSONArray()
|
||||||
@ -2684,10 +2698,15 @@ public class JSONObjectTest {
|
|||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
fail("Expected JSONException");
|
fail("Expected JSONException");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test a more slightly complex object
|
// test a more slightly complex object
|
||||||
try (StringWriter writer = new StringWriter()) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
new JSONObject()
|
new JSONObject()
|
||||||
.put("somethingElse", "a value")
|
.put("somethingElse", "a value")
|
||||||
.put("someKey", new JSONArray()
|
.put("someKey", new JSONArray()
|
||||||
@ -2700,6 +2719,10 @@ public class JSONObjectTest {
|
|||||||
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
fail("Expected JSONException");
|
fail("Expected JSONException");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2739,15 +2762,21 @@ public class JSONObjectTest {
|
|||||||
" ]\n" +
|
" ]\n" +
|
||||||
" }";
|
" }";
|
||||||
JSONObject jsonObject = new JSONObject(str0);
|
JSONObject jsonObject = new JSONObject(str0);
|
||||||
try (StringWriter stringWriter = new StringWriter();) {
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
try {
|
||||||
String actualStr = jsonObject.write(stringWriter,0,0).toString();
|
String actualStr = jsonObject.write(stringWriter,0,0).toString();
|
||||||
|
|
||||||
assertEquals("length", str0.length(), actualStr.length());
|
assertEquals("length", str0.length(), actualStr.length());
|
||||||
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
|
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
|
||||||
assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]"));
|
assertTrue("key2", actualStr.contains("\"key2\":[1,false,3.14]"));
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
stringWriter.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter stringWriter = new StringWriter();) {
|
stringWriter = new StringWriter();
|
||||||
|
try {
|
||||||
String actualStr = jsonObject.write(stringWriter,2,1).toString();
|
String actualStr = jsonObject.write(stringWriter,2,1).toString();
|
||||||
|
|
||||||
assertEquals("length", str2.length(), actualStr.length());
|
assertEquals("length", str2.length(), actualStr.length());
|
||||||
@ -2758,6 +2787,10 @@ public class JSONObjectTest {
|
|||||||
" 3.14\n" +
|
" 3.14\n" +
|
||||||
" ]")
|
" ]")
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
stringWriter.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3039,7 +3072,7 @@ public class JSONObjectTest {
|
|||||||
@SuppressWarnings("boxing")
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void testGenericBean() {
|
public void testGenericBean() {
|
||||||
GenericBean<Integer> bean = new GenericBean<>(42);
|
GenericBean<Integer> bean = new GenericBean(42);
|
||||||
final JSONObject jo = new JSONObject(bean);
|
final JSONObject jo = new JSONObject(bean);
|
||||||
assertEquals(jo.keySet().toString(), 8, jo.length());
|
assertEquals(jo.keySet().toString(), 8, jo.length());
|
||||||
assertEquals(42, jo.get("genericValue"));
|
assertEquals(42, jo.get("genericValue"));
|
||||||
|
@ -49,84 +49,114 @@ public class JSONStringTest {
|
|||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put((Object)null);
|
jsonArray.put((Object)null);
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[null]".equals(output));
|
assertTrue("String values should be equal", "[null]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(JSONObject.NULL);
|
jsonArray.put(JSONObject.NULL);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[null]".equals(output));
|
assertTrue("String values should be equal", "[null]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(new JSONObject());
|
jsonArray.put(new JSONObject());
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String 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", "[{}]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(new JSONArray());
|
jsonArray.put(new JSONArray());
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String 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", "[[]]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
Map<?,?> singleMap = Collections.singletonMap("key1", "value1");
|
||||||
jsonArray.put((Object)singleMap);
|
jsonArray.put((Object)singleMap);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
|
assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
List<?> singleList = Collections.singletonList("entry1");
|
List<?> singleList = Collections.singletonList("entry1");
|
||||||
jsonArray.put((Object)singleList);
|
jsonArray.put((Object)singleList);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String 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", "[[\"entry1\"]]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
int[] intArray = new int[] { 1, 2, 3 };
|
int[] intArray = new int[] { 1, 2, 3 };
|
||||||
jsonArray.put(intArray);
|
jsonArray.put(intArray);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
|
assertTrue("String values should be equal", "[[1,2,3]]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(24);
|
jsonArray.put(24);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[24]".equals(output));
|
assertTrue("String values should be equal", "[24]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put("string value");
|
jsonArray.put("string value");
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
|
assertTrue("String values should be equal", "[\"string value\"]".equals(output));
|
||||||
|
|
||||||
jsonArray = new JSONArray();
|
jsonArray = new JSONArray();
|
||||||
jsonArray.put(true);
|
jsonArray.put(true);
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[true]".equals(output));
|
assertTrue("String values should be equal", "[true]".equals(output));
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -185,13 +215,15 @@ public class JSONStringTest {
|
|||||||
|
|
||||||
jsonArray.put(jsonString);
|
jsonArray.put(jsonString);
|
||||||
|
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
try (StringWriter writer = new StringWriter();) {
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
|
assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output));
|
||||||
|
|
||||||
output = JSONObject.valueToString(jsonString);
|
output = JSONObject.valueToString(jsonString);
|
||||||
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
|
assertTrue("String values should be equal", "\"the JSON string value\"".equals(output));
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,7 +238,8 @@ public class JSONStringTest {
|
|||||||
|
|
||||||
jsonArray.put(jsonString);
|
jsonArray.put(jsonString);
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
|
assertTrue("String values should be equal", "[\"the toString value\"]".equals(output));
|
||||||
|
|
||||||
@ -219,6 +252,8 @@ public class JSONStringTest {
|
|||||||
assertTrue("Expected JSONException", e instanceof JSONException);
|
assertTrue("Expected JSONException", e instanceof JSONException);
|
||||||
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
|
assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,13 +269,18 @@ public class JSONStringTest {
|
|||||||
|
|
||||||
jsonArray.put(jsonString);
|
jsonArray.put(jsonString);
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
jsonArray.write(writer).toString();
|
jsonArray.write(writer).toString();
|
||||||
fail("Expected an exception, got a String value");
|
fail("Expected an exception, got a String value");
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
|
assertEquals("Unable to write JSONArray value at index: 0", e.getMessage());
|
||||||
} catch(Exception e) {
|
} catch(Exception e) {
|
||||||
fail("Expected JSONException");
|
fail("Expected JSONException");
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
writer.close();
|
||||||
|
} catch (Exception e){}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -264,12 +304,15 @@ public class JSONStringTest {
|
|||||||
|
|
||||||
jsonArray.put(nonJsonString);
|
jsonArray.put(nonJsonString);
|
||||||
|
|
||||||
try (StringWriter writer = new StringWriter();) {
|
StringWriter writer = new StringWriter();
|
||||||
|
try {
|
||||||
String output = jsonArray.write(writer).toString();
|
String output = jsonArray.write(writer).toString();
|
||||||
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
|
assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output));
|
||||||
|
|
||||||
output = JSONObject.valueToString(nonJsonString);
|
output = JSONObject.valueToString(nonJsonString);
|
||||||
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
|
assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output));
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,13 +327,15 @@ public class JSONStringTest {
|
|||||||
|
|
||||||
jsonArray.put(nonJsonString);
|
jsonArray.put(nonJsonString);
|
||||||
|
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
try (StringWriter writer = new StringWriter();) {
|
try {
|
||||||
String 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", "[\"\"]".equals(output));
|
||||||
|
|
||||||
output = JSONObject.valueToString(nonJsonString);
|
output = JSONObject.valueToString(nonJsonString);
|
||||||
assertTrue("String values should be equal", "\"\"".equals(output));
|
assertTrue("String values should be equal", "\"\"".equals(output));
|
||||||
|
} finally {
|
||||||
|
writer.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,8 @@ public class JSONTokenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifyBackFailureZeroIndex() throws IOException {
|
public void verifyBackFailureZeroIndex() throws IOException {
|
||||||
try(Reader reader = new StringReader("some test string")) {
|
Reader reader = new StringReader("some test string");
|
||||||
|
try {
|
||||||
final JSONTokener tokener = new JSONTokener(reader);
|
final JSONTokener tokener = new JSONTokener(reader);
|
||||||
try {
|
try {
|
||||||
// this should fail since the index is 0;
|
// this should fail since the index is 0;
|
||||||
@ -67,6 +68,8 @@ public class JSONTokenerTest {
|
|||||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -75,7 +78,8 @@ public class JSONTokenerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void verifyBackFailureDoubleBack() throws IOException {
|
public void verifyBackFailureDoubleBack() throws IOException {
|
||||||
try(Reader reader = new StringReader("some test string")) {
|
Reader reader = new StringReader("some test string");
|
||||||
|
try {
|
||||||
final JSONTokener tokener = new JSONTokener(reader);
|
final JSONTokener tokener = new JSONTokener(reader);
|
||||||
tokener.next();
|
tokener.next();
|
||||||
tokener.back();
|
tokener.back();
|
||||||
@ -88,6 +92,8 @@ public class JSONTokenerTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -164,7 +170,8 @@ public class JSONTokenerTest {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private Object nextValue(String testStr) throws JSONException {
|
private Object nextValue(String testStr) throws JSONException {
|
||||||
try(StringReader sr = new StringReader(testStr);){
|
StringReader sr = new StringReader(testStr);
|
||||||
|
try {
|
||||||
JSONTokener tokener = new JSONTokener(sr);
|
JSONTokener tokener = new JSONTokener(sr);
|
||||||
|
|
||||||
Object result = tokener.nextValue();
|
Object result = tokener.nextValue();
|
||||||
@ -179,6 +186,8 @@ public class JSONTokenerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
} finally {
|
||||||
|
sr.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -196,7 +205,10 @@ public class JSONTokenerTest {
|
|||||||
for(int i=0;i<superLongBuffer.length;i++) {
|
for(int i=0;i<superLongBuffer.length;i++) {
|
||||||
superLongBuffer[i] = 'A';
|
superLongBuffer[i] = 'A';
|
||||||
}
|
}
|
||||||
try(Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(superLongBuffer)))) {
|
|
||||||
|
Reader reader = new BufferedReader(new InputStreamReader(
|
||||||
|
new ByteArrayInputStream(superLongBuffer)));
|
||||||
|
try {
|
||||||
final JSONTokener tokener = new JSONTokener(reader);
|
final JSONTokener tokener = new JSONTokener(reader);
|
||||||
try {
|
try {
|
||||||
// this should fail since the internal markAhead buffer is only 1,000,000
|
// this should fail since the internal markAhead buffer is only 1,000,000
|
||||||
@ -208,6 +220,8 @@ public class JSONTokenerTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,7 +237,8 @@ public class JSONTokenerTest {
|
|||||||
for(int i=0;i<superLongBuffer.length();i++) {
|
for(int i=0;i<superLongBuffer.length();i++) {
|
||||||
superLongBuffer.append('A');
|
superLongBuffer.append('A');
|
||||||
}
|
}
|
||||||
try(Reader reader = new StringReader(superLongBuffer.toString())) {
|
Reader reader = new StringReader(superLongBuffer.toString());
|
||||||
|
try {
|
||||||
final JSONTokener tokener = new JSONTokener(reader);
|
final JSONTokener tokener = new JSONTokener(reader);
|
||||||
try {
|
try {
|
||||||
// this should not fail since the internal markAhead is ignored for StringReaders
|
// this should not fail since the internal markAhead is ignored for StringReaders
|
||||||
@ -231,6 +246,8 @@ public class JSONTokenerTest {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -918,11 +918,16 @@ public class XMLConfigurationTest {
|
|||||||
* to support XML.toJSONObject(reader)
|
* to support XML.toJSONObject(reader)
|
||||||
*/
|
*/
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
try(Reader reader = new StringReader(xmlStr);) {
|
Reader reader = new StringReader(xmlStr);
|
||||||
|
try {
|
||||||
JSONObject jsonObject = XML.toJSONObject(reader, config);
|
JSONObject jsonObject = XML.toJSONObject(reader, config);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
} catch (IOException e) {
|
} catch (Exception e) {
|
||||||
assertTrue("IO Reader error: " +e.getMessage(), false);
|
assertTrue("Reader error: " +e.getMessage(), false);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (Exception e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -944,15 +949,22 @@ public class XMLConfigurationTest {
|
|||||||
try {
|
try {
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
|
File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
|
||||||
try(FileWriter fileWriter = new FileWriter(tempFile);){
|
FileWriter fileWriter = new FileWriter(tempFile);
|
||||||
|
try {
|
||||||
fileWriter.write(xmlStr);
|
fileWriter.write(xmlStr);
|
||||||
|
} finally {
|
||||||
|
fileWriter.close();
|
||||||
}
|
}
|
||||||
try(Reader reader = new FileReader(tempFile);){
|
|
||||||
|
Reader reader = new FileReader(tempFile);
|
||||||
|
try {
|
||||||
JSONObject jsonObject = XML.toJSONObject(reader);
|
JSONObject jsonObject = XML.toJSONObject(reader);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
assertTrue("file writer error: " +e.getMessage(), false);
|
assertTrue("Error: " +e.getMessage(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -787,15 +787,22 @@ public class XMLTest {
|
|||||||
try {
|
try {
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
|
File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
|
||||||
try(FileWriter fileWriter = new FileWriter(tempFile);){
|
FileWriter fileWriter = new FileWriter(tempFile);
|
||||||
|
try {
|
||||||
fileWriter.write(xmlStr);
|
fileWriter.write(xmlStr);
|
||||||
|
} finally {
|
||||||
|
fileWriter.close();
|
||||||
}
|
}
|
||||||
try(Reader reader = new FileReader(tempFile);){
|
|
||||||
|
Reader reader = new FileReader(tempFile);
|
||||||
|
try {
|
||||||
JSONObject jsonObject = XML.toJSONObject(reader);
|
JSONObject jsonObject = XML.toJSONObject(reader);
|
||||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
|
} finally {
|
||||||
|
reader.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
fail("file writer error: " +e.getMessage());
|
fail("Error: " +e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class WeirdList {
|
public class WeirdList {
|
||||||
/** */
|
/** */
|
||||||
private final List<Integer> list = new ArrayList<>();
|
private final List<Integer> list = new ArrayList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param vals
|
* @param vals
|
||||||
@ -25,14 +25,14 @@ public class WeirdList {
|
|||||||
* @return a copy of the list
|
* @return a copy of the list
|
||||||
*/
|
*/
|
||||||
public List<Integer> get() {
|
public List<Integer> get() {
|
||||||
return new ArrayList<>(this.list);
|
return new ArrayList(this.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return a copy of the list
|
* @return a copy of the list
|
||||||
*/
|
*/
|
||||||
public List<Integer> getALL() {
|
public List<Integer> getALL() {
|
||||||
return new ArrayList<>(this.list);
|
return new ArrayList(this.list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user