Skip to content

Commit 38d1122

Browse files
committed
Adds exception tests
1 parent e94783f commit 38d1122

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.json.JSONPointerException;
3333
import org.json.XML;
3434
import org.json.junit.data.BrokenToString;
35+
import org.json.junit.data.ExceptionalBean;
3536
import org.json.junit.data.Fraction;
3637
import org.json.junit.data.GenericBean;
3738
import org.json.junit.data.GenericBeanInt;
@@ -2680,4 +2681,17 @@ public void testWierdListBean() {
26802681
1, jo.length());
26812682
assertNotNull(jo.get("ALL"));
26822683
}
2684+
2685+
/**
2686+
* Tests the exception portions of populateMap.
2687+
*/
2688+
@Test
2689+
public void testExceptionalBean() {
2690+
ExceptionalBean bean = new ExceptionalBean();
2691+
final JSONObject jo = new JSONObject(bean);
2692+
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
2693+
1, jo.length());
2694+
assertTrue(jo.get("closeable") instanceof JSONObject);
2695+
assertTrue(jo.getJSONObject("closeable").has("string"));
2696+
}
26832697
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
*
3+
*/
4+
package org.json.junit.data;
5+
6+
import java.io.Closeable;
7+
import java.io.IOException;
8+
import java.lang.reflect.InvocationTargetException;
9+
10+
import org.json.JSONObject;
11+
12+
/**
13+
* Object for testing the exception handling in {@link JSONObject#populateMap}.
14+
*
15+
* @author John Aylward
16+
*/
17+
public class ExceptionalBean {
18+
/**
19+
* @return a closeable.
20+
*/
21+
public Closeable getCloseable() {
22+
// anonymous inner class did not work...
23+
return new MyCloseable();
24+
}
25+
26+
/**
27+
* @return Nothing really. Just can't be void.
28+
* @throws IllegalAccessException
29+
* always thrown
30+
*/
31+
public int getIllegalAccessException() throws IllegalAccessException {
32+
throw new IllegalAccessException("Yup, it's illegal");
33+
}
34+
35+
/**
36+
* @return Nothing really. Just can't be void.
37+
* @throws IllegalArgumentException
38+
* always thrown
39+
*/
40+
public int getIllegalArgumentException() throws IllegalArgumentException {
41+
throw new IllegalArgumentException("Yup, it's illegal");
42+
}
43+
44+
/**
45+
* @return Nothing really. Just can't be void.
46+
* @throws InvocationTargetException
47+
* always thrown
48+
*/
49+
public int getInvocationTargetException() throws InvocationTargetException {
50+
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
51+
}
52+
53+
/** My closeable class. */
54+
public static final class MyCloseable implements Closeable {
55+
56+
/**
57+
* @return a string
58+
*/
59+
@SuppressWarnings("unused")
60+
public String getString() {
61+
return "Yup, it's closeable";
62+
}
63+
64+
@Override
65+
public void close() throws IOException {
66+
throw new IOException("Closing is too hard!");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)