Skip to content

Commit

Permalink
Migrate XML module tests to JUnit 5
Browse files Browse the repository at this point in the history
* Use Java text blocks for xml snippets
  • Loading branch information
artembilan committed Mar 30, 2023
1 parent e39449b commit 63937ab
Show file tree
Hide file tree
Showing 30 changed files with 808 additions and 631 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -19,7 +19,7 @@
import java.io.ByteArrayInputStream;
import java.util.Properties;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
Expand All @@ -31,7 +31,7 @@
import org.springframework.integration.xml.transformer.XPathTransformer;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;


/**
Expand All @@ -43,21 +43,12 @@
public class ChainElementsTests {

@Test
public void chainXPathTransformer() throws Exception {
try {
bootStrap("xpath-transformer");
fail("Expected a BeanDefinitionParsingException to be thrown.");
}
catch (BeanDefinitionParsingException e) {
final String expectedMessage = "Configuration problem: " +
"The 'input-channel' attribute isn't allowed for a nested " +
"(e.g. inside a <chain/>) endpoint element: 'int-xml:xpath-transformer'.";
final String actualMessage = e.getMessage();
assertThat(actualMessage.startsWith(expectedMessage))
.as("Error message did not start with '" + expectedMessage +
"' but instead returned: '" + actualMessage + "'").isTrue();
}

public void chainXPathTransformer() {
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> bootStrap("xpath-transformer"))
.withMessageStartingWith("Configuration problem: " +
"The 'input-channel' attribute isn't allowed for a nested " +
"(e.g. inside a <chain/>) endpoint element: 'int-xml:xpath-transformer'.");
}

@Test
Expand All @@ -68,37 +59,21 @@ public void chainXPathTransformerId() throws Exception {
}

@Test
public void chainXPathRouterOrder() throws Exception {
try {
bootStrap("xpath-router-order");
fail("Expected a BeanDefinitionParsingException to be thrown.");
}
catch (BeanDefinitionParsingException e) {
final String expectedMessage = "Configuration problem: " +
"'int-xml:xpath-router' must not define an 'order' attribute " +
"when used within a chain.";
final String actualMessage = e.getMessage();
assertThat(actualMessage.startsWith(expectedMessage))
.as("Error message did not start with '" + expectedMessage +
"' but instead returned: '" + actualMessage + "'").isTrue();
}

public void chainXPathRouterOrder() {
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> bootStrap("xpath-router-order"))
.withMessageStartingWith("Configuration problem: " +
"'int-xml:xpath-router' must not define an 'order' attribute " +
"when used within a chain.");
}

@Test
public void chainXPathTransformerPoller() throws Exception {
try {
bootStrap("xpath-transformer-poller");
fail("Expected a BeanDefinitionParsingException to be thrown.");
}
catch (BeanDefinitionParsingException e) {
final String expectedMessage = "Configuration problem: " +
"'int-xml:xpath-transformer' must not define a 'poller' " +
"sub-element when used within a chain.";
final String actualMessage = e.getMessage();
assertThat(actualMessage).as("Error message did not start with '" + expectedMessage +
"' but instead returned: '" + actualMessage + "'").startsWith(expectedMessage);
}
public void chainXPathTransformerPoller() {
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() -> bootStrap("xpath-transformer-poller"))
.withMessageStartingWith("Configuration problem: " +
"'int-xml:xpath-transformer' must not define a 'poller' " +
"sub-element when used within a chain.");
}

@Test
Expand All @@ -114,11 +89,11 @@ private ConfigurableApplicationContext bootStrap(String configProperty) throws E
"org/springframework/integration/xml/config/chain-elements-config.properties"));
pfb.afterPropertiesSet();
Properties prop = pfb.getObject();
StringBuffer buffer = new StringBuffer();
buffer.append(prop.getProperty("xmlheaders"))
.append(prop.getProperty(configProperty))
.append(prop.getProperty("xmlfooter"));
ByteArrayInputStream stream = new ByteArrayInputStream(buffer.toString().getBytes());
String buffer =
prop.getProperty("xmlheaders") +
prop.getProperty(configProperty) +
prop.getProperty("xmlfooter");
ByteArrayInputStream stream = new ByteArrayInputStream(buffer.getBytes());

GenericApplicationContext ac = new GenericApplicationContext();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(ac);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -16,8 +16,8 @@

package org.springframework.integration.xml.config;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
Expand All @@ -28,8 +28,7 @@
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.ErrorHandler;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -38,10 +37,10 @@
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
*
* @since 1.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@SpringJUnitConfig
public class DefaultConfigurationTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -20,13 +20,12 @@

import javax.xml.transform.dom.DOMResult;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.SmartLifecycleRoleController;
import org.springframework.integration.test.util.TestUtils;
Expand All @@ -35,6 +34,7 @@
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.MultiValueMap;
import org.springframework.xml.transform.StringResult;

Expand All @@ -44,23 +44,19 @@
* @author Jonas Partner
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
*/
@SpringJUnitConfig
public class MarshallingTransformerParserTests {

@Autowired
private ApplicationContext appContext;

@Autowired
private PollableChannel output;


@Before
public void setUp() {
this.appContext = new ClassPathXmlApplicationContext("MarshallingTransformerParserTests-context.xml", getClass());
this.output = (PollableChannel) appContext.getBean("output");
}


@Test
public void testParse() throws Exception {
public void testParse() {
EventDrivenConsumer consumer = (EventDrivenConsumer) appContext.getBean("parseOnly");
assertThat(TestUtils.getPropertyValue(consumer, "handler.order")).isEqualTo(2);
assertThat(TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout")).isEqualTo(123L);
Expand All @@ -70,13 +66,13 @@ public void testParse() throws Exception {
@SuppressWarnings("unchecked")
List<SmartLifecycle> list = (List<SmartLifecycle>) TestUtils.getPropertyValue(roleController, "lifecycles",
MultiValueMap.class).get("foo");
assertThat(list).containsExactly((SmartLifecycle) consumer);
assertThat(list).containsExactly(consumer);
}

@Test
public void testDefault() throws Exception {
public void testDefault() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerNoResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type").isTrue();
Expand All @@ -85,9 +81,9 @@ public void testDefault() throws Exception {
}

@Test
public void testDefaultWithResultTransformer() throws Exception {
public void testDefaultWithResultTransformer() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerWithResultTransformer");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof String).as("Wrong payload type").isTrue();
Expand All @@ -96,9 +92,9 @@ public void testDefaultWithResultTransformer() throws Exception {
}

@Test
public void testDOMResult() throws Exception {
public void testDOMResult() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerDOMResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type ").isTrue();
Expand All @@ -107,27 +103,27 @@ public void testDOMResult() throws Exception {
}

@Test
public void testStringResult() throws Exception {
public void testStringResult() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerStringResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof StringResult).as("Wrong payload type").isTrue();
assertThat(result.getPayload()).as("Wrong payload type").isInstanceOf(StringResult.class);
}

@Test
public void testCustomResultFactory() throws Exception {
public void testCustomResultFactory() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerCustomResultFactory");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof StubStringResult).as("Wrong payload type").isTrue();
assertThat(result.getPayload()).as("Wrong payload type").isInstanceOf(StubStringResult.class);
}

@Test
public void testFullMessage() throws Exception {
public void testFullMessage() {
MessageChannel input = (MessageChannel) appContext.getBean("marshallingTransformerWithFullMessage");
GenericMessage<Object> message = new GenericMessage<Object>("hello");
GenericMessage<Object> message = new GenericMessage<>("hello");
input.send(message);
Message<?> result = output.receive(0);
assertThat(result.getPayload() instanceof DOMResult).as("Wrong payload type").isTrue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -16,8 +16,6 @@

package org.springframework.integration.xml.config;

import java.io.IOException;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
Expand All @@ -29,14 +27,17 @@
/**
*
* @author Jonas Partner
* @author Artem Bilan
*
*/
public class StubMarshaller implements Marshaller {

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

}

@SuppressWarnings("rawtypes")
public boolean supports(Class clazz) {
public boolean supports(Class<?> clazz) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -27,7 +27,7 @@ public Result createResult(Object payload) {
return new StubStringResult();
}

public class StubStringResult extends StringResult {
public static class StubStringResult extends StringResult {

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.integration.xml.config;

import java.io.IOException;
import java.util.LinkedList;

import javax.xml.transform.Source;
Expand All @@ -27,19 +26,19 @@
/**
*
* @author Jonas Partner
* @author Artem Bilan
*
*/
public class StubUnmarshaller implements Unmarshaller {

public LinkedList<Source> sourcesPassed = new LinkedList<Source>();
public final LinkedList<Source> sourcesPassed = new LinkedList<>();

@SuppressWarnings("rawtypes")
public boolean supports(Class clazz) {
public boolean supports(Class<?> clazz) {
return true;
}

public Object unmarshal(Source source) throws XmlMappingException, IOException {
sourcesPassed.addFirst(source);
public Object unmarshal(Source source) throws XmlMappingException {
this.sourcesPassed.addFirst(source);
return "unmarshalled";
}

Expand Down
Loading

0 comments on commit 63937ab

Please sign in to comment.