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.testFramework;
019    
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.EventObject;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.apache.commons.pipeline.Feeder;
029    import org.apache.commons.pipeline.Stage;
030    import org.apache.commons.pipeline.StageContext;
031    import org.apache.commons.pipeline.StageEventListener;
032    
033    
034    /**
035     * Stage Context for test harness.
036     */
037    public class TestStageContext implements StageContext {
038        public List<StageEventListener> listeners = Collections.synchronizedList( new ArrayList<StageEventListener>() );
039        public List<EventObject> raisedEvents = new ArrayList<EventObject>();
040        public Map<String, TestFeeder> branchFeeders = new HashMap<String,TestFeeder>();
041        public Map<Stage, Feeder> downstreamFeeders = new HashMap<Stage,Feeder>();
042        public Map<String, Object> env = new HashMap<String, Object>();
043        
044        public void registerListener(StageEventListener listener) {
045            this.listeners.add(listener);
046        }
047        
048        public void raise(EventObject ev) {
049            this.raisedEvents.add(ev);
050            notifyListeners( ev );
051        }
052        
053        private void notifyListeners( EventObject event )
054        {
055            for( StageEventListener listener : listeners )
056            {
057                listener.notify( event );
058            }
059        }
060        
061        /**
062         * Dynamically adds branch feeders as needed to provide a feeder for
063         * the requested branch key.
064         */
065        public Feeder getBranchFeeder(String key) {
066            if (branchFeeders.containsKey(key)) {
067                return branchFeeders.get(key);
068            } else {
069                TestFeeder feeder = new TestFeeder();
070                branchFeeders.put(key, feeder);
071                return feeder;
072            }
073        }
074        
075        /**
076         * Dynamically adds downstream feeders as needed to provide a downstream
077         * feeder for the specified stage.
078         */
079        public Feeder getDownstreamFeeder(Stage stage) {
080            if (downstreamFeeders.containsKey(stage)) {
081                return downstreamFeeders.get(stage);
082            } else {
083                TestFeeder feeder = new TestFeeder();
084                downstreamFeeders.put(stage, feeder);
085                return feeder;
086            }
087        }
088        
089        /**
090         * This method is used by the test implementation to set up the feeders
091         * for a stage as though they were provided by drivers in a pipeline.
092         */
093        public void registerDownstreamFeeder(Stage stage, Feeder feeder) {
094            this.downstreamFeeders.put(stage, feeder);
095        }
096        
097        public Collection<StageEventListener> getRegisteredListeners() {
098            return this.listeners;
099        }
100    
101        /**
102         * This method allows objects in the global environment
103         * to be accessed by the stages running in this context.
104         * 
105         * @return the object corresponding to the specified string key, or null
106         * if no such key exists.
107         */
108        public Object getEnv(String key) {
109            return this.env.get(key);
110        }    
111    }