1   /*
2    * $Id: CollectionUtilsTestCase.java 7963 2007-08-21 08:53:15Z 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.ArrayList;
16  import java.util.Arrays;
17  import java.util.Collection;
18  import java.util.Collections;
19  
20  public class CollectionUtilsTestCase extends AbstractMuleTestCase
21  {
22  
23      public void testToArrayOfComponentTypeNullCollection()
24      {
25          assertNull(CollectionUtils.toArrayOfComponentType(null, String.class));
26      }
27  
28      public void testToArrayOfComponentTypeNullType()
29      {
30          try
31          {
32              CollectionUtils.toArrayOfComponentType(Collections.EMPTY_LIST, null);
33              fail("should have thrown IllegalArgumentException");
34          }
35          catch (IllegalArgumentException iex)
36          {
37              // OK
38          }
39      }
40  
41      public void testToArrayOfComponentTypeEmptyCollection()
42      {
43          assertTrue(Arrays.equals(new String[0], CollectionUtils.toArrayOfComponentType(
44              Collections.EMPTY_LIST, String.class)));
45      }
46  
47      public void testToArrayOfComponentTypeWrongElement()
48      {
49          try
50          {
51              CollectionUtils.toArrayOfComponentType(Collections.singleton("foo"), Integer.class);
52              fail("should have thrown ArrayStoreException");
53          }
54          catch (ArrayStoreException asx)
55          {
56              // OK
57          }
58      }
59  
60      public void testToArrayOfComponentTypeOK()
61      {
62          String[] objects = new String[]{"foo", "bar", "baz"};
63          assertTrue(Arrays.equals(objects, CollectionUtils.toArrayOfComponentType(Arrays.asList(objects),
64              String.class)));
65      }
66  
67      public void testToStringNull() throws Exception
68      {
69          Collection c = null;
70          assertEquals("[]", CollectionUtils.toString(c, false));
71          assertEquals("[]", CollectionUtils.toString(c, true));
72      }
73  
74      public void testToStringEmpty() throws Exception
75      {
76          Collection c = new ArrayList();
77          assertEquals("[]", CollectionUtils.toString(c, false));
78          assertEquals("[]", CollectionUtils.toString(c, true));
79      }
80  
81      public void testToStringSingleElement() throws Exception
82      {
83          Collection c = Arrays.asList(new Object[]{"foo"});
84  
85          assertEquals("[foo]", CollectionUtils.toString(c, false));
86          assertEquals("[" + SystemUtils.LINE_SEPARATOR + "foo" + SystemUtils.LINE_SEPARATOR + "]",
87              CollectionUtils.toString(c, true));
88      }
89  
90      public void testToStringMultipleElements() throws Exception
91      {
92          Collection c = Arrays.asList(new Object[]{"foo", this.getClass()});
93  
94          assertEquals("[foo, " + this.getClass().getName() + "]", CollectionUtils.toString(c, false));
95  
96          assertEquals("[" + SystemUtils.LINE_SEPARATOR + "foo" + SystemUtils.LINE_SEPARATOR
97                          + this.getClass().getName() + SystemUtils.LINE_SEPARATOR + "]", CollectionUtils
98              .toString(c, true));
99      }
100 
101     public void testToStringTooManyElements()
102     {
103         Collection test = new ArrayList(100);
104         for (int i = 0; i < 100; i++)
105         {
106             test.add(new Integer(i));
107         }
108 
109         // the String will contain not more than exactly MAX_ARRAY_LENGTH elements
110         String result = CollectionUtils.toString(test, 10);
111         assertTrue(result.endsWith("[..]]"));
112         assertEquals(9, StringUtils.countMatches(result, ","));
113     }
114 
115 }