View Javadoc

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