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: MessageCopyTest.java,v 1.6 2004/02/03 07:31:02 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.copy;
46
47 import java.util.LinkedList;
48 import java.util.List;
49
50 import javax.jms.BytesMessage;
51 import javax.jms.Message;
52 import javax.jms.Session;
53 import javax.jms.StreamMessage;
54
55 import junit.framework.Test;
56
57 import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
58 import org.exolab.jmscts.core.ChainedMessagePopulator;
59 import org.exolab.jmscts.core.MessageCreator;
60 import org.exolab.jmscts.core.MessageBodyComparer;
61 import org.exolab.jmscts.core.MessagePopulator;
62 import org.exolab.jmscts.core.MessagePropertyComparer;
63 import org.exolab.jmscts.core.MessageReceiver;
64 import org.exolab.jmscts.core.MessageSender;
65 import org.exolab.jmscts.core.MessageTypes;
66 import org.exolab.jmscts.core.SequenceMessagePopulator;
67 import org.exolab.jmscts.core.SequencePropertyPopulator;
68 import org.exolab.jmscts.core.TestContext;
69 import org.exolab.jmscts.core.TestCreator;
70
71
72 /***
73 * This class tests that message properties and message bodies are copied
74 * on send and receive.
75 *
76 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
77 * @version $Revision: 1.6 $
78 * @see AbstractSendReceiveTestCase
79 * @see org.exolab.jmscts.core.SendReceiveTestRunner
80 */
81 public class MessageCopyTest extends AbstractSendReceiveTestCase {
82
83 /***
84 * The destination used by this test case
85 */
86 private static final String DESTINATION = "MessageCopyTest";
87
88
89 /***
90 * Construct a new <code>MessageCopyTest</code>
91 *
92 * @param name the name of test case
93 */
94 public MessageCopyTest(String name) {
95 super(name);
96 }
97
98 /***
99 * Sets up the test suite
100 *
101 * @return an instance of this class that may be run by
102 * {@link org.exolab.jmscts.core.JMSTestRunner}
103 */
104 public static Test suite() {
105 return TestCreator.createSendReceiveTest(MessageCopyTest.class);
106 }
107
108 /***
109 * Get the message populator. This implementation always returns null
110 *
111 * @return null
112 */
113 public MessagePopulator getMessagePopulator() {
114 return null;
115 }
116
117 /***
118 * Returns the list of destination names used by this test case. These
119 * are used to pre-create destinations prior to running the test case.
120 *
121 * @return the list of destinations used by this test case
122 */
123 public String[] getDestinations() {
124 return new String[] {DESTINATION};
125 }
126
127 /***
128 * Verifies that messages are copied by the provider on send.
129 *
130 * @jmscts.requirement message.send
131 * @throws Exception for any error
132 */
133 public void testCopyOnSend() throws Exception {
134 final int count = 10;
135 TestContext context = getContext();
136 Session session = context.getSession();
137 Message message = context.getMessage();
138 List expected = new LinkedList();
139
140 MessageReceiver receiver = createReceiver(DESTINATION);
141 MessageSender sender = createSender(DESTINATION);
142
143 MessagePopulator property = new SequencePropertyPopulator();
144 MessagePopulator body = new SequenceMessagePopulator();
145 MessagePopulator propertyDuplicator = new SequencePropertyPopulator();
146 MessagePopulator bodyDuplicator = new SequenceMessagePopulator();
147
148 // the message populator
149 MessagePopulator populator = new ChainedMessagePopulator(
150 new MessagePopulator[] {property, body});
151
152 // the message populator to create duplicate copies
153 MessagePopulator duplicator = new ChainedMessagePopulator(
154 new MessagePopulator[] {propertyDuplicator, bodyDuplicator});
155
156 List messages = null;
157 boolean appender = (message instanceof BytesMessage
158 || message instanceof StreamMessage);
159
160 Class type = MessageTypes.getType(message);
161 try {
162 for (int i = 0; i < count; ++i) {
163 sender.send(message, 1, populator);
164 MessageCreator creator;
165 Message copy;
166 if (appender) {
167 creator = new MessageCreator(session, propertyDuplicator);
168 copy = creator.create(type);
169 bodyDuplicator = new SequenceMessagePopulator();
170 for (int j = 0; j <= i; ++j) {
171 bodyDuplicator.populate(copy);
172 }
173 } else {
174 creator = new MessageCreator(session, duplicator);
175 copy = creator.create(type);
176 }
177 expected.add(copy);
178 }
179 if (session.getTransacted()) {
180 session.commit();
181 }
182 messages = receive(receiver, count);
183
184 MessagePropertyComparer propertyComparer =
185 new MessagePropertyComparer(true);
186 MessageBodyComparer bodyComparer = new MessageBodyComparer();
187 for (int i = 0; i < count; ++i) {
188 Message sent = (Message) expected.get(i);
189 Message received = (Message) messages.get(i);
190
191 // need to reset BytesMessage and StreamMessage instances so
192 // they can be read
193 if (sent instanceof BytesMessage) {
194 ((BytesMessage) sent).reset();
195 ((BytesMessage) received).reset();
196 } else if (sent instanceof StreamMessage) {
197 ((StreamMessage) sent).reset();
198 ((StreamMessage) received).reset();
199 }
200
201 if (!propertyComparer.compare(sent, received)) {
202 String msg = "Received message has different properties "
203 + "to that sent";
204 fail(msg);
205 }
206 if (!bodyComparer.compare(sent, received)) {
207 String msg = "Received message has different data "
208 + "to that sent";
209 fail(msg);
210 }
211 }
212 } finally {
213 receiver.remove();
214 sender.close();
215 }
216 }
217
218 }
This page was automatically generated by Maven