Skip to content

Commit 43e23de

Browse files
committed
Converting to unit tests
1 parent af9326d commit 43e23de

File tree

3 files changed

+109
-233
lines changed

3 files changed

+109
-233
lines changed

json/streaming-generate/src/main/java/org/javaee7/json/streaming/generate/StreamingGeneratorServlet.java

Lines changed: 0 additions & 176 deletions
This file was deleted.

json/streaming-generate/src/main/webapp/index.jsp

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.generate;
7+
8+
import java.io.File;
9+
import java.io.StringWriter;
10+
import javax.json.Json;
11+
import javax.json.JsonArray;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonWriter;
14+
import javax.json.stream.JsonGenerator;
15+
import javax.json.stream.JsonGeneratorFactory;
16+
import org.jboss.arquillian.container.test.api.Deployment;
17+
import org.jboss.arquillian.junit.Arquillian;
18+
import org.jboss.shrinkwrap.api.Archive;
19+
import org.jboss.shrinkwrap.api.ShrinkWrap;
20+
import org.jboss.shrinkwrap.api.spec.WebArchive;
21+
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
22+
import org.json.JSONException;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.skyscreamer.jsonassert.JSONAssert;
26+
import org.skyscreamer.jsonassert.JSONCompareMode;
27+
28+
/**
29+
*
30+
* @author Arun Gupta
31+
*/
32+
@RunWith(Arquillian.class)
33+
public class StreamingGeneratorTest {
34+
35+
@Deployment
36+
public static Archive<?> deploy() {
37+
File[] requiredLibraries = Maven.resolver().loadPomFromFile("pom.xml")
38+
.resolve("org.json:json", "org.skyscreamer:jsonassert")
39+
.withTransitivity().asFile();
40+
41+
return ShrinkWrap.create(WebArchive.class)
42+
.addAsLibraries(requiredLibraries);
43+
}
44+
45+
@Test
46+
public void testEmptyObject() throws JSONException {
47+
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
48+
StringWriter w = new StringWriter();
49+
JsonGenerator gen = factory.createGenerator(w);
50+
gen.writeStartObject().writeEnd();
51+
gen.flush();
52+
53+
JSONAssert.assertEquals("{}", w.toString(), JSONCompareMode.STRICT);
54+
}
55+
56+
@Test
57+
public void testSimpleObject() throws JSONException {
58+
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
59+
StringWriter w = new StringWriter();
60+
JsonGenerator gen = factory.createGenerator(w);
61+
62+
gen
63+
.writeStartObject()
64+
.write("apple", "red")
65+
.write("banana", "yellow")
66+
.writeEnd();
67+
gen.flush();
68+
JSONAssert.assertEquals("{\"apple\" : \"red\", \"banana\" : \"yellow\" }", w.toString(), JSONCompareMode.STRICT);
69+
}
70+
71+
@Test
72+
public void testArray() throws JSONException {
73+
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
74+
StringWriter w = new StringWriter();
75+
JsonGenerator gen = factory.createGenerator(w);
76+
77+
gen
78+
.writeStartArray()
79+
.writeStartObject()
80+
.write("apple", "red")
81+
.writeEnd()
82+
.writeStartObject()
83+
.write("banana", "yellow")
84+
.writeEnd()
85+
.writeEnd();
86+
gen.flush();
87+
JSONAssert.assertEquals("[{\"apple\":\"red\"},{\"banana\":\"yellow\"}]", w.toString(), JSONCompareMode.STRICT);
88+
}
89+
90+
@Test
91+
public void testNestedStructure() throws JSONException {
92+
JsonGeneratorFactory factory = Json.createGeneratorFactory(null);
93+
StringWriter w = new StringWriter();
94+
JsonGenerator gen = factory.createGenerator(w);
95+
96+
gen
97+
.writeStartObject()
98+
.write("title", "The Matrix")
99+
.write("year", 1999)
100+
.writeStartArray("cast")
101+
.write("Keanu Reaves")
102+
.write("Laurence Fishburne")
103+
.write("Carrie-Anne Moss")
104+
.writeEnd()
105+
.writeEnd();
106+
gen.flush();
107+
JSONAssert.assertEquals("{\"title\":\"The Matrix\",\"year\":1999,\"cast\":[\"Keanu Reaves\",\"Laurence Fishburne\",\"Carrie-Anne Moss\"]}", w.toString(), JSONCompareMode.STRICT);
108+
}
109+
}

0 commit comments

Comments
 (0)