1   /*
2    * $Id: StringUtilsTestCase.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.AbstractMuleTestCase;
14  
15  import java.util.Arrays;
16  
17  public class StringUtilsTestCase extends AbstractMuleTestCase
18  {
19  
20      public void testSplitWithTrimming()
21      {
22          String[] result = StringUtils.splitAndTrim(null, ",,");
23          assertNull(result);
24  
25          result = StringUtils.splitAndTrim("", ",");
26          assertNotNull(result);
27          assertTrue(Arrays.equals(ArrayUtils.EMPTY_STRING_ARRAY, result));
28  
29          result = StringUtils.splitAndTrim(" ", ",");
30          assertNotNull(result);
31          assertTrue(Arrays.equals(new String[]{""}, result));
32  
33          String[] inputValues = new String[]{"foo", "bar", "baz", "kaboom"};
34          String inputString = new StringBuffer(40)
35              .append(inputValues[0])
36              .append(" ,")
37              .append(",  ")
38              .append(inputValues[1])
39              .append(" ,")
40              .append(inputValues[2])
41              .append("  ,  ")
42              .append(inputValues[3])
43              .append(" ")
44              .toString();
45  
46          assertTrue(Arrays.equals(inputValues, StringUtils.splitAndTrim(inputString, ",")));
47      }
48  
49      public void testHexStringToByteArray()
50      {
51          assertNull(StringUtils.hexStringToByteArray(null));
52  
53          try
54          {
55              StringUtils.hexStringToByteArray("1");
56              fail();
57          }
58          catch (IllegalArgumentException iex)
59          {
60              // OK
61          }
62  
63          assertTrue(Arrays.equals(new byte[]{}, StringUtils.hexStringToByteArray("")));
64          assertTrue(Arrays.equals(new byte[]{1}, StringUtils.hexStringToByteArray("01")));
65          assertTrue(Arrays.equals(new byte[]{1, 2}, StringUtils.hexStringToByteArray("0102")));
66          assertTrue(Arrays.equals(new byte[]{10, 14}, StringUtils.hexStringToByteArray("0A0E")));
67          assertTrue(Arrays.equals(new byte[]{10, 14}, StringUtils.hexStringToByteArray("0a0e")));
68          assertTrue(Arrays.equals(new byte[]{10, (byte)0xff}, StringUtils.hexStringToByteArray("0AFF")));
69          assertTrue(Arrays.equals(new byte[]{10, (byte)0xff}, StringUtils.hexStringToByteArray("0aff")));
70      }
71  
72      public void testByteArrayToHexString()
73      {
74          assertNull(StringUtils.toHexString(null));
75          assertEquals("", StringUtils.toHexString(new byte[]{}));
76          assertEquals("01", StringUtils.toHexString(new byte[]{1}));
77          assertEquals("0102", StringUtils.toHexString(new byte[]{1, 2}));
78          assertEquals("0a0e", StringUtils.toHexString(new byte[]{10, 14}));
79          assertEquals("0A0E", StringUtils.toHexString(new byte[]{10, 14}, true));
80          assertEquals("0aff", StringUtils.toHexString(new byte[]{10, (byte)0xff}));
81          assertEquals("0AFF", StringUtils.toHexString(new byte[]{10, (byte)0xff}, true));
82      }
83  
84  }