|
| 1 | +package org.json.junit; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.StringWriter; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +import org.json.*; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * Tests for JSONString implementations, and the difference between |
| 13 | + * {@link JSONObject#valueToString} and {@link JSONObject#writeValue}. |
| 14 | + */ |
| 15 | +public class JSONStringTest { |
| 16 | + |
| 17 | + /** |
| 18 | + * This tests the JSONObject.writeValue() method. We can't test directly |
| 19 | + * due to it being a package-protected method. Instead, we can call |
| 20 | + * JSONArray.write(), which delegates the writing of each entry to |
| 21 | + * writeValue(). |
| 22 | + */ |
| 23 | + @Test |
| 24 | + public void writeValues() throws Exception { |
| 25 | + JSONArray jsonArray = new JSONArray(); |
| 26 | + jsonArray.put((Object)null); |
| 27 | + |
| 28 | + StringWriter writer = new StringWriter(); |
| 29 | + String output = jsonArray.write(writer).toString(); |
| 30 | + assertTrue("String values should be equal", "[null]".equals(output)); |
| 31 | + |
| 32 | + jsonArray = new JSONArray(); |
| 33 | + jsonArray.put(JSONObject.NULL); |
| 34 | + writer = new StringWriter(); |
| 35 | + output = jsonArray.write(writer).toString(); |
| 36 | + assertTrue("String values should be equal", "[null]".equals(output)); |
| 37 | + |
| 38 | + jsonArray = new JSONArray(); |
| 39 | + jsonArray.put(new JSONObject()); |
| 40 | + writer = new StringWriter(); |
| 41 | + output = jsonArray.write(writer).toString(); |
| 42 | + assertTrue("String values should be equal", "[{}]".equals(output)); |
| 43 | + |
| 44 | + jsonArray = new JSONArray(); |
| 45 | + jsonArray.put(new JSONArray()); |
| 46 | + writer = new StringWriter(); |
| 47 | + output = jsonArray.write(writer).toString(); |
| 48 | + assertTrue("String values should be equal", "[[]]".equals(output)); |
| 49 | + |
| 50 | + jsonArray = new JSONArray(); |
| 51 | + Map singleMap = Collections.singletonMap("key1", "value1"); |
| 52 | + jsonArray.put((Object)singleMap); |
| 53 | + writer = new StringWriter(); |
| 54 | + output = jsonArray.write(writer).toString(); |
| 55 | + assertTrue("String values should be equal", "[{\"key1\":\"value1\"}]".equals(output)); |
| 56 | + |
| 57 | + jsonArray = new JSONArray(); |
| 58 | + List singleList = Collections.singletonList("entry1"); |
| 59 | + jsonArray.put((Object)singleList); |
| 60 | + writer = new StringWriter(); |
| 61 | + output = jsonArray.write(writer).toString(); |
| 62 | + assertTrue("String values should be equal", "[[\"entry1\"]]".equals(output)); |
| 63 | + |
| 64 | + jsonArray = new JSONArray(); |
| 65 | + int[] intArray = new int[] { 1, 2, 3 }; |
| 66 | + jsonArray.put(intArray); |
| 67 | + writer = new StringWriter(); |
| 68 | + output = jsonArray.write(writer).toString(); |
| 69 | + assertTrue("String values should be equal", "[[1,2,3]]".equals(output)); |
| 70 | + |
| 71 | + jsonArray = new JSONArray(); |
| 72 | + jsonArray.put(24); |
| 73 | + writer = new StringWriter(); |
| 74 | + output = jsonArray.write(writer).toString(); |
| 75 | + assertTrue("String values should be equal", "[24]".equals(output)); |
| 76 | + |
| 77 | + jsonArray = new JSONArray(); |
| 78 | + jsonArray.put("string value"); |
| 79 | + writer = new StringWriter(); |
| 80 | + output = jsonArray.write(writer).toString(); |
| 81 | + assertTrue("String values should be equal", "[\"string value\"]".equals(output)); |
| 82 | + |
| 83 | + jsonArray = new JSONArray(); |
| 84 | + jsonArray.put(true); |
| 85 | + writer = new StringWriter(); |
| 86 | + output = jsonArray.write(writer).toString(); |
| 87 | + assertTrue("String values should be equal", "[true]".equals(output)); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * This tests the JSONObject.valueToString() method. These should be |
| 93 | + * identical to the values above, except for the enclosing [ and ]. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void valuesToString() throws Exception { |
| 97 | + |
| 98 | + String output = JSONObject.valueToString(null); |
| 99 | + assertTrue("String values should be equal", "null".equals(output)); |
| 100 | + |
| 101 | + output = JSONObject.valueToString(JSONObject.NULL); |
| 102 | + assertTrue("String values should be equal", "null".equals(output)); |
| 103 | + |
| 104 | + output = JSONObject.valueToString(new JSONObject()); |
| 105 | + assertTrue("String values should be equal", "{}".equals(output)); |
| 106 | + |
| 107 | + output = JSONObject.valueToString(new JSONArray()); |
| 108 | + assertTrue("String values should be equal", "[]".equals(output)); |
| 109 | + |
| 110 | + Map singleMap = Collections.singletonMap("key1", "value1"); |
| 111 | + output = JSONObject.valueToString(singleMap); |
| 112 | + assertTrue("String values should be equal", "{\"key1\":\"value1\"}".equals(output)); |
| 113 | + |
| 114 | + List singleList = Collections.singletonList("entry1"); |
| 115 | + output = JSONObject.valueToString(singleList); |
| 116 | + assertTrue("String values should be equal", "[\"entry1\"]".equals(output)); |
| 117 | + |
| 118 | + int[] intArray = new int[] { 1, 2, 3 }; |
| 119 | + output = JSONObject.valueToString(intArray); |
| 120 | + assertTrue("String values should be equal", "[1,2,3]".equals(output)); |
| 121 | + |
| 122 | + output = JSONObject.valueToString(24); |
| 123 | + assertTrue("String values should be equal", "24".equals(output)); |
| 124 | + |
| 125 | + output = JSONObject.valueToString("string value"); |
| 126 | + assertTrue("String values should be equal", "\"string value\"".equals(output)); |
| 127 | + |
| 128 | + output = JSONObject.valueToString(true); |
| 129 | + assertTrue("String values should be equal", "true".equals(output)); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Test what happens when toJSONString() returns a well-formed JSON value. |
| 135 | + * This is the usual case. |
| 136 | + */ |
| 137 | + @Test |
| 138 | + public void testJSONStringValue() throws Exception { |
| 139 | + JSONStringValue jsonString = new JSONStringValue(); |
| 140 | + JSONArray jsonArray = new JSONArray(); |
| 141 | + |
| 142 | + jsonArray.put(jsonString); |
| 143 | + |
| 144 | + StringWriter writer = new StringWriter(); |
| 145 | + String output = jsonArray.write(writer).toString(); |
| 146 | + assertTrue("String values should be equal", "[\"the JSON string value\"]".equals(output)); |
| 147 | + |
| 148 | + output = JSONObject.valueToString(jsonString); |
| 149 | + assertTrue("String values should be equal", "\"the JSON string value\"".equals(output)); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Test what happens when toJSONString() returns null. In one case, |
| 154 | + * use the object's toString() method. In the other, throw a JSONException. |
| 155 | + */ |
| 156 | + @Test |
| 157 | + public void testJSONNullStringValue() throws Exception { |
| 158 | + JSONNullStringValue jsonString = new JSONNullStringValue(); |
| 159 | + JSONArray jsonArray = new JSONArray(); |
| 160 | + |
| 161 | + jsonArray.put(jsonString); |
| 162 | + |
| 163 | + StringWriter writer = new StringWriter(); |
| 164 | + String output = jsonArray.write(writer).toString(); |
| 165 | + assertTrue("String values should be equal", "[\"the toString value\"]".equals(output)); |
| 166 | + |
| 167 | + // The first different between writeValue() and valueToString(): |
| 168 | + // in this case, valueToString throws a JSONException |
| 169 | + try { |
| 170 | + output = JSONObject.valueToString(jsonString); |
| 171 | + fail("Expected an exception, got a String value"); |
| 172 | + } catch (Exception e) { |
| 173 | + assertTrue("Expected JSONException", e instanceof JSONException); |
| 174 | + assertTrue("Exception message does not match", "Bad value from toJSONString: null".equals(e.getMessage())); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Test what happens when toJSONString() returns an exception. In both |
| 180 | + * cases, a JSONException is thrown, with the cause and message set from |
| 181 | + * the original exception. |
| 182 | + */ |
| 183 | + @Test |
| 184 | + public void testJSONStringExceptionValue() throws Exception { |
| 185 | + JSONStringExceptionValue jsonString = new JSONStringExceptionValue(); |
| 186 | + JSONArray jsonArray = new JSONArray(); |
| 187 | + |
| 188 | + jsonArray.put(jsonString); |
| 189 | + |
| 190 | + StringWriter writer = new StringWriter(); |
| 191 | + String output = null; |
| 192 | + try { |
| 193 | + output = jsonArray.write(writer).toString(); |
| 194 | + fail("Expected an exception, got a String value"); |
| 195 | + } catch (Exception e) { |
| 196 | + assertTrue("Expected JSONException", e instanceof JSONException); |
| 197 | + assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); |
| 198 | + } |
| 199 | + |
| 200 | + try { |
| 201 | + output = JSONObject.valueToString(jsonString); |
| 202 | + fail("Expected an exception, got a String value"); |
| 203 | + } catch (Exception e) { |
| 204 | + assertTrue("Expected JSONException", e instanceof JSONException); |
| 205 | + assertTrue("Exception message does not match", "the exception value".equals(e.getMessage())); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Test what happens when a Java object's toString() returns a String value. |
| 211 | + * This is the usual case. |
| 212 | + */ |
| 213 | + @Test |
| 214 | + public void testStringValue() throws Exception { |
| 215 | + StringValue nonJsonString = new StringValue(); |
| 216 | + JSONArray jsonArray = new JSONArray(); |
| 217 | + |
| 218 | + jsonArray.put(nonJsonString); |
| 219 | + |
| 220 | + StringWriter writer = new StringWriter(); |
| 221 | + String output = jsonArray.write(writer).toString(); |
| 222 | + assertTrue("String values should be equal", "[\"the toString value for StringValue\"]".equals(output)); |
| 223 | + |
| 224 | + output = JSONObject.valueToString(nonJsonString); |
| 225 | + assertTrue("String values should be equal", "\"the toString value for StringValue\"".equals(output)); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Test what happens when a Java object's toString() returns null. |
| 230 | + * Defaults to empty string. |
| 231 | + */ |
| 232 | + @Test |
| 233 | + public void testNullStringValue() throws Exception { |
| 234 | + NullStringValue nonJsonString = new NullStringValue(); |
| 235 | + JSONArray jsonArray = new JSONArray(); |
| 236 | + |
| 237 | + jsonArray.put(nonJsonString); |
| 238 | + |
| 239 | + StringWriter writer = new StringWriter(); |
| 240 | + String output = jsonArray.write(writer).toString(); |
| 241 | + assertTrue("String values should be equal", "[\"\"]".equals(output)); |
| 242 | + |
| 243 | + output = JSONObject.valueToString(nonJsonString); |
| 244 | + assertTrue("String values should be equal", "\"\"".equals(output)); |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * A JSONString that returns a valid JSON string value. |
| 249 | + */ |
| 250 | + private static final class JSONStringValue implements JSONString { |
| 251 | + |
| 252 | + @Override |
| 253 | + public String toJSONString() { |
| 254 | + return "\"the JSON string value\""; |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public String toString() { |
| 259 | + return "the toString value for JSONStringValue"; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * A JSONString that returns null when calling toJSONString(). |
| 265 | + */ |
| 266 | + private static final class JSONNullStringValue implements JSONString { |
| 267 | + |
| 268 | + @Override |
| 269 | + public String toJSONString() { |
| 270 | + return null; |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public String toString() { |
| 275 | + return "the toString value"; |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + /** |
| 280 | + * A JSONString that throw an exception when calling toJSONString(). |
| 281 | + */ |
| 282 | + private static final class JSONStringExceptionValue implements JSONString { |
| 283 | + |
| 284 | + @Override |
| 285 | + public String toJSONString() { |
| 286 | + throw new IllegalStateException("the exception value"); |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public String toString() { |
| 291 | + return "the toString value for JSONStringExceptionValue"; |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + public static final class StringValue { |
| 296 | + |
| 297 | + @Override |
| 298 | + public String toString() { |
| 299 | + return "the toString value for StringValue"; |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + public static final class NullStringValue { |
| 304 | + |
| 305 | + @Override |
| 306 | + public String toString() { |
| 307 | + return null; |
| 308 | + } |
| 309 | + } |
| 310 | +} |
0 commit comments