Coverage Report - org.webslinger.json.tests.TestJSON
 
Classes in this File Line Coverage Branch Coverage Complexity
TestJSON
82%
97/118
100%
1/1
2.857
 
 1  
 package org.webslinger.json.tests;
 2  
 
 3  
 import org.webslinger.httpunit.WebslingerTestCase;
 4  
 
 5  
 import java.io.IOException;
 6  
 import java.io.StringReader;
 7  
 import java.io.StringWriter;
 8  
 import java.util.ArrayList;
 9  
 import java.util.Arrays;
 10  
 import java.util.Collection;
 11  
 import java.util.HashMap;
 12  
 import java.util.HashSet;
 13  
 import java.util.Iterator;
 14  
 import java.util.List;
 15  
 import java.util.Map;
 16  
 
 17  
 import com.meterware.httpunit.HttpNotFoundException;
 18  
 import com.meterware.httpunit.WebResponse;
 19  
 
 20  
 import org.webslinger.javacc.GenericToken;
 21  
 import org.webslinger.json.JSON;
 22  
 import org.webslinger.json.JSONConstants;
 23  
 import org.webslinger.json.JSONWriter;
 24  
 import org.webslinger.json.ParseException;
 25  
 import org.webslinger.json.TokenMgrError;
 26  
 import org.webslinger.util.GenericTestCase;
 27  
 
 28  
 public class TestJSON extends GenericTestCase {
 29  
     public TestJSON(String name) {
 30  4
         super(name);
 31  4
     }
 32  
 
 33  
     protected void setUp() throws Exception {
 34  4
         super.setUp();
 35  4
     }
 36  
 
 37  
     protected void tearDown() throws Exception {
 38  4
         super.tearDown();
 39  4
     }
 40  
 
 41  
     protected Object parseJSON(String value) {
 42  
         try {
 43  1013
             return new JSON(new StringReader(value)).JSONValue();
 44  0
         } catch (RuntimeException e) {
 45  0
             throw e;
 46  0
         } catch (Exception e) {
 47  0
             fail("Caught exception parsing json");
 48  0
             return null;
 49  
         }
 50  
     }
 51  
 
 52  
     protected Object parseJSONResolve(String value) {
 53  
         try {
 54  1
             return new JSON(new StringReader(value)).allowResolve(true).JSONValue();
 55  0
         } catch (RuntimeException e) {
 56  0
             throw e;
 57  0
         } catch (Exception e) {
 58  0
             fail("Caught exception parsing json");
 59  0
             return null;
 60  
         }
 61  
     }
 62  
 
 63  
     protected String getJSON(Object object) {
 64  13
         StringWriter writer = new StringWriter();
 65  13
         JSONWriter jsonWriter = new JSONWriter(writer);
 66  
         try {
 67  13
             jsonWriter.write(object);
 68  0
         } catch (IOException e) {
 69  0
             fail("Caught IOException writing for json");
 70  13
         }
 71  13
         return writer.toString();
 72  
     }
 73  
 
 74  
     protected void assertSimpleJSONInteger(byte n, String json) {
 75  1
         assertSimpleJSON("integer - byte", new Byte(n), json, new Long(n));
 76  1
         assertSimpleJSONInteger((short) n, json);
 77  1
     }
 78  
 
 79  
     protected void assertSimpleJSONInteger(short n, String json) {
 80  1
         assertSimpleJSON("integer - short", new Integer(n), json, new Long(n));
 81  1
         assertSimpleJSONInteger((int) n, json);
 82  1
     }
 83  
 
 84  
     protected void assertSimpleJSONInteger(int n, String json) {
 85  1
         assertSimpleJSON("integer - int", new Short((short) n), json, new Long(n));
 86  1
         assertSimpleJSONInteger((long) n, json);
 87  1
     }
 88  
 
 89  
     protected void assertSimpleJSONInteger(long n, String json) {
 90  1
         assertSimpleJSON("integer - long", new Long(n), json, new Long(n));
 91  1
     }
 92  
 
 93  
     protected void assertSimpleJSONFloat(float n, String json) {
 94  0
         assertSimpleJSON("float - float", new Float(n), json, new Double(n));
 95  0
         assertSimpleJSONFloat((double) n, json);
 96  0
     }
 97  
 
 98  
     protected void assertSimpleJSONFloat(double n, String json) {
 99  1
         assertSimpleJSON("float - double", new Double(n), json);
 100  1
     }
 101  
 
 102  
     protected void assertSimpleJSON(String type, Object object, String json) {
 103  6
         assertSimpleJSON(type, object, json, object);
 104  6
     }
 105  
 
 106  
     protected void assertSimpleJSON(String type, Object before, String json, Object after) {
 107  13
         assertEquals("write " + type, json, getJSON(before));
 108  13
         assertEquals("parse " + type, after, parseJSON(json));
 109  13
     }
 110  
 
 111  
     public void testParseBasicTypes() {
 112  1
         assertSimpleJSON("character", new Character('c'), "\"c\"", "c");
 113  1
         assertSimpleJSON("false", Boolean.FALSE, "false");
 114  1
         assertSimpleJSON("null", null, "null");
 115  1
         assertSimpleJSON("true", Boolean.TRUE, "true");
 116  1
         assertSimpleJSON("simple string", "foo", "\"foo\"");
 117  1
         assertSimpleJSONInteger((byte) 42, "42");
 118  1
         assertSimpleJSONFloat(1.234, "1.234");
 119  1
         assertSimpleJSON(
 120  
             "complex string",
 121  
             "quote(\") backslash(\\) forwardslash(/) backspace(\b) formfeed(\f) newline(\n) carriagereturn(\r) tab(\t) trademark(\u2122)",
 122  
             "\"quote(\\\") backslash(\\\\) forwardslash(\\/) backspace(\\b) formfeed(\\f) newline(\\n) carriagereturn(\\r) tab(\\t) trademark(\\u2122)\""
 123  
         );
 124  1
     }
 125  
 
 126  
     public void testParseComplexTypes() {
 127  1
         assertEquals(
 128  
             "parse simple array",
 129  
             list(new Object[] {"foo", new Long(1234), new Double(5.678)}),
 130  
             parseJSON("[, ,\t,\r,\n,\r\n,\"foo\", 1234, 5.678,]")
 131  
         );
 132  1
         assertSimpleJSON(
 133  
             "simple array",
 134  
             new Object[] {"foo", new Long(1234), new Double(5.678)},
 135  
             "[\n \"foo\",\n 1234,\n 5.678\n]",
 136  
             list(new Object[] {"foo", new Long(1234), new Double(5.678)})
 137  
         );
 138  1
         assertSimpleJSON(
 139  
             "simple array",
 140  
             list(new Object[] {"foo", new Long(1234), new Double(5.678)}),
 141  
             "[\n \"foo\",\n 1234,\n 5.678\n]",
 142  
             list(new Object[] {"foo", new Long(1234), new Double(5.678)})
 143  
         );
 144  1
         assertEquals(
 145  
             "parse simple map",
 146  
             map(new Object[] {"foo", new Long(1234), "bar", new Double(5.678)}),
 147  
             parseJSON("{, ,\t,\r,\n,\r\n,\"foo\": 1234, \"bar\": 5.678,}")
 148  
         );
 149  1
         assertEquals(
 150  
             "parse nested map",
 151  
             map(new Object[] {
 152  
                 "string",       "this is a string",
 153  
                 "integer",      new Long(5000),
 154  
                 "double",       new Double(3.1415926),
 155  
                 "array",        new Object[] {
 156  
                     "string",
 157  
                     new Long(6000)
 158  
                 },
 159  
                 "list",         list(new Object[] {
 160  
                     "nested string",
 161  
                     "something",
 162  
                 }),
 163  
                 "empty-list",   new ArrayList(),
 164  
                 "empty-array",  new String[0],
 165  
                 "empty-map",    new HashMap(),
 166  
             }),
 167  
             parseJSON("{\"string\": \"this is a string\", \"integer\": 5000, \"double\": 3.1415926, \"array\": [\"string\", 6000], \"list\": [\"nested string\", \"something\"], \"empty-list\": [], \"empty-array\": [], \"empty-map\": {}}")
 168  
         );
 169  1
     }
 170  
 
 171  
     public void testParseErrors() {
 172  1024
         for (char c = 1; c < 1024; c++) {
 173  1023
             switch (c) {
 174  
                 case '[':
 175  1
                     doParseExceptionTest("[:", JSONConstants.KEY_SEP);
 176  1
                     break;
 177  
                 case ']':
 178  1
                     doParseExceptionTest("]", JSONConstants.ARRAY_END);
 179  1
                     break;
 180  
                 case '{':
 181  1
                     doParseExceptionTest("{:", JSONConstants.KEY_SEP);
 182  1
                     break;
 183  
                 case '}':
 184  1
                     doParseExceptionTest("}", JSONConstants.OBJECT_END);
 185  1
                     break;
 186  
                 case ':':
 187  1
                     doParseExceptionTest(":", JSONConstants.KEY_SEP);
 188  1
                     break;
 189  
                 case ',':
 190  1
                     doParseExceptionTest(",", JSONConstants.ITEM_SEP);
 191  1
                     break;
 192  
                 case '"':
 193  1
                     doParseExceptionTest("\"", JSONConstants.EOF);
 194  1
                     break;
 195  
                 case 't':
 196  1
                     doParseExceptionTest("true:", JSONConstants.KEY_SEP);
 197  1
                     break;
 198  
                 case 'f':
 199  1
                     doParseExceptionTest("false:", JSONConstants.KEY_SEP);
 200  1
                     break;
 201  
                 case 'n':
 202  1
                     doParseExceptionTest("null:", JSONConstants.KEY_SEP);
 203  1
                     break;
 204  
                 case '-':   // numbers
 205  
                 case '.':
 206  
                 case '0': case '1': case '2': case '3': case '4':
 207  
                 case '5': case '6': case '7': case '8': case '9':
 208  12
                     break;
 209  
                 case '\t':
 210  1
                     doWhitespaceExceptionTest(Character.toString(c), 8);
 211  1
                     break;
 212  
                 case '\n':
 213  
                 case '\r':
 214  
                 case ' ':
 215  3
                     doWhitespaceExceptionTest(Character.toString(c), 1);
 216  3
                     break;
 217  
                 default:
 218  997
                     doTokenMgrErrorTest(c);
 219  
                     break;
 220  
             }
 221  
         }
 222  1
     }
 223  
 
 224  
     protected void doWhitespaceExceptionTest(String s, int column) {
 225  
         try {
 226  4
             new JSON(new StringReader(s)).JSONValue();
 227  0
             fail("No parse exception thrown for (" + s + ")");
 228  4
         } catch (ParseException e) {
 229  4
             assertNotNull("next token(" + s + ")", e.currentToken);
 230  4
             GenericToken next = e.currentToken.getNextToken();
 231  4
             assertEquals("next token(" + s + ") is eof", 0, next.kind);
 232  4
             assertEquals("begin line(" + s + ")", 1, next.beginLine);
 233  4
             assertEquals("begin column(" + s + ")", column, next.beginColumn);
 234  0
         }
 235  4
     }
 236  
 
 237  
     protected void doParseExceptionTest(String s, int nextKind) {
 238  
         try {
 239  10
             new JSON(new StringReader(s)).JSONValue();
 240  0
             fail("No parse exception thrown for (" + s + ")");
 241  10
         } catch (ParseException e) {
 242  10
             assertNotNull("exception message(" + s + ")", e.getMessage());
 243  10
             assertNotNull("next token(" + s + ")", e.currentToken);
 244  10
             GenericToken next = e.currentToken.getNextToken();
 245  10
             assertEquals("next token(" + s + ") is correct", nextKind, next.kind);
 246  10
             assertEquals("begin line(" + s + ")", 1, next.beginLine);
 247  10
             assertEquals("begin column(" + s + ")", s.length(), next.beginColumn);
 248  0
         }
 249  10
     }
 250  
 
 251  
     protected void doTokenMgrErrorTest(char c) {
 252  
         try {
 253  997
             parseJSON(c + "\"string\"");
 254  0
             fail("No TokenMgrError thrown for character(" + ((int) c) + ")");
 255  997
         } catch (TokenMgrError e) {
 256  
             // ignore
 257  
             // FIXME: maybe extend javacc to return more info in TokenMgrError
 258  0
         }
 259  997
     }
 260  
 
 261  
     public void testResolve() throws Exception {
 262  1
         parseJSONResolve("resolve(\"java.net.URL\")\"http://www.brainfood.com/\"");
 263  1
     }
 264  
 }