View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pipeline.testFramework;
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.EventObject;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.pipeline.Feeder;
29  import org.apache.commons.pipeline.Stage;
30  import org.apache.commons.pipeline.StageContext;
31  import org.apache.commons.pipeline.StageEventListener;
32  
33  
34  /**
35   * Stage Context for test harness.
36   */
37  public class TestStageContext implements StageContext {
38      public List<StageEventListener> listeners = Collections.synchronizedList( new ArrayList<StageEventListener>() );
39      public List<EventObject> raisedEvents = new ArrayList<EventObject>();
40      public Map<String, TestFeeder> branchFeeders = new HashMap<String,TestFeeder>();
41      public Map<Stage, Feeder> downstreamFeeders = new HashMap<Stage,Feeder>();
42      public Map<String, Object> env = new HashMap<String, Object>();
43      
44      public void registerListener(StageEventListener listener) {
45          this.listeners.add(listener);
46      }
47      
48      public void raise(EventObject ev) {
49          this.raisedEvents.add(ev);
50          notifyListeners( ev );
51      }
52      
53      private void notifyListeners( EventObject event )
54      {
55          for( StageEventListener listener : listeners )
56          {
57              listener.notify( event );
58          }
59      }
60      
61      /**
62       * Dynamically adds branch feeders as needed to provide a feeder for
63       * the requested branch key.
64       */
65      public Feeder getBranchFeeder(String key) {
66          if (branchFeeders.containsKey(key)) {
67              return branchFeeders.get(key);
68          } else {
69              TestFeeder feeder = new TestFeeder();
70              branchFeeders.put(key, feeder);
71              return feeder;
72          }
73      }
74      
75      /**
76       * Dynamically adds downstream feeders as needed to provide a downstream
77       * feeder for the specified stage.
78       */
79      public Feeder getDownstreamFeeder(Stage stage) {
80          if (downstreamFeeders.containsKey(stage)) {
81              return downstreamFeeders.get(stage);
82          } else {
83              TestFeeder feeder = new TestFeeder();
84              downstreamFeeders.put(stage, feeder);
85              return feeder;
86          }
87      }
88      
89      /**
90       * This method is used by the test implementation to set up the feeders
91       * for a stage as though they were provided by drivers in a pipeline.
92       */
93      public void registerDownstreamFeeder(Stage stage, Feeder feeder) {
94          this.downstreamFeeders.put(stage, feeder);
95      }
96      
97      public Collection<StageEventListener> getRegisteredListeners() {
98          return this.listeners;
99      }
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 }