View Javadoc
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  import java.util.Collection;
25  import java.util.List;
26  
27  import org.apache.commons.functor.BaseFunctorTest;
28  import org.apache.commons.functor.Predicate;
29  import org.apache.commons.functor.core.Constant;
30  import org.junit.Test;
31  
32  /**
33   * @version $Revision: 1541658 $ $Date: 2013-11-13 19:54:05 +0100 (Mi, 13 Nov 2013) $
34   */
35  public class TestIsElementOf extends BaseFunctorTest {
36  
37      // Functor Testing Framework
38      // ------------------------------------------------------------------------
39  
40      @Override
41      protected Object makeFunctor() {
42          return new IsElementOf<Integer, Collection<Integer>>();
43      }
44  
45      // Tests
46      // ------------------------------------------------------------------------
47  
48      @Test
49      public void testTestCollection() throws Exception {
50          List<Integer> list = new ArrayList<Integer>();
51          list.add(Integer.valueOf(5));
52          list.add(Integer.valueOf(10));
53          list.add(Integer.valueOf(15));
54  
55          Predicate<Integer> p = IsElementOf.instance(list);
56          assertTrue(p.test(Integer.valueOf(5)));
57          assertTrue(p.test(Integer.valueOf(10)));
58          assertTrue(p.test(Integer.valueOf(15)));
59  
60          assertTrue(!p.test(Integer.valueOf(4)));
61          assertTrue(!p.test(Integer.valueOf(11)));
62  
63      }
64  
65      @Test
66      public void testTestArray() throws Exception {
67          int[] list = new int[] { 5, 10, 15 };
68  
69          Predicate<Integer> p = IsElementOf.instance(list);
70          assertTrue(p.test(Integer.valueOf(5)));
71          assertTrue(p.test(Integer.valueOf(10)));
72          assertTrue(p.test(Integer.valueOf(15)));
73  
74          assertTrue(!p.test(Integer.valueOf(4)));
75          assertTrue(!p.test(Integer.valueOf(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[] { Integer.valueOf(5), null, Integer.valueOf(15) }));
82          assertTrue(IsElementOf.instance().test(Integer.valueOf(15),new Integer[] { Integer.valueOf(5), null, Integer.valueOf(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(Integer.valueOf(3));
99              fail("expected IllegalArgumentException");
100         } catch (IllegalArgumentException e) {
101             // expected
102         }
103     }
104 
105     @Test(expected = NullPointerException.class)
106     public void testTestNull() {
107         IsElementOf.instance().test(Integer.valueOf(5),null);
108     }
109 
110     @Test
111     public void testTestNonCollection() {
112         try {
113             IsElementOf.instance().test(Integer.valueOf(5),Long.valueOf(5));
114             fail("expected IllegalArgumentException");
115         } catch (IllegalArgumentException e) {
116             // expected
117         }
118     }
119 
120     @Test
121     public void testEquals() throws Exception {
122         IsElementOf<Integer, Collection<Integer>> p1 = new IsElementOf<Integer, Collection<Integer>>();
123         assertObjectsAreEqual(p1, p1);
124         assertObjectsAreEqual(p1, new IsElementOf<Integer, Collection<Integer>>());
125         assertObjectsAreEqual(p1, IsElementOf.instance());
126         assertSame(IsElementOf.instance(), IsElementOf.instance());
127         assertObjectsAreNotEqual(p1, Constant.falsePredicate());
128     }
129 }