Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * $Id: JsonBeanRoundTripTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
  * --------------------------------------------------------------------------------------
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
  *
  * The software in this package is published under the terms of the CPAL v1.0
  * license, a copy of which has been included with this distribution in the
  * LICENSE.txt file.
  */
package org.mule.module.json.transformers;
For this test I picked difficult beans in that they are not real beans, so I could test how to use mixins to decorate the objects

FruitCleaner is ignored since there is no concrete implementation to construct bitten - is ignored because the Orange because there is no setter method. On the apple I tested using a constructor

    //Note that Banana has been excluded
    public static final String JSON_STRING = "{\"apple\":{\"washed\":false,\"bitten\":true},\"orange\":{\"brand\":\"JuicyFruit\",\"segments\":8,\"radius\":3.45,\"listProperties\":null,\"mapProperties\":null,\"arrayProperties\":null}}";
    //Note that Banana is null
    public static final FruitCollection JSON_OBJECT = new FruitCollection(new Apple(true), nullnew Orange(8, new Double(3.45), "JuicyFruit"));
    @Override
    public Transformer getTransformer() throws Exception
    {
        ObjectToJson trans = new ObjectToJson();
        trans.getSerializationMixins().put(FruitCollection.classFruitCollectionMixin.class);
        trans.getSerializationMixins().put(Apple.classAppleMixin.class);
        trans.getSerializationMixins().put(Orange.classOrangeMixin.class);
        trans.setSourceClass(FruitCollection.class);
        initialiseObject(trans);
        return trans;
    }
    @Override
    public Transformer getRoundTripTransformer() throws Exception
    {
        JsonToObject trans = new JsonToObject();
        trans.setReturnDataType(DataTypeFactory.create(getTestData().getClass()));
        trans.getDeserializationMixins().put(FruitCollection.classFruitCollectionMixin.class);
        trans.getDeserializationMixins().put(Apple.classAppleMixin.class);
        trans.getDeserializationMixins().put(Orange.classOrangeMixin.class);
        initialiseObject(trans);
        return trans;
    }
    @Override
    public Object getTestData()
    {
        //Banana is null
        return ;
    }
    @Override
    public Object getResultData()
    {
        //Note that Banana has been excluded
        return ;
    }
    @Override
    public boolean compareResults(Object expectedObject result)
    {
        //MULE-4879 field ordering is not guaranteed by the JVM so we cannot compare result strings
        if(expected instanceof String)
        {
            try
            {
                Transformer toObject = getRoundTripTransformer();
                expected = toObject.transform(expected);
                result = toObject.transform(result);
            }
            catch (Exception e)
            {
                fail(e.getMessage());
                return false;
            }
        }
        return super.compareResults(expectedresult);
    }
New to GrepCode? Check out our FAQ X