1   /*
2    * $Id: SerializedUMOMessageTransformersTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.transformers.simple;
12  
13  import org.mule.impl.MuleEvent;
14  import org.mule.impl.MuleMessage;
15  import org.mule.impl.RequestContext;
16  import org.mule.tck.AbstractTransformerTestCase;
17  import org.mule.tck.MuleTestUtils;
18  import org.mule.tck.testmodels.fruit.Apple;
19  import org.mule.umo.UMOMessage;
20  import org.mule.umo.transformer.UMOTransformer;
21  
22  import java.io.IOException;
23  import java.io.ObjectOutputStream;
24  import java.util.Arrays;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.io.output.ByteArrayOutputStream;
29  
30  public class SerializedUMOMessageTransformersTestCase extends AbstractTransformerTestCase
31  {
32      private UMOMessage testObject = null;
33  
34      // @Override
35      protected void doSetUp() throws Exception
36      {
37          RequestContext.setEvent(new MuleEvent(testObject, getTestEndpoint("test", "sender"), MuleTestUtils
38              .getTestSession(), true));
39      }
40  
41      // @Override
42      protected void doTearDown() throws Exception
43      {
44          RequestContext.clear();
45      }
46  
47      // @Override
48  
49      public void testTransform() throws Exception
50      {
51          // this depends on the ordering of properties in the map.
52          // because we now make a copy of maps in RequestContext this order can change
53          //super.testTransform();
54      }
55  
56      public SerializedUMOMessageTransformersTestCase()
57      {
58          Map props = new HashMap();
59          props.put("object", new Apple());
60          props.put("number", new Integer(1));
61          props.put("string", "hello");
62          testObject = new MuleMessage("test", props);
63      }
64  
65      public UMOTransformer getTransformer() throws Exception
66      {
67          SerializableToByteArray t = new SerializableToByteArray();
68          t.setAcceptUMOMessage(true);
69          return t;
70      }
71  
72      public UMOTransformer getRoundTripTransformer() throws Exception
73      {
74          return new ByteArrayToSerializable();
75      }
76  
77      public Object getTestData()
78      {
79          return testObject;
80      }
81  
82      public Object getResultData()
83      {
84          try
85          {
86              ByteArrayOutputStream bs = null;
87              ObjectOutputStream os = null;
88  
89              bs = new ByteArrayOutputStream();
90              os = new ObjectOutputStream(bs);
91              os.writeObject(testObject);
92              os.flush();
93              os.close();
94              return bs.toByteArray();
95          }
96          catch (IOException e)
97          {
98              throw new IllegalStateException(e.getMessage());
99          }
100     }
101 
102     // @Override
103     public boolean compareResults(Object src, Object result)
104     {
105         if (src == null && result == null)
106         {
107             return true;
108         }
109         if (src == null || result == null)
110         {
111             return false;
112         }
113         return Arrays.equals((byte[])src, (byte[])result);
114     }
115 
116     // @Override
117     public boolean compareRoundtripResults(Object src, Object result)
118     {
119         if (src == null && result == null)
120         {
121             return true;
122         }
123         if (src == null || result == null)
124         {
125             return false;
126         }
127         if (src instanceof UMOMessage && result instanceof UMOMessage)
128         {
129             return ((UMOMessage)src).getPayload().equals(((UMOMessage)result).getPayload())
130                             && ((UMOMessage)src).getProperty("object").equals(
131                                 ((UMOMessage)result).getProperty("object"))
132                             && ((UMOMessage)src).getProperty("string").equals(
133                                 ((UMOMessage)result).getProperty("string"))
134                             && ((UMOMessage)src).getIntProperty("number", -1) == ((UMOMessage)result)
135                                 .getIntProperty("number", -2);
136         }
137         else
138         {
139             return false;
140         }
141     }
142 
143     // @Override
144     protected void doTestClone(UMOTransformer original, UMOTransformer clone) throws Exception
145     {
146         super.doTestClone(original, clone);
147         assertTrue(((SerializableToByteArray)clone).isAcceptUMOMessage());
148     }
149     
150 }