diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/UnmarshallingTransformer.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/UnmarshallingTransformer.java index 15b3338e58..841c4d8c6b 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/UnmarshallingTransformer.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/UnmarshallingTransformer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ package org.springframework.integration.xml.transformer; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import javax.xml.transform.Source; import javax.xml.transform.dom.DOMSource; @@ -35,11 +37,11 @@ import org.springframework.util.Assert; import org.springframework.xml.transform.StringSource; /** - * An implementation of {@link Transformer} that delegates to an OXM - * {@link Unmarshaller}. Expects the payload to be of type {@link Document}, - * {@link String}, {@link File}, {@link Source} or to have an instance of - * {@link SourceFactory} that can convert to a {@link Source}. If - * alwaysUseSourceFactory is set to true, then the {@link SourceFactory} + * An implementation of {@link org.springframework.integration.transformer.Transformer} + * that delegates to an OXM {@link Unmarshaller}. + * Expects the payload to be of type {@link Document}, {@link String}, {@link File}, {@link Source} + * or to have an instance of {@link SourceFactory} that can convert to a {@link Source}. + * If {@link #alwaysUseSourceFactory} is set to true, then the {@link SourceFactory} * will be used to create the {@link Source} regardless of payload type. *

* The Unmarshaller may return a Message, but if the return value is not @@ -88,35 +90,50 @@ public class UnmarshallingTransformer extends AbstractPayloadTransformer(person)); - GenericMessage res = (GenericMessage) marshalledOut.receive(2000); - assertNotNull("No response recevied", res); - assertTrue("payload was not a DOMResult", res.getPayload() instanceof DOMResult); + this.marshallIn.send(new GenericMessage<>(person)); + Message res = this.marshalledOut.receive(2000); + assertNotNull(res); + assertTrue(res.getPayload() instanceof DOMResult); Document doc = (Document) ((DOMResult) res.getPayload()).getNode(); assertEquals("Wrong name for root element ", "person", doc.getDocumentElement().getLocalName()); } - @SuppressWarnings("unchecked") @Test - public void testUnmarshalling() throws Exception { + public void testUnmarshalling() { StringSource source = new StringSource("bob"); - unmarshallIn.send(new GenericMessage(source)); - GenericMessage res = (GenericMessage) unmarshallOut.receive(2000); + this.unmarshallIn.send(new GenericMessage(source)); + Message res = this.unmarshallOut.receive(2000); assertNotNull("No response", res); assertTrue("Not a Person ", res.getPayload() instanceof JaxbAnnotatedPerson); JaxbAnnotatedPerson person = (JaxbAnnotatedPerson) res.getPayload(); - assertEquals("Worng firstname", "bob", person.getFirstName()); - + assertEquals("bob", person.getFirstName()); } + @Test + public void testFileUnlockedAfterUnmarshallingFailure() throws IOException { + File tempFile = tempDirectory.newFile(); + FileWriter myWriter = new FileWriter(tempFile); + myWriter.write("junk"); + myWriter.close(); + + try { + this.unmarshallIn.send(new GenericMessage<>(tempFile)); + } + catch (MessageTransformationException ex) { + assertTrue(ex.getCause() instanceof UnmarshallingFailureException); + assertTrue(ex.getMessage().contains("Content is not allowed in prolog.")); + } + + assertTrue(tempFile.delete()); + } }