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: ObjectMessageTest.java,v 1.5 2004/02/03 07:31:02 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.copy;
46
47 import java.io.Serializable;
48 import java.util.Arrays;
49
50 import javax.jms.ObjectMessage;
51 import javax.jms.Session;
52
53 import junit.framework.Test;
54
55 import org.exolab.jmscts.core.AbstractMessageTestCase;
56 import org.exolab.jmscts.core.MessagePopulator;
57 import org.exolab.jmscts.core.TestContext;
58 import org.exolab.jmscts.core.TestCreator;
59 import org.exolab.jmscts.test.message.util.MessageValues;
60
61
62 /***
63 * This class tests that <code>ObjectMessage</code> copies content
64 *
65 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
66 * @version $Revision: 1.5 $
67 * @see javax.jms.ObjectMessage
68 * @see AbstractMessageTestCase
69 * @jmscts.message ObjectMessage
70 */
71 public class ObjectMessageTest extends AbstractMessageTestCase
72 implements MessageValues {
73
74 /***
75 * Construct a new <code>ObjectMessageTest</code>
76 *
77 * @param name the name of test case
78 */
79 public ObjectMessageTest(String name) {
80 super(name);
81 }
82
83 /***
84 * Sets up the test suite
85 *
86 * @return an instance of this class that may be run by
87 * {@link org.exolab.jmscts.core.JMSTestRunner}
88 */
89 public static Test suite() {
90 return TestCreator.createMessageTest(ObjectMessageTest.class);
91 }
92
93 /***
94 * Get the message populator. This implementation always returns null
95 *
96 * @return null
97 */
98 public MessagePopulator getMessagePopulator() {
99 return null;
100 }
101
102 /***
103 * Verifies that <code>ObjectMessage.setObject()</code> copies byte arrays
104 *
105 * @jmscts.requirement message.copy
106 * @throws Exception for any error
107 */
108 public void testByteArrayCopy() throws Exception {
109 final int size = 10;
110 TestContext context = getContext();
111 ObjectMessage message = (ObjectMessage) context.getMessage();
112
113 byte[] bytes = new byte[size];
114 Arrays.fill(bytes, (byte) 0);
115 message.setObject(bytes);
116
117 // verify that the input was copied, by modifying it
118 Arrays.fill(bytes, (byte) 1);
119 byte[] value = (byte[]) message.getObject();
120 assertTrue("ObjectMessage.setObject() did not copy the byte array",
121 !Arrays.equals(value, bytes));
122
123 // verify that the return value is not the same each time
124 byte[] value1 = (byte[]) message.getObject();
125 byte[] value2 = (byte[]) message.getObject();
126 assertTrue("ObjectMessage.getObject() did not copy the byte array",
127 value1 != value2);
128 }
129
130 /***
131 * Verifies that <code>ObjectMessage.setObject()</code> copies user objects
132 *
133 * @jmscts.requirement message.copy
134 * @throws Exception for any error
135 */
136 public void testCustomObjectCopy() throws Exception {
137 TestContext context = getContext();
138 ObjectMessage message = (ObjectMessage) context.getMessage();
139
140 MyObject source = new MyObject();
141 source.setContent("ABC");
142
143 message.setObject(source);
144 MyObject copy = (MyObject) message.getObject();
145 assertTrue("ObjectMessage.setObject() did not copy the object",
146 source != copy);
147 // ensure it did a deep copy
148 assertTrue("ObjectMessage.setObject() did not perform a deep copy of"
149 + " the object", source.getContent() != copy.getContent());
150
151 source.setContent("DEF");
152 copy = (MyObject) message.getObject();
153 assertTrue("ObjectMessage.setObject() did not copy the object",
154 !source.getContent().equals(copy.getContent()));
155 }
156
157 /***
158 * Verifies that <code>ObjectMessage.getObject()</code> returns a
159 * different reference to that set, when the object is supplied at
160 * construction
161 *
162 * @jmscts.requirement message.copy
163 * @throws Exception for any error
164 */
165 public void testCopyAtConstruction() throws Exception {
166 TestContext context = getContext();
167 Session session = context.getSession();
168
169 MyObject source = new MyObject();
170 source.setContent("ABC");
171 ObjectMessage message = session.createObjectMessage(source);
172
173 MyObject copy = (MyObject) message.getObject();
174 assertTrue("ObjectMessage.setObject() did not copy the object",
175 source != copy);
176 // ensure it did a deep copy
177 assertTrue("ObjectMessage.setObject() did not perform a deep copy of"
178 + " the object", source.getContent() != copy.getContent());
179 }
180
181 /***
182 * Helper object for testing copying
183 */
184 public static class MyObject implements Serializable {
185
186 /***
187 * The content
188 */
189 private String _content = null;
190
191
192 /***
193 * Set the content
194 *
195 * @param content the content
196 */
197 public void setContent(String content) {
198 _content = content;
199 }
200
201 /***
202 * Returns the content
203 *
204 * @return the content
205 */
206 public String getContent() {
207 return _content;
208 }
209 }
210
211 }
This page was automatically generated by Maven