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.assertSame;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.ArrayList;
24  
25  import org.apache.commons.functor.BaseFunctorTest;
26  import org.apache.commons.functor.UnaryPredicate;
27  import org.apache.commons.functor.core.Constant;
28  import org.junit.Test;
29  
30  /**
31   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
32   * @author Rodney Waldhoff
33   * @author Jason Horman
34   */
35  @SuppressWarnings("unchecked")
36  public class TestIsElementOf extends BaseFunctorTest {
37  
38      // Functor Testing Framework
39      // ------------------------------------------------------------------------
40  
41      protected Object makeFunctor() {
42          return new IsElementOf();
43      }
44  
45      // Tests
46      // ------------------------------------------------------------------------
47  
48      @Test
49      public void testTestCollection() throws Exception {
50          ArrayList list = new ArrayList();
51          list.add(new Integer(5));
52          list.add(new Integer(10));
53          list.add(new Integer(15));
54  
55          UnaryPredicate p = IsElementOf.instance(list);
56          assertTrue(p.test(new Integer(5)));
57          assertTrue(p.test(new Integer(10)));
58          assertTrue(p.test(new Integer(15)));
59  
60          assertTrue(!p.test(new Integer(4)));
61          assertTrue(!p.test(new Integer(11)));
62  
63      }
64  
65      @Test
66      public void testTestArray() throws Exception {
67          int[] list = new int[] { 5, 10, 15 };
68  
69          UnaryPredicate p = IsElementOf.instance(list);
70          assertTrue(p.test(new Integer(5)));
71          assertTrue(p.test(new Integer(10)));
72          assertTrue(p.test(new Integer(15)));
73  
74          assertTrue(!p.test(new Integer(4)));
75          assertTrue(!p.test(new Integer(11)));
76      }
77  
78      @Test
79      public void testTestArrayWithNull() throws Exception {
80          assertTrue(! IsElementOf.instance().test(null,new int[] { 5, 10, 15 }));
81          assertTrue(IsElementOf.instance().test(null,new Integer[] { new Integer(5), null, new Integer(15) }));
82          assertTrue(IsElementOf.instance().test(new Integer(15),new Integer[] { new Integer(5), null, new Integer(15) }));
83      }
84  
85      @Test
86      public void testWrapNull() {
87          try {
88              IsElementOf.instance(null);
89              fail("expected NullPointerException");
90          } catch (NullPointerException e) {
91              // expected
92          }
93      }
94  
95      @Test
96      public void testWrapNonCollection() {
97          try {
98              IsElementOf.instance(new Integer(3));
99              fail("expected IllegalArgumentException");
100         } catch (IllegalArgumentException e) {
101             // expected
102         }
103     }
104 
105     @Test
106     public void testTestNull() {
107         try {
108             IsElementOf.instance().test(new Integer(5),null);
109             fail("expected IllegalArgumentException");
110         } catch (IllegalArgumentException e) {
111             // expected
112         }
113     }
114 
115     @Test
116     public void testTestNonCollection() {
117         try {
118             IsElementOf.instance().test(new Integer(5),new Long(5));
119             fail("expected IllegalArgumentException");
120         } catch (IllegalArgumentException e) {
121             // expected
122         }
123     }
124 
125     @Test
126     public void testEquals() throws Exception {
127         IsElementOf p1 = new IsElementOf();
128         assertObjectsAreEqual(p1, p1);
129         assertObjectsAreEqual(p1, new IsElementOf());
130         assertObjectsAreEqual(p1, IsElementOf.instance());
131         assertSame(IsElementOf.instance(), IsElementOf.instance());
132         assertObjectsAreNotEqual(p1, Constant.falsePredicate());
133     }
134 }