1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
38   * @author Rodney Waldhoff
39   */
40  @SuppressWarnings("unchecked")
41  public class TestIsEmpty extends BaseFunctorTest {
42  
43      // Functor Testing Framework
44      // ------------------------------------------------------------------------
45  
46      protected Object makeFunctor() {
47          return new IsEmpty();
48      }
49  
50      // Tests
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              // expected
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              // expected
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 }