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