View Javadoc

1   /*
2    * $Id: ObjectToInputStreamTestCase.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.transformer.simple;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.tck.testmodels.fruit.Apple;
16  import org.mule.util.IOUtils;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  
23  import org.apache.commons.lang.SerializationUtils;
24  
25  public class ObjectToInputStreamTestCase extends AbstractMuleTestCase
26  {
27  
28      private ObjectToInputStream transformer = new ObjectToInputStream();
29  
30      public void testTransformString() throws TransformerException, IOException
31      {
32          assertTrue(InputStream.class.isAssignableFrom(transformer.transform(AbstractMuleTestCase.TEST_MESSAGE)
33              .getClass()));
34          assertTrue(compare(new ByteArrayInputStream(AbstractMuleTestCase.TEST_MESSAGE.getBytes()),
35              (InputStream) transformer.transform(AbstractMuleTestCase.TEST_MESSAGE)));
36      }
37  
38      public void testTransformByteArray() throws TransformerException, IOException
39      {
40          assertTrue(InputStream.class.isAssignableFrom(transformer.transform(
41              AbstractMuleTestCase.TEST_MESSAGE.getBytes()).getClass()));
42          assertTrue(compare(new ByteArrayInputStream(AbstractMuleTestCase.TEST_MESSAGE.getBytes()),
43              (InputStream) transformer.transform(AbstractMuleTestCase.TEST_MESSAGE)));
44      }
45  
46      public void testTransformInputStream()
47      {
48          InputStream inputStream = new ByteArrayInputStream(AbstractMuleTestCase.TEST_MESSAGE.getBytes());
49          try
50          {
51              assertEquals(inputStream, transformer.transform(inputStream));
52          }
53          catch (Exception e)
54          {
55              assertTrue(e instanceof TransformerException);
56              assertTrue(e.getMessage().contains("does not support source type"));
57          }
58      }
59  
60      public void testTransformSerializable()
61      {
62          Apple apple = new Apple();
63          InputStream serializedApple = new ByteArrayInputStream(SerializationUtils.serialize(apple));
64          try
65          {
66              assertTrue(compare(serializedApple, (InputStream) transformer.transform(apple)));
67          }
68          catch (Exception e)
69          {
70              assertTrue(e instanceof TransformerException);
71              assertTrue(e.getMessage().contains("does not support source type"));
72          }
73      }
74  
75      public static boolean compare(InputStream input1, InputStream input2)
76      {
77          byte[] bytes1 = IOUtils.toByteArray(input1);
78          byte[] bytes2 = IOUtils.toByteArray(input2);
79          return Arrays.equals(bytes1, bytes2);
80      }
81  }