View Javadoc

1   /*
2    * $Id: HexStringByteArrayTransformersTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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  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      public void testUppercase() throws TransformerException
74      {
75          ByteArrayToHexString t = new ByteArrayToHexString();
76          t.setUpperCase(true);
77  
78          assertEquals(((String)getTestData()).toUpperCase(), t.transform(getResultData()));
79      }
80      
81      public void testStreaming() throws TransformerException
82      {
83          ByteArrayToHexString transformer = new ByteArrayToHexString();
84          InputStream input = new ByteArrayInputStream((byte[]) this.getResultData());
85          
86          assertEquals(this.getTestData(), transformer.transform(input));
87      }
88  
89  }