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.assertSame;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import org.apache.commons.functor.UnaryProcedure;
28  import org.apache.commons.functor.generator.util.CollectionTransformer;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  /**
34   * Tests the Base Generator class.
35   * @author Jason Horman (jason@jhorman.org)
36   */
37  @SuppressWarnings("unchecked")
38  public class TestBaseGenerator {
39  
40      private Generator simpleGenerator = null;
41  
42      // Lifecycle
43      // ------------------------------------------------------------------------
44  
45      @Before
46      public void setUp() throws Exception {
47          simpleGenerator = new BaseGenerator() {
48              public void run(UnaryProcedure proc) {
49                  for (int i=0;i<5;i++) {
50                      proc.run(new Integer(i));
51                      if (isStopped()) {
52                          break;
53                      }
54                  }
55              }
56          };
57  
58          list = new ArrayList();
59          evens = new ArrayList();
60          doubled = new ArrayList();
61          listWithDuplicates = new ArrayList();
62          sum = 0;
63          for (int i=0;i<10;i++) {
64              list.add(new Integer(i));
65              doubled.add(new Integer(i*2));
66              listWithDuplicates.add(new Integer(i));
67              listWithDuplicates.add(new Integer(i));
68              sum += i;
69              if (i%2 == 0) {
70                  evens.add(new Integer(i));
71              }
72          }
73      }
74  
75      @After
76      public void tearDown() throws Exception {
77          simpleGenerator = null;
78          list = null;
79          evens = null;
80          listWithDuplicates = null;
81          sum = 0;
82      }
83  
84      // Tests
85      // ------------------------------------------------------------------------
86  
87      @Test
88      public void testSimpleGenerator() {
89          final StringBuffer result = new StringBuffer();
90          simpleGenerator.run(new UnaryProcedure() {
91              public void run(Object obj) {
92                  result.append(obj);
93              }
94          });
95  
96          assertEquals("01234", result.toString());
97      }
98  
99      @Test
100     public void testStop() {
101         final StringBuffer result = new StringBuffer();
102         simpleGenerator.run(new UnaryProcedure() {
103             int i=0;
104             public void run(Object obj) {
105                 result.append(obj);
106                 if (i++ > 1) {
107                     simpleGenerator.stop();
108                 }
109             }
110         });
111 
112         assertEquals("012", result.toString());
113     }
114 
115     @Test
116     public void testWrappingGenerator() {
117         final StringBuffer result = new StringBuffer();
118         final Generator gen = new BaseGenerator(simpleGenerator) {
119             public void run(final UnaryProcedure proc) {
120                 Generator wrapped = getWrappedGenerator();
121                 assertSame(simpleGenerator, wrapped);
122                 wrapped.run(new UnaryProcedure() {
123                     public void run(Object obj) {
124                         proc.run(new Integer(((Integer) obj).intValue() + 1));
125                     }
126                 });
127             }
128         };
129 
130         gen.run(new UnaryProcedure() {
131             public void run(Object obj) {
132                 result.append(obj);
133             }
134         });
135 
136         assertEquals("12345", result.toString());
137 
138         // try to stop the wrapped generator
139         final StringBuffer result2 = new StringBuffer();
140         gen.run(new UnaryProcedure() {
141             int i=0;
142             public void run(Object obj) {
143                 result2.append(obj);
144                 if (i++ > 1) {
145                     gen.stop();
146                 }
147             }
148         });
149 
150         assertEquals("123", result2.toString());
151     }
152 
153     // Tests
154     // ------------------------------------------------------------------------
155 
156     @Test
157     public void testTo() {
158         Collection col = (Collection) simpleGenerator.to(new CollectionTransformer());
159         assertEquals("[0, 1, 2, 3, 4]", col.toString());
160 
161         Collection fillThis = new LinkedList();
162         col = (Collection) simpleGenerator.to(new CollectionTransformer(fillThis));
163         assertSame(fillThis, col);
164         assertEquals("[0, 1, 2, 3, 4]", col.toString());
165 
166         col = simpleGenerator.toCollection();
167         assertEquals("[0, 1, 2, 3, 4]", col.toString());
168         assertEquals("[0, 1, 2, 3, 4]", col.toString());
169 
170         fillThis = new LinkedList();
171         col = simpleGenerator.to(fillThis);
172         assertSame(fillThis, col);
173         assertEquals("[0, 1, 2, 3, 4]", col.toString());
174     }
175 
176     // Attributes
177     // ------------------------------------------------------------------------
178     private List list = null;
179     private List doubled = null;
180     private List evens = null;
181     private List listWithDuplicates = null;
182     private int sum = 0;
183 //    private UnaryPredicate equalsThree = LeftBoundPredicate.bind(IsEqual.instance(),new Integer(3));
184 //    private UnaryPredicate equalsTwentyThree = LeftBoundPredicate.bind(IsEqual.instance(),new Integer(23));
185 //    private UnaryPredicate isEven = new UnaryPredicate() {
186 //        public boolean test(Object obj) {
187 //            return ((Number) obj).intValue() % 2 == 0;
188 //        }
189 //    };
190 //    private UnaryPredicate isOdd = new UnaryPredicate() {
191 //        public boolean test(Object obj) {
192 //            return ((Number) obj).intValue() % 2 != 0;
193 //        }
194 //    };
195 
196     // Classes
197     // ------------------------------------------------------------------------
198 
199     static class Summer implements UnaryProcedure {
200         public void run(Object that) {
201             sum += ((Number) that).intValue();
202         }
203         public int sum = 0;
204     }
205 }