View Javadoc

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