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  /*
33   * Copyright (c) 2002, Reuters
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions are met:
38   *
39   * Redistributions of source code must retain the above copyright notice, this
40   * list of conditions and the following disclaimer.
41   * Redistributions in binary form must reproduce the above copyright notice,
42   * this list of conditions and the following disclaimer in the documentation
43   * and/or other materials provided with the distribution.
44   * Neither the name of the Reuters nor the names of its contributors may be
45   * used to endorse or promote products derived from this software without
46   * specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   *
60   */
61  package com.reuters.msgtest;
62  
63  import com.reuters.msgtest.fixture.Response;
64  import com.reuters.msgtest.fixture.Responses;
65  import com.reuters.msgtest.fixture.RvFixture;
66  import com.reuters.msgtest.fixture.Stimuli;
67  import com.reuters.msgtest.fixture.Stimulus;
68  import com.tibco.tibrv.TibrvException;
69  import com.tibco.tibrv.TibrvTransport;
70  import org.apache.commons.logging.Log;
71  import org.apache.commons.logging.LogFactory;
72  import java.util.Iterator;
73  import java.util.List;
74  
75  
76  /***
77   * This class provides a helper method for typical testing criteria encountered
78   * when doing functional testing. FunctionalTests will most likely subclass this
79   * class instead of RvTestCase directly.
80   *
81   * @author <a href="mailto:mark.pollack@reuters.com">Mark Pollack </a>
82   * @version @VERSION@
83   */
84  public abstract class FunctionalTestCase extends RvTestCase {
85      private Log log = LogFactory.getLog(FunctionalTestCase.class);
86  
87      /***
88       * Creates a test with the given name.
89       *
90       * @param name
91       *            The name of the test.
92       */
93      public FunctionalTestCase(String name) {
94          super(name);
95      }
96  
97      /***
98       * This will setup the recorder to record all expected responses, send the
99       * stimulus messages out, then compare actual to recorded messges.
100      *
101      * @param fixture
102      *            An RvFixture
103      * @throws TibrvException
104      *             if errors accessing TibrvMsg
105      */
106     public RvRecorder performStandardTest(RvFixture fixture)
107         throws Exception {
108         log.debug("Performing Standard Test: " + fixture.getName());
109 
110         RvRecorder recorder = null;
111 
112         try {
113             // TODO Investigate if this too slow
114             recorder = new RvRecorder(getRvTestConfiguration().getTransport());
115             recorder.start();
116 
117             recordResponses(fixture.getResponses(), recorder);
118 
119             sendStimuli(fixture.getStimuli(), recorder.getTransport());
120 
121             verifyExpectedResponses(fixture.getResponses(), recorder);
122         } finally {
123             if (recorder != null) {
124                 recorder.stop();
125             }
126         }
127 
128         return recorder;
129     }
130 
131     /***
132      * @param responses
133      * @throws TibrvException
134      */
135     public void verifyExpectedResponses(Responses responses, RvRecorder recorder)
136         throws TibrvException {
137         log.debug("Verifying Expected Responses");
138 
139         for (Iterator iter = responses.iterator(); iter.hasNext();) {
140             Response response = (Response) iter.next();
141             verifyExpectedResponse(response, recorder);
142         }
143     }
144 
145     /***
146      * @param response
147      * @throws TibrvException
148      */
149     public void verifyExpectedResponse(Response response, RvRecorder recorder)
150         throws TibrvException {
151         log.debug("Verifying Response: " + response.getName());
152 
153         String expectedSubject = response.getTibrvMsg().getSendSubject();
154         List actualMsgs = recorder.getRecorderEntries(expectedSubject);
155 
156         assertNotNull("Subject <" + expectedSubject +
157             "> was not requested to be recorded.", actualMsgs);
158 
159         assertTrue("Too few messages received for subject [" + expectedSubject +
160             "]." + "Minimum [" + response.getMinexpected() + "] Received [" +
161             actualMsgs.size() + "]",
162             response.getMinexpected() <= actualMsgs.size());
163 
164         assertTrue("Too many messages received for subject [" +
165             expectedSubject + "]." + "Maximum [" + response.getMaxexpected() +
166             "] Received [" + actualMsgs.size() + "]",
167             response.getMaxexpected() >= actualMsgs.size());
168 
169         for (Iterator iter = actualMsgs.iterator(); iter.hasNext();) {
170             RecorderEntry recorderEntry = (RecorderEntry) iter.next();
171 
172             switch (response.getComparisonMethod()) {
173             case Response.NONE:
174                 break;
175 
176             case Response.EQUALS:
177                 assertEquals("messages not equal", response.getTibrvMsg(),
178                     recorderEntry.getMsg());
179 
180                 break;
181 
182             case Response.EQUALSIGNORE:
183                 assertEquals("messages not equal", response.getTibrvMsg(),
184                     recorderEntry.getMsg(), response.getIgnoredFields());
185 
186                 break;
187             }
188         }
189     }
190 
191     /***
192      * @param stimuli
193      * @throws TibrvException
194      * @throws InterruptedException
195      */
196     public void sendStimuli(Stimuli stimuli, TibrvTransport transport)
197         throws TibrvException, InterruptedException {
198         log.debug("Sending Stimuli");
199 
200         for (Iterator iter = stimuli.iterator(); iter.hasNext();) {
201             Stimulus stimulus = (Stimulus) iter.next();
202             log.debug("Sending Stimulus:" + stimulus.getName());
203             transport.send(stimulus.getMsg());
204             log.debug("Sleeping: " + stimulus.getDelay() + "ms");
205             Thread.sleep(stimulus.getDelay());
206         }
207     }
208 
209     /***
210      * @param responses
211      * @throws TibrvException
212      */
213     public void recordResponses(Responses responses, RvRecorder recorder)
214         throws TibrvException {
215         log.debug("Recording Responses");
216 
217         for (Iterator iter = responses.iterator(); iter.hasNext();) {
218             Response response = (Response) iter.next();
219             log.debug("Recording Response: " + response.getName());
220             assertNotNull("Response subject is null for " + response.getName(),
221                 response.getTibrvMsg().getSendSubject());
222             recorder.record(response.getTibrvMsg().getSendSubject());
223         }
224     }
225 }