mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Adds exception tests
This commit is contained in:
parent
e94783f91b
commit
38d11227ee
2 changed files with 83 additions and 0 deletions
|
@ -32,6 +32,7 @@ import org.json.JSONObject;
|
|||
import org.json.JSONPointerException;
|
||||
import org.json.XML;
|
||||
import org.json.junit.data.BrokenToString;
|
||||
import org.json.junit.data.ExceptionalBean;
|
||||
import org.json.junit.data.Fraction;
|
||||
import org.json.junit.data.GenericBean;
|
||||
import org.json.junit.data.GenericBeanInt;
|
||||
|
@ -2680,4 +2681,17 @@ public class JSONObjectTest {
|
|||
1, jo.length());
|
||||
assertNotNull(jo.get("ALL"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the exception portions of populateMap.
|
||||
*/
|
||||
@Test
|
||||
public void testExceptionalBean() {
|
||||
ExceptionalBean bean = new ExceptionalBean();
|
||||
final JSONObject jo = new JSONObject(bean);
|
||||
assertEquals("Expected 1 key to be mapped. Instead found: "+jo.keySet().toString(),
|
||||
1, jo.length());
|
||||
assertTrue(jo.get("closeable") instanceof JSONObject);
|
||||
assertTrue(jo.getJSONObject("closeable").has("string"));
|
||||
}
|
||||
}
|
||||
|
|
69
src/test/java/org/json/junit/data/ExceptionalBean.java
Normal file
69
src/test/java/org/json/junit/data/ExceptionalBean.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.json.junit.data;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Object for testing the exception handling in {@link JSONObject#populateMap}.
|
||||
*
|
||||
* @author John Aylward
|
||||
*/
|
||||
public class ExceptionalBean {
|
||||
/**
|
||||
* @return a closeable.
|
||||
*/
|
||||
public Closeable getCloseable() {
|
||||
// anonymous inner class did not work...
|
||||
return new MyCloseable();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws IllegalAccessException
|
||||
* always thrown
|
||||
*/
|
||||
public int getIllegalAccessException() throws IllegalAccessException {
|
||||
throw new IllegalAccessException("Yup, it's illegal");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws IllegalArgumentException
|
||||
* always thrown
|
||||
*/
|
||||
public int getIllegalArgumentException() throws IllegalArgumentException {
|
||||
throw new IllegalArgumentException("Yup, it's illegal");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nothing really. Just can't be void.
|
||||
* @throws InvocationTargetException
|
||||
* always thrown
|
||||
*/
|
||||
public int getInvocationTargetException() throws InvocationTargetException {
|
||||
throw new InvocationTargetException(new Exception("Yup, it's illegal"));
|
||||
}
|
||||
|
||||
/** My closeable class. */
|
||||
public static final class MyCloseable implements Closeable {
|
||||
|
||||
/**
|
||||
* @return a string
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public String getString() {
|
||||
return "Yup, it's closeable";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
throw new IOException("Closing is too hard!");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue