1 /* 2 * Copyright 2002-2012 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.jms.support.converter; 18 19 import java.util.Collections; 20 import java.util.Date; 21 import java.util.HashMap; 22 import java.util.Map; 23 import javax.jms.BytesMessage; 24 import javax.jms.Session; 25 import javax.jms.TextMessage; 26 27 import org.easymock.Capture; 28 import org.easymock.EasyMock; 29 import org.junit.Before; 30 import org.junit.Test; 31 32 import static org.easymock.EasyMock.*; 33 import static org.junit.Assert.*; 34 35 /** 36 * @author Arjen Poutsma 37 * @author Dave Syer 38 */ 39 public class MappingJackson2MessageConverterTests { 40 41 private MappingJackson2MessageConverter converter; 42 43 private Session sessionMock; 44 45 @Before setUp()46 public void setUp() throws Exception { 47 sessionMock = createMock(Session.class); 48 converter = new MappingJackson2MessageConverter(); 49 converter.setEncodingPropertyName("__encoding__"); 50 converter.setTypeIdPropertyName("__typeid__"); 51 } 52 53 @Test toBytesMessage()54 public void toBytesMessage() throws Exception { 55 BytesMessage bytesMessageMock = createMock(BytesMessage.class); 56 Date toBeMarshalled = new Date(); 57 58 expect(sessionMock.createBytesMessage()).andReturn(bytesMessageMock); 59 bytesMessageMock.setStringProperty("__encoding__", "UTF-8"); 60 bytesMessageMock.setStringProperty("__typeid__", Date.class.getName()); 61 bytesMessageMock.writeBytes(isA(byte[].class)); 62 63 replay(sessionMock, bytesMessageMock); 64 65 converter.toMessage(toBeMarshalled, sessionMock); 66 67 verify(sessionMock, bytesMessageMock); 68 } 69 70 @Test fromBytesMessage()71 public void fromBytesMessage() throws Exception { 72 BytesMessage bytesMessageMock = createMock(BytesMessage.class); 73 Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); 74 75 final byte[] bytes = "{\"foo\":\"bar\"}".getBytes(); 76 Capture<byte[]> captured = new Capture<byte[]>() { 77 @Override 78 public void setValue(byte[] value) { 79 super.setValue(value); 80 System.arraycopy(bytes, 0, value, 0, bytes.length); 81 } 82 }; 83 84 expect( 85 bytesMessageMock.getStringProperty("__typeid__")) 86 .andReturn(Object.class.getName()); 87 expect( 88 bytesMessageMock.propertyExists("__encoding__")) 89 .andReturn(false); 90 expect(bytesMessageMock.getBodyLength()).andReturn( 91 new Long(bytes.length)); 92 expect(bytesMessageMock.readBytes(EasyMock.capture(captured))) 93 .andReturn(bytes.length); 94 95 replay(sessionMock, bytesMessageMock); 96 97 Object result = converter.fromMessage(bytesMessageMock); 98 assertEquals("Invalid result", result, unmarshalled); 99 100 verify(sessionMock, bytesMessageMock); 101 } 102 103 @Test toTextMessageWithObject()104 public void toTextMessageWithObject() throws Exception { 105 converter.setTargetType(MessageType.TEXT); 106 TextMessage textMessageMock = createMock(TextMessage.class); 107 Date toBeMarshalled = new Date(); 108 109 textMessageMock.setStringProperty("__typeid__", Date.class.getName()); 110 expect(sessionMock.createTextMessage(isA(String.class))).andReturn( textMessageMock); 111 112 replay(sessionMock, textMessageMock); 113 114 converter.toMessage(toBeMarshalled, sessionMock); 115 116 verify(sessionMock, textMessageMock); 117 } 118 119 @Test toTextMessageWithMap()120 public void toTextMessageWithMap() throws Exception { 121 converter.setTargetType(MessageType.TEXT); 122 TextMessage textMessageMock = createMock(TextMessage.class); 123 Map<String, String> toBeMarshalled = new HashMap<String, String>(); 124 toBeMarshalled.put("foo", "bar"); 125 126 textMessageMock.setStringProperty("__typeid__", HashMap.class.getName()); 127 expect(sessionMock.createTextMessage(isA(String.class))).andReturn( 128 textMessageMock); 129 130 replay(sessionMock, textMessageMock); 131 132 converter.toMessage(toBeMarshalled, sessionMock); 133 134 verify(sessionMock, textMessageMock); 135 } 136 137 @Test fromTextMessageAsObject()138 public void fromTextMessageAsObject() throws Exception { 139 TextMessage textMessageMock = createMock(TextMessage.class); 140 Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); 141 142 String text = "{\"foo\":\"bar\"}"; 143 expect( 144 textMessageMock.getStringProperty("__typeid__")) 145 .andReturn(Object.class.getName()); 146 expect(textMessageMock.getText()).andReturn(text); 147 148 replay(sessionMock, textMessageMock); 149 150 Object result = converter.fromMessage(textMessageMock); 151 assertEquals("Invalid result", result, unmarshalled); 152 153 verify(sessionMock, textMessageMock); 154 } 155 156 @Test fromTextMessageAsMap()157 public void fromTextMessageAsMap() throws Exception { 158 TextMessage textMessageMock = createMock(TextMessage.class); 159 Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); 160 161 String text = "{\"foo\":\"bar\"}"; 162 expect( 163 textMessageMock.getStringProperty("__typeid__")) 164 .andReturn(HashMap.class.getName()); 165 expect(textMessageMock.getText()).andReturn(text); 166 167 replay(sessionMock, textMessageMock); 168 169 Object result = converter.fromMessage(textMessageMock); 170 assertEquals("Invalid result", result, unmarshalled); 171 172 verify(sessionMock, textMessageMock); 173 } 174 175 } 176