1   /*
2    * $Id: ArrayUtilsTestCase.java 10529 2008-01-25 05:58:36Z dfeist $
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.List;
16  
17  public class ArrayUtilsTestCase extends AbstractMuleTestCase
18  {
19  
20      public void testToArrayOfComponentType()
21      {
22          // null array
23          assertNull(ArrayUtils.toArrayOfComponentType(null, String.class));
24  
25          // empty array, same result
26          String[] a = new String[]{};
27          String[] a2 = (String[])ArrayUtils.toArrayOfComponentType(a, String.class);
28          assertSame(a2, a);
29  
30          // null service type is not allowed
31          try
32          {
33              ArrayUtils.toArrayOfComponentType(a, null);
34              fail();
35          }
36          catch (IllegalArgumentException iex)
37          {
38              // ok
39          }
40  
41          // single element
42          a = new String[]{":-)"};
43          CharSequence[] cs = (CharSequence[])ArrayUtils.toArrayOfComponentType(a, CharSequence.class);
44          assertEquals(a.length, cs.length);
45          assertSame(a[0], cs[0]);
46  
47          // incompatible element types are not a good idea either
48          try
49          {
50              ArrayUtils.toArrayOfComponentType(a, List.class);
51              fail();
52          }
53          catch (ArrayStoreException asx)
54          {
55              // ok
56          }
57  
58      }
59  
60      public void testToStringMaxLength()
61      {
62          Object test = new byte[100];
63          for (int i = 0; i < ((byte[])test).length; i++)
64          {
65              ((byte[])test)[i] = (byte)i;
66          }
67  
68          // the String will contain not more than exactly MAX_ARRAY_LENGTH elements
69          String result = ArrayUtils.toString(test, 10);
70          assertTrue(result.endsWith("[..]}"));
71          assertEquals(9, StringUtils.countMatches(result, ","));
72      }
73  
74  }