|
12 | 12 | import java.io.Reader; |
13 | 13 | import java.io.StringReader; |
14 | 14 |
|
| 15 | +import org.json.JSONArray; |
15 | 16 | import org.json.JSONException; |
| 17 | +import org.json.JSONObject; |
16 | 18 | import org.json.JSONTokener; |
17 | 19 | import org.junit.Test; |
18 | 20 |
|
@@ -64,7 +66,99 @@ public void verifyBackFailureDoubleBack() throws IOException { |
64 | 66 | } |
65 | 67 | } |
66 | 68 | } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testValid() { |
| 72 | + checkValid("0",Number.class); |
| 73 | + checkValid(" 0 ",Number.class); |
| 74 | + checkValid("23",Number.class); |
| 75 | + checkValid("23.5",Number.class); |
| 76 | + checkValid(" 23.5 ",Number.class); |
| 77 | + checkValid("null",null); |
| 78 | + checkValid(" null ",null); |
| 79 | + checkValid("true",Boolean.class); |
| 80 | + checkValid(" true\n",Boolean.class); |
| 81 | + checkValid("false",Boolean.class); |
| 82 | + checkValid("\nfalse ",Boolean.class); |
| 83 | + checkValid("{}",JSONObject.class); |
| 84 | + checkValid(" {} ",JSONObject.class); |
| 85 | + checkValid("{\"a\":1}",JSONObject.class); |
| 86 | + checkValid(" {\"a\":1} ",JSONObject.class); |
| 87 | + checkValid("[]",JSONArray.class); |
| 88 | + checkValid(" [] ",JSONArray.class); |
| 89 | + checkValid("[1,2]",JSONArray.class); |
| 90 | + checkValid("\n\n[1,2]\n\n",JSONArray.class); |
| 91 | + checkValid("1 2", String.class); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testErrors() { |
| 96 | + // Check that stream can detect that a value is found after |
| 97 | + // the first one |
| 98 | + checkError(" { \"a\":1 } 4 "); |
| 99 | + checkError("null \"a\""); |
| 100 | + checkError("{} true"); |
| 101 | + } |
| 102 | + |
| 103 | + private Object checkValid(String testStr, Class<?> aClass) { |
| 104 | + Object result = nextValue(testStr); |
67 | 105 |
|
| 106 | + // Check class of object returned |
| 107 | + if( null == aClass ) { |
| 108 | + if(JSONObject.NULL.equals(result)) { |
| 109 | + // OK |
| 110 | + } else { |
| 111 | + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); |
| 112 | + } |
| 113 | + } else { |
| 114 | + if( null == result ) { |
| 115 | + throw new JSONException("Unexpected null result"); |
| 116 | + } else if(!aClass.isAssignableFrom(result.getClass()) ) { |
| 117 | + throw new JSONException("Unexpected class: "+result.getClass().getSimpleName()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + private void checkError(String testStr) { |
| 125 | + try { |
| 126 | + nextValue(testStr); |
| 127 | + |
| 128 | + fail("Error should be triggered: (\""+testStr+"\")"); |
| 129 | + } catch (JSONException e) { |
| 130 | + // OK |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Verifies that JSONTokener can read a stream that contains a value. After |
| 136 | + * the reading is done, check that the stream is left in the correct state |
| 137 | + * by reading the characters after. All valid cases should reach end of stream. |
| 138 | + * @param testStr |
| 139 | + * @return |
| 140 | + * @throws Exception |
| 141 | + */ |
| 142 | + private Object nextValue(String testStr) throws JSONException { |
| 143 | + try(StringReader sr = new StringReader(testStr);){ |
| 144 | + JSONTokener tokener = new JSONTokener(sr); |
| 145 | + |
| 146 | + Object result = tokener.nextValue(); |
| 147 | + |
| 148 | + if( result == null ) { |
| 149 | + throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr); |
| 150 | + } |
| 151 | + |
| 152 | + char c = tokener.nextClean(); |
| 153 | + if( 0 != c ) { |
| 154 | + throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr); |
| 155 | + } |
| 156 | + |
| 157 | + return result; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
68 | 162 | /** |
69 | 163 | * Tests the failure of the skipTo method with a buffered reader. Preferably |
70 | 164 | * we'd like this not to fail but at this time we don't have a good recovery. |
|
0 commit comments