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