001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded.jsf;
034:
035: import com.flexive.war.JsonWriter;
036: import org.testng.annotations.Test;
037:
038: import java.io.IOException;
039: import java.io.StringWriter;
040: import java.text.DateFormat;
041: import java.util.Arrays;
042: import java.util.Date;
043: import java.util.Locale;
044:
045: /**
046: * Test cases for the JsonWriter component.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: *
050: */
051: @Test(groups="shared")
052: public class JsonWriterTest {
053:
054: @Test
055: public void testEmptyArray() throws IOException {
056: StringWriter out = new StringWriter();
057: JsonWriter writer = new JsonWriter(out);
058: writer.startArray();
059: writer.closeArray();
060: writer.finishResponse();
061: assert "[]".equals(out.toString());
062: }
063:
064: @Test
065: public void testEmptyMap() throws IOException {
066: StringWriter out = new StringWriter();
067: JsonWriter writer = new JsonWriter(out);
068: writer.startMap();
069: writer.closeMap();
070: writer.finishResponse();
071: assert "{}".equals(out.toString());
072: }
073:
074: @Test
075: public void testSimpleMap() throws IOException {
076: StringWriter out = new StringWriter();
077: JsonWriter writer = new JsonWriter(out);
078: writer.startMap();
079: writer.writeAttribute("simpleStringProperty", "test");
080: writer.writeAttribute("intProperty", 1234);
081: writer.closeMap();
082: writer.finishResponse();
083: assert "{\"simpleStringProperty\":'test',\"intProperty\":1234}"
084: .equals(out.toString()) : "Unexpected output: "
085: + out.toString();
086: }
087:
088: @Test
089: public void testSimpleArray() throws IOException {
090: StringWriter out = new StringWriter();
091: JsonWriter writer = new JsonWriter(out);
092: writer.startArray();
093: writer.writeLiteral("abc");
094: writer.writeLiteral(1234);
095: writer.closeArray();
096: writer.finishResponse();
097: assert "['abc',1234]".equals(out.toString()) : "Unexpected output: "
098: + out.toString();
099: }
100:
101: @Test
102: public void testArrayOfObjects() throws IOException {
103: StringWriter out = new StringWriter();
104: JsonWriter writer = new JsonWriter(out);
105: writer.startArray();
106: String expected = "[";
107: for (int i = 0; i < 10; i++) {
108: writer.startMap();
109: writer.writeAttribute("name", "object" + i);
110: writer.writeAttribute("color", "black");
111: writer.writeAttribute("id", i);
112: expected += (i > 0 ? "," : "") + "{\"name\":'object" + i
113: + "',\"color\":'black',\"id\":" + i + "}";
114: writer.closeMap();
115: }
116: expected += "]";
117: writer.closeArray();
118: writer.finishResponse();
119: assert expected.equals(out.toString()) : "Unexpected output: "
120: + out.toString();
121: }
122:
123: @Test
124: public void testMatchingParensArray() throws IOException {
125: StringWriter out = new StringWriter();
126: JsonWriter writer = new JsonWriter(out);
127: writer.startArray();
128: try {
129: writer.closeMap();
130: assert false : "Map should not be closed if an array was opened before.";
131: } catch (Exception e) {
132: writer.closeArray();
133: }
134: writer.finishResponse();
135: assert "[]".equals(out.toString());
136: }
137:
138: @Test
139: public void testMatchingParensMap() throws IOException {
140: StringWriter out = new StringWriter();
141: JsonWriter writer = new JsonWriter(out);
142: writer.startMap();
143: try {
144: writer.closeArray();
145: assert false : "Array should not be closed if a map was opened before.";
146: } catch (Exception e) {
147: writer.closeMap();
148: }
149: writer.finishResponse();
150: assert "{}".equals(out.toString());
151: }
152:
153: @Test
154: public void testAttributeOnlyInMap() throws IOException {
155: StringWriter out = new StringWriter();
156: JsonWriter writer = new JsonWriter(out);
157: writer.startArray();
158: try {
159: writer.writeAttribute("test", "test");
160: assert false : "Attributes may not be written in arrays.";
161: } catch (Exception e) {
162: // pass
163: }
164: writer.startMap();
165: writer.writeAttribute("id", 1234);
166: writer.closeMap();
167: writer.closeArray();
168: writer.finishResponse();
169: assert "[{\"id\":1234}]".equals(out.toString()) : "Unexpected output: "
170: + out.toString();
171: }
172:
173: @Test
174: public void testNoTopLevelAttributes() {
175: StringWriter out = new StringWriter();
176: JsonWriter writer = new JsonWriter(out);
177: try {
178: writer.writeAttribute("test", 1234);
179: assert false : "Attributes must be contained in maps.";
180: } catch (Exception e) {
181: // pass
182: }
183: }
184:
185: @Test
186: public void testNoTopLevelLiterals() {
187: StringWriter out = new StringWriter();
188: JsonWriter writer = new JsonWriter(out);
189: try {
190: writer.writeLiteral(1234);
191: assert false : "Literals must be contained in arrays.";
192: } catch (Exception e) {
193: // pass
194: }
195: }
196:
197: @Test
198: public void testLiteralsOnlyInArray() throws IOException {
199: StringWriter out = new StringWriter();
200: JsonWriter writer = new JsonWriter(out);
201: writer.startArray();
202: writer.startMap();
203: try {
204: writer.writeLiteral(1234);
205: assert false : "Literals must be contained in arrays.";
206: } catch (Exception e) {
207: writer.closeMap();
208: }
209: writer.writeLiteral(4567);
210: writer.closeArray();
211: writer.finishResponse();
212: assert "[{},4567]".equals(out.toString()) : "Unexpected output: "
213: + out.toString();
214: }
215:
216: @Test
217: public void testFinishResponseOpenArray() throws IOException {
218: StringWriter out = new StringWriter();
219: JsonWriter writer = new JsonWriter(out);
220: writer.startArray();
221: try {
222: writer.finishResponse();
223: assert false : "Response invalid, finishResponse may not succeed.";
224: } catch (Exception e) {
225: // pass
226: }
227: assert "[".equals(out.toString());
228: }
229:
230: @Test
231: public void testWriteAttribute() throws IOException {
232: StringWriter out = new StringWriter();
233: JsonWriter writer = new JsonWriter(out);
234: writer.startMap();
235: writer.startAttribute("name");
236: try {
237: writer.closeMap();
238: assert false : "Map must not be closed before attribute is written.";
239: } catch (Exception e) {
240: // pass
241: }
242: writer.writeAttributeValue("test");
243: writer.closeMap();
244: writer.finishResponse();
245: assert "{\"name\":'test'}".equals(out.toString());
246: }
247:
248: @Test
249: public void testInvalidWriteAttributeValue() throws IOException {
250: StringWriter out = new StringWriter();
251: JsonWriter writer = new JsonWriter(out);
252: writer.startMap();
253: try {
254: writer.writeAttributeValue(1234);
255: assert false : "May not write attribute value without attribute name.";
256: } catch (Exception e) {
257: // pass
258: }
259: writer.closeMap();
260: assert "{}".equals(out.toString());
261: }
262:
263: @Test
264: public void testFilteringQuotes() throws IOException {
265: StringWriter out = new StringWriter();
266: JsonWriter writer = new JsonWriter(out);
267: writer.startMap();
268: writer.writeAttribute("name", "\"test\"");
269: writer.writeAttribute("name2", "'test'");
270: writer.closeMap();
271: writer.finishResponse();
272: assert "{\"name\":'\"test\"',\"name2\":'\\'test\\''}"
273: .equals(out.toString()) : "Unexpected response: "
274: + out.toString();
275: }
276:
277: @Test
278: public void testAttributeArray() throws IOException {
279: StringWriter out = new StringWriter();
280: JsonWriter writer = new JsonWriter(out);
281: writer.startMap();
282: writer.writeAttribute("id", 1234);
283: writer.startAttribute("myList");
284: writer.startArray();
285: writer.writeLiteral(1);
286: writer.writeLiteral(2);
287: writer.writeLiteral(3);
288: writer.closeArray();
289: writer.writeAttribute("name", "test");
290: writer.startAttribute("anotherList");
291: writer.startArray();
292: writer.writeLiteral(99);
293: writer.closeArray();
294: writer.startAttribute("nestedList");
295: writer.startArray();
296: writer.startArray();
297: writer.writeLiteral(1);
298: writer.writeLiteral(2);
299: writer.closeArray();
300: writer.writeLiteral(3);
301: writer.startArray();
302: writer.writeLiteral(4);
303: writer.writeLiteral(5);
304: writer.closeArray();
305: writer.closeArray();
306: writer.closeMap();
307: writer.finishResponse();
308: assert "{\"id\":1234,\"myList\":[1,2,3],\"name\":'test',\"anotherList\":[99],\"nestedList\":[[1,2],3,[4,5]]}"
309: .equals(out.toString()) : "Unexpected response: "
310: + out.toString();
311: }
312:
313: @Test
314: public void testAttributeMap() throws IOException {
315: StringWriter out = new StringWriter();
316: JsonWriter writer = new JsonWriter(out);
317: writer.startArray();
318:
319: writer.startMap();
320: writer.startAttribute("child");
321: writer.startMap();
322: writer.writeAttribute("id", 1234);
323: writer.startAttribute("group");
324: writer.startMap();
325: writer.writeAttribute("value", 21);
326: writer.closeMap();
327: writer.closeMap();
328: writer.closeMap();
329:
330: writer.writeLiteral(42);
331:
332: writer.startMap();
333: writer.startAttribute("child");
334: writer.startMap();
335: writer.writeAttribute("id", 44);
336: writer.closeMap();
337: writer.closeMap();
338:
339: writer.closeArray();
340: writer.finishResponse();
341: assert "[{\"child\":{\"id\":1234,\"group\":{\"value\":21}}},42,{\"child\":{\"id\":44}}]"
342: .equals(out.toString()) : "Unexpected output: "
343: + out.toString();
344: }
345:
346: @Test
347: public void testDoubleQuotedStringValues() throws IOException {
348: StringWriter out = new StringWriter();
349: JsonWriter writer = new JsonWriter(out);
350: writer.setSingleQuotesForStrings(false);
351: writer.startMap();
352: writer.writeAttribute("name", "\"quotedValue\"");
353: writer.closeMap();
354: writer.finishResponse();
355: assert "{\"name\":\"\\\"quotedValue\\\"\"}".equals(out
356: .toString()) : "Unexpected output: " + out.toString();
357: }
358:
359: @Test
360: public void testWriteArrayValue() throws IOException {
361: StringWriter out = new StringWriter();
362: JsonWriter writer = new JsonWriter(out);
363: writer.startMap();
364: writer.writeAttribute("value", Arrays.asList(new Object[] {
365: "stringValue", 123, 123.456 }));
366: writer.closeMap();
367: writer.finishResponse();
368: assert "{\"value\":['stringValue',123,123.456]}".equals(out
369: .toString()) : "Unexpected output: " + out.toString();
370: }
371:
372: @Test
373: public void testWriteDate() throws IOException {
374: StringWriter out = new StringWriter();
375: JsonWriter writer = new JsonWriter(out, Locale.ENGLISH);
376: writer.startArray();
377: writer.writeLiteral(new Date(0));
378: writer.closeArray();
379: writer.finishResponse();
380: assert ("['"
381: + DateFormat.getDateInstance(DateFormat.MEDIUM,
382: Locale.ENGLISH).format(new Date(0)) + "']")
383: .equals(out.toString()) : "Unexepcted output: "
384: + out.toString();
385: }
386: }
|