| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests.json; |
| 6 |
|
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Context; |
| 10 |
import org.mozilla.javascript.NativeArray; |
| 11 |
import org.mozilla.javascript.NativeObject; |
| 12 |
|
| 13 |
import org.mozilla.javascript.json.JsonParser; |
| 14 |
import org.mozilla.javascript.json.JsonParser.ParseException; |
| 15 |
|
| 16 |
import org.junit.Before; |
| 17 |
import org.junit.After; |
| 18 |
import org.junit.Test; |
| 19 |
|
| 20 |
public class JsonParserTest { |
| 21 |
private JsonParser parser; |
| 22 |
private Context cx; |
| 23 |
|
| 24 |
@Before |
| 25 |
public void setUp() { |
| 26 |
cx = Context.enter(); |
| 27 |
parser = new JsonParser(cx, cx.initStandardObjects()); |
| 28 |
} |
| 29 |
|
| 30 |
@After |
| 31 |
public void tearDown() { |
| 32 |
Context.exit(); |
| 33 |
} |
| 34 |
|
| 35 |
@Test(expected = ParseException.class) |
| 36 |
public void shouldFailToParseIllegalWhitespaceChars() throws Exception { |
| 37 |
parser.parseValue(" \u000b 1"); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
@Test |
| 42 |
public void shouldParseJsonNull() throws Exception { |
| 43 |
assertEquals(null, parser.parseValue("null")); |
| 44 |
} |
| 45 |
|
| 46 |
@Test(expected = ParseException.class) |
| 47 |
public void shouldFailToParseJavaNull() throws Exception { |
| 48 |
parser.parseValue(null); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void shouldParseJsonBoolean() throws Exception { |
| 53 |
assertEquals(true, parser.parseValue("true")); |
| 54 |
assertEquals(false, parser.parseValue("false")); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void shouldParseJsonNumbers() throws Exception { |
| 59 |
assertEquals(1, parser.parseValue("1")); |
| 60 |
assertEquals(-1, parser.parseValue("-1")); |
| 61 |
assertEquals(1.5, parser.parseValue("1.5")); |
| 62 |
assertEquals(1.5e13, parser.parseValue("1.5e13")); |
| 63 |
assertEquals(1.0e16, parser.parseValue("9999999999999999")); |
| 64 |
assertEquals(Double.POSITIVE_INFINITY, parser.parseValue("1.5e99999999")); |
| 65 |
} |
| 66 |
|
| 67 |
@Test(expected = ParseException.class) |
| 68 |
public void shouldFailToParseDoubleNegativeNumbers() throws Exception { |
| 69 |
parser.parseValue("--5"); |
| 70 |
} |
| 71 |
|
| 72 |
@Test(expected = ParseException.class) |
| 73 |
public void shouldFailToParseNumbersWithDecimalExponent() throws Exception { |
| 74 |
parser.parseValue("5e5.5"); |
| 75 |
} |
| 76 |
|
| 77 |
@Test(expected = ParseException.class) |
| 78 |
public void shouldFailToParseNumbersBeginningWithZero() throws Exception { |
| 79 |
parser.parseValue("05"); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void shouldParseJsonString() throws Exception { |
| 84 |
assertEquals("hello", parser.parseValue("\"hello\"")); |
| 85 |
assertEquals("Sch\u00f6ne Gr\u00fc\u00dfe", |
| 86 |
parser.parseValue("\"Sch\\u00f6ne Gr\\u00fc\\u00dfe\"")); |
| 87 |
assertEquals("", parser.parseValue(str('"', '"'))); |
| 88 |
assertEquals(" ", parser.parseValue(str('"', ' ', '"'))); |
| 89 |
assertEquals("\r", parser.parseValue(str('"', '\\', 'r', '"'))); |
| 90 |
assertEquals("\n", parser.parseValue(str('"', '\\', 'n', '"'))); |
| 91 |
assertEquals("\t", parser.parseValue(str('"', '\\', 't', '"'))); |
| 92 |
assertEquals("\\", parser.parseValue(str('"', '\\', '\\', '"'))); |
| 93 |
assertEquals("/", parser.parseValue(str('"', '/', '"'))); |
| 94 |
assertEquals("/", parser.parseValue(str('"', '\\', '/', '"'))); |
| 95 |
assertEquals("\"", parser.parseValue(str('"', '\\', '"', '"'))); |
| 96 |
} |
| 97 |
|
| 98 |
@Test(expected = ParseException.class) |
| 99 |
public void shouldFailToParseEmptyJavaString() throws Exception { |
| 100 |
parser.parseValue(""); |
| 101 |
} |
| 102 |
|
| 103 |
@Test(expected = ParseException.class) |
| 104 |
public void shouldFailToParseSingleDoubleQuote() throws Exception { |
| 105 |
parser.parseValue(str('"')); |
| 106 |
} |
| 107 |
|
| 108 |
@Test(expected = ParseException.class) |
| 109 |
public void shouldFailToParseStringContainingSingleBackslash() throws Exception { |
| 110 |
parser.parseValue(str('"', '\\', '"')); |
| 111 |
} |
| 112 |
|
| 113 |
@Test(expected = ParseException.class) |
| 114 |
public void shouldFailToParseStringIllegalStringChars() throws Exception { |
| 115 |
parser.parseValue(str('"', '\n', '"')); |
| 116 |
} |
| 117 |
|
| 118 |
@Test |
| 119 |
public void shouldParseEmptyJsonArray() throws Exception { |
| 120 |
assertEquals(0, ((NativeArray) parser.parseValue("[]")).getLength() ); |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
@SuppressWarnings("unchecked") |
| 125 |
public void shouldParseHeterogeneousJsonArray() throws Exception { |
| 126 |
NativeArray actual = (NativeArray) parser |
| 127 |
.parseValue("[ \"hello\" , 3, null, [false] ]"); |
| 128 |
assertEquals("hello", actual.get(0, actual)); |
| 129 |
assertEquals(3, actual.get(1, actual)); |
| 130 |
assertEquals(null, actual.get(2, actual)); |
| 131 |
|
| 132 |
NativeArray innerArr = (NativeArray) actual.get(3, actual); |
| 133 |
assertEquals(false, innerArr.get(0, innerArr)); |
| 134 |
|
| 135 |
assertEquals(4, actual.getLength()); |
| 136 |
} |
| 137 |
|
| 138 |
@Test(expected = ParseException.class) |
| 139 |
public void shouldFailToParseArrayWithInvalidElements() throws Exception { |
| 140 |
parser.parseValue("[wtf]"); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
@SuppressWarnings({ "serial", "unchecked" }) |
| 145 |
public void shouldParseJsonObject() throws Exception { |
| 146 |
String json = "{" + |
| 147 |
"\"bool\" : false, " + |
| 148 |
"\"str\" : \"xyz\", " + |
| 149 |
"\"obj\" : {\"a\":1} " + |
| 150 |
"}"; |
| 151 |
NativeObject actual = (NativeObject) parser.parseValue(json); |
| 152 |
assertEquals(false, actual.get("bool", actual)); |
| 153 |
assertEquals("xyz", actual.get("str", actual)); |
| 154 |
|
| 155 |
NativeObject innerObj = (NativeObject) actual.get("obj", actual); |
| 156 |
assertEquals(1, innerObj.get("a", innerObj)); |
| 157 |
} |
| 158 |
|
| 159 |
@Test(expected = ParseException.class) |
| 160 |
public void shouldFailToParseJsonObjectsWithInvalidFormat() throws Exception { |
| 161 |
parser.parseValue("{\"only\", \"keys\"}"); |
| 162 |
} |
| 163 |
|
| 164 |
@Test(expected = ParseException.class) |
| 165 |
public void shouldFailToParseMoreThanOneToplevelValue() throws Exception { |
| 166 |
parser.parseValue("1 2"); |
| 167 |
} |
| 168 |
|
| 169 |
@Test(expected = ParseException.class) |
| 170 |
public void shouldFailToParseStringTruncatedUnicode() throws Exception { |
| 171 |
parser.parseValue("\"\\u00f\""); |
| 172 |
} |
| 173 |
|
| 174 |
@Test(expected = ParseException.class) |
| 175 |
public void shouldFailToParseStringControlChars1() throws Exception { |
| 176 |
parser.parseValue("\"\u0000\""); |
| 177 |
} |
| 178 |
|
| 179 |
@Test(expected = ParseException.class) |
| 180 |
public void shouldFailToParseStringControlChars2() throws Exception { |
| 181 |
parser.parseValue("\"\u001f\""); |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void shouldAllowTrailingWhitespace() throws Exception { |
| 186 |
parser.parseValue("1 "); |
| 187 |
} |
| 188 |
|
| 189 |
@Test(expected = ParseException.class) |
| 190 |
public void shouldThrowParseExceptionWhenIncompleteObject() throws Exception { |
| 191 |
parser.parseValue("{\"a\" "); |
| 192 |
} |
| 193 |
|
| 194 |
@Test(expected = ParseException.class) |
| 195 |
public void shouldThrowParseExceptionWhenIncompleteArray() throws Exception { |
| 196 |
parser.parseValue("[1 "); |
| 197 |
} |
| 198 |
|
| 199 |
@Test(expected = ParseException.class) |
| 200 |
public void shouldFailToParseIllegalUnicodeEscapeSeq() throws Exception { |
| 201 |
parser.parseValue("\"\\u-123\""); |
| 202 |
} |
| 203 |
|
| 204 |
@Test(expected = ParseException.class) |
| 205 |
public void shouldFailToParseIllegalUnicodeEscapeSeq2() throws Exception { |
| 206 |
parser.parseValue("\"\\u006\u0661\""); |
| 207 |
} |
| 208 |
|
| 209 |
@Test(expected = ParseException.class) |
| 210 |
public void shouldFailToParseIllegalUnicodeEscapeSeq3() throws Exception { |
| 211 |
parser.parseValue("\"\\u006١\""); |
| 212 |
} |
| 213 |
|
| 214 |
@Test(expected = ParseException.class) |
| 215 |
public void shouldFailToParseTrailingCommaInObject1() throws Exception { |
| 216 |
parser.parseValue("{\"a\": 1,}"); |
| 217 |
} |
| 218 |
|
| 219 |
@Test(expected = ParseException.class) |
| 220 |
public void shouldFailToParseTrailingCommaInObject2() throws Exception { |
| 221 |
parser.parseValue("{,\"a\": 1}"); |
| 222 |
} |
| 223 |
|
| 224 |
@Test(expected = ParseException.class) |
| 225 |
public void shouldFailToParseTrailingCommaInObject3() throws Exception { |
| 226 |
parser.parseValue("{,}"); |
| 227 |
} |
| 228 |
|
| 229 |
@Test |
| 230 |
public void shouldParseEmptyObject() throws Exception { |
| 231 |
parser.parseValue("{}"); |
| 232 |
} |
| 233 |
|
| 234 |
@Test(expected = ParseException.class) |
| 235 |
public void shouldFailToParseTrailingCommaInArray1() throws Exception { |
| 236 |
parser.parseValue("[1,]"); |
| 237 |
} |
| 238 |
|
| 239 |
@Test(expected = ParseException.class) |
| 240 |
public void shouldFailToParseTrailingCommaInArray2() throws Exception { |
| 241 |
parser.parseValue("[,1]"); |
| 242 |
} |
| 243 |
|
| 244 |
@Test(expected = ParseException.class) |
| 245 |
public void shouldFailToParseTrailingCommaInArray3() throws Exception { |
| 246 |
parser.parseValue("[,]"); |
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void shouldParseEmptyArray() throws Exception { |
| 251 |
parser.parseValue("[]"); |
| 252 |
} |
| 253 |
|
| 254 |
@Test(expected = ParseException.class) |
| 255 |
public void shouldFailToParseIllegalNumber() throws Exception { |
| 256 |
parser.parseValue("1."); |
| 257 |
} |
| 258 |
|
| 259 |
private String str(char... chars) { |
| 260 |
return new String(chars); |
| 261 |
} |
| 262 |
} |
| 263 |
|