Skip to content

Commit 63937ab

Browse files
committed
Migrate XML module tests to JUnit 5
* Use Java text blocks for xml snippets
1 parent e39449b commit 63937ab

30 files changed

+808
-631
lines changed

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/ChainElementsTests.java

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.util.Properties;
2121

22-
import org.junit.Test;
22+
import org.junit.jupiter.api.Test;
2323

2424
import org.springframework.beans.factory.config.PropertiesFactoryBean;
2525
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
@@ -31,7 +31,7 @@
3131
import org.springframework.integration.xml.transformer.XPathTransformer;
3232

3333
import static org.assertj.core.api.Assertions.assertThat;
34-
import static org.assertj.core.api.Assertions.fail;
34+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3535

3636

3737
/**
@@ -43,21 +43,12 @@
4343
public class ChainElementsTests {
4444

4545
@Test
46-
public void chainXPathTransformer() throws Exception {
47-
try {
48-
bootStrap("xpath-transformer");
49-
fail("Expected a BeanDefinitionParsingException to be thrown.");
50-
}
51-
catch (BeanDefinitionParsingException e) {
52-
final String expectedMessage = "Configuration problem: " +
53-
"The 'input-channel' attribute isn't allowed for a nested " +
54-
"(e.g. inside a <chain/>) endpoint element: 'int-xml:xpath-transformer'.";
55-
final String actualMessage = e.getMessage();
56-
assertThat(actualMessage.startsWith(expectedMessage))
57-
.as("Error message did not start with '" + expectedMessage +
58-
"' but instead returned: '" + actualMessage + "'").isTrue();
59-
}
60-
46+
public void chainXPathTransformer() {
47+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
48+
.isThrownBy(() -> bootStrap("xpath-transformer"))
49+
.withMessageStartingWith("Configuration problem: " +
50+
"The 'input-channel' attribute isn't allowed for a nested " +
51+
"(e.g. inside a <chain/>) endpoint element: 'int-xml:xpath-transformer'.");
6152
}
6253

6354
@Test
@@ -68,37 +59,21 @@ public void chainXPathTransformerId() throws Exception {
6859
}
6960

7061
@Test
71-
public void chainXPathRouterOrder() throws Exception {
72-
try {
73-
bootStrap("xpath-router-order");
74-
fail("Expected a BeanDefinitionParsingException to be thrown.");
75-
}
76-
catch (BeanDefinitionParsingException e) {
77-
final String expectedMessage = "Configuration problem: " +
78-
"'int-xml:xpath-router' must not define an 'order' attribute " +
79-
"when used within a chain.";
80-
final String actualMessage = e.getMessage();
81-
assertThat(actualMessage.startsWith(expectedMessage))
82-
.as("Error message did not start with '" + expectedMessage +
83-
"' but instead returned: '" + actualMessage + "'").isTrue();
84-
}
85-
62+
public void chainXPathRouterOrder() {
63+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
64+
.isThrownBy(() -> bootStrap("xpath-router-order"))
65+
.withMessageStartingWith("Configuration problem: " +
66+
"'int-xml:xpath-router' must not define an 'order' attribute " +
67+
"when used within a chain.");
8668
}
8769

8870
@Test
89-
public void chainXPathTransformerPoller() throws Exception {
90-
try {
91-
bootStrap("xpath-transformer-poller");
92-
fail("Expected a BeanDefinitionParsingException to be thrown.");
93-
}
94-
catch (BeanDefinitionParsingException e) {
95-
final String expectedMessage = "Configuration problem: " +
96-
"'int-xml:xpath-transformer' must not define a 'poller' " +
97-
"sub-element when used within a chain.";
98-
final String actualMessage = e.getMessage();
99-
assertThat(actualMessage).as("Error message did not start with '" + expectedMessage +
100-
"' but instead returned: '" + actualMessage + "'").startsWith(expectedMessage);
101-
}
71+
public void chainXPathTransformerPoller() {
72+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
73+
.isThrownBy(() -> bootStrap("xpath-transformer-poller"))
74+
.withMessageStartingWith("Configuration problem: " +
75+
"'int-xml:xpath-transformer' must not define a 'poller' " +
76+
"sub-element when used within a chain.");
10277
}
10378

10479
@Test
@@ -114,11 +89,11 @@ private ConfigurableApplicationContext bootStrap(String configProperty) throws E
11489
"org/springframework/integration/xml/config/chain-elements-config.properties"));
11590
pfb.afterPropertiesSet();
11691
Properties prop = pfb.getObject();
117-
StringBuffer buffer = new StringBuffer();
118-
buffer.append(prop.getProperty("xmlheaders"))
119-
.append(prop.getProperty(configProperty))
120-
.append(prop.getProperty("xmlfooter"));
121-
ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toString().getBytes());
92+
String buffer =
93+
prop.getProperty("xmlheaders") +
94+
prop.getProperty(configProperty) +
95+
prop.getProperty("xmlfooter");
96+
ByteArrayInputStream stream = new ByteArrayInputStream(buffer.getBytes());
12297

12398
GenericApplicationContext ac = new GenericApplicationContext();
12499
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ac);

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/DefaultConfigurationTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.integration.xml.config;
1818

19-
import org.junit.Test;
20-
import org.junit.runner.RunWith;
19+
20+
import org.junit.jupiter.api.Test;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.context.ApplicationContext;
@@ -28,8 +28,7 @@
2828
import org.springframework.integration.test.util.TestUtils;
2929
import org.springframework.messaging.MessageChannel;
3030
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
31-
import org.springframework.test.context.ContextConfiguration;
32-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3332
import org.springframework.util.ErrorHandler;
3433

3534
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,10 +37,10 @@
3837
* @author Mark Fisher
3938
* @author Artem Bilan
4039
* @author Gary Russell
40+
*
4141
* @since 1.0.3
4242
*/
43-
@RunWith(SpringJUnit4ClassRunner.class)
44-
@ContextConfiguration
43+
@SpringJUnitConfig
4544
public class DefaultConfigurationTests {
4645

4746
@Autowired

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/MarshallingTransformerParserTests.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,13 +20,12 @@
2020

2121
import javax.xml.transform.dom.DOMResult;
2222

23-
import org.junit.Before;
24-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2524
import org.w3c.dom.Document;
2625

26+
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.context.ApplicationContext;
2828
import org.springframework.context.SmartLifecycle;
29-
import org.springframework.context.support.ClassPathXmlApplicationContext;
3029
import org.springframework.integration.endpoint.EventDrivenConsumer;
3130
import org.springframework.integration.support.SmartLifecycleRoleController;
3231
import org.springframework.integration.test.util.TestUtils;
@@ -35,6 +34,7 @@
3534
import org.springframework.messaging.MessageChannel;
3635
import org.springframework.messaging.PollableChannel;
3736
import org.springframework.messaging.support.GenericMessage;
37+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3838
import org.springframework.util.MultiValueMap;
3939
import org.springframework.xml.transform.StringResult;
4040

@@ -44,23 +44,19 @@
4444
* @author Jonas Partner
4545
* @author Mark Fisher
4646
* @author Gary Russell
47+
* @author Artem Bilan
4748
*/
49+
@SpringJUnitConfig
4850
public class MarshallingTransformerParserTests {
4951

52+
@Autowired
5053
private ApplicationContext appContext;
5154

55+
@Autowired
5256
private PollableChannel output;
5357

54-
55-
@Before
56-
public void setUp() {
57-
this.appContext = new ClassPathXmlApplicationContext("MarshallingTransformerParserTests-context.xml", getClass());
58-
this.output = (PollableChannel) appContext.getBean("output");
59-
}
60-
61-
6258
@Test
63-
public void testParse() throws Exception {
59+
public void testParse() {
6460
EventDrivenConsumer consumer = (EventDrivenConsumer) appContext.getBean("parseOnly");
6561
assertThat(TestUtils.getPropertyValue(consumer, "handler.order")).isEqualTo(2);
6662
assertThat(TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout")).isEqualTo(123L);
@@ -70,13 +66,13 @@ public void testParse() throws Exception {
7066
@SuppressWarnings("unchecked")
7167
List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles",
7268
MultiValueMap.class).get("foo");
73-
assertThat(list).containsExactly((SmartLifecycle) consumer);
69+
assertThat(list).containsExactly(consumer);
7470
}
7571

7672
@Test
77-
public void testDefault() throws Exception {
73+
public void testDefault() {
7874
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerNoResultFactory");
79-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
75+
GenericMessage<Object> message = new GenericMessage<>("hello");
8076
input.send(message);
8177
Message<?> result = output.receive(0);
8278
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type").isTrue();
@@ -85,9 +81,9 @@ public void testDefault() throws Exception {
8581
}
8682

8783
@Test
88-
public void testDefaultWithResultTransformer() throws Exception {
84+
public void testDefaultWithResultTransformer() {
8985
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerWithResultTransformer");
90-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
86+
GenericMessage<Object> message = new GenericMessage<>("hello");
9187
input.send(message);
9288
Message<?> result = output.receive(0);
9389
assertThat(result.getPayload() instanceof String).as("Wrong payload type").isTrue();
@@ -96,9 +92,9 @@ public void testDefaultWithResultTransformer() throws Exception {
9692
}
9793

9894
@Test
99-
public void testDOMResult() throws Exception {
95+
public void testDOMResult() {
10096
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerDOMResultFactory");
101-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
97+
GenericMessage<Object> message = new GenericMessage<>("hello");
10298
input.send(message);
10399
Message<?> result = output.receive(0);
104100
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type ").isTrue();
@@ -107,27 +103,27 @@ public void testDOMResult() throws Exception {
107103
}
108104

109105
@Test
110-
public void testStringResult() throws Exception {
106+
public void testStringResult() {
111107
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerStringResultFactory");
112-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
108+
GenericMessage<Object> message = new GenericMessage<>("hello");
113109
input.send(message);
114110
Message<?> result = output.receive(0);
115-
assertThat(result.getPayload() instanceof StringResult).as("Wrong payload type").isTrue();
111+
assertThat(result.getPayload()).as("Wrong payload type").isInstanceOf(StringResult.class);
116112
}
117113

118114
@Test
119-
public void testCustomResultFactory() throws Exception {
115+
public void testCustomResultFactory() {
120116
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerCustomResultFactory");
121-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
117+
GenericMessage<Object> message = new GenericMessage<>("hello");
122118
input.send(message);
123119
Message<?> result = output.receive(0);
124-
assertThat(result.getPayload() instanceof StubStringResult).as("Wrong payload type").isTrue();
120+
assertThat(result.getPayload()).as("Wrong payload type").isInstanceOf(StubStringResult.class);
125121
}
126122

127123
@Test
128-
public void testFullMessage() throws Exception {
124+
public void testFullMessage() {
129125
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerWithFullMessage");
130-
GenericMessage<Object> message = new GenericMessage<Object>("hello");
126+
GenericMessage<Object> message = new GenericMessage<>("hello");
131127
input.send(message);
132128
Message<?> result = output.receive(0);
133129
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type").isTrue();

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/StubMarshaller.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.integration.xml.config;
1818

19-
import java.io.IOException;
20-
2119
import javax.xml.transform.Result;
2220
import javax.xml.transform.Transformer;
2321
import javax.xml.transform.TransformerFactory;
@@ -29,14 +27,17 @@
2927
/**
3028
*
3129
* @author Jonas Partner
30+
* @author Artem Bilan
3231
*
3332
*/
3433
public class StubMarshaller implements Marshaller {
3534

36-
public void marshal(Object graph, Result result) throws XmlMappingException, IOException {
35+
public void marshal(Object graph, Result result) throws XmlMappingException {
3736
try {
3837
Transformer transformer = TransformerFactory.newInstance().newTransformer();
39-
StringSource stringSource = new StringSource("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root>" + graph.toString() + "</root>");
38+
StringSource stringSource = new StringSource("""
39+
<?xml version="1.0" encoding="ISO-8859-1"?>
40+
<root>""" + graph + "</root>");
4041
transformer.transform(stringSource, result);
4142
}
4243
catch (Exception e) {
@@ -45,8 +46,7 @@ public void marshal(Object graph, Result result) throws XmlMappingException, IOE
4546

4647
}
4748

48-
@SuppressWarnings("rawtypes")
49-
public boolean supports(Class clazz) {
49+
public boolean supports(Class<?> clazz) {
5050
return true;
5151
}
5252

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/StubResultFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@ public Result createResult(Object payload) {
2727
return new StubStringResult();
2828
}
2929

30-
public class StubStringResult extends StringResult {
30+
public static class StubStringResult extends StringResult {
3131

3232
}
3333

spring-integration-xml/src/test/java/org/springframework/integration/xml/config/StubUnmarshaller.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.xml.config;
1818

19-
import java.io.IOException;
2019
import java.util.LinkedList;
2120

2221
import javax.xml.transform.Source;
@@ -27,19 +26,19 @@
2726
/**
2827
*
2928
* @author Jonas Partner
29+
* @author Artem Bilan
3030
*
3131
*/
3232
public class StubUnmarshaller implements Unmarshaller {
3333

34-
public LinkedList<Source> sourcesPassed = new LinkedList<Source>();
34+
public final LinkedList<Source> sourcesPassed = new LinkedList<>();
3535

36-
@SuppressWarnings("rawtypes")
37-
public boolean supports(Class clazz) {
36+
public boolean supports(Class<?> clazz) {
3837
return true;
3938
}
4039

41-
public Object unmarshal(Source source) throws XmlMappingException, IOException {
42-
sourcesPassed.addFirst(source);
40+
public Object unmarshal(Source source) throws XmlMappingException {
41+
this.sourcesPassed.addFirst(source);
4342
return "unmarshalled";
4443
}
4544

0 commit comments

Comments
 (0)