1
2
3
4 package org.springframework.core.closure;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import junit.framework.TestCase;
10
11 import org.springframework.rules.closure.support.Block;
12 import org.springframework.rules.closure.support.IteratorTemplate;
13 import org.springframework.rules.constraint.Constraint;
14 import org.springframework.rules.closure.ElementGenerator;
15
16
17
18
19 public class ClosureTests extends TestCase {
20 public void testIteratorProcessTemplateRunOnce() {
21 List collection = new ArrayList();
22 collection.add("Item 1");
23 collection.add("Item 2");
24 collection.add("Item 3");
25 collection.add("Item 4");
26 collection.add("Item 5");
27 IteratorTemplate template = new IteratorTemplate(collection.iterator());
28 assertTrue(template.allTrue(new Constraint() {
29 public boolean test(Object o) {
30 return ((String)o).startsWith("Item");
31 }
32 }));
33 try {
34 assertEquals(false, template.allTrue(new Constraint() {
35 public boolean test(Object o) {
36 return ((String)o).startsWith("Element");
37 }
38 }));
39 fail("Should have failed");
40 }
41 catch (UnsupportedOperationException e) {
42
43 }
44 }
45
46 public void testIteratorProcessTemplateRunMultiple() {
47 List collection = new ArrayList();
48 collection.add("Item 1");
49 collection.add("Item 2");
50 collection.add("Item 3");
51 collection.add("Item 4");
52 collection.add("Item 5");
53 IteratorTemplate template = new IteratorTemplate(collection);
54 assertTrue(template.allTrue(new Constraint() {
55 public boolean test(Object o) {
56 return ((String)o).startsWith("Item");
57 }
58 }));
59 assertEquals(false, template.allTrue(new Constraint() {
60 public boolean test(Object o) {
61 return ((String)o).startsWith("Element");
62 }
63 }));
64 }
65
66 public void testAnyTrue() {
67 List collection = new ArrayList();
68 collection.add("Item 1");
69 collection.add("Item 2");
70 collection.add("Item 3");
71 collection.add("Item 4");
72 collection.add("Item 5");
73 IteratorTemplate template = new IteratorTemplate(collection);
74 assertTrue(template.anyTrue(new Constraint() {
75 public boolean test(Object o) {
76 return ((String)o).startsWith("Item 5");
77 }
78 }));
79 assertEquals(false, template.anyTrue(new Constraint() {
80 public boolean test(Object o) {
81 return ((String)o).startsWith("Element");
82 }
83 }));
84 }
85
86 public void testFindFirst() {
87 List collection = new ArrayList();
88 collection.add("Item 1");
89 collection.add("Item 2");
90 collection.add("Item 3");
91 collection.add("Item 4");
92 collection.add("Item 5");
93 IteratorTemplate template = new IteratorTemplate(collection);
94 assertEquals("Item 4", template.findFirst(new Constraint() {
95 public boolean test(Object o) {
96 return ((String)o).startsWith("Item 4");
97 }
98 }));
99 assertEquals(null, template.findFirst(new Constraint() {
100 public boolean test(Object o) {
101 return ((String)o).startsWith("Element");
102 }
103 }));
104 }
105
106 public void testFindAll() {
107 List collection = new ArrayList();
108 collection.add("Item 1");
109 collection.add("Item 2");
110 collection.add("Item 3");
111 collection.add("Item 4");
112 collection.add("Item 5");
113 IteratorTemplate template = new IteratorTemplate(collection);
114 ElementGenerator finder = template.findAll(new Constraint() {
115 public boolean test(Object o) {
116 return ((String)o).startsWith("Item 4");
117 }
118 });
119 finder.run(new Block() {
120 protected void handle(Object o) {
121 assertEquals("Item 4", o);
122 }
123 });
124 finder = template.findAll(new Constraint() {
125 public boolean test(Object o) {
126 return ((String)o).startsWith("Element");
127 }
128 });
129 finder.run(new Block() {
130 protected void handle(Object o) {
131 fail("Should not be called");
132 }
133 });
134 }
135
136
137 int runUntilCounter;
138
139 public void testRunUntil() {
140 List collection = new ArrayList();
141 collection.add("Item 1");
142 collection.add("Item 2");
143 collection.add("Item 3");
144 collection.add("Item 4");
145 collection.add("Item 5");
146 IteratorTemplate template = new IteratorTemplate(collection);
147 runUntilCounter = 0;
148 template.runUntil(new Block() {
149 protected void handle(Object o) {
150 runUntilCounter++;
151 }
152 }, new Constraint() {
153 public boolean test(Object o) {
154 return o.equals("Item 4");
155 }
156 });
157 assertEquals(3, runUntilCounter);
158 }
159 }