View Javadoc
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: MessageBodyComparer.java,v 1.3 2004/01/31 13:44:24 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.util.Arrays; 48 import java.util.Enumeration; 49 import java.util.HashMap; 50 51 import javax.jms.BytesMessage; 52 import javax.jms.MapMessage; 53 import javax.jms.Message; 54 import javax.jms.MessageEOFException; 55 import javax.jms.ObjectMessage; 56 import javax.jms.StreamMessage; 57 import javax.jms.TextMessage; 58 59 60 /*** 61 * A helper class for comparing the body of two messages. 62 * 63 * @version $Revision: 1.3 $ $Date: 2004/01/31 13:44:24 $ 64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 65 */ 66 public class MessageBodyComparer extends AbstractMessageComparer { 67 68 /*** 69 * Compare two Message instances. This is a no-op, as Message instances 70 * don't have a body 71 * 72 * @param message1 one message to be tested for equality 73 * @param message2 the other message to be tested for equality 74 * @return <code>true</code> if the messages bodies are equal 75 * @throws Exception for any error 76 */ 77 public boolean compareMessages(Message message1, Message message2) 78 throws Exception { 79 return true; 80 } 81 82 /*** 83 * Compare two BytesMessage instances 84 * 85 * @param message1 one message to be tested for equality 86 * @param message2 the other message to be tested for equality 87 * @return <code>true</code> if the messages bodies are equal 88 * @throws Exception for any error 89 */ 90 public boolean compareBytesMessages(BytesMessage message1, 91 BytesMessage message2) 92 throws Exception { 93 final int size = 1024; 94 95 boolean equal = true; 96 byte[] stream1 = new byte[size]; 97 byte[] stream2 = new byte[stream1.length]; 98 while (equal) { 99 int read1 = message1.readBytes(stream1); 100 int read2 = message2.readBytes(stream2); 101 if (read1 == read2) { 102 if (read1 != -1) { 103 for (int i = 0; i < read1; ++i) { 104 if (stream1[i] != stream2[i]) { 105 equal = false; 106 break; 107 } 108 } 109 } else { 110 break; // end of stream 111 } 112 } else { 113 equal = false; // mismatch in bytes read 114 } 115 } 116 117 return equal; 118 } 119 120 /*** 121 * Compare two MapMessage instances 122 * 123 * @param message1 one message to be tested for equality 124 * @param message2 the other message to be tested for equality 125 * @return <code>true</code> if the messages bodies are equal 126 * @throws Exception for any error 127 */ 128 public boolean compareMapMessages(MapMessage message1, 129 MapMessage message2) throws Exception { 130 int count1 = 0; // maintains a count of the elements in each message 131 int count2 = 0; // in the unlikely event than a provider allows 132 // duplicate map names 133 HashMap map1 = new HashMap(); 134 HashMap map2 = new HashMap(); 135 Enumeration iter1 = message1.getMapNames(); 136 Enumeration iter2 = message2.getMapNames(); 137 while (iter1.hasMoreElements()) { 138 String name = (String) iter1.nextElement(); 139 map1.put(name, message1.getObject(name)); 140 ++count1; 141 } 142 while (iter2.hasMoreElements()) { 143 String name = (String) iter2.nextElement(); 144 map2.put(name, message2.getObject(name)); 145 ++count2; 146 } 147 return (count1 == count2) ? map1.equals(map2) : false; 148 } 149 150 /*** 151 * Compare two ObjectMessage instances 152 * 153 * @param message1 one message to be tested for equality 154 * @param message2 the other message to be tested for equality 155 * @return <code>true</code> if the messages bodies are equal 156 * @throws Exception for any error 157 */ 158 public boolean compareObjectMessages(ObjectMessage message1, 159 ObjectMessage message2) 160 throws Exception { 161 boolean equal = false; 162 Object value1 = message1.getObject(); 163 Object value2 = message2.getObject(); 164 if (value1 != null && value2 != null) { 165 equal = value1.equals(value2); 166 } else { 167 equal = (value1 == value2); 168 } 169 170 return equal; 171 } 172 173 /*** 174 * Compare two StreamMessage instances 175 * 176 * @param message1 one message to be tested for equality 177 * @param message2 the other message to be tested for equality 178 * @return <code>true</code> if the messages bodies are equal 179 * @throws Exception for any error 180 */ 181 public boolean compareStreamMessages(StreamMessage message1, 182 StreamMessage message2) 183 throws Exception { 184 boolean equal = true; 185 while (equal) { 186 Object object1 = null; 187 Object object2 = null; 188 int eof = 0; // count of MessageEOFException generated 189 try { 190 object1 = message1.readObject(); 191 } catch (MessageEOFException ignore) { 192 ++eof; 193 } 194 try { 195 object2 = message2.readObject(); 196 } catch (MessageEOFException ignore) { 197 ++eof; 198 } 199 if (eof == 1) { 200 equal = false; 201 } else if (eof == 2) { 202 break; 203 } else { 204 if (object1 instanceof byte[] && object2 instanceof byte[]) { 205 equal = Arrays.equals((byte[]) object1, (byte[]) object2); 206 } else if (object1 != null) { 207 equal = object1.equals(object2); 208 } else if (object2 != null) { 209 equal = false; 210 } 211 } 212 } 213 214 return equal; 215 } 216 217 /*** 218 * Compare two TextMessage instances 219 * 220 * @param message1 one message to be tested for equality 221 * @param message2 the other message to be tested for equality 222 * @return <code>true</code> if the messages bodies are equal 223 * @throws Exception for any error 224 */ 225 public boolean compareTextMessages(TextMessage message1, 226 TextMessage message2) throws Exception { 227 boolean equal = false; 228 String value1 = message1.getText(); 229 String value2 = message2.getText(); 230 if (value1 != null && value2 != null) { 231 equal = value1.equals(value2); 232 } else { 233 equal = (value1 == value2); 234 } 235 236 return equal; 237 } 238 239 }

This page was automatically generated by Maven