|
1 | 1 | /* |
2 | | - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
17 | 17 | package org.springframework.integration.xml.transformer.jaxbmarshaling; |
18 | 18 |
|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
20 | 25 |
|
21 | | -import javax.xml.transform.Result; |
22 | 26 | import javax.xml.transform.Source; |
23 | 27 | import javax.xml.transform.dom.DOMResult; |
24 | 28 |
|
25 | | -import org.junit.Test; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
26 | 31 | import org.w3c.dom.Document; |
27 | 32 |
|
28 | 33 | import org.springframework.beans.factory.annotation.Autowired; |
29 | 34 | import org.springframework.beans.factory.annotation.Qualifier; |
| 35 | +import org.springframework.integration.transformer.MessageTransformationException; |
| 36 | +import org.springframework.messaging.Message; |
30 | 37 | import org.springframework.messaging.MessageChannel; |
31 | 38 | import org.springframework.messaging.PollableChannel; |
32 | 39 | import org.springframework.messaging.support.GenericMessage; |
33 | | -import org.springframework.test.context.ContextConfiguration; |
34 | | -import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; |
| 40 | +import org.springframework.oxm.UnmarshallingFailureException; |
| 41 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
35 | 42 | import org.springframework.xml.transform.StringSource; |
36 | 43 |
|
37 | 44 | /** |
38 | 45 | * @author Jonas Partner |
| 46 | + * @author Artem Bilan |
39 | 47 | */ |
40 | | -@ContextConfiguration |
41 | | -public class JaxbMarshallingIntegrationTests extends AbstractJUnit4SpringContextTests { |
| 48 | +@SpringJUnitConfig |
| 49 | +public class JaxbMarshallingIntegrationTests { |
42 | 50 |
|
43 | | - @Autowired @Qualifier("marshallIn") |
| 51 | + @Autowired |
| 52 | + @Qualifier("marshallIn") |
44 | 53 | MessageChannel marshallIn; |
45 | 54 |
|
46 | | - @Autowired @Qualifier("marshallOut") |
| 55 | + @Autowired |
| 56 | + @Qualifier("marshallOut") |
47 | 57 | PollableChannel marshalledOut; |
48 | 58 |
|
49 | | - @Autowired @Qualifier("unmarshallIn") |
| 59 | + @Autowired |
| 60 | + @Qualifier("unmarshallIn") |
50 | 61 | MessageChannel unmarshallIn; |
51 | 62 |
|
52 | | - @Autowired @Qualifier("unmarshallOut") |
| 63 | + @Autowired |
| 64 | + @Qualifier("unmarshallOut") |
53 | 65 | PollableChannel unmarshallOut; |
54 | 66 |
|
| 67 | + @TempDir |
| 68 | + Path tempDirectory; |
55 | 69 |
|
56 | | - @SuppressWarnings("unchecked") |
57 | 70 | @Test |
58 | | - public void testMarshalling() throws Exception { |
| 71 | + public void testMarshalling() { |
59 | 72 | JaxbAnnotatedPerson person = new JaxbAnnotatedPerson(); |
60 | 73 | person.setFirstName("john"); |
61 | | - marshallIn.send(new GenericMessage<Object>(person)); |
62 | | - GenericMessage<Result> res = (GenericMessage<Result>) marshalledOut.receive(2000); |
63 | | - assertThat(res).as("No response recevied").isNotNull(); |
| 74 | + this.marshallIn.send(new GenericMessage<>(person)); |
| 75 | + Message<?> res = this.marshalledOut.receive(2000); |
| 76 | + assertThat(res).as("No response received").isNotNull(); |
64 | 77 | assertThat(res.getPayload() instanceof DOMResult).as("payload was not a DOMResult").isTrue(); |
65 | 78 | Document doc = (Document) ((DOMResult) res.getPayload()).getNode(); |
66 | 79 | assertThat(doc.getDocumentElement().getLocalName()).as("Wrong name for root element ").isEqualTo("person"); |
67 | 80 | } |
68 | 81 |
|
69 | 82 |
|
70 | | - @SuppressWarnings("unchecked") |
71 | 83 | @Test |
72 | | - public void testUnmarshalling() throws Exception { |
| 84 | + public void testUnmarshalling() { |
73 | 85 | StringSource source = new StringSource("<person><firstname>bob</firstname></person>"); |
74 | | - unmarshallIn.send(new GenericMessage<Source>(source)); |
75 | | - GenericMessage<Object> res = (GenericMessage<Object>) unmarshallOut.receive(2000); |
| 86 | + this.unmarshallIn.send(new GenericMessage<Source>(source)); |
| 87 | + Message<?> res = this.unmarshallOut.receive(2000); |
76 | 88 | assertThat(res).as("No response").isNotNull(); |
77 | 89 | assertThat(res.getPayload() instanceof JaxbAnnotatedPerson).as("Not a Person ").isTrue(); |
78 | 90 | JaxbAnnotatedPerson person = (JaxbAnnotatedPerson) res.getPayload(); |
79 | | - assertThat(person.getFirstName()).as("Worng firstname").isEqualTo("bob"); |
80 | | - |
| 91 | + assertThat(person.getFirstName()).as("Wrong firstname").isEqualTo("bob"); |
81 | 92 | } |
82 | 93 |
|
| 94 | + @Test |
| 95 | + public void testFileUnlockedAfterUnmarshallingFailure() throws IOException { |
| 96 | + Path tempFile = Files.createTempFile(this.tempDirectory, null, null); |
| 97 | + Files.write(tempFile, "junk".getBytes()); |
| 98 | + assertThatExceptionOfType(MessageTransformationException.class) |
| 99 | + .isThrownBy(() -> this.unmarshallIn.send(new GenericMessage<>(tempFile.toFile()))) |
| 100 | + .withCauseInstanceOf(UnmarshallingFailureException.class) |
| 101 | + .withStackTraceContaining("Content is not allowed in prolog."); |
| 102 | + |
| 103 | + Files.delete(tempFile); |
| 104 | + } |
83 | 105 |
|
84 | 106 | } |
0 commit comments