1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.functor.core.collection;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.commons.functor.BaseFunctorTest;
32 import org.apache.commons.functor.UnaryPredicate;
33 import org.apache.commons.functor.core.Constant;
34 import org.junit.Test;
35
36
37
38
39
40 @SuppressWarnings("unchecked")
41 public class TestIsEmpty extends BaseFunctorTest {
42
43
44
45
46 protected Object makeFunctor() {
47 return new IsEmpty();
48 }
49
50
51
52
53 @Test
54 public void testTest() throws Exception {
55 assertTrue(IsEmpty.instance().test(Collections.EMPTY_LIST));
56 assertTrue(IsEmpty.instance().test(Collections.EMPTY_SET));
57 {
58 List list = new ArrayList();
59 assertTrue(IsEmpty.instance().test(list));
60 list.add("Xyzzy");
61 assertTrue(!IsEmpty.instance().test(list));
62 }
63 {
64 Set set = new HashSet();
65 assertTrue(IsEmpty.instance().test(set));
66 set.add("Xyzzy");
67 assertTrue(!IsEmpty.instance().test(set));
68 }
69 }
70
71 @Test
72 public void testTestNull() throws Exception {
73 try {
74 IsEmpty.instance().test(null);
75 fail("Expected IllegalArgumentException");
76 } catch(IllegalArgumentException e) {
77
78 }
79 }
80
81 @Test
82 public void testTestNonCollection() throws Exception {
83 try {
84 IsEmpty.instance().test(new Integer(3));
85 fail("Expected IllegalArgumentException");
86 } catch(IllegalArgumentException e) {
87
88 }
89 }
90
91 @Test
92 public void testTestArray() throws Exception {
93 assertTrue(! IsEmpty.instance().test(new int[10]));
94 assertTrue(! IsEmpty.instance().test(new Object[10]));
95 assertTrue(IsEmpty.instance().test(new int[0]));
96 assertTrue(IsEmpty.instance().test(new Object[0]));
97 }
98
99 @Test
100 public void testTestString() throws Exception {
101 assertTrue(! IsEmpty.instance().test("xyzzy"));
102 assertTrue(IsEmpty.instance().test(""));
103 }
104
105 @Test
106 public void testTestMap() throws Exception {
107 Map map = new HashMap();
108 assertTrue(IsEmpty.instance().test(map));
109 map.put("x","y");
110 assertTrue(! IsEmpty.instance().test(map));
111 }
112
113 @Test
114 public void testEquals() throws Exception {
115 UnaryPredicate p = new IsEmpty();
116 assertEquals(p,p);
117 assertObjectsAreEqual(p,new IsEmpty());
118 assertObjectsAreEqual(p,IsEmpty.instance());
119 assertObjectsAreNotEqual(p,new Constant(true));
120 }
121
122 }