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: ReadWriteTest.java,v 1.6 2004/02/03 07:31:03 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.readwrite;
46
47 import javax.jms.BytesMessage;
48 import javax.jms.Message;
49 import javax.jms.MessageEOFException;
50 import javax.jms.MessageNotReadableException;
51 import javax.jms.StreamMessage;
52
53 import org.apache.log4j.Category;
54
55 import junit.framework.Test;
56
57 import org.exolab.jmscts.core.AbstractMessageTestCase;
58 import org.exolab.jmscts.core.MessagePopulator;
59 import org.exolab.jmscts.core.MessageVerifier;
60 import org.exolab.jmscts.core.TestContext;
61 import org.exolab.jmscts.core.TestCreator;
62 import org.exolab.jmscts.test.message.util.EmptyMessageVerifier;
63 import org.exolab.jmscts.test.message.util.EmptyPropertyVerifier;
64 import org.exolab.jmscts.test.message.util.MessagePopulatorVerifier;
65 import org.exolab.jmscts.test.message.util.MessagePropertyVerifier;
66 import org.exolab.jmscts.test.message.util.PropertyValues;
67 import org.exolab.jmscts.test.message.util.PopulatorVerifierFactory;
68
69
70 /***
71 * This class tests that message properties and message bodies may be read
72 * and written on creation of a new message
73 *
74 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
75 * @version $Revision: 1.6 $
76 * @see AbstractMessageTestCase
77 */
78 public class ReadWriteTest extends AbstractMessageTestCase
79 implements PropertyValues {
80
81 /***
82 * The logger
83 */
84 private static final Category log =
85 Category.getInstance(ReadWriteTest.class);
86
87
88 /***
89 * Construct a new <code>ReadWriteTest</code>
90 *
91 * @param name the name of test case
92 */
93 public ReadWriteTest(String name) {
94 super(name);
95 }
96
97 /***
98 * Sets up the test suite
99 *
100 * @return an instance of this class that may be run by
101 * {@link org.exolab.jmscts.core.JMSTestRunner}
102 */
103 public static Test suite() {
104 return TestCreator.createMessageTest(ReadWriteTest.class);
105 }
106
107 /***
108 * Get the message populator. This implementation always returns null
109 *
110 * @return null
111 */
112 public MessagePopulator getMessagePopulator() {
113 return null;
114 }
115
116 /***
117 * Verifies that a message is writable on creation.
118 *
119 * @jmscts.message all
120 * @jmscts.requirement message.creation
121 * @throws Exception for any error
122 */
123 public void testWriteableOnCreation() throws Exception {
124 TestContext context = getContext();
125 Message message = context.getMessage();
126
127 try {
128 MessagePropertyVerifier populator = new MessagePropertyVerifier();
129 populator.populate(message);
130 } catch (Exception exception) {
131 log.debug(exception, exception);
132 fail("Failed to populate message properties on creation: "
133 + exception);
134 }
135
136 try {
137 MessagePopulatorVerifier populator =
138 PopulatorVerifierFactory.create(message, null);
139 populator.populate(message);
140 } catch (Exception exception) {
141 log.debug(exception, exception);
142 fail("Failed to populate message body on creation: " + exception);
143 }
144 }
145
146 /***
147 * Verifies that a message is readable on creation.
148 *
149 * @jmscts.message MapMessage
150 * @jmscts.message Message
151 * @jmscts.message ObjectMessage
152 * @jmscts.message TextMessage
153 * @jmscts.requirement message.creation
154 * @throws Exception for any error
155 */
156 public void testReadableOnCreation() throws Exception {
157 TestContext context = getContext();
158 Message message = context.getMessage();
159
160 // verify that the message has no properties set (with the possible
161 // exception of provider properties)
162 try {
163 MessageVerifier verifier = new EmptyPropertyVerifier();
164 verifier.verify(message);
165 } catch (Exception exception) {
166 String msg = "Attempt to read the properties of a new "
167 + "message should not throw any exceptions";
168 log.debug(msg, exception);
169 fail(msg + ": " + exception);
170 }
171
172 MessageVerifier verifier = new EmptyMessageVerifier();
173 try {
174 verifier.verify(message);
175 } catch (Exception exception) {
176 String msg = "Failed to read the message body of a new "
177 + "message";
178 log.debug(msg, exception);
179 fail(msg + ": " + exception);
180 }
181 }
182
183 /***
184 * Verifies that attempting to read a BytesMessage on creation
185 * throws <code>MessageNotReadableException</code>, until
186 * <code>BytesMessage.reset()</code> is invoked, and that subsequent
187 * reads throw <code>MessageEOFException</code>
188 *
189 * @jmscts.message BytesMessage
190 * @jmscts.requirement message.bytes.creation
191 * @jmscts.requirement message.bytes.eof
192 * @jmscts.requirement message.creation
193 * @throws Exception for any error
194 */
195 public void testBytesReadableOnCreation() throws Exception {
196 checkBytesStreamReadableOnCreation();
197 }
198
199 /***
200 * Verifies that attempting to read a StreamMessage on creation
201 * throws <code>MessageNotReadableException</code>, until
202 * <code>StreamMessage.reset()</code> is invoked, and that subsequent
203 * reads throw <code>MessageEOFException</code>
204 *
205 * @jmscts.message StreamMessage
206 * @jmscts.requirement message.creation
207 * @jmscts.requirement message.stream.creation
208 * @jmscts.requirement message.stream.eof
209 * @throws Exception for any error
210 */
211 public void testStreamReadableOnCreation() throws Exception {
212 checkBytesStreamReadableOnCreation();
213 }
214
215 /***
216 * Verifies that a message is writeable after clearing its body.
217 *
218 * @jmscts.requirement message.method.clearBody
219 * @throws Exception for any error
220 */
221 public void testWriteableOnClear() throws Exception {
222 TestContext context = getContext();
223 Message message = context.getMessage();
224
225 MessagePopulatorVerifier populator =
226 PopulatorVerifierFactory.create(message, null);
227
228 try {
229 populator.populate(message);
230 } catch (Exception exception) {
231 log.debug(exception, exception);
232 fail("Failed to populate message body on creation: " + exception);
233 }
234
235 message.clearBody();
236
237 try {
238 populator.populate(message);
239 } catch (Exception exception) {
240 log.debug(exception, exception);
241 fail("Failed to populate message body after invoking clearBody: "
242 + exception);
243 }
244 }
245
246 /***
247 * Verifies that attempting to read a BytesMessage or StreamMessage
248 * on creation <code>MessageNotReadableException</code>, until
249 * <code>reset()</code> is invoked, and that subsequent reads throw
250 * <code>MessageEOFException</code>
251 *
252 * @throws Exception for any error
253 */
254 private void checkBytesStreamReadableOnCreation() throws Exception {
255 TestContext context = getContext();
256 Message message = context.getMessage();
257
258 // verify that the message has no properties set (with the possible
259 // exception of provider properties)
260 try {
261 MessageVerifier verifier = new EmptyPropertyVerifier();
262 verifier.verify(message);
263 } catch (Exception exception) {
264 String msg = "Attempt to read the properties of a new "
265 + "message should not throw any exceptions";
266 log.debug(msg, exception);
267 fail(msg + ": " + exception);
268 }
269
270 // check that each method throws MessageNotReadableException
271 MessageVerifier verifier = new EmptyMessageVerifier(
272 MessageNotReadableException.class);
273 try {
274 verifier.verify(message);
275 } catch (Exception exception) {
276 String msg = "Attempt to read body of a new "
277 + context.getMessageType().getName()
278 + " should throw MessageNotReadableException";
279 log.debug(msg, exception);
280 fail(msg + ": " + exception);
281 }
282
283 // reset the message so that it can be read.
284 if (message instanceof BytesMessage) {
285 ((BytesMessage) message).reset();
286 } else {
287 ((StreamMessage) message).reset();
288 }
289
290 // check that each applicable method throws MessageEOFException
291 verifier = new EmptyMessageVerifier(MessageEOFException.class);
292 try {
293 verifier.verify(message);
294 } catch (Exception exception) {
295 String msg = "Attempt to read body of a new "
296 + context.getMessageType().getName()
297 + " after reset() should throw MessageEOFException";
298 log.debug(msg, exception);
299 fail(msg + ": " + exception);
300 }
301 }
302
303 }
This page was automatically generated by Maven