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.generator.util;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.functor.BaseFunctorTest;
30  import org.apache.commons.functor.core.Limit;
31  import org.apache.commons.functor.core.Offset;
32  import org.apache.commons.functor.generator.GenerateUntil;
33  import org.apache.commons.functor.generator.GenerateWhile;
34  import org.apache.commons.functor.generator.UntilGenerate;
35  import org.apache.commons.functor.generator.WhileGenerate;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  /**
40   * @author Jason Horman (jason@jhorman.org)
41   */
42  @SuppressWarnings("unchecked")
43  public class TestEachElement extends BaseFunctorTest {
44  
45      private List list = null;
46      private Map map = null;
47      private Object[] array = null;
48  
49      protected Object makeFunctor() throws Exception {
50          return EachElement.from(new ArrayList());
51      }
52  
53      // Lifecycle
54      // ------------------------------------------------------------------------
55  
56      @Before
57      public void setUp() throws Exception {
58          list = new ArrayList();
59          list.add(new Integer(0));
60          list.add(new Integer(1));
61          list.add(new Integer(2));
62          list.add(new Integer(3));
63          list.add(new Integer(4));
64  
65          map = new HashMap();
66          map.put("1", "1-1");
67          map.put("2", "2-1");
68          map.put("3", "3-1");
69          map.put("4", "4-1");
70          map.put("5", "5-1");
71  
72          array = new String[5];
73          array[0] = "1";
74          array[1] = "2";
75          array[2] = "3";
76          array[3] = "4";
77          array[4] = "5";
78      }
79  
80      // Tests
81      // ------------------------------------------------------------------------
82  
83      @Test
84      public void testFromNull() {
85          assertNull(EachElement.from((Collection) null));
86          assertNull(EachElement.from((Map) null));
87          assertNull(EachElement.from((Iterator) null));
88          assertNull(EachElement.from((Object[]) null));
89      }
90  
91  
92      @Test
93      public void testWithList() {
94          Collection col = EachElement.from(list).toCollection();
95          assertEquals("[0, 1, 2, 3, 4]", col.toString());
96      }
97  
98      @Test
99      public void testWithMap() {
100         List col = (List) EachElement.from(map).toCollection();
101         int i = 0;
102         for (;i<col.size();i++) {
103             Map.Entry entry = (Map.Entry) col.get(i);
104             if (entry.getKey().equals("1")) {
105                 assertEquals("1-1", entry.getValue());
106             } else if (entry.getKey().equals("2")) {
107                 assertEquals("2-1", entry.getValue());
108             } else if (entry.getKey().equals("3")) {
109                 assertEquals("3-1", entry.getValue());
110             } else if (entry.getKey().equals("4")) {
111                 assertEquals("4-1", entry.getValue());
112             } else if (entry.getKey().equals("5")) {
113                 assertEquals("5-1", entry.getValue());
114             }
115         }
116 
117         assertEquals(5, i);
118     }
119 
120     @Test
121     public void testWithArray() {
122         Collection col = EachElement.from(array).toCollection();
123         assertEquals("[1, 2, 3, 4, 5]", col.toString());
124     }
125 
126     @Test
127     public void testWithStop() {
128         assertEquals("[0, 1, 2]", new UntilGenerate(new Offset(3), EachElement.from(list)).toCollection().toString());
129         assertEquals("[0, 1, 2, 3]", new GenerateUntil(EachElement.from(list), new Offset(3)).toCollection().toString());
130         assertEquals("[0, 1, 2]", new WhileGenerate(new Limit(3), EachElement.from(list)).toCollection().toString());
131         assertEquals("[0, 1, 2, 3]", new GenerateWhile(EachElement.from(list), new Limit(3)).toCollection().toString());
132     }
133 
134     @Test
135     public void testWithIterator() {
136         Collection col = EachElement.from(list.iterator()).toCollection();
137         assertEquals("[0, 1, 2, 3, 4]", col.toString());
138     }
139 
140 }