1 16 17 package org.springframework.jms.support; 18 19 import java.util.Arrays ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.jms.BytesMessage ; 25 import javax.jms.JMSException ; 26 import javax.jms.MapMessage ; 27 import javax.jms.Message ; 28 import javax.jms.ObjectMessage ; 29 import javax.jms.Session ; 30 import javax.jms.TextMessage ; 31 32 import junit.framework.TestCase; 33 import org.easymock.ArgumentsMatcher; 34 import org.easymock.MockControl; 35 36 import org.springframework.jms.support.converter.SimpleMessageConverter; 37 import org.springframework.jms.support.converter.SimpleMessageConverter102; 38 39 43 public class SimpleMessageConverterTests extends TestCase { 44 45 public void testStringConversion() throws JMSException { 46 MockControl sessionControl = MockControl.createControl(Session .class); 47 Session session = (Session ) sessionControl.getMock(); 48 MockControl messageControl = MockControl.createControl(TextMessage .class); 49 TextMessage message = (TextMessage ) messageControl.getMock(); 50 51 String content = "test"; 52 53 session.createTextMessage(content); 54 sessionControl.setReturnValue(message, 1); 55 message.getText(); 56 messageControl.setReturnValue(content, 1); 57 sessionControl.replay(); 58 messageControl.replay(); 59 60 SimpleMessageConverter converter = new SimpleMessageConverter(); 61 Message msg = converter.toMessage(content, session); 62 assertEquals(content, converter.fromMessage(msg)); 63 64 sessionControl.verify(); 65 messageControl.verify(); 66 } 67 68 public void testByteArrayConversion() throws JMSException { 69 MockControl sessionControl = MockControl.createControl(Session .class); 70 Session session = (Session ) sessionControl.getMock(); 71 MockControl messageControl = MockControl.createControl(BytesMessage .class); 72 BytesMessage message = (BytesMessage ) messageControl.getMock(); 73 74 byte[] content = "test".getBytes(); 75 76 session.createBytesMessage(); 77 sessionControl.setReturnValue(message, 1); 78 message.writeBytes(content); 79 messageControl.setVoidCallable(1); 80 message.getBodyLength(); 81 messageControl.setReturnValue(content.length, 1); 82 message.readBytes(new byte[content.length]); 83 messageControl.setMatcher(new ArgumentsMatcher() { 84 public boolean matches(Object [] arg0, Object [] arg1) { 85 byte[] one = (byte[]) arg0[0]; 86 byte[] two = (byte[]) arg1[0]; 87 return Arrays.equals(one, two); 88 } 89 public String toString(Object [] arg0) { 90 return "bla"; 91 } 92 }); 93 messageControl.setReturnValue(content.length, 1); 94 sessionControl.replay(); 95 messageControl.replay(); 96 97 SimpleMessageConverter converter = new SimpleMessageConverter(); 98 Message msg = converter.toMessage(content, session); 99 assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length); 100 101 sessionControl.verify(); 102 messageControl.verify(); 103 } 104 105 public void testMapConversion(MapMessage message) throws JMSException { 106 MockControl sessionControl = MockControl.createControl(Session .class); 107 Session session = (Session ) sessionControl.getMock(); 108 MockControl messageControl = MockControl.createControl(MapMessage .class); 109 110 Map content = new HashMap (); 111 content.put("key1", "value1"); 112 content.put("key2", "value2"); 113 114 session.createMapMessage(); 115 sessionControl.setReturnValue(message, 1); 116 message.setObject("key1", "value1"); 117 messageControl.setVoidCallable(1); 118 message.setObject("key2", "value2"); 119 messageControl.setVoidCallable(1); 120 message.getMapNames(); 121 messageControl.setReturnValue(Collections.enumeration(content.keySet())); 122 message.getObject("key1"); 123 messageControl.setReturnValue("value1", 1); 124 message.getObject("key2"); 125 messageControl.setReturnValue("value2", 1); 126 sessionControl.replay(); 127 messageControl.replay(); 128 129 SimpleMessageConverter converter = new SimpleMessageConverter(); 130 Message msg = converter.toMessage(content, session); 131 assertEquals(content, converter.fromMessage(msg)); 132 133 sessionControl.verify(); 134 messageControl.verify(); 135 } 136 137 public void testSerializableConversion() throws JMSException { 138 MockControl sessionControl = MockControl.createControl(Session .class); 139 Session session = (Session ) sessionControl.getMock(); 140 MockControl messageControl = MockControl.createControl(ObjectMessage .class); 141 ObjectMessage message = (ObjectMessage ) messageControl.getMock(); 142 143 Integer content = new Integer (5); 144 145 session.createObjectMessage(content); 146 sessionControl.setReturnValue(message, 1); 147 message.getObject(); 148 messageControl.setReturnValue(content, 1); 149 sessionControl.replay(); 150 messageControl.replay(); 151 152 SimpleMessageConverter converter = new SimpleMessageConverter(); 153 Message msg = converter.toMessage(content, session); 154 assertEquals(content, converter.fromMessage(msg)); 155 156 sessionControl.verify(); 157 messageControl.verify(); 158 } 159 160 public void testByteArrayConversion102() throws JMSException { 161 MockControl sessionControl = MockControl.createControl(Session .class); 162 Session session = (Session ) sessionControl.getMock(); 163 MockControl messageControl = MockControl.createControl(BytesMessage .class); 164 BytesMessage message = (BytesMessage ) messageControl.getMock(); 165 166 byte[] content = new byte[5000]; 167 168 session.createBytesMessage(); 169 sessionControl.setReturnValue(message, 1); 170 message.writeBytes(content); 171 messageControl.setVoidCallable(1); 172 message.readBytes(new byte[SimpleMessageConverter102.BUFFER_SIZE]); 173 messageControl.setMatcher(new ArgumentsMatcher() { 174 public boolean matches(Object [] arg0, Object [] arg1) { 175 byte[] one = (byte[]) arg0[0]; 176 byte[] two = (byte[]) arg1[0]; 177 return Arrays.equals(one, two); 178 } 179 public String toString(Object [] arg0) { 180 return "bla"; 181 } 182 }); 183 messageControl.setReturnValue(SimpleMessageConverter102.BUFFER_SIZE, 1); 184 message.readBytes(new byte[SimpleMessageConverter102.BUFFER_SIZE]); 185 messageControl.setReturnValue(5000 - SimpleMessageConverter102.BUFFER_SIZE, 1); 186 sessionControl.replay(); 187 messageControl.replay(); 188 189 SimpleMessageConverter102 converter = new SimpleMessageConverter102(); 190 Message msg = converter.toMessage(content, session); 191 assertEquals(content.length, ((byte[]) converter.fromMessage(msg)).length); 192 193 sessionControl.verify(); 194 messageControl.verify(); 195 } 196 197 } 198 | Popular Tags |