View Javadoc

1   /*
2    * $Id: JsonBeanRoundTripTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.json.transformers;
12  
13  import org.mule.api.transformer.Transformer;
14  import org.mule.tck.testmodels.fruit.Apple;
15  import org.mule.tck.testmodels.fruit.Orange;
16  import org.mule.transformer.AbstractTransformerTestCase;
17  import org.mule.transformer.types.DataTypeFactory;
18  
19  /**
20   * For this test I picked difficult beans in that they are not real beans, so I could test how to use
21   * mixins to decorate the objects
22   * <p/>
23   * FruitCleaner is ignored since there is no concrete implementation to construct
24   * bitten - is ignored because the Orange because there is no setter method.  On the apple I tested using a
25   * constructor
26   */
27  public class JsonBeanRoundTripTestCase extends AbstractTransformerTestCase
28  {
29      //Note that Banana has been excluded
30      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}}";
31  
32      //Note that Banana is null
33      public static final FruitCollection JSON_OBJECT = new FruitCollection(new Apple(true), null, new Orange(8, new Double(3.45), "JuicyFruit"));
34  
35      @Override
36      public Transformer getTransformer() throws Exception
37      {
38          ObjectToJson trans = new ObjectToJson();
39          trans.getSerializationMixins().put(FruitCollection.class, FruitCollectionMixin.class);
40          trans.getSerializationMixins().put(Apple.class, AppleMixin.class);
41          trans.getSerializationMixins().put(Orange.class, OrangeMixin.class);
42          trans.setSourceClass(FruitCollection.class);
43          initialiseObject(trans);
44          return trans;
45      }
46  
47      @Override
48      public Transformer getRoundTripTransformer() throws Exception
49      {
50          JsonToObject trans = new JsonToObject();
51          trans.setReturnDataType(DataTypeFactory.create(getTestData().getClass()));
52          trans.getDeserializationMixins().put(FruitCollection.class, FruitCollectionMixin.class);
53          trans.getDeserializationMixins().put(Apple.class, AppleMixin.class);
54          trans.getDeserializationMixins().put(Orange.class, OrangeMixin.class);
55          initialiseObject(trans);
56          return trans;
57      }
58  
59      @Override
60      public Object getTestData()
61      {
62          //Banana is null
63          return JSON_OBJECT;
64      }
65  
66      @Override
67      public Object getResultData()
68      {
69          //Note that Banana has been excluded
70          return JSON_STRING;
71      }
72  
73      @Override
74      public boolean compareResults(Object expected, Object result)
75      {
76          //MULE-4879 field ordering is not guaranteed by the JVM so we cannot compare result strings
77          if(expected instanceof String)
78          {
79              try
80              {
81                  Transformer toObject = getRoundTripTransformer();
82                  expected = toObject.transform(expected);
83                  result = toObject.transform(result);
84              }
85              catch (Exception e)
86              {
87                  fail(e.getMessage());
88                  return false;
89              }
90          }
91  
92          return super.compareResults(expected, result);
93      }
94  }