Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring Junit tests to Java 1.6 compatibility #539

Merged
merged 1 commit into from Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -118,8 +118,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
Expand Down
31 changes: 20 additions & 11 deletions src/test/java/org/json/junit/JSONArrayTest.java
Expand Up @@ -940,18 +940,21 @@ public void write() throws IOException {
String str = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":2,\"key3\":3}]";
JSONArray jsonArray = new JSONArray(str);
String expectedStr = str;
try (StringWriter stringWriter = new StringWriter();) {
jsonArray.write(stringWriter);
String actualStr = stringWriter.toString();
StringWriter stringWriter = new StringWriter();
jsonArray.write(stringWriter);
String actualStr = stringWriter.toString();
try {
JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
assertTrue("write() expected " + expectedStr +
" but found " + actualStr,
actualStr.startsWith("[\"value1\",\"value2\",{")
&& actualStr.contains("\"key1\":1")
&& actualStr.contains("\"key2\":2")
&& actualStr.contains("\"key3\":3")
);
" but found " + actualStr,
actualStr.startsWith("[\"value1\",\"value2\",{")
&& actualStr.contains("\"key1\":1")
&& actualStr.contains("\"key2\":2")
&& actualStr.contains("\"key3\":3")
);
} finally {
stringWriter.close();
}
}

Expand Down Expand Up @@ -981,7 +984,8 @@ public void write3Param() throws IOException {
String str0 = "[\"value1\",\"value2\",{\"key1\":1,\"key2\":false,\"key3\":3.14}]";
JSONArray jsonArray = new JSONArray(str0);
String expectedStr = str0;
try (StringWriter stringWriter = new StringWriter();) {
StringWriter stringWriter = new StringWriter();
try {
String actualStr = jsonArray.write(stringWriter, 0, 0).toString();
JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
Expand All @@ -992,9 +996,12 @@ public void write3Param() throws IOException {
&& actualStr.contains("\"key2\":false")
&& 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();
JSONArray finalArray = new JSONArray(actualStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, finalArray);
Expand All @@ -1008,6 +1015,8 @@ public void write3Param() throws IOException {
&& actualStr.contains("\"key2\": false")
&& actualStr.contains("\"key3\": 3.14")
);
} finally {
stringWriter.close();
}
}

Expand Down
51 changes: 42 additions & 9 deletions src/test/java/org/json/junit/JSONObjectTest.java
Expand Up @@ -1879,7 +1879,7 @@ public void jsonObjectToStringIndent() {
@Test
public void jsonObjectToStringSuppressWarningOnCastToMap() {
JSONObject jsonObject = new JSONObject();
Map<String, String> map = new HashMap<>();
Map<String, String> map = new HashMap();
map.put("abc", "def");
jsonObject.put("key", map);

Expand Down Expand Up @@ -2632,12 +2632,15 @@ public void write() throws IOException {
String str = "{\"key1\":\"value1\",\"key2\":[1,2,3]}";
String expectedStr = str;
JSONObject jsonObject = new JSONObject(str);
try (StringWriter stringWriter = new StringWriter()) {
StringWriter stringWriter = new StringWriter();
try {
String actualStr = jsonObject.write(stringWriter).toString();
// key order may change. verify length and individual key content
assertEquals("length", expectedStr.length(), actualStr.length());
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
assertTrue("key2", actualStr.contains("\"key2\":[1,2,3]"));
} finally {
stringWriter.close();
}
}

Expand All @@ -2651,29 +2654,40 @@ public void testJSONWriterException() {
jsonObject.put("someKey",new BrokenToString());

// test single element JSONObject
try(StringWriter writer = new StringWriter();) {
StringWriter writer = new StringWriter();
try {
jsonObject.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
} catch(Exception e) {
fail("Expected JSONException");
} finally {
try {
writer.close();
} catch (Exception e) {}
}

//test multiElement
jsonObject.put("somethingElse", "a value");

try (StringWriter writer = new StringWriter()) {
writer = new StringWriter();
try {
jsonObject.write(writer).toString();
fail("Expected an exception, got a String value");
} catch (JSONException e) {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
} catch(Exception e) {
fail("Expected JSONException");
} finally {
try {
writer.close();
} catch (Exception e) {}
}

// test a more complex object
try (StringWriter writer = new StringWriter()) {
writer = new StringWriter();
try {
new JSONObject()
.put("somethingElse", "a value")
.put("someKey", new JSONArray()
Expand All @@ -2684,10 +2698,15 @@ public void testJSONWriterException() {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
} catch(Exception e) {
fail("Expected JSONException");
} finally {
try {
writer.close();
} catch (Exception e) {}
}

// test a more slightly complex object
try (StringWriter writer = new StringWriter()) {
writer = new StringWriter();
try {
new JSONObject()
.put("somethingElse", "a value")
.put("someKey", new JSONArray()
Expand All @@ -2700,6 +2719,10 @@ public void testJSONWriterException() {
assertEquals("Unable to write JSONObject value for key: someKey", e.getMessage());
} catch(Exception e) {
fail("Expected JSONException");
} finally {
try {
writer.close();
} catch (Exception e) {}
}

}
Expand Down Expand Up @@ -2739,15 +2762,21 @@ public void write3Param() throws IOException {
" ]\n" +
" }";
JSONObject jsonObject = new JSONObject(str0);
try (StringWriter stringWriter = new StringWriter();) {
StringWriter stringWriter = new StringWriter();
try {
String actualStr = jsonObject.write(stringWriter,0,0).toString();

assertEquals("length", str0.length(), actualStr.length());
assertTrue("key1", actualStr.contains("\"key1\":\"value1\""));
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();

assertEquals("length", str2.length(), actualStr.length());
Expand All @@ -2758,6 +2787,10 @@ public void write3Param() throws IOException {
" 3.14\n" +
" ]")
);
} finally {
try {
stringWriter.close();
} catch (Exception e) {}
}
}

Expand Down Expand Up @@ -3039,7 +3072,7 @@ public void testSingletonEnumBean() {
@SuppressWarnings("boxing")
@Test
public void testGenericBean() {
GenericBean<Integer> bean = new GenericBean<>(42);
GenericBean<Integer> bean = new GenericBean(42);
final JSONObject jo = new JSONObject(bean);
assertEquals(jo.keySet().toString(), 8, jo.length());
assertEquals(42, jo.get("genericValue"));
Expand Down