org.springframework.oxm.Unmarshaller Java Examples

The following examples show how to use org.springframework.oxm.Unmarshaller. 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
Source File: MarshallingHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: MarshallingHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
Source File: MarshallingHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #4
Source File: MarshallingHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #5
Source File: MarshallingHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 File: MarshallingMessageConverter.java    From java-technology-stack with 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 #7
Source File: MarshallingMessageConverter.java    From java-technology-stack with 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 #8
Source File: MarshallingHttpMessageConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: MarshallingHttpMessageConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #10
Source File: MarshallingMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example #11
Source File: MarshallingHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #12
Source File: MarshallingHttpMessageConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #13
Source File: MarshallingHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #14
Source File: MarshallingHttpMessageConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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
Source File: MarshallingHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #16
Source File: MarshallingHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #17
Source File: MarshallingMessageConverter.java    From spring4-understanding with Apache License 2.0 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 #18
Source File: MarshallingMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example #19
Source File: MarshallingMessageConverter.java    From spring-analysis-note with 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 #20
Source File: AmazonProductService.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #21
Source File: MarshallingHttpMessageConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 #22
Source File: MarshallingHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #23
Source File: MarshallingHttpMessageConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #24
Source File: MarshallingMessageConverter.java    From spring-analysis-note with 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 #25
Source File: MarshallingMessageConverter.java    From spring-analysis-note with 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 #26
Source File: MarshallingMessageConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	marshallerMock = mock(Marshaller.class);
	unmarshallerMock = mock(Unmarshaller.class);
	sessionMock = mock(Session.class);
	converter = new MarshallingMessageConverter(marshallerMock, unmarshallerMock);
}
 
Example #27
Source File: MarshallingMessageConverter.java    From spring4-understanding with Apache License 2.0 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 #28
Source File: MarshallingMessageConverter.java    From spring4-understanding with Apache License 2.0 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 #29
Source File: MarshallingMessageConverter.java    From java-technology-stack with 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 #30
Source File: MarshallingMessageConverter.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Set the {@link Unmarshaller} to be used by this message converter.
 */
public void setUnmarshaller(Unmarshaller unmarshaller) {
	this.unmarshaller = unmarshaller;
}