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;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNotNull;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.fail;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.apache.commons.functor.BaseFunctorTest;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   * @version $Revision: 1171255 $ $Date: 2011-09-15 22:27:39 +0200 (Thu, 15 Sep 2011) $
35   * @author Rodney Waldhoff
36   */
37  @SuppressWarnings("unchecked")
38  public class TestIteratorToGeneratorAdapter extends BaseFunctorTest {
39  
40      public Object makeFunctor() {
41          List list = new ArrayList();
42          list.add("1");
43          return new IteratorToGeneratorAdapter(list.iterator());
44      }
45  
46      // Lifecycle
47      // ------------------------------------------------------------------------
48  
49      private List list = null;
50  
51      @Before
52      public void setUp() throws Exception {
53          list = new ArrayList();
54          list.add("1");
55          list.add("two");
56          list.add("c");
57      }
58  
59      @After
60      public void tearDown() throws Exception {
61          list = null;
62      }
63  
64      // Tests
65      // ------------------------------------------------------------------------
66  
67      @Test
68      public void testAdaptNull() {
69          assertNull(IteratorToGeneratorAdapter.adapt(null));
70      }
71  
72      @Test
73      public void testAdaptNonNull() {
74          assertNotNull(IteratorToGeneratorAdapter.adapt(list.iterator()));
75      }
76  
77      @Test
78      public void testGenerate() {
79          Iterator iter = list.iterator();
80          Generator gen = new IteratorToGeneratorAdapter(iter);
81          List list2 = new ArrayList();
82          list2.addAll(gen.toCollection());
83          assertEquals(list,list2);
84      }
85  
86      @Test
87      public void testConstructNull() {
88          try {
89              new IteratorToGeneratorAdapter(null);
90              fail("Expected NullPointerException");
91          } catch(IllegalArgumentException e) {
92              // expected
93          }
94      }
95  
96      @Test
97      public void testEquals() {
98          Iterator iter = list.iterator();
99          Generator gen = new IteratorToGeneratorAdapter(iter);
100         assertObjectsAreEqual(gen,gen);
101         assertObjectsAreEqual(gen,new IteratorToGeneratorAdapter(iter));
102     }
103 }