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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
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 }