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.assertNull;
21  import static org.junit.Assert.assertSame;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.NoSuchElementException;
30  
31  import org.apache.commons.functor.BaseFunctorTest;
32  import org.apache.commons.functor.UnaryFunction;
33  import org.apache.commons.functor.core.Identity;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  /**
39   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
40   * @author Rodney Waldhoff
41   */
42  @SuppressWarnings("unchecked")
43  public class TestTransformedIterator extends BaseFunctorTest {
44  
45      public Object makeFunctor() {
46          List list = new ArrayList();
47          list.add("xyzzy");
48          return TransformedIterator.transform(list.iterator(),Identity.instance());
49      }
50  
51      // Lifecycle
52      // ------------------------------------------------------------------------
53  
54      @Before
55      public void setUp() throws Exception {
56          list = new ArrayList();
57          negatives = new ArrayList();
58          for (int i=0;i<10;i++) {
59              list.add(new Integer(i));
60              negatives.add(new Integer(i*-1));
61          }
62      }
63  
64      @After
65      public void tearDown() throws Exception {
66          list = null;
67          negatives = null;
68      }
69  
70      // Tests
71      // ------------------------------------------------------------------------
72  
73      @Test
74      public void testBasicTransform() {
75          Iterator expected = negatives.iterator();
76          Iterator testing = new TransformedIterator(list.iterator(),negate);
77          while(expected.hasNext()) {
78              assertTrue(testing.hasNext());
79              assertEquals(expected.next(),testing.next());
80          }
81          assertTrue(!testing.hasNext());
82      }
83  
84      @Test
85      public void testEmptyList() {
86          Iterator testing = new TransformedIterator(Collections.EMPTY_LIST.iterator(),negate);
87          assertTrue(!testing.hasNext());
88      }
89  
90      @Test
91      public void testNextWithoutHasNext() {
92          Iterator testing = new TransformedIterator(list.iterator(),negate);
93          Iterator expected = negatives.iterator();
94          while(expected.hasNext()) {
95              assertEquals(expected.next(),testing.next());
96          }
97          assertTrue(!(testing.hasNext()));
98      }
99  
100     @Test
101     public void testNextAfterEndOfList() {
102         Iterator testing = new TransformedIterator(list.iterator(),negate);
103         Iterator expected = negatives.iterator();
104         while(expected.hasNext()) {
105             assertEquals(expected.next(),testing.next());
106         }
107         try {
108             testing.next();
109             fail("ExpectedNoSuchElementException");
110         } catch(NoSuchElementException e) {
111             // expected
112         }
113     }
114 
115     @Test
116     public void testNextOnEmptyList() {
117         Iterator testing = new TransformedIterator(Collections.EMPTY_LIST.iterator(),negate);
118         try {
119             testing.next();
120             fail("ExpectedNoSuchElementException");
121         } catch(NoSuchElementException e) {
122             // expected
123         }
124     }
125 
126     @Test
127     public void testRemoveBeforeNext() {
128         Iterator testing = new TransformedIterator(list.iterator(),negate);
129         try {
130             testing.remove();
131             fail("IllegalStateException");
132         } catch(IllegalStateException e) {
133             // expected
134         }
135     }
136 
137     @Test
138     public void testRemoveAfterNext() {
139         Iterator testing = new TransformedIterator(list.iterator(),negate);
140         testing.next();
141         testing.remove();
142         try {
143             testing.remove();
144             fail("IllegalStateException");
145         } catch(IllegalStateException e) {
146             // expected
147         }
148     }
149 
150     @Test
151     public void testRemoveAll() {
152         Iterator testing = new TransformedIterator(list.iterator(),negate);
153         while(testing.hasNext()) {
154             testing.next();
155             testing.remove();
156         }
157         assertTrue(list.isEmpty());
158     }
159 
160     @Test
161     public void testRemoveWithoutHasNext() {
162         Iterator testing = new TransformedIterator(list.iterator(),negate);
163         for (int i=0,m = list.size();i<m;i++) {
164             testing.next();
165             testing.remove();
166         }
167         assertTrue(list.isEmpty());
168     }
169 
170     @Test
171     public void testTransformWithNullIteratorReturnsNull() {
172         assertNull(TransformedIterator.transform(null,negate));
173     }
174 
175     @Test
176     public void testTransformWithNullPredicateReturnsIdentity() {
177         Iterator iter = list.iterator();
178         assertSame(iter,TransformedIterator.maybeTransform(iter,null));
179     }
180 
181     @Test
182     public void testConstructorProhibitsNull() {
183         try {
184             new TransformedIterator(null,null);
185             fail("ExpectedNullPointerException");
186         } catch(IllegalArgumentException e) {
187             // expected
188         }
189         try {
190             new TransformedIterator(null,negate);
191             fail("ExpectedNullPointerException");
192         } catch(IllegalArgumentException e) {
193             // expected
194         }
195         try {
196             new TransformedIterator(list.iterator(),null);
197             fail("ExpectedNullPointerException");
198         } catch(IllegalArgumentException e) {
199             // expected
200         }
201     }
202 
203 
204     // Attributes
205     // ------------------------------------------------------------------------
206     private List list = null;
207     private List negatives = null;
208     private UnaryFunction negate = new UnaryFunction() {
209         public Object evaluate(Object obj) {
210             return new Integer(((Number) obj).intValue() * -1);
211         }
212     };
213 
214 }