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.util;
8   
9   import org.mule.tck.junit4.AbstractMuleTestCase;
10  
11  import java.util.Arrays;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertNotNull;
17  import static org.junit.Assert.assertNull;
18  import static org.junit.Assert.assertTrue;
19  import static org.junit.Assert.fail;
20  
21  public class StringUtilsTestCase extends AbstractMuleTestCase
22  {
23  
24      @Test
25      public void testSplitAndTrim1()
26      {
27          String[] result = StringUtils.splitAndTrim(null, ",,");
28          assertNull(result);
29  
30          result = StringUtils.splitAndTrim("", ",");
31          assertNotNull(result);
32          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, result));
33  
34          result = StringUtils.splitAndTrim(" ", ",");
35          assertNotNull(result);
36          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, result));
37      }
38  
39      @Test
40      public void testSplitAndTrim2()
41      {
42          String[] inputValues = new String[]{"foo", "bar", "baz", "kaboom"};
43          String inputString = new StringBuffer(40)
44              .append(inputValues[0])
45              .append(" ,")
46              .append(",  ")
47              .append(inputValues[1])
48              .append(" ,")
49              .append(inputValues[2])
50              .append("  ,  ")
51              .append(inputValues[3])
52              .append(" ")
53              .toString();
54  
55          assertTrue(Arrays.equals(inputValues, StringUtils.splitAndTrim(inputString, ",")));
56      }
57  
58      @Test
59      public void testSplitAndTrim3()
60      {
61          String[] inputValues = new String[]{"foo", "bar", "baz", "kaboom"};
62          String inputString = "foo,  bar,\nbaz,  \nkaboom";
63          assertTrue(Arrays.equals(inputValues, StringUtils.splitAndTrim(inputString, ",")));
64      }
65  
66      @Test
67      public void testHexStringToByteArray()
68      {
69          assertNull(StringUtils.hexStringToByteArray(null));
70  
71          try
72          {
73              StringUtils.hexStringToByteArray("1");
74              fail();
75          }
76          catch (IllegalArgumentException iex)
77          {
78              // OK
79          }
80  
81          assertTrue(Arrays.equals(new byte[]{}, StringUtils.hexStringToByteArray("")));
82          assertTrue(Arrays.equals(new byte[]{1}, StringUtils.hexStringToByteArray("01")));
83          assertTrue(Arrays.equals(new byte[]{1, 2}, StringUtils.hexStringToByteArray("0102")));
84          assertTrue(Arrays.equals(new byte[]{10, 14}, StringUtils.hexStringToByteArray("0A0E")));
85          assertTrue(Arrays.equals(new byte[]{10, 14}, StringUtils.hexStringToByteArray("0a0e")));
86          assertTrue(Arrays.equals(new byte[]{10, (byte)0xff}, StringUtils.hexStringToByteArray("0AFF")));
87          assertTrue(Arrays.equals(new byte[]{10, (byte)0xff}, StringUtils.hexStringToByteArray("0aff")));
88      }
89  
90      @Test
91      public void testByteArrayToHexString()
92      {
93          assertNull(StringUtils.toHexString(null));
94          assertEquals("", StringUtils.toHexString(new byte[]{}));
95          assertEquals("01", StringUtils.toHexString(new byte[]{1}));
96          assertEquals("0102", StringUtils.toHexString(new byte[]{1, 2}));
97          assertEquals("0a0e", StringUtils.toHexString(new byte[]{10, 14}));
98          assertEquals("0A0E", StringUtils.toHexString(new byte[]{10, 14}, true));
99          assertEquals("0aff", StringUtils.toHexString(new byte[]{10, (byte)0xff}));
100         assertEquals("0AFF", StringUtils.toHexString(new byte[]{10, (byte)0xff}, true));
101     }
102 
103 }