001: /*
002: * Copyright (C) 2007, 2008 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 30. April 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.io.json;
012:
013: import java.io.IOException;
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.io.StringReader;
017: import java.io.StringWriter;
018: import java.util.ArrayList;
019:
020: import junit.framework.TestCase;
021:
022: import com.thoughtworks.acceptance.objects.Category;
023: import com.thoughtworks.acceptance.objects.Product;
024: import com.thoughtworks.xstream.XStream;
025: import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
026:
027: /**
028: * Testing serialization to and from JSON with Jettison driver.
029: *
030: * @author Dejan Bosanac
031: */
032: public class JettisonMappedXmlDriverTest extends TestCase {
033:
034: private final static String SIMPLE = "{'product':{'name':'Banana','id':'123','price':'23.0'}}"
035: .replace('\'', '"');
036: private final static String HIERARCHY = "{'category':{'name':'fruit','id':'111','products':{'product':[{'name':'Banana','id':'123','price':'23.01','tags':{'string':['yellow','fresh','tasty']}},{'name':'Mango','id':'124','price':'34.01'}]}}}"
037: .replace('\'', '"');
038:
039: private XStream xstream;
040:
041: /**
042: * @see junit.framework.TestCase#setUp()
043: */
044: protected void setUp() throws Exception {
045: super .setUp();
046: xstream = new XStream(new JettisonMappedXmlDriver());
047: xstream.alias("category", Category.class);
048: xstream.alias("product", Product.class);
049: }
050:
051: public void testReadSimple() {
052: Product product = (Product) xstream.fromXML(SIMPLE);
053: assertEquals(product.getName(), "Banana");
054: assertEquals(product.getId(), "123");
055: assertEquals("" + product.getPrice(), "" + 23.00);
056: }
057:
058: public void testWriteSimple() {
059: Product product = new Product("Banana", "123", 23.00);
060: String result = xstream.toXML(product);
061: assertEquals(SIMPLE, result);
062: }
063:
064: public void testWriteHierarchy() {
065: Category category = new Category("fruit", "111");
066: ArrayList products = new ArrayList();
067: Product banana = new Product("Banana", "123", 23.01);
068: ArrayList bananaTags = new ArrayList();
069: bananaTags.add("yellow");
070: bananaTags.add("fresh");
071: bananaTags.add("tasty");
072: banana.setTags(bananaTags);
073: products.add(banana);
074: Product mango = new Product("Mango", "124", 34.01);
075: products.add(mango);
076: category.setProducts(products);
077: String result = xstream.toXML(category);
078: assertEquals(HIERARCHY, result);
079: }
080:
081: public void testHierarchyRead() {
082: Category parsedCategory = (Category) xstream.fromXML(HIERARCHY);
083: Product parsedBanana = (Product) parsedCategory.getProducts()
084: .get(0);
085: assertEquals("Banana", parsedBanana.getName());
086: assertEquals(3, parsedBanana.getTags().size());
087: assertEquals("yellow", parsedBanana.getTags().get(0));
088: assertEquals("tasty", parsedBanana.getTags().get(2));
089: }
090:
091: public void testObjectStream() throws IOException,
092: ClassNotFoundException {
093: Product product = new Product("Banana", "123", 23.00);
094: StringWriter writer = new StringWriter();
095: ObjectOutputStream oos = xstream.createObjectOutputStream(
096: writer, "oos");
097: oos.writeObject(product);
098: oos.close();
099: String json = writer.toString();
100: assertEquals("{\"oos\":" + SIMPLE + "}", json);
101: ObjectInputStream ois = xstream
102: .createObjectInputStream(new StringReader(json));
103: Product parsedProduct = (Product) ois.readObject();
104: assertEquals(product.toString(), parsedProduct.toString());
105: }
106:
107: public void testDoesHandleQuotesAndEscapes() {
108: String[] strings = new String[] { "last\"", "\"first",
109: "\"between\"", "around \"\" it", "back\\slash", };
110: String expected = ("" + "{#string-array#:{#string#:["
111: + "#last\\\"#," + "#\\\"first#," + "#\\\"between\\\"#,"
112: + "#around \\\"\\\" it#," + "#back\\\\slash#" + "]}}")
113: .replace('#', '"');
114: assertEquals(expected, xstream.toXML(strings));
115: }
116:
117: public void testDoesEscapeValuesAccordingRfc4627() {
118: String expected = "{'string':'\\u0000\\u0001\\u001f \uffee'}"
119: .replace('\'', '"');
120: assertEquals(expected, xstream
121: .toXML("\u0000\u0001\u001f\u0020\uffee"));
122: }
123:
124: // TODO: See XSTR-460
125: public void todoTestArrayList() throws IOException {
126: ArrayList list1 = new ArrayList();
127: list1.clear();
128: list1.add(new Integer(12));
129:
130: list1.add("string");
131: list1.add(new Integer(13));
132: // StringWriter writer = new StringWriter();
133: // xstream.marshal(list1, new JsonHierarchicalStreamWriter(writer));
134: // writer.close();
135: // String json = writer.toString();
136: String json = xstream.toXML(list1);
137:
138: ArrayList list2 = (ArrayList) xstream.fromXML(json);
139: assertEquals(json, xstream.toXML(list2));
140: }
141: }
|