|
|
|
@ -18,6 +18,9 @@ package org.springframework.http.converter.json; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
@ -28,17 +31,22 @@ import org.springframework.http.MockHttpInputMessage; |
|
|
|
import org.springframework.http.MockHttpOutputMessage; |
|
|
|
import org.springframework.http.MockHttpOutputMessage; |
|
|
|
|
|
|
|
|
|
|
|
/** @author Arjen Poutsma */ |
|
|
|
/** @author Arjen Poutsma */ |
|
|
|
public class JacksonHttpMessageConverterTest { |
|
|
|
public class BindingJacksonHttpMessageConverterTest { |
|
|
|
|
|
|
|
|
|
|
|
private JacksonHttpMessageConverter<MyBean> converter; |
|
|
|
private BindingJacksonHttpMessageConverter<MyBean> converter; |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() { |
|
|
|
public void setUp() { |
|
|
|
converter = new JacksonHttpMessageConverter<MyBean>(); |
|
|
|
converter = new BindingJacksonHttpMessageConverter<MyBean>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void supports() { |
|
|
|
|
|
|
|
assertTrue(converter.supports(MyBean.class)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void read() throws IOException { |
|
|
|
public void readTyped() throws IOException { |
|
|
|
String body = |
|
|
|
String body = |
|
|
|
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}"; |
|
|
|
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}"; |
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
@ -52,6 +60,26 @@ public class JacksonHttpMessageConverterTest { |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
assertArrayEquals(new byte[]{0x1, 0x2}, result.getBytes()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked"}) |
|
|
|
|
|
|
|
public void readUntyped() throws IOException { |
|
|
|
|
|
|
|
BindingJacksonHttpMessageConverter<HashMap> converter = new BindingJacksonHttpMessageConverter<HashMap>(); |
|
|
|
|
|
|
|
String body = |
|
|
|
|
|
|
|
"{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}"; |
|
|
|
|
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
|
|
|
|
inputMessage.getHeaders().setContentType(new MediaType("application", "json")); |
|
|
|
|
|
|
|
HashMap<String,Object> result = converter.read(HashMap.class, inputMessage); |
|
|
|
|
|
|
|
assertEquals("Foo", result.get("string")); |
|
|
|
|
|
|
|
assertEquals(42, result.get("number")); |
|
|
|
|
|
|
|
assertEquals(42D, (Double)result.get("fraction"), 0D); |
|
|
|
|
|
|
|
List array = new ArrayList(); |
|
|
|
|
|
|
|
array.add("Foo"); |
|
|
|
|
|
|
|
array.add("Bar"); |
|
|
|
|
|
|
|
assertEquals(array, result.get("array")); |
|
|
|
|
|
|
|
assertEquals(Boolean.TRUE, result.get("bool")); |
|
|
|
|
|
|
|
assertEquals("AQI=", result.get("bytes")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void write() throws IOException { |
|
|
|
public void write() throws IOException { |
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |