1
2
3
4
5
6
7
8
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
21
22
23
24
25
26
27 public class JsonBeanRoundTripTestCase extends AbstractTransformerTestCase
28 {
29
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
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
63 return JSON_OBJECT;
64 }
65
66 @Override
67 public Object getResultData()
68 {
69
70 return JSON_STRING;
71 }
72
73 @Override
74 public boolean compareResults(Object expected, Object result)
75 {
76
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 }