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: MessageBodyReferenceComparer.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import java.util.Enumeration;
48 import java.util.HashMap;
49 import java.util.Iterator;
50
51 import javax.jms.BytesMessage;
52 import javax.jms.MapMessage;
53 import javax.jms.ObjectMessage;
54 import javax.jms.StreamMessage;
55 import javax.jms.TextMessage;
56
57
58 /***
59 * A helper class for comparing if the body of two messages refer to the
60 * same object(s).
61 *
62 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $
63 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
64 */
65 public class MessageBodyReferenceComparer extends AbstractMessageComparer {
66
67 /***
68 * Compare two BytesMessage instances. It is impossible to tell via
69 * the BytesMessage API if the message bodies refer to the same byte
70 * array, so this implementation always returns <code>false</code>
71 *
72 * @param message1 the message to compare
73 * @param message2 the message to compare
74 * @return <code>false</code> when invoked
75 */
76 public boolean compareBytesMessages(BytesMessage message1,
77 BytesMessage message2) {
78 return false;
79 }
80
81 /***
82 * Compare two MapMessage instances
83 *
84 * @param message1 the message to compare
85 * @param message2 the message to compare
86 * @return <code>true</code> if the messages bodies contain references
87 * to the same objects
88 * @throws Exception for any error
89 */
90 public boolean compareMapMessages(MapMessage message1,
91 MapMessage message2) throws Exception {
92 boolean equal = false;
93 HashMap map1 = new HashMap();
94 HashMap map2 = new HashMap();
95 Enumeration iter1 = message1.getMapNames();
96 Enumeration iter2 = message2.getMapNames();
97 while (iter1.hasMoreElements()) {
98 String name = (String) iter1.nextElement();
99 map1.put(name, message1.getObject(name));
100 }
101 while (iter2.hasMoreElements()) {
102 String name = (String) iter2.nextElement();
103 map2.put(name, message2.getObject(name));
104 }
105 Object[] set1 = map1.keySet().toArray();
106 Object[] set2 = map2.keySet().toArray();
107 Iterator setIter = map1.keySet().iterator();
108 while (!equal && setIter.hasNext()) {
109 String name = (String) setIter.next();
110 if (map2.keySet().contains(name)) {
111 for (int i = 0; i < set2.length; ++i) {
112 // check if the keys are identical
113 if (name.equals(set2[i]) && name == set2[i]) {
114 equal = true;
115 break;
116 }
117 }
118 if (!equal) {
119 // check if the values are identical
120 if (map1.get(name) == map2.get(name)) {
121 equal = true;
122 }
123 }
124 }
125 }
126
127 return equal;
128 }
129
130 /***
131 * Compare two ObjectMessage instances
132 *
133 * @param message1 the message to compare
134 * @param message2 the message to compare
135 * @return <code>true</code> if the messages bodies contain references
136 * to the same objects
137 * @throws Exception for any error
138 */
139 public boolean compareObjectMessages(ObjectMessage message1,
140 ObjectMessage message2)
141 throws Exception {
142 boolean equal = false;
143 Object value1 = message1.getObject();
144 Object value2 = message2.getObject();
145 if (value1 != null) {
146 equal = (value1 == value2);
147 }
148 return equal;
149 }
150
151 /***
152 * Compare two StreamMessage instances
153 *
154 * @param message1 the message to compare
155 * @param message2 the message to compare
156 * @return <code>true</code> if the messages bodies contain references
157 * to the same objects
158 * @throws Exception for any error
159 */
160 public boolean compareStreamMessages(StreamMessage message1,
161 StreamMessage message2)
162 throws Exception {
163 boolean equal = false;
164 Object value1 = message1.readObject();
165 Object value2 = message2.readObject();
166 if (value1 != null) {
167 equal = (value1 == value2);
168 }
169 return equal;
170 }
171
172 /***
173 * Compare two TextMessage instances
174 *
175 * @param message1 the message to compare
176 * @param message2 the message to compare
177 * @return <code>true</code> if the messages bodies contain references
178 * to the same objects
179 * @throws Exception for any error
180 */
181 public boolean compareTextMessages(TextMessage message1,
182 TextMessage message2) throws Exception {
183 boolean equal = false;
184 String value1 = message1.getText();
185 String value2 = message2.getText();
186 if (value1 != null) {
187 equal = (value1 == value2);
188 }
189
190 return equal;
191 }
192
193 }
This page was automatically generated by Maven