1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: AbstractMessageVerifier.java,v 1.4 2004/02/03 21:52:06 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.Arrays;
48
49 import javax.jms.BytesMessage;
50 import javax.jms.MapMessage;
51 import javax.jms.Message;
52 import javax.jms.ObjectMessage;
53 import javax.jms.StreamMessage;
54 import javax.jms.TextMessage;
55
56
57 /***
58 * A helper class for verifying the content of messages. This class provides
59 * no verification functionality - that is left to sublasses.
60 * <p>
61 * It does provide helper methods to:
62 * <ul>
63 * <li>invoke methods on messages</li>
64 * <li>invoke methods on messages and verify that any exception thrown matches
65 * that expected</li>
66 * <li>verify the return value of a method against an expected result
67 * </ul>
68 *
69 * @version $Revision: 1.4 $ $Date: 2004/02/03 21:52:06 $
70 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
71 * @see MessageVerifier
72 */
73 public abstract class AbstractMessageVerifier implements MessageVerifier {
74
75 /***
76 * The expected exception type, or null, if no exceptions are expected
77 */
78 private Class _exception = null;
79
80 /***
81 * The method invoker
82 */
83 private MethodInvoker _invoker = null;
84
85
86 /***
87 * Construct a new instance. No exceptions are expected to be thrown
88 * when invoking methods
89 */
90 public AbstractMessageVerifier() {
91 _invoker = new MethodInvoker();
92 }
93
94 /***
95 * Construct an instance with the expected exception thrown when
96 * methods are invoked
97 *
98 * @param exception the expected exception type when methods are invoked
99 */
100 public AbstractMessageVerifier(Class exception) {
101 _exception = exception;
102 _invoker = new MethodInvoker(null, _exception);
103 }
104
105 /***
106 * Verify a message's content. This method delegates the message to
107 * the appropriate verifier method.
108 *
109 * @param message the message to verify
110 * @throws Exception for any error
111 */
112 public void verify(Message message) throws Exception {
113 if (message instanceof BytesMessage) {
114 verifyBytesMessage((BytesMessage) message);
115 } else if (message instanceof MapMessage) {
116 verifyMapMessage((MapMessage) message);
117 } else if (message instanceof ObjectMessage) {
118 verifyObjectMessage((ObjectMessage) message);
119 } else if (message instanceof StreamMessage) {
120 verifyStreamMessage((StreamMessage) message);
121 } else if (message instanceof TextMessage) {
122 verifyTextMessage((TextMessage) message);
123 } else {
124 verifyMessage(message);
125 }
126 }
127
128 /***
129 * Verify a Message instance. This method throws
130 * UnsupportedOperationException if invoked. Subclasses needing the
131 * functionality must implement it.
132 *
133 * @param message the message to verify
134 * @throws Exception for any error
135 */
136 public void verifyMessage(Message message) throws Exception {
137 throw new UnsupportedOperationException(
138 "verifyMessage() not implemented by " + getClass().getName());
139 }
140
141 /***
142 * Verify a BytesMessage instance. This method throws
143 * UnsupportedOperationException if invoked. Subclasses needing the
144 * functionality must implement it.
145 *
146 * @param message the message to verify
147 * @throws Exception for any error
148 */
149 public void verifyBytesMessage(BytesMessage message) throws Exception {
150 throw new UnsupportedOperationException(
151 "verifyBytesMessage() not implemented by " + getClass().getName());
152 }
153
154 /***
155 * Verify a MapMessage instance. This method throws
156 * UnsupportedOperationException if invoked. Subclasses needing the
157 * functionality must implement it.
158 *
159 * @param message the message to verify
160 * @throws Exception for any error
161 */
162 public void verifyMapMessage(MapMessage message) throws Exception {
163 throw new UnsupportedOperationException(
164 "verifyMapMessage() not implemented by " + getClass().getName());
165 }
166
167 /***
168 * Verify an ObjectMessage instance. This method throws
169 * UnsupportedOperationException if invoked. Subclasses needing the
170 * functionality must implement it.
171 *
172 * @param message the message to verify
173 * @throws Exception for any error
174 */
175 public void verifyObjectMessage(ObjectMessage message) throws Exception {
176 throw new UnsupportedOperationException(
177 "verifyObjectMessage() not implemented by "
178 + getClass().getName());
179 }
180
181 /***
182 * Verify a StreamMessage instance. This method throws
183 * UnsupportedOperationException if invoked. Subclasses needing the
184 * functionality must implement it.
185 *
186 * @param message the message to verify
187 * @throws Exception for any error
188 */
189 public void verifyStreamMessage(StreamMessage message) throws Exception {
190 throw new UnsupportedOperationException(
191 "verifyStreamMessage() not implemented by "
192 + getClass().getName());
193 }
194
195 /***
196 * Verify a TextMessage instance. This method throws
197 * UnsupportedOperationException if invoked. Subclasses needing the
198 * functionality must implement it.
199 *
200 * @param message the message to verify
201 * @throws Exception for any error
202 */
203 public void verifyTextMessage(TextMessage message) throws Exception {
204 throw new UnsupportedOperationException(
205 "verifyTextMessage() not implemented by " + getClass().getName());
206 }
207
208 /***
209 * Return the exception type expected to be thrown by methods
210 *
211 * @return the expected exception type, or null, if no exceptions are
212 * expected
213 */
214 public Class getExpectedException() {
215 return _exception;
216 }
217
218 /***
219 * Invoke a method taking no arguments
220 *
221 * @param message the message to invoke the method on
222 * @param method the method to invoke
223 * @return the result of the method invocation, or null if it returns
224 * void, or didn't complete
225 * @throws Exception for any error
226 */
227 protected Object invoke(Message message, String method) throws Exception {
228 MethodCache cache = getMethods();
229 Object result = null;
230 if (cache != null) {
231 result = _invoker.invoke(message, cache.getMethod(method));
232 } else {
233 result = _invoker.invoke(message, method);
234 }
235 return result;
236 }
237
238 /***
239 * Invoke a method taking a single argument
240 *
241 * @param message the message to invoke the method on
242 * @param method the method to invoke
243 * @param value the argument to pass to the method
244 * @return the result of the method invocation, or null if it returns
245 * void, or didn't complete
246 * @throws Exception for any error
247 */
248 protected Object invoke(Message message, String method, Object value)
249 throws Exception {
250 MethodCache cache = getMethods();
251 Object result = null;
252 if (cache != null) {
253 result = _invoker.invoke(
254 message, cache.getMethod(method, 1), value);
255 } else {
256 result = _invoker.invoke(message, method, value);
257 }
258 return result;
259 }
260
261 /***
262 * Invoke a method taking multiple arguments
263 *
264 * @param message the message to invoke the method on
265 * @param method the method to invoke
266 * @param values the arguments to pass to the method
267 * @return the result of the method invocation, or null if it returns
268 * void, or didn't complete
269 * @throws Exception for any error
270 */
271 protected Object invoke(Message message, String method, Object[] values)
272 throws Exception {
273 MethodCache cache = getMethods();
274 Object result;
275 if (cache != null) {
276 result = _invoker.invoke(message,
277 cache.getMethod(method, values.length),
278 values);
279 } else {
280 result = _invoker.invoke(message, method, values);
281 }
282 return result;
283 }
284
285 /***
286 * Helper to invoke a method taking no arguments, and verify that the
287 * result matches that expected
288 *
289 * @param message the message to invoke the method on
290 * @param method the method to invoke
291 * @param expected the expected return value
292 * @throws Exception for any error
293 */
294 protected void expect(Message message, String method, Object expected)
295 throws Exception {
296 equal(invoke(message, method), expected);
297 }
298
299 /***
300 * Helper to invoke a method taking a single argument, and verify that the
301 * result matches that expected
302 *
303 * @param message the message to invoke the method on
304 * @param method the method to invoke
305 * @param value the argument to pass to the method
306 * @param expected the expected return value
307 * @throws Exception for any error
308 */
309 protected void expect(Message message, String method, Object value,
310 Object expected)
311 throws Exception {
312 equal(invoke(message, method, value), expected);
313 }
314
315 /***
316 * Helper to invoke a method taking a list of arguments, and verify that
317 * the result matches that expected
318 *
319 * @param message the message to invoke the method on
320 * @param method the method to invoke
321 * @param values the arguments to pass to the method
322 * @param expected the expected return value
323 * @throws Exception for any error
324 */
325 protected void expect(Message message, String method, Object[] values,
326 Object expected)
327 throws Exception {
328 equal(invoke(message, method, values), expected);
329 }
330
331 /***
332 * Helper to compare two objects for equality
333 *
334 * @param value the value to compare against
335 * @param expected the expected value
336 * @throws Exception if the objects aren't equal
337 */
338 protected void equal(Object value, Object expected) throws Exception {
339 if (expected == null && value == null) {
340 // no-op
341 } else if (expected == null || value == null) {
342 throw new Exception("Expected value=" + expected
343 + ", but got value=" + value);
344 } else if (expected.getClass() == value.getClass()) {
345 if (expected instanceof byte[]) {
346 if (!Arrays.equals((byte[]) expected, (byte[]) value)) {
347 throw new Exception("Byte arrays are not equal");
348 }
349 } else if (!expected.equals(value)) {
350 throw new Exception("Expected value=" + expected
351 + ", but got value=" + value);
352 }
353 } else {
354 throw new Exception(
355 "Class mismatch. Expected class="
356 + expected.getClass().getName() + ", but got class="
357 + value.getClass().getName());
358 }
359 }
360
361 /***
362 * Returns a cache of methods for a message, to avoid expensive reflect
363 * operations. This implementation returns null - subclasses needing
364 * improved performance should provide their own implementation
365 *
366 * @return null
367 */
368 protected MethodCache getMethods() {
369 return null;
370 }
371
372 }
This page was automatically generated by Maven