001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pipeline;
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.EventObject;
023    
024    import junit.framework.Test;
025    import junit.framework.TestCase;
026    import junit.framework.TestSuite;
027    
028    import org.apache.commons.pipeline.driver.SynchronousStageDriverFactory;
029    import org.apache.commons.pipeline.event.ObjectProcessedEvent;
030    import org.apache.commons.pipeline.listener.ObjectProcessedEventCounter;
031    import org.apache.commons.pipeline.testFramework.TestStage;
032    
033    /**
034     * Test cases
035     */
036    public class PipelineTest extends TestCase {
037        
038        public PipelineTest(String testName) {
039            super(testName);
040        }
041        
042        public static Test suite() {
043            TestSuite suite = new TestSuite(PipelineTest.class);
044            
045            return suite;
046        }
047        
048        /**
049         * Test of registerListener method, of class org.apache.commons.pipeline.Pipeline.
050         */
051        public void testRegisterListener() {
052            StageEventListener listener = new ObjectProcessedEventCounter();
053            Pipeline instance = new Pipeline();
054            
055            instance.registerListener(listener);
056            
057            assertEquals(1, instance.getRegisteredListeners().size());
058        }
059        
060        /**
061         * Test of getRegisteredListeners method, of class org.apache.commons.pipeline.Pipeline.
062         */
063        public void testGetRegisteredListeners() {
064            Pipeline instance = new Pipeline();
065            
066            Collection<StageEventListener> expResult = Collections.EMPTY_LIST;
067            Collection<StageEventListener> result = instance.getRegisteredListeners();
068            assertEquals(expResult, result);
069        }
070        
071        /**
072         * Test of raise method, of class org.apache.commons.pipeline.Pipeline.
073         */
074        public void testRaise() throws Exception {
075            Stage testStage = new TestStage(0);
076            EventObject ev = new ObjectProcessedEvent(testStage, "Hello, World!");
077            Pipeline instance = new Pipeline();
078            ObjectProcessedEventCounter counter = new ObjectProcessedEventCounter();
079            instance.registerListener(counter);
080            
081            instance.raise(ev);
082            
083            synchronized(counter) {
084                while (counter.getCounts().get(testStage) == null) counter.wait(100);
085            }
086            
087            assertNotNull("No events were received.", counter.getCounts().get(testStage));
088            assertEquals("Only one event should have been received.", 1, counter.getCounts().get(testStage).intValue());
089        }
090        
091        public void testRaiseOnBranch() throws Exception {
092            Pipeline root = new Pipeline();
093            
094            Pipeline branch1 = new Pipeline();
095            root.addBranch("b1", branch1);
096            
097            Pipeline branch2 = new Pipeline();
098            root.addBranch("b2", branch2);
099            
100            ObjectProcessedEventCounter counter = new ObjectProcessedEventCounter();
101            branch2.registerListener(counter);
102            
103            Stage testStage = new TestStage(0);
104            EventObject ev = new ObjectProcessedEvent(testStage, "Hello, World!");
105            branch1.raise(ev);
106            
107            synchronized(counter) {
108                while (counter.getCounts().get(testStage) == null) counter.wait(100);
109            }
110            
111            assertNotNull(counter.getCounts().get(testStage));
112            assertEquals(1, counter.getCounts().get(testStage).intValue());
113        }
114        
115        /**
116         * Test of getDownstreamFeeder method, of class org.apache.commons.pipeline.Pipeline.
117         */
118        public void testGetDownstreamFeeder() throws Exception {
119            Stage stage1 = new TestStage(0);
120            Stage stage2 = new TestStage(1);
121            StageDriverFactory sdf = new SynchronousStageDriverFactory();
122    
123            Pipeline instance = new Pipeline();
124            instance.addStage(stage1, sdf);
125            instance.addStage(stage2, sdf);
126    
127            Feeder expResult = instance.getStageDriver(stage2).getFeeder();
128            Feeder result = instance.getDownstreamFeeder(stage1);
129            assertSame(expResult, result);
130        }
131        
132        /**
133         * Test of getBranchFeeder method, of class org.apache.commons.pipeline.Pipeline.
134         */
135        public void testGetBranchFeeder() throws Exception {
136            String branchKey = "b1";
137            Pipeline root = new Pipeline();
138            Pipeline branch = new Pipeline();
139            root.addBranch(branchKey, branch);
140            
141            Feeder expResult = branch.getTerminalFeeder(); //no feeders registered
142            Feeder result = root.getBranchFeeder(branchKey);
143            assertSame(expResult, result);
144            
145            StageDriverFactory sdf = new SynchronousStageDriverFactory();
146            Stage testStage = new TestStage(0);
147            branch.addStage(testStage, sdf);
148            
149            expResult = branch.getStageDriver(testStage).getFeeder();
150            result = root.getBranchFeeder(branchKey);
151            assertSame(expResult, result);        
152        }
153        
154    //    /**
155    //     * Test of addStage method, of class org.apache.commons.pipeline.Pipeline.
156    //     */
157    //    public void testAddStage() throws Exception {
158    //        System.out.println("addStage");
159    //
160    //        Stage stage = null;
161    //        StageDriverFactory driverFactory = null;
162    //        Pipeline instance = new Pipeline();
163    //
164    //        instance.addStage(stage, driverFactory);
165    //
166    //        fail("The test case is a prototype.");
167    //    }
168    //
169    //    /**
170    //     * Test of getStages method, of class org.apache.commons.pipeline.Pipeline.
171    //     */
172    //    public void testGetStages() {
173    //        System.out.println("getStages");
174    //
175    //        Pipeline instance = new Pipeline();
176    //
177    //        List<Stage> expResult = null;
178    //        List<Stage> result = instance.getStages();
179    //        assertEquals(expResult, result);
180    //
181    //        fail("The test case is a prototype.");
182    //    }
183    //
184    //    /**
185    //     * Test of getStageDriver method, of class org.apache.commons.pipeline.Pipeline.
186    //     */
187    //    public void testGetStageDriver() {
188    //        System.out.println("getStageDriver");
189    //
190    //        Stage stage = null;
191    //        Pipeline instance = new Pipeline();
192    //
193    //        StageDriver expResult = null;
194    //        StageDriver result = instance.getStageDriver(stage);
195    //        assertEquals(expResult, result);
196    //
197    //        fail("The test case is a prototype.");
198    //    }
199    //
200    //    /**
201    //     * Test of getStageDrivers method, of class org.apache.commons.pipeline.Pipeline.
202    //     */
203    //    public void testGetStageDrivers() {
204    //        System.out.println("getStageDrivers");
205    //
206    //        Pipeline instance = new Pipeline();
207    //
208    //        List<StageDriver> expResult = null;
209    //        List<StageDriver> result = instance.getStageDrivers();
210    //        assertEquals(expResult, result);
211    //
212    //        fail("The test case is a prototype.");
213    //    }
214    //
215    //    /**
216    //     * Test of addBranch method, of class org.apache.commons.pipeline.Pipeline.
217    //     */
218    //    public void testAddBranch() throws Exception {
219    //        System.out.println("addBranch");
220    //
221    //        String key = "";
222    //        Pipeline branch = null;
223    //        Pipeline instance = new Pipeline();
224    //
225    //        instance.addBranch(key, branch);
226    //
227    //        fail("The test case is a prototype.");
228    //    }
229    //
230    //    /**
231    //     * Test of getBranches method, of class org.apache.commons.pipeline.Pipeline.
232    //     */
233    //    public void testGetBranches() {
234    //        System.out.println("getBranches");
235    //
236    //        Pipeline instance = new Pipeline();
237    //
238    //        Map<String, Pipeline> expResult = null;
239    //        Map<String, Pipeline> result = instance.getBranches();
240    //        assertEquals(expResult, result);
241    //
242    //        fail("The test case is a prototype.");
243    //    }
244    //
245    //    /**
246    //     * Test of getSourceFeeder method, of class org.apache.commons.pipeline.Pipeline.
247    //     */
248    //    public void testGetSourceFeeder() {
249    //        System.out.println("getSourceFeeder");
250    //
251    //        Pipeline instance = new Pipeline();
252    //
253    //        Feeder expResult = null;
254    //        Feeder result = instance.getSourceFeeder();
255    //        assertEquals(expResult, result);
256    //
257    //        fail("The test case is a prototype.");
258    //    }
259    //
260    //    /**
261    //     * Test of getTerminalFeeder method, of class org.apache.commons.pipeline.Pipeline.
262    //     */
263    //    public void testGetTerminalFeeder() {
264    //        System.out.println("getTerminalFeeder");
265    //
266    //        Pipeline instance = new Pipeline();
267    //
268    //        Feeder expResult = null;
269    //        Feeder result = instance.getTerminalFeeder();
270    //        assertEquals(expResult, result);
271    //
272    //        fail("The test case is a prototype.");
273    //    }
274    //
275    //    /**
276    //     * Test of setTerminalFeeder method, of class org.apache.commons.pipeline.Pipeline.
277    //     */
278    //    public void testSetTerminalFeeder() {
279    //        System.out.println("setTerminalFeeder");
280    //
281    //        Feeder terminalFeeder = null;
282    //        Pipeline instance = new Pipeline();
283    //
284    //        instance.setTerminalFeeder(terminalFeeder);
285    //
286    //        fail("The test case is a prototype.");
287    //    }
288    //
289    //    /**
290    //     * Test of start method, of class org.apache.commons.pipeline.Pipeline.
291    //     */
292    //    public void testStart() throws Exception {
293    //        System.out.println("start");
294    //
295    //        Pipeline instance = new Pipeline();
296    //
297    //        instance.start();
298    //
299    //        fail("The test case is a prototype.");
300    //    }
301    //
302    //    /**
303    //     * Test of finish method, of class org.apache.commons.pipeline.Pipeline.
304    //     */
305    //    public void testFinish() throws Exception {
306    //        System.out.println("finish");
307    //
308    //        Pipeline instance = new Pipeline();
309    //
310    //        instance.finish();
311    //
312    //        fail("The test case is a prototype.");
313    //    }
314    //
315    //    /**
316    //     * Test of run method, of class org.apache.commons.pipeline.Pipeline.
317    //     */
318    //    public void testRun() {
319    //        System.out.println("run");
320    //
321    //        Pipeline instance = new Pipeline();
322    //
323    //        instance.run();
324    //
325    //        fail("The test case is a prototype.");
326    //    }
327    //
328    //    /**
329    //     * Test of getValidator method, of class org.apache.commons.pipeline.Pipeline.
330    //     */
331    //    public void testGetValidator() {
332    //        System.out.println("getValidator");
333    //
334    //        Pipeline instance = new Pipeline();
335    //
336    //        PipelineValidator expResult = null;
337    //        PipelineValidator result = instance.getValidator();
338    //        assertEquals(expResult, result);
339    //
340    //        fail("The test case is a prototype.");
341    //    }
342    //
343    //    /**
344    //     * Test of setValidator method, of class org.apache.commons.pipeline.Pipeline.
345    //     */
346    //    public void testSetValidator() {
347    //        System.out.println("setValidator");
348    //
349    //        PipelineValidator validator = null;
350    //        Pipeline instance = new Pipeline();
351    //
352    //        instance.setValidator(validator);
353    //
354    //        fail("The test case is a prototype.");
355    //    }
356        
357    }