|
|
|
@ -16,36 +16,60 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.http.converter.xml; |
|
|
|
package org.springframework.http.converter.xml; |
|
|
|
|
|
|
|
|
|
|
|
import javax.xml.transform.stream.StreamResult; |
|
|
|
import javax.xml.transform.Result; |
|
|
|
import javax.xml.transform.stream.StreamSource; |
|
|
|
import javax.xml.transform.stream.StreamSource; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
import org.springframework.beans.TypeMismatchException; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MockHttpInputMessage; |
|
|
|
import org.springframework.http.MockHttpInputMessage; |
|
|
|
import org.springframework.http.MockHttpOutputMessage; |
|
|
|
import org.springframework.http.MockHttpOutputMessage; |
|
|
|
|
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException; |
|
|
|
|
|
|
|
import org.springframework.http.converter.HttpMessageNotWritableException; |
|
|
|
import org.springframework.oxm.Marshaller; |
|
|
|
import org.springframework.oxm.Marshaller; |
|
|
|
|
|
|
|
import org.springframework.oxm.MarshallingFailureException; |
|
|
|
import org.springframework.oxm.Unmarshaller; |
|
|
|
import org.springframework.oxm.Unmarshaller; |
|
|
|
|
|
|
|
import org.springframework.oxm.UnmarshallingFailureException; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
import static org.mockito.Matchers.*; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* Tests for {@link MarshallingHttpMessageConverter}. |
|
|
|
|
|
|
|
* |
|
|
|
* @author Arjen Poutsma |
|
|
|
* @author Arjen Poutsma |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class MarshallingHttpMessageConverterTests { |
|
|
|
public class MarshallingHttpMessageConverterTests { |
|
|
|
|
|
|
|
|
|
|
|
private MarshallingHttpMessageConverter converter; |
|
|
|
@Test |
|
|
|
|
|
|
|
public void canRead() throws Exception { |
|
|
|
|
|
|
|
Unmarshaller unmarshaller = mock(Unmarshaller.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(unmarshaller.supports(Integer.class)).thenReturn(false); |
|
|
|
|
|
|
|
when(unmarshaller.supports(String.class)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); |
|
|
|
|
|
|
|
converter.setUnmarshaller(unmarshaller); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertFalse(converter.canRead(Boolean.class, MediaType.TEXT_PLAIN)); |
|
|
|
|
|
|
|
assertFalse(converter.canRead(Integer.class, MediaType.TEXT_XML)); |
|
|
|
|
|
|
|
assertTrue(converter.canRead(String.class, MediaType.TEXT_XML)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void canWrite() throws Exception { |
|
|
|
|
|
|
|
Marshaller marshaller = mock(Marshaller.class); |
|
|
|
|
|
|
|
|
|
|
|
private Marshaller marshaller; |
|
|
|
when(marshaller.supports(Integer.class)).thenReturn(false); |
|
|
|
|
|
|
|
when(marshaller.supports(String.class)).thenReturn(true); |
|
|
|
|
|
|
|
|
|
|
|
private Unmarshaller unmarshaller; |
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); |
|
|
|
|
|
|
|
converter.setMarshaller(marshaller); |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
assertFalse(converter.canWrite(Boolean.class, MediaType.TEXT_PLAIN)); |
|
|
|
public void setUp() { |
|
|
|
assertFalse(converter.canWrite(Integer.class, MediaType.TEXT_XML)); |
|
|
|
marshaller = mock(Marshaller.class); |
|
|
|
assertTrue(converter.canWrite(String.class, MediaType.TEXT_XML)); |
|
|
|
unmarshaller = mock(Unmarshaller.class); |
|
|
|
|
|
|
|
converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -53,20 +77,84 @@ public class MarshallingHttpMessageConverterTests { |
|
|
|
String body = "<root>Hello World</root>"; |
|
|
|
String body = "<root>Hello World</root>"; |
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); |
|
|
|
|
|
|
|
|
|
|
|
given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); |
|
|
|
Unmarshaller unmarshaller = mock(Unmarshaller.class); |
|
|
|
|
|
|
|
when(unmarshaller.unmarshal(isA(StreamSource.class))).thenReturn(body); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); |
|
|
|
|
|
|
|
converter.setUnmarshaller(unmarshaller); |
|
|
|
|
|
|
|
|
|
|
|
String result = (String) converter.read(Object.class, inputMessage); |
|
|
|
String result = (String) converter.read(Object.class, inputMessage); |
|
|
|
assertEquals("Invalid result", body, result); |
|
|
|
assertEquals("Invalid result", body, result); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = TypeMismatchException.class) |
|
|
|
|
|
|
|
public void readWithTypeMismatchException() throws Exception { |
|
|
|
|
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Marshaller marshaller = mock(Marshaller.class); |
|
|
|
|
|
|
|
Unmarshaller unmarshaller = mock(Unmarshaller.class); |
|
|
|
|
|
|
|
when(unmarshaller.unmarshal(isA(StreamSource.class))).thenReturn(Integer.valueOf(3)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller); |
|
|
|
|
|
|
|
converter.read(String.class, inputMessage); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void readWithMarshallingFailureException() throws Exception { |
|
|
|
|
|
|
|
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); |
|
|
|
|
|
|
|
UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Unmarshaller unmarshaller = mock(Unmarshaller.class); |
|
|
|
|
|
|
|
when(unmarshaller.unmarshal(isA(StreamSource.class))).thenThrow(ex); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); |
|
|
|
|
|
|
|
converter.setUnmarshaller(unmarshaller); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
converter.read(Object.class, inputMessage); |
|
|
|
|
|
|
|
fail("HttpMessageNotReadableException should be thrown"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (HttpMessageNotReadableException e) { |
|
|
|
|
|
|
|
assertTrue("Invalid exception hierarchy", e.getCause() == ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void write() throws Exception { |
|
|
|
public void write() throws Exception { |
|
|
|
String body = "<root>Hello World</root>"; |
|
|
|
String body = "<root>Hello World</root>"; |
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Marshaller marshaller = mock(Marshaller.class); |
|
|
|
|
|
|
|
doNothing().when(marshaller).marshal(eq(body), isA(Result.class)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); |
|
|
|
converter.write(body, null, outputMessage); |
|
|
|
converter.write(body, null, outputMessage); |
|
|
|
assertEquals("Invalid content-type", new MediaType("application", "xml"), |
|
|
|
|
|
|
|
outputMessage.getHeaders().getContentType()); |
|
|
|
assertEquals("Invalid content-type", new MediaType("application", "xml"), outputMessage.getHeaders() |
|
|
|
verify(marshaller).marshal(eq(body), isA(StreamResult.class)); |
|
|
|
.getContentType()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void writeWithMarshallingFailureException() throws Exception { |
|
|
|
|
|
|
|
String body = "<root>Hello World</root>"; |
|
|
|
|
|
|
|
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); |
|
|
|
|
|
|
|
MarshallingFailureException ex = new MarshallingFailureException("forced"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Marshaller marshaller = mock(Marshaller.class); |
|
|
|
|
|
|
|
doThrow(ex).when(marshaller).marshal(eq(body), isA(Result.class)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); |
|
|
|
|
|
|
|
converter.write(body, null, outputMessage); |
|
|
|
|
|
|
|
fail("HttpMessageNotWritableException should be thrown"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (HttpMessageNotWritableException e) { |
|
|
|
|
|
|
|
assertTrue("Invalid exception hierarchy", e.getCause() == ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = UnsupportedOperationException.class) |
|
|
|
|
|
|
|
public void supports() throws Exception { |
|
|
|
|
|
|
|
new MarshallingHttpMessageConverter().supports(Object.class); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|