| /******************************************************************************* |
| * Copyright (c) 2009 IBM Corporation and others All rights reserved. This |
| * program and the accompanying materials are made available under the terms of |
| * the Eclipse Public License v1.0 which accompanies this distribution, and is |
| * available at http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.e4.languages.javascript.test; |
| |
| import java.math.BigDecimal; |
| import java.util.*; |
| import junit.framework.TestCase; |
| |
| import org.eclipse.e4.languages.javascript.JSONUtil; |
| |
| public class JSONUtilTest extends TestCase { |
| |
| public void testNull() { |
| String result = JSONUtil.write(null); |
| assertEquals(result, "null"); |
| |
| assertNull(JSONUtil.read(result)); |
| } |
| |
| public void testTrue() { |
| String result = JSONUtil.write(Boolean.TRUE); |
| assertEquals(result, Boolean.TRUE.toString()); |
| |
| assertEquals(JSONUtil.read(result), Boolean.TRUE); |
| } |
| |
| public void testFalse() { |
| String result = JSONUtil.write(Boolean.FALSE); |
| assertEquals(result, Boolean.FALSE.toString()); |
| |
| assertEquals(JSONUtil.read(result), Boolean.FALSE); |
| } |
| |
| public void testNumber() { |
| String result = JSONUtil.write(new Integer(-38)); |
| assertEquals(result, "-38"); |
| |
| assertEquals(JSONUtil.read(result), new BigDecimal(-38)); |
| |
| result = JSONUtil.write(new Double(-3.56e-5)); |
| assertEquals(result, "-3.56E-5"); |
| |
| assertEquals(JSONUtil.read(result), new BigDecimal("-3.56E-5")); |
| } |
| |
| public void testString() { |
| String result = JSONUtil.write("test"); |
| assertEquals(result, "\"test\""); |
| assertEquals(JSONUtil.read(result), "test"); |
| |
| result = JSONUtil.write("\\/\"\b\f\n\r\t\0"); |
| assertEquals(result, "\"\\\\\\/\\\"\\b\\f\\n\\r\\t\\u0000\""); |
| assertEquals(JSONUtil.read(result), "\\/\"\b\f\n\r\t\0");; |
| } |
| |
| public void testMap() { |
| Map map = new HashMap(); |
| map.put("number", new Integer(8675309)); |
| map.put("string", "goog"); |
| map.put("escape", "\\/\"\b\f\n\r\t\0"); |
| map.put("true", Boolean.TRUE); |
| map.put("false", Boolean.FALSE); |
| map.put("null", null); |
| Map object = new HashMap(); |
| object.put("test", "test\n"); |
| map.put("object", object); |
| Collection array = new ArrayList(); |
| array.add("anothertest"); //$NON-NLS-1$ |
| map.put("array", array); |
| String result = JSONUtil.write(map); |
| |
| Map resultMap = (Map) JSONUtil.read(result); |
| assertEquals(resultMap.get("number"), new BigDecimal(8675309)); |
| assertEquals(resultMap.get("string"), "goog"); |
| assertEquals(resultMap.get("escape"), "\\/\"\b\f\n\r\t\0"); |
| assertEquals(resultMap.get("true"), Boolean.TRUE); |
| assertEquals(resultMap.get("false"), Boolean.FALSE); |
| assertEquals(resultMap.get("null"), null); |
| |
| Map objectResult = (Map) resultMap.get("object"); |
| assertEquals(objectResult.get("test"), "test\n"); |
| assertEquals(1, objectResult.size()); |
| Collection arrayResult = (Collection) resultMap.get("array"); |
| assertTrue(arrayResult.contains("anothertest")); |
| assertEquals(1, arrayResult.size()); |
| } |
| |
| public void testWhiteSpaceMap() { |
| String result = "\t\r\n {\t\r\n \"string\"\t\r\n :\t\r\n \"goog\"\t\r\n ,\t\r\n \"false\"\t\r\n :\t\r\n false\t\r\n ,\t\r\n \"true\"\t\r\n :\t\r\n true\t\r\n ,\t\r\n \"number\"\t\r\n :\t\r\n 8675309\t\r\n ,\t\r\n \"object\"\t\r\n :\t\r\n {\t\r\n \"test\"\t\r\n :\t\r\n \"test\\n\"\t\r\n }\t\r\n ,\t\r\n \"null\"\t\r\n :\t\r\n null\t\r\n ,\t\r\n "; |
| result += "\t\r\n \"escape\"\t\r\n :\t\r\n \"\\\\\\/\\\"\\b\\f\\n\\r\\t\\u0000\"\t\r\n "; |
| result += "\t\r\n ,\t\r\n \"array\"\t\r\n :\t\r\n [\t\r\n \"anothertest\"\t\r\n ]\t\r\n }\t\r\n "; |
| |
| Map resultMap = (Map) JSONUtil.read(result); |
| assertEquals(resultMap.get("number"), new BigDecimal(8675309)); |
| assertEquals(resultMap.get("string"), "goog"); |
| assertEquals(resultMap.get("escape"), "\\/\"\b\f\n\r\t\0"); |
| assertEquals(resultMap.get("true"), Boolean.TRUE); |
| assertEquals(resultMap.get("false"), Boolean.FALSE); |
| assertEquals(resultMap.get("null"), null); |
| |
| Map objectResult = (Map) resultMap.get("object"); |
| assertEquals(objectResult.get("test"), "test\n"); |
| assertEquals(1, objectResult.size()); |
| Collection arrayResult = (Collection) resultMap.get("array"); |
| assertTrue(arrayResult.contains("anothertest")); |
| assertEquals(1, arrayResult.size()); |
| |
| } |
| |
| public void testArray() { |
| Collection list = new ArrayList(); |
| list.add(new Integer(8675309)); |
| list.add("goog"); |
| list.add("\\/\"\b\f\n\r\t\0"); |
| list.add(Boolean.TRUE); |
| list.add(Boolean.FALSE); |
| list.add(null); |
| Map object = new HashMap(); |
| object.put("test", "test\n"); |
| list.add(object); |
| Collection array = new ArrayList(); |
| array.add("anothertest"); //$NON-NLS-1$ |
| list.add(array); |
| String result = JSONUtil.write(list); |
| |
| List resultList = (List) JSONUtil.read(result); |
| assertEquals(resultList.get(0), new BigDecimal(8675309)); |
| assertEquals(resultList.get(1), "goog"); |
| assertEquals(resultList.get(2), "\\/\"\b\f\n\r\t\0"); |
| assertEquals(resultList.get(3), Boolean.TRUE); |
| assertEquals(resultList.get(4), Boolean.FALSE); |
| assertEquals(resultList.get(5), null); |
| |
| Map objectResult = (Map) resultList.get(6); |
| assertEquals(objectResult.get("test"), "test\n"); |
| assertEquals(1, objectResult.size()); |
| Collection arrayResult = (Collection) resultList.get(7); |
| assertTrue(arrayResult.contains("anothertest")); |
| assertEquals(1, arrayResult.size()); |
| } |
| |
| public void testWhiteSpaceArray() { |
| String result = " \t\r\n [ \t\r\n 8675309 \t\r\n , \t\r\n \"goog\" \t\r\n , \t\r\n "; |
| result += "\"\\\\\\/\\\"\\b\\f\\n\\r\\t\\u0000\""; |
| result += " \t\r\n , \t\r\n true \t\r\n , \t\r\n false \t\r\n , \t\r\n null \t\r\n ,{ \t\r\n \"test\" \t\r\n : \t\r\n \"test\\n\" \t\r\n } \t\r\n , \t\r\n [ \t\r\n \"anothertest\" \t\r\n ] \t\r\n ] \t\r\n "; |
| |
| List resultList = (List) JSONUtil.read(result); |
| assertEquals(resultList.get(0), new BigDecimal(8675309)); |
| assertEquals(resultList.get(1), "goog"); |
| assertEquals(resultList.get(2), "\\/\"\b\f\n\r\t\0"); |
| assertEquals(resultList.get(3), Boolean.TRUE); |
| assertEquals(resultList.get(4), Boolean.FALSE); |
| assertEquals(resultList.get(5), null); |
| |
| Map objectResult = (Map) resultList.get(6); |
| assertEquals(objectResult.get("test"), "test\n"); |
| assertEquals(1, objectResult.size()); |
| Collection arrayResult = (Collection) resultList.get(7); |
| assertTrue(arrayResult.contains("anothertest")); |
| assertEquals(1, arrayResult.size()); |
| } |
| } |