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.io.Serializable;
12  import java.util.ArrayList;
13  import java.util.Arrays;
14  import java.util.Collection;
15  import java.util.Collections;
16  import java.util.Date;
17  
18  import org.junit.Test;
19  
20  import static junit.framework.Assert.assertFalse;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  public class CollectionUtilsTestCase extends AbstractMuleTestCase
27  {
28  
29      @Test
30      public void testToArrayOfComponentTypeNullCollection()
31      {
32          assertNull(CollectionUtils.toArrayOfComponentType(null, String.class));
33      }
34  
35      @Test(expected = IllegalArgumentException.class)
36      public void testToArrayOfComponentTypeNullType()
37      {
38          CollectionUtils.toArrayOfComponentType(Collections.EMPTY_LIST, null);
39      }
40  
41      @Test
42      public void testToArrayOfComponentTypeEmptyCollection()
43      {
44          assertTrue(Arrays.equals(new String[0], CollectionUtils.toArrayOfComponentType(
45                  Collections.EMPTY_LIST, String.class)));
46      }
47  
48      @Test(expected = ArrayStoreException.class)
49      public void testToArrayOfComponentTypeWrongElement()
50      {
51          CollectionUtils.toArrayOfComponentType(Collections.singleton("foo"), Integer.class);
52      }
53  
54      @Test
55      public void testToArrayOfComponentTypeOK()
56      {
57          String[] objects = new String[] {"foo", "bar", "baz"};
58          assertTrue(Arrays.equals(objects, CollectionUtils.toArrayOfComponentType(Arrays.asList(objects),
59                                                                                   String.class)));
60      }
61  
62      @Test
63      public void testToStringNull() throws Exception
64      {
65          Collection c = null;
66          assertEquals("[]", CollectionUtils.toString(c, false));
67          assertEquals("[]", CollectionUtils.toString(c, true));
68      }
69  
70      @Test
71      public void testToStringEmpty() throws Exception
72      {
73          Collection c = new ArrayList();
74          assertEquals("[]", CollectionUtils.toString(c, false));
75          assertEquals("[]", CollectionUtils.toString(c, true));
76      }
77  
78      @Test
79      public void testToStringSingleElement() throws Exception
80      {
81          Collection<String> c = Arrays.asList("foo");
82  
83          assertEquals("[foo]", CollectionUtils.toString(c, false));
84          assertEquals("[" + SystemUtils.LINE_SEPARATOR + "foo" + SystemUtils.LINE_SEPARATOR + "]",
85                       CollectionUtils.toString(c, true));
86      }
87  
88      @Test
89      public void testToStringMultipleElements() throws Exception
90      {
91          Collection<Serializable> c = Arrays.asList("foo", this.getClass());
92  
93          assertEquals("[foo, " + this.getClass().getName() + "]", CollectionUtils.toString(c, false));
94  
95          assertEquals("[" + SystemUtils.LINE_SEPARATOR + "foo" + SystemUtils.LINE_SEPARATOR
96                       + this.getClass().getName() + SystemUtils.LINE_SEPARATOR + "]", CollectionUtils
97                  .toString(c, true));
98      }
99  
100     @Test
101     public void testToStringTooManyElements()
102     {
103         Collection<Number> test = new ArrayList<Number>(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     @Test
116     public void testContainsTypeTrue()
117     {
118         Collection c = new ArrayList();
119         c.add(new String());
120         c.add(new Date());
121         assertTrue(CollectionUtils.containsType(c, Date.class));
122     }
123 
124     @Test
125     public void testContainsTypeFalse()
126     {
127         Collection c = new ArrayList();
128         c.add(new String());
129         c.add(new Integer(1));
130         assertFalse(CollectionUtils.containsType(c, Date.class));
131     }
132 
133     @Test
134     public void testContainsTypeNullChecks()
135     {
136         Collection c = new ArrayList();
137         c.add(new String());
138         c.add(new Integer(1));
139         assertFalse(CollectionUtils.containsType(c, null));
140         assertFalse(CollectionUtils.containsType(null, Date.class));
141     }
142 
143     @Test
144     public void testRemoveType()
145     {
146         Collection c = new ArrayList();
147         c.add(new String());
148         c.add(new Integer(1));
149         CollectionUtils.removeType(c, String.class);
150         assertEquals(1, c.size());
151         assertFalse(CollectionUtils.containsType(c, null));
152         assertFalse(CollectionUtils.containsType(null, Date.class));
153     }
154 
155 }