|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package org.javaee7.json.streaming.parser; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.StringReader; |
| 10 | +import javax.json.Json; |
| 11 | +import javax.json.stream.JsonParser; |
| 12 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 13 | +import org.jboss.arquillian.junit.Arquillian; |
| 14 | +import org.jboss.shrinkwrap.api.Archive; |
| 15 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 16 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 17 | +import org.jboss.shrinkwrap.resolver.api.maven.Maven; |
| 18 | +import org.json.JSONException; |
| 19 | +import org.junit.Test; |
| 20 | +import static org.junit.Assert.*; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Arun Gupta |
| 25 | + */ |
| 26 | +@RunWith(Arquillian.class) |
| 27 | +public class JsonParserFromReaderTest { |
| 28 | + |
| 29 | + @Deployment |
| 30 | + public static Archive<?> deploy() { |
| 31 | + File[] requiredLibraries = Maven.resolver().loadPomFromFile("pom.xml") |
| 32 | + .resolve("org.json:json") |
| 33 | + .withTransitivity().asFile(); |
| 34 | + |
| 35 | + return ShrinkWrap.create(WebArchive.class) |
| 36 | + .addAsLibraries(requiredLibraries); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testEmptyObject() throws JSONException { |
| 41 | + String json = "{}"; |
| 42 | + JsonParser parser = Json.createParser(new StringReader(json)); |
| 43 | + assertEquals(JsonParser.Event.START_OBJECT, parser.next()); |
| 44 | + assertEquals(JsonParser.Event.END_OBJECT, parser.next()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testSimpleObject() throws JSONException { |
| 49 | + String json = "{" |
| 50 | + + " \"apple\":\"red\"," |
| 51 | + + " \"banana\":\"yellow\"" |
| 52 | + + "}"; |
| 53 | + JsonParser parser = Json.createParser(new StringReader(json)); |
| 54 | + assertEquals(JsonParser.Event.START_OBJECT, parser.next()); |
| 55 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 56 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 57 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 58 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 59 | + assertEquals(JsonParser.Event.END_OBJECT, parser.next()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testArray() throws JSONException { |
| 64 | + String json = "[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]"; |
| 65 | + JsonParser parser = Json.createParser(new StringReader(json)); |
| 66 | + assertEquals(JsonParser.Event.START_ARRAY, parser.next()); |
| 67 | + assertEquals(JsonParser.Event.START_OBJECT, parser.next()); |
| 68 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 69 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 70 | + assertEquals(JsonParser.Event.END_OBJECT, parser.next()); |
| 71 | + assertEquals(JsonParser.Event.START_OBJECT, parser.next()); |
| 72 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 73 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 74 | + assertEquals(JsonParser.Event.END_OBJECT, parser.next()); |
| 75 | + assertEquals(JsonParser.Event.END_ARRAY, parser.next()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testNestedStructure() throws JSONException { |
| 80 | + String json = "{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}"; |
| 81 | + JsonParser parser = Json.createParser(new StringReader(json)); |
| 82 | + assertEquals(JsonParser.Event.START_OBJECT, parser.next()); |
| 83 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 84 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 85 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 86 | + assertEquals(JsonParser.Event.VALUE_NUMBER, parser.next()); |
| 87 | + assertEquals(JsonParser.Event.KEY_NAME, parser.next()); |
| 88 | + assertEquals(JsonParser.Event.START_ARRAY, parser.next()); |
| 89 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 90 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 91 | + assertEquals(JsonParser.Event.VALUE_STRING, parser.next()); |
| 92 | + assertEquals(JsonParser.Event.END_ARRAY, parser.next()); |
| 93 | + assertEquals(JsonParser.Event.END_OBJECT, parser.next()); |
| 94 | + } |
| 95 | +} |
0 commit comments