1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.tck.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 public class CollectionUtilsTestCase extends AbstractMuleTestCase
23 {
24
25 public void testToArrayOfComponentTypeNullCollection()
26 {
27 assertNull(CollectionUtils.toArrayOfComponentType(null, String.class));
28 }
29
30 public void testToArrayOfComponentTypeNullType()
31 {
32 try
33 {
34 CollectionUtils.toArrayOfComponentType(Collections.EMPTY_LIST, null);
35 fail("should have thrown IllegalArgumentException");
36 }
37 catch (IllegalArgumentException iex)
38 {
39
40 }
41 }
42
43 public void testToArrayOfComponentTypeEmptyCollection()
44 {
45 assertTrue(Arrays.equals(new String[0], CollectionUtils.toArrayOfComponentType(
46 Collections.EMPTY_LIST, String.class)));
47 }
48
49 public void testToArrayOfComponentTypeWrongElement()
50 {
51 try
52 {
53 CollectionUtils.toArrayOfComponentType(Collections.singleton("foo"), Integer.class);
54 fail("should have thrown ArrayStoreException");
55 }
56 catch (ArrayStoreException asx)
57 {
58
59 }
60 }
61
62 public void testToArrayOfComponentTypeOK()
63 {
64 String[] objects = new String[]{"foo", "bar", "baz"};
65 assertTrue(Arrays.equals(objects, CollectionUtils.toArrayOfComponentType(Arrays.asList(objects),
66 String.class)));
67 }
68
69 public void testToStringNull() throws Exception
70 {
71 Collection c = null;
72 assertEquals("[]", CollectionUtils.toString(c, false));
73 assertEquals("[]", CollectionUtils.toString(c, true));
74 }
75
76 public void testToStringEmpty() throws Exception
77 {
78 Collection c = new ArrayList();
79 assertEquals("[]", CollectionUtils.toString(c, false));
80 assertEquals("[]", CollectionUtils.toString(c, true));
81 }
82
83 public void testToStringSingleElement() throws Exception
84 {
85 Collection<String> c = Arrays.asList("foo");
86
87 assertEquals("[foo]", CollectionUtils.toString(c, false));
88 assertEquals("[" + SystemUtils.LINE_SEPARATOR + "foo" + SystemUtils.LINE_SEPARATOR + "]",
89 CollectionUtils.toString(c, true));
90 }
91
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 public void testToStringTooManyElements()
104 {
105 Collection<Number> test = new ArrayList<Number>(100);
106 for (int i = 0; i < 100; i++)
107 {
108 test.add(new Integer(i));
109 }
110
111
112 String result = CollectionUtils.toString(test, 10);
113 assertTrue(result.endsWith("[..]]"));
114 assertEquals(9, StringUtils.countMatches(result, ","));
115 }
116
117 public void testContainsTypeTrue()
118 {
119 Collection c = new ArrayList();
120 c.add(new String());
121 c.add(new Date());
122 assertTrue(CollectionUtils.containsType(c, Date.class));
123 }
124
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 public void testContainsTypeNullChecks()
134 {
135 Collection c = new ArrayList();
136 c.add(new String());
137 c.add(new Integer(1));
138 assertFalse(CollectionUtils.containsType(c, null));
139 assertFalse(CollectionUtils.containsType(null, Date.class));
140 }
141
142 public void testRemoveType()
143 {
144 Collection c = new ArrayList();
145 c.add(new String());
146 c.add(new Integer(1));
147 CollectionUtils.removeType(c, String.class);
148 assertEquals(1, c.size());
149 assertFalse(CollectionUtils.containsType(c, null));
150 assertFalse(CollectionUtils.containsType(null, Date.class));
151 }
152
153 }