View Javadoc

1   /*
2    * Copyright (c) 2004, RV Test Team
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10   *
11   * Redistributions in binary form must reproduce the above copyright notice,
12   * this list of conditions and the following disclaimer in the documentation
13   * and/or other materials provided with the distribution.
14   *
15   * Neither the name of the "RV Test Team" nor the names of its contributors may
16   * be used to endorse or promote products derived from this software without
17   * specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29   * THE POSSIBILITY OF SUCH DAMAGE.
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 }