View Javadoc

1   /*
2    * $Id: HexStringByteArrayTransformersTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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.Transformer;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.transformer.AbstractTransformerTestCase;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.InputStream;
19  import java.util.Arrays;
20  
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertEquals;
24  
25  public class HexStringByteArrayTransformersTestCase extends AbstractTransformerTestCase
26  {
27  
28      public Transformer getTransformer()
29      {
30          return new HexStringToByteArray();
31      }
32  
33      public Transformer getRoundTripTransformer()
34      {
35          return new ByteArrayToHexString();
36      }
37  
38      public Object getTestData()
39      {
40          return "01020aff";
41      }
42  
43      public Object getResultData()
44      {
45          return new byte[]{1, 2, 10, (byte)0xff};
46      }
47  
48      @Override
49      public boolean compareResults(Object src, Object result)
50      {
51          if (src == null && result == null)
52          {
53              return true;
54          }
55          if (src == null || result == null)
56          {
57              return false;
58          }
59          return Arrays.equals((byte[])src, (byte[])result);
60      }
61  
62      @Override
63      public boolean compareRoundtripResults(Object src, Object result)
64      {
65          if (src == null && result == null)
66          {
67              return true;
68          }
69          if (src == null || result == null)
70          {
71              return false;
72          }
73          return src.equals(result);
74      }
75  
76      // extra test for uppercase output
77      @Test
78      public void testUppercase() throws TransformerException
79      {
80          ByteArrayToHexString t = new ByteArrayToHexString();
81          t.setUpperCase(true);
82  
83          assertEquals(((String)getTestData()).toUpperCase(), t.transform(getResultData()));
84      }
85      
86      @Test
87      public void testStreaming() throws TransformerException
88      {
89          ByteArrayToHexString transformer = new ByteArrayToHexString();
90          InputStream input = new ByteArrayInputStream((byte[]) this.getResultData());
91          
92          assertEquals(this.getTestData(), transformer.transform(input));
93      }
94  
95  }