View Javadoc

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