View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transformer.simple;
8   
9   import org.mule.api.transformer.Transformer;
10  import org.mule.api.transformer.TransformerException;
11  import org.mule.transformer.AbstractTransformerTestCase;
12  
13  import java.io.ByteArrayInputStream;
14  import java.io.InputStream;
15  import java.util.Arrays;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  public class HexStringByteArrayTransformersTestCase extends AbstractTransformerTestCase
22  {
23  
24      public Transformer getTransformer()
25      {
26          return new HexStringToByteArray();
27      }
28  
29      public Transformer getRoundTripTransformer()
30      {
31          return new ByteArrayToHexString();
32      }
33  
34      public Object getTestData()
35      {
36          return "01020aff";
37      }
38  
39      public Object getResultData()
40      {
41          return new byte[]{1, 2, 10, (byte)0xff};
42      }
43  
44      @Override
45      public boolean compareResults(Object src, Object result)
46      {
47          if (src == null && result == null)
48          {
49              return true;
50          }
51          if (src == null || result == null)
52          {
53              return false;
54          }
55          return Arrays.equals((byte[])src, (byte[])result);
56      }
57  
58      @Override
59      public boolean compareRoundtripResults(Object src, Object result)
60      {
61          if (src == null && result == null)
62          {
63              return true;
64          }
65          if (src == null || result == null)
66          {
67              return false;
68          }
69          return src.equals(result);
70      }
71  
72      // extra test for uppercase output
73      @Test
74      public void testUppercase() throws TransformerException
75      {
76          ByteArrayToHexString t = new ByteArrayToHexString();
77          t.setUpperCase(true);
78  
79          assertEquals(((String)getTestData()).toUpperCase(), t.transform(getResultData()));
80      }
81      
82      @Test
83      public void testStreaming() throws TransformerException
84      {
85          ByteArrayToHexString transformer = new ByteArrayToHexString();
86          InputStream input = new ByteArrayInputStream((byte[]) this.getResultData());
87          
88          assertEquals(this.getTestData(), transformer.transform(input));
89      }
90  
91  }