1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  package com.reuters.msgtest.fixture;
33  
34  import com.reuters.msgtest.RvTestException;
35  import java.util.Iterator;
36  import java.util.LinkedList;
37  import java.util.List;
38  
39  
40  /***
41   * This class represents a collection of <code>Stimulus</code>
42   * messages that will be created as part of the fixture for the test.
43   * It allows type safe access to a list of
44   * <code>Stimulus</code> messages.  There is also a time delay that
45   * instructs the testing classes to wait for that amount of
46   * time after sending all of the stimulus messages.  The default value
47   * for the delay is 1 second.
48   *
49   * @author <a href="mailto:mark.pollack@reuters.com">Mark Pollack</a>
50   * @author Michael Ward
51   * @see RvFixture
52   * @see Stimulus
53   * @version @VERSION@
54   */
55  public class Stimuli {
56      protected List stimulusList;
57  
58      public Stimuli() {
59          this.stimulusList = new LinkedList();
60      }
61  
62      /***
63      * Add a <code>Stimulus</code> to the collection.  It is possible to
64      * add two stimulus message with the same name, but this should be
65      * avoided if you want retrieve later by name.
66      *
67      * @param stimulus a <code>Stimulus</code> value
68      */
69      public void addStimulus(Stimulus stimulus) {
70          stimulusList.add(stimulus);
71      }
72  
73      /***
74       * This method is called from the digester
75       */
76      public void handleStimulus(Stimulus stimulus) {
77          addAll(stimulus.getStimuli());
78      }
79  
80      /***
81      * Return a <code>Stimulus</code> from the collection.
82      * @param i an <code>int</code> value
83      * @return a <code>Stimulus</code> value
84      */
85      public Stimulus getStimulus(int i) {
86          return (Stimulus) stimulusList.get(i);
87      }
88  
89      /***
90      * Return a <code>Stimulus</code> from the collection with a given name.
91      * It returns the first match if successful and null if no match was found.
92      *
93      * @param name The name of a Stimulus to retrieve.
94      * @return The first matching stimulus if successful or null otherwise.
95      * @throws RvTestException
96      */
97      public Stimulus getStimulus(String name) throws RvTestException {
98          Iterator iterator = stimulusList.iterator();
99  
100         while (iterator.hasNext()) {
101             Stimulus stimulus = (Stimulus) iterator.next();
102 
103             if (name.equalsIgnoreCase(stimulus.getName())) {
104                 return stimulus;
105             }
106         }
107 
108         throw new RvTestException("Unknown stimulus: " + name);
109     }
110 
111     public Iterator iterator() {
112         return this.stimulusList.iterator();
113     }
114 
115     public int size() {
116         return this.stimulusList.size();
117     }
118 
119     public void addAll(List list) {
120         stimulusList.addAll(list);
121     }
122 }