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.assertSame;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.commons.functor.BaseFunctorTest;
31  import org.apache.commons.functor.UnaryFunction;
32  import org.apache.commons.functor.core.Constant;
33  import org.junit.Test;
34  
35  /**
36   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
37   * @author Rodney Waldhoff
38   */
39  @SuppressWarnings("unchecked")
40  public class TestSize extends BaseFunctorTest {
41  
42      // Functor Testing Framework
43      // ------------------------------------------------------------------------
44  
45      protected Object makeFunctor() {
46          return new Size();
47      }
48  
49      // Tests
50      // ------------------------------------------------------------------------
51  
52      @Test
53      public void testEvaluate() throws Exception {
54          assertEquals(new Integer(0),Size.instance().evaluate(Collections.EMPTY_LIST));
55          assertEquals(new Integer(0),Size.instance().evaluate(Collections.EMPTY_SET));
56          {
57              List list = new ArrayList();
58              assertEquals(new Integer(0),Size.instance().evaluate(list));
59              for (int i=0;i<2;i++) {
60                  assertEquals(new Integer(i),Size.instance().evaluate(list));
61                  list.add(new Integer(i));
62                  assertEquals(new Integer(i+1),Size.instance().evaluate(list));
63              }
64          }
65          {
66              Set set = new HashSet();
67              assertEquals(new Integer(0),Size.instance().evaluate(set));
68              for (int i=0;i<2;i++) {
69                  assertEquals(new Integer(i),Size.instance().evaluate(set));
70                  set.add(new Integer(i));
71                  assertEquals(new Integer(i+1),Size.instance().evaluate(set));
72              }
73          }
74      }
75  
76      @Test
77      public void testEvaluateNull() throws Exception {
78          try {
79              Size.instance().evaluate(null);
80              fail("Expected IllegalArgumentException");
81          } catch(IllegalArgumentException e) {
82              // expected
83          }
84      }
85  
86      @Test
87      public void testEvaluateNonCollection() throws Exception {
88          try {
89              Size.instance().evaluate(new Integer(3));
90              fail("Expected IllegalArgumentException");
91          } catch(IllegalArgumentException e) {
92              // expected
93          }
94      }
95  
96      @Test
97      public void testEvaluateArray() throws Exception {
98          assertEquals(new Integer(10),Size.instance().evaluate(new int[10]));
99          assertEquals(new Integer(7),Size.instance().evaluate(new String[7]));
100     }
101 
102     @Test
103     public void testEvaluateString() throws Exception {
104         assertEquals(new Integer("xyzzy".length()),Size.instance().evaluate("xyzzy"));
105     }
106 
107     @Test
108     public void testEquals() throws Exception {
109         UnaryFunction f = new Size();
110         assertEquals(f,f);
111         assertObjectsAreEqual(f,new Size());
112         assertObjectsAreEqual(f,Size.instance());
113         assertSame(Size.instance(),Size.instance());
114         assertObjectsAreNotEqual(f,new Constant(null));
115         assertTrue(! f.equals((Size) null) );
116     }
117 
118 }