1 /*
2  * Copyright 2002-2011 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.HashMap;
21 import java.util.Map;
22 import javax.jms.BytesMessage;
23 import javax.jms.Session;
24 import javax.jms.TextMessage;
25 
26 import org.easymock.Capture;
27 import org.easymock.EasyMock;
28 import org.junit.Before;
29 import org.junit.Test;
30 
31 import static org.easymock.EasyMock.*;
32 import static org.junit.Assert.*;
33 
34 /**
35  * @author Arjen Poutsma
36  * @author Dave Syer
37  */
38 public class MappingJacksonMessageConverterTests {
39 
40 	private MappingJacksonMessageConverter converter;
41 
42 	private Session sessionMock;
43 
44 	@Before
setUp()45 	public void setUp() throws Exception {
46 		sessionMock = createMock(Session.class);
47 		converter = new MappingJacksonMessageConverter();
48 		converter.setEncodingPropertyName("__encoding__");
49 		converter.setTypeIdPropertyName("__typeid__");
50 	}
51 
52 	@Test
toBytesMessage()53 	public void toBytesMessage() throws Exception {
54 		BytesMessage bytesMessageMock = createMock(BytesMessage.class);
55 		Object toBeMarshalled = new Object();
56 
57 		expect(sessionMock.createBytesMessage()).andReturn(bytesMessageMock);
58 		bytesMessageMock.setStringProperty("__encoding__", "UTF-8");
59 		bytesMessageMock.setStringProperty("__typeid__", Object.class.getName());
60 		bytesMessageMock.writeBytes(isA(byte[].class));
61 
62 		replay(sessionMock, bytesMessageMock);
63 
64 		converter.toMessage(toBeMarshalled, sessionMock);
65 
66 		verify(sessionMock, bytesMessageMock);
67 	}
68 
69 	@Test
fromBytesMessage()70 	public void fromBytesMessage() throws Exception {
71 		BytesMessage bytesMessageMock = createMock(BytesMessage.class);
72 		Map<String, String> unmarshalled = Collections.singletonMap("foo",
73 				"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 		Object toBeMarshalled = new Object();
108 
109 		textMessageMock.setStringProperty("__typeid__", Object.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",
141 				"bar");
142 
143 		String text = "{\"foo\":\"bar\"}";
144 		expect(
145 				textMessageMock.getStringProperty("__typeid__"))
146 				.andReturn(Object.class.getName());
147 		expect(textMessageMock.getText()).andReturn(text);
148 
149 		replay(sessionMock, textMessageMock);
150 
151 		Object result = converter.fromMessage(textMessageMock);
152 		assertEquals("Invalid result", result, unmarshalled);
153 
154 		verify(sessionMock, textMessageMock);
155 	}
156 
157 	@Test
fromTextMessageAsMap()158 	public void fromTextMessageAsMap() throws Exception {
159 		TextMessage textMessageMock = createMock(TextMessage.class);
160 		Map<String, String> unmarshalled = Collections.singletonMap("foo",
161 				"bar");
162 
163 		String text = "{\"foo\":\"bar\"}";
164 		expect(
165 				textMessageMock.getStringProperty("__typeid__"))
166 				.andReturn(HashMap.class.getName());
167 		expect(textMessageMock.getText()).andReturn(text);
168 
169 		replay(sessionMock, textMessageMock);
170 
171 		Object result = converter.fromMessage(textMessageMock);
172 		assertEquals("Invalid result", result, unmarshalled);
173 
174 		verify(sessionMock, textMessageMock);
175 	}
176 
177 }
178