001    /*
002     * Copyright 2004-2005 the original author or authors.
003     */
004    package org.springframework.core.closure;
005    
006    import java.util.ArrayList;
007    import java.util.List;
008    
009    import junit.framework.TestCase;
010    
011    import org.springframework.rules.closure.support.Block;
012    import org.springframework.rules.closure.support.IteratorTemplate;
013    import org.springframework.rules.constraint.Constraint;
014    import org.springframework.rules.closure.ElementGenerator;
015    
016    /**
017     * @author Keith Donald
018     */
019    public class ClosureTests extends TestCase {
020            public void testIteratorProcessTemplateRunOnce() {
021                    List collection = new ArrayList();
022                    collection.add("Item 1");
023                    collection.add("Item 2");
024                    collection.add("Item 3");
025                    collection.add("Item 4");
026                    collection.add("Item 5");
027                    IteratorTemplate template = new IteratorTemplate(collection.iterator());
028                    assertTrue(template.allTrue(new Constraint() {
029                            public boolean test(Object o) {
030                                    return ((String)o).startsWith("Item");
031                            }
032                    }));
033                    try {
034                            assertEquals(false, template.allTrue(new Constraint() {
035                                    public boolean test(Object o) {
036                                            return ((String)o).startsWith("Element");
037                                    }
038                            }));
039                            fail("Should have failed");
040                    }
041                    catch (UnsupportedOperationException e) {
042    
043                    }
044            }
045    
046            public void testIteratorProcessTemplateRunMultiple() {
047                    List collection = new ArrayList();
048                    collection.add("Item 1");
049                    collection.add("Item 2");
050                    collection.add("Item 3");
051                    collection.add("Item 4");
052                    collection.add("Item 5");
053                    IteratorTemplate template = new IteratorTemplate(collection);
054                    assertTrue(template.allTrue(new Constraint() {
055                            public boolean test(Object o) {
056                                    return ((String)o).startsWith("Item");
057                            }
058                    }));
059                    assertEquals(false, template.allTrue(new Constraint() {
060                            public boolean test(Object o) {
061                                    return ((String)o).startsWith("Element");
062                            }
063                    }));
064            }
065    
066            public void testAnyTrue() {
067                    List collection = new ArrayList();
068                    collection.add("Item 1");
069                    collection.add("Item 2");
070                    collection.add("Item 3");
071                    collection.add("Item 4");
072                    collection.add("Item 5");
073                    IteratorTemplate template = new IteratorTemplate(collection);
074                    assertTrue(template.anyTrue(new Constraint() {
075                            public boolean test(Object o) {
076                                    return ((String)o).startsWith("Item 5");
077                            }
078                    }));
079                    assertEquals(false, template.anyTrue(new Constraint() {
080                            public boolean test(Object o) {
081                                    return ((String)o).startsWith("Element");
082                            }
083                    }));
084            }
085    
086            public void testFindFirst() {
087                    List collection = new ArrayList();
088                    collection.add("Item 1");
089                    collection.add("Item 2");
090                    collection.add("Item 3");
091                    collection.add("Item 4");
092                    collection.add("Item 5");
093                    IteratorTemplate template = new IteratorTemplate(collection);
094                    assertEquals("Item 4", template.findFirst(new Constraint() {
095                            public boolean test(Object o) {
096                                    return ((String)o).startsWith("Item 4");
097                            }
098                    }));
099                    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        // using a simple int instead of a ValueHolder to have no extra dependencies.
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    }