package com.thoughtworks.xstream.io.json;

import java.util.ArrayList;

import junit.framework.TestCase;

import com.thoughtworks.acceptance.objects.Category;
import com.thoughtworks.acceptance.objects.Product;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;


/**
 * Testing serialization to and from JSON with Jettison driver.
 * 
 * @author Dejan Bosanac
 */
public class JettisonMappedXmlDriverTest extends TestCase {

    private String simpleJson = "{\"com.thoughtworks.acceptance.objects.Product\":{\"name\":\"Banana\",\"id\":\"123\",\"price\":\"23.0\"}}";
    private String hiearchyJson = "{\"category\":{\"name\":\"fruit\",\"id\":\"111\",\"products\":{\"product\":[{\"name\":\"Banana\",\"id\":\"123\",\"price\":\"23.0\",\"tags\":{\"string\":[\"yellow\",\"fresh\",\"tasty\"]}},{\"name\":\"Mango\",\"id\":\"124\",\"price\":\"34.0\"}]}}}";

    public void testReadSimple() {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("category", Category.class);
        xstream.alias("product", Product.class);
        Product product = (Product)xstream.fromXML(simpleJson);
        assertEquals(product.getName(), "Banana");
        assertEquals(product.getId(), "123");
        assertEquals("" + product.getPrice(), "" + 23.0);
    }

    public void testWriteSimple() {
        Product product = new Product("Banana", "123", 23.00);
        XStream xstream = new XStream(new JettisonMappedXmlDriver());

        String result = xstream.toXML(product);

        assertEquals(result, simpleJson);
    }

    public void testWriteHierarchy() {
        Category category = new Category("fruit", "111");
        ArrayList products = new ArrayList();
        Product banana = new Product("Banana", "123", 23.00);
        ArrayList bananaTags = new ArrayList();
        bananaTags.add("yellow");
        bananaTags.add("fresh");
        bananaTags.add("tasty");
        banana.setTags(bananaTags);
        products.add(banana);
        Product mango = new Product("Mango", "124", 34.00);
        products.add(mango);
        category.setProducts(products);
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("category", Category.class);
        xstream.alias("product", Product.class);
        String result = xstream.toXML(category);
        assertEquals(result, hiearchyJson);
    }

    public void testHierarchyRead() {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.alias("category", Category.class);
        xstream.alias("product", Product.class);
        Category parsedCategory = (Category)xstream.fromXML(hiearchyJson);
        Product parsedBanana = (Product)parsedCategory.getProducts().get(0);

        assertEquals(parsedBanana.getName(), "Banana");
        assertEquals(parsedBanana.getTags().size(), 3);
        assertEquals(parsedBanana.getTags().get(0), "yellow");
        assertEquals(parsedBanana.getTags().get(2), "tasty");
    }

}

@@ -514,45 +514,9 @@
 %22%7B%5C%22
-com.thoughtworks.acceptance.objects.P
+p
 rodu
@@ -866,59 +866,175 @@
 %7D%22;%0A
-%0A
     p
-ublic void testReadSimple() %7B%0A        XStream
+rivate XStream xstream;%0A%0A    /**%0A     * @see junit.framework.TestCase#setUp()%0A     */%0A    protected void setUp() throws Exception %7B%0A        super.setUp();%0A       
  xst
@@ -1176,32 +1176,74 @@
 Product.class);%0A
+    %7D%0A%0A    public void testReadSimple() %7B%0A
         Product 
@@ -1555,79 +1555,8 @@
 0);%0A
-        XStream xstream = new XStream(new JettisonMappedXmlDriver());%0A%0A
     
@@ -1599,17 +1599,16 @@
 oduct);%0A
-%0A
         
@@ -2208,477 +2208,137 @@
     
-XStream xstream = new XStream(new JettisonMappedXmlDriver());%0A        xstream.alias(%22category%22, Category.class);%0A        xstream.alias(%22product%22, Product.class);%0A        String result = xstream.toXML(category);%0A        assertEquals(result, hiearchyJson);%0A    %7D%0A%0A    public void testHierarchyRead() %7B%0A        XStream xstream = new XStream(new JettisonMappedXmlDriver());%0A        xstream.alias(%22category%22, Category.class);%0A        xstream.alias(%22product%22, Product.class);
+String result = xstream.toXML(category);%0A        assertEquals(result, hiearchyJson);%0A    %7D%0A%0A    public void testHierarchyRead() %7B
 %0A   
@@ -2486,17 +2486,16 @@
 get(0);%0A
-%0A
         

Unify Jettison-based unit tests.