Java Code Examples for org.springframework.oxm.Unmarshaller

The following examples show how to use org.springframework.oxm.Unmarshaller. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void readWithTypeMismatchException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);

	Marshaller marshaller = mock(Marshaller.class);
	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3));

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller);
	try {
		converter.read(String.class, inputMessage);
		fail("Should have thrown HttpMessageNotReadableException");
	}
	catch (HttpMessageNotReadableException ex) {
		assertTrue(ex.getCause() instanceof TypeMismatchException);
	}
}
 
Example 2
@Test
public void readWithMarshallingFailureException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
	UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(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);
	}
}
 
Example 3
@Test
public void readWithTypeMismatchException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);

	Marshaller marshaller = mock(Marshaller.class);
	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3));

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller);
	try {
		converter.read(String.class, inputMessage);
		fail("Should have thrown HttpMessageNotReadableException");
	}
	catch (HttpMessageNotReadableException ex) {
		assertTrue(ex.getCause() instanceof TypeMismatchException);
	}
}
 
Example 4
@Test
public void readWithMarshallingFailureException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
	UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(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);
	}
}
 
Example 5
@Test
public void readWithMarshallingFailureException() throws Exception {
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
	UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(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);
	}
}
 
Example 6
Source Project: spring-analysis-note   Source File: MarshallingMessageConverter.java    License: MIT License 5 votes vote down vote up
/**
 * Constructor with {@link Marshaller}. If the given {@link Marshaller} also
 * implements {@link Unmarshaller}, it is also used for unmarshalling.
 * <p>Note that all {@code Marshaller} implementations in Spring also implement
 * {@code Unmarshaller} so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	this();
	Assert.notNull(marshaller, "Marshaller must not be null");
	this.marshaller = marshaller;
	if (marshaller instanceof Unmarshaller) {
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 7
/**
 * Construct a new {@code MarshallingMessageConverter} with the given
 * {@code Marshaller} and {@code Unmarshaller}.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 8
@Test
public void canRead() {
	Unmarshaller unmarshaller = mock(Unmarshaller.class);

	given(unmarshaller.supports(Integer.class)).willReturn(false);
	given(unmarshaller.supports(String.class)).willReturn(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));
}
 
Example 9
@Test
public void read() throws Exception {
	String body = "<root>Hello World</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
	converter.setUnmarshaller(unmarshaller);

	String result = (String) converter.read(Object.class, inputMessage);
	assertEquals("Invalid result", body, result);
}
 
Example 10
Source Project: spring-analysis-note   Source File: MarshallingMessageConverter.java    License: MIT License 5 votes vote down vote up
/**
 * Construct a new {@code MarshallingMessageConverter} with the given {@link Marshaller} set.
 * <p>If the given {@link Marshaller} also implements the {@link Unmarshaller} interface,
 * it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.
 * <p>Note that all {@link Marshaller} implementations in Spring also implement the
 * {@link Unmarshaller} interface, so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 * @throws IllegalArgumentException when {@code marshaller} does not implement the
 * {@link Unmarshaller} interface as well
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	if (!(marshaller instanceof Unmarshaller)) {
		throw new IllegalArgumentException(
				"Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
				"interface. Please set an Unmarshaller explicitly by using the " +
				"MarshallingMessageConverter(Marshaller, Unmarshaller) constructor.");
	}
	else {
		this.marshaller = marshaller;
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 11
Source Project: spring-analysis-note   Source File: MarshallingMessageConverter.java    License: MIT License 5 votes vote down vote up
/**
 * Construct a new {@code MarshallingMessageConverter} with the
 * given Marshaller and Unmarshaller.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 12
@Before
public void setup() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example 13
/**
 * Constructor with {@link Marshaller}. If the given {@link Marshaller} also
 * implements {@link Unmarshaller}, it is also used for unmarshalling.
 * <p>Note that all {@code Marshaller} implementations in Spring also implement
 * {@code Unmarshaller} so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	this();
	Assert.notNull(marshaller, "Marshaller must not be null");
	this.marshaller = marshaller;
	if (marshaller instanceof Unmarshaller) {
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 14
/**
 * Construct a new {@code MarshallingMessageConverter} with the given
 * {@code Marshaller} and {@code Unmarshaller}.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 15
@Test
public void canRead() {
	Unmarshaller unmarshaller = mock(Unmarshaller.class);

	given(unmarshaller.supports(Integer.class)).willReturn(false);
	given(unmarshaller.supports(String.class)).willReturn(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));
}
 
Example 16
@Test
public void read() throws Exception {
	String body = "<root>Hello World</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
	converter.setUnmarshaller(unmarshaller);

	String result = (String) converter.read(Object.class, inputMessage);
	assertEquals("Invalid result", body, result);
}
 
Example 17
/**
 * Construct a new {@code MarshallingMessageConverter} with the given {@link Marshaller} set.
 * <p>If the given {@link Marshaller} also implements the {@link Unmarshaller} interface,
 * it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.
 * <p>Note that all {@link Marshaller} implementations in Spring also implement the
 * {@link Unmarshaller} interface, so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 * @throws IllegalArgumentException when {@code marshaller} does not implement the
 * {@link Unmarshaller} interface as well
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	if (!(marshaller instanceof Unmarshaller)) {
		throw new IllegalArgumentException(
				"Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
				"interface. Please set an Unmarshaller explicitly by using the " +
				"MarshallingMessageConverter(Marshaller, Unmarshaller) constructor.");
	}
	else {
		this.marshaller = marshaller;
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 18
/**
 * Construct a new {@code MarshallingMessageConverter} with the
 * given Marshaller and Unmarshaller.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 19
@Before
public void setup() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example 20
/**
 * Construct a new {@code MarshallingMessageConverter} with the given
 * {@code Marshaller} and {@code Unmarshaller}.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 21
/**
 * Constructor with {@link Marshaller}. If the given {@link Marshaller} also
 * implements {@link Unmarshaller}, it is also used for unmarshalling.
 * <p>Note that all {@code Marshaller} implementations in Spring also implement
 * {@code Unmarshaller} so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	this();
	Assert.notNull(marshaller, "Marshaller must not be null");
	this.marshaller = marshaller;
	if (marshaller instanceof Unmarshaller) {
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 22
/**
 * Construct a new {@code MarshallingMessageConverter} with the given
 * {@code Marshaller} and {@code Unmarshaller}.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 23
@Test
public void canRead() throws Exception {
	Unmarshaller unmarshaller = mock(Unmarshaller.class);

	given(unmarshaller.supports(Integer.class)).willReturn(false);
	given(unmarshaller.supports(String.class)).willReturn(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));
}
 
Example 24
@Test
public void read() throws Exception {
	String body = "<root>Hello World</root>";
	MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));

	Unmarshaller unmarshaller = mock(Unmarshaller.class);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body);

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
	converter.setUnmarshaller(unmarshaller);

	String result = (String) converter.read(Object.class, inputMessage);
	assertEquals("Invalid result", body, result);
}
 
Example 25
@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);
	given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(Integer.valueOf(3));

	MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller, unmarshaller);
	converter.read(String.class, inputMessage);
}
 
Example 26
/**
 * Construct a new {@code MarshallingMessageConverter} with the given {@link Marshaller} set.
 * <p>If the given {@link Marshaller} also implements the {@link Unmarshaller} interface,
 * it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.
 * <p>Note that all {@link Marshaller} implementations in Spring also implement the
 * {@link Unmarshaller} interface, so that you can safely use this constructor.
 * @param marshaller object used as marshaller and unmarshaller
 * @throws IllegalArgumentException when {@code marshaller} does not implement the
 * {@link Unmarshaller} interface as well
 */
public MarshallingMessageConverter(Marshaller marshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	if (!(marshaller instanceof Unmarshaller)) {
		throw new IllegalArgumentException(
				"Marshaller [" + marshaller + "] does not implement the Unmarshaller " +
				"interface. Please set an Unmarshaller explicitly by using the " +
				"MarshallingMessageConverter(Marshaller, Unmarshaller) constructor.");
	}
	else {
		this.marshaller = marshaller;
		this.unmarshaller = (Unmarshaller) marshaller;
	}
}
 
Example 27
/**
 * Construct a new {@code MarshallingMessageConverter} with the
 * given Marshaller and Unmarshaller.
 * @param marshaller the Marshaller to use
 * @param unmarshaller the Unmarshaller to use
 */
public MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) {
	Assert.notNull(marshaller, "Marshaller must not be null");
	Assert.notNull(unmarshaller, "Unmarshaller must not be null");
	this.marshaller = marshaller;
	this.unmarshaller = unmarshaller;
}
 
Example 28
@Before
public void setUp() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example 29
private Unmarshaller createUnmarshaller() throws Exception {
   	CastorMarshaller unmarshaller = new CastorMarshaller();
   	unmarshaller.setMappingLocations(new Resource[] {new ClassPathResource("/castor/amazonProductSearchResponse.xml"), new ClassPathResource("/castor/amazonProduct.xml")});
   	unmarshaller.setIgnoreExtraElements(true);
   	unmarshaller.afterPropertiesSet();
	return unmarshaller;
}
 
Example 30
Source Project: spring-analysis-note   Source File: MarshallingMessageConverter.java    License: MIT License 4 votes vote down vote up
/**
 * Set the {@link Unmarshaller} to be used by this message converter.
 */
public void setUnmarshaller(@Nullable Unmarshaller unmarshaller) {
	this.unmarshaller = unmarshaller;
}