1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36
37 @SuppressWarnings("unchecked")
38 public class TestBaseGenerator {
39
40 private Generator simpleGenerator = null;
41
42
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
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
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
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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
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 }