GH-2760: use assertThatExceptionOfType in tests

Fixes https://github.com/spring-projects/spring-integration/issues/2760

* The `assertThatExceptionOfType()` is more convenient,
than `assertThatThrownBy`, so, replace all the usages accordingly
* Fix JavaDoc typo in the `AbstractScriptExecutingMessageProcessor`
* Fix Sonar smells for `throws Exception` in `AbstractScriptExecutingMessageProcessor`
hierarchy and some code polishing for them
This commit is contained in:
Artem Bilan
2019-02-22 13:54:36 -05:00
parent 82ecd5a75d
commit 1d54f9663a
22 changed files with 187 additions and 159 deletions

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.amqp.channel;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
@@ -248,9 +248,9 @@ public class ChannelTests {
MessageListener.class);
willThrow(new MessageConversionException("foo", new IllegalStateException("bar")))
.given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
assertThatThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class)))
.isInstanceOf(MessageConversionException.class)
.hasCauseInstanceOf(IllegalStateException.class);
assertThatExceptionOfType(MessageConversionException.class)
.isThrownBy(() -> listener.onMessage(mock(org.springframework.amqp.core.Message.class)))
.withCauseInstanceOf(IllegalStateException.class);
}
public static class Foo {

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.config;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -34,21 +34,20 @@ public class InvalidPriorityChannelParserTests {
@Test
public void testMessageStoreAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testComparatorAndMessageStoreIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("The 'message-store' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidPriorityChannelWithComparatorAndMessageStoreParserTests.xml", getClass()))
.withMessageContaining("The 'message-store' attribute is not allowed");
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.config;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -32,29 +32,29 @@ public class InvalidQueueChannelParserTests {
@Test
public void testMessageStoreAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithMessageStoreAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testRefAndCapacityIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndCapacityParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("'capacity' attribute is not allowed");
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithRefAndCapacityParserTests.xml", getClass()))
.withMessageContaining("'capacity' attribute is not allowed");
}
@Test
public void testRefAndMessageStoreIllegal() {
assertThatThrownBy(() ->
new ClassPathXmlApplicationContext("InvalidQueueChannelWithRefAndMessageStoreParserTests.xml",
getClass()))
.isInstanceOf(BeanDefinitionParsingException.class)
.hasMessageContaining("The 'message-store' attribute is not allowed " +
assertThatExceptionOfType(BeanDefinitionParsingException.class)
.isThrownBy(() ->
new ClassPathXmlApplicationContext(
"InvalidQueueChannelWithRefAndMessageStoreParserTests.xml", getClass()))
.withMessageContaining("The 'message-store' attribute is not allowed " +
"when providing a 'ref' to a custom queue.");
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -31,6 +31,7 @@ import org.springframework.integration.handler.LambdaMessageProcessor;
import org.springframework.integration.support.converter.ConfigurableCompositeMessageConverter;
import org.springframework.integration.transformer.GenericTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.GenericMessage;
@@ -57,7 +58,8 @@ public class LambdaMessageProcessorTests {
@Test
public void testMessageAsArgument() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(new GenericTransformer<Message<?>, Message<?>>() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
new GenericTransformer<Message<?>, Message<?>>() { // Must not be lambda
@Override
public Message<?> transform(Message<?> source) {
@@ -74,10 +76,12 @@ public class LambdaMessageProcessorTests {
@Test
public void testMessageAsArgumentLambda() {
LambdaMessageProcessor lmp = new LambdaMessageProcessor(
(GenericTransformer<Message<?>, Message<?>>) source -> messageTransformer(source), null);
(GenericTransformer<Message<?>, Message<?>>) this::messageTransformer, null);
lmp.setBeanFactory(mock(BeanFactory.class));
GenericMessage<String> testMessage = new GenericMessage<>("foo");
assertThatThrownBy(() -> lmp.processMessage(testMessage)).hasCauseExactlyInstanceOf(ClassCastException.class);
assertThatExceptionOfType(MessageHandlingException.class)
.isThrownBy(() -> lmp.processMessage(testMessage))
.withCauseInstanceOf(ClassCastException.class);
}
private void handle(GenericHandler<?> h) {

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.gateway;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.jupiter.api.Test;
@@ -87,9 +87,9 @@ public class GatewayDslTests {
@Test
void testNestedGatewayErrorPropagation() {
assertThatThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
.hasCauseInstanceOf(RuntimeException.class)
.hasMessageContaining("intentional");
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> this.nestedGatewayErrorPropagationFlowInput.send(new GenericMessage<>("test")))
.withMessageContaining("intentional");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.manualflow;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import java.util.Arrays;
@@ -513,13 +513,13 @@ public class ManualFlowTests {
@Test
public void testDisabledBeansOverride() {
assertThatThrownBy(
() -> this.integrationFlowContext
.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
.register())
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() ->
this.integrationFlowContext.registration(f -> f.channel(c -> c.direct("doNotOverrideChannel")))
.register())
.isExactlyInstanceOf(BeanCreationException.class)
.hasCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
.hasMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
.withCauseExactlyInstanceOf(BeanDefinitionOverrideException.class)
.withMessageContaining("Invalid bean definition with name 'doNotOverrideChannel'");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.dsl.routers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import java.util.Arrays;
@@ -587,8 +587,9 @@ public class RouterTests {
@Test
public void propagateErrorFromGatherer() {
assertThatThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
.hasMessage("intentional");
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> propagateErrorFromGathererGateway.apply("bar"))
.withMessage("intentional");
}
@Configuration

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import org.junit.Test;
@@ -25,6 +25,7 @@ import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.predicate.MessagePredicate;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.core.DestinationResolutionException;
import org.springframework.messaging.support.GenericMessage;
@@ -33,6 +34,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Mark Fisher
* @author Iwein Fuld
* @author Gary Russell
* @author Artem Bilan
*/
public class BridgeHandlerTests {
@@ -41,9 +43,9 @@ public class BridgeHandlerTests {
@Test
public void simpleBridge() {
QueueChannel outputChannel = new QueueChannel();
handler.setOutputChannel(outputChannel);
this.handler.setOutputChannel(outputChannel);
Message<?> request = new GenericMessage<>("test");
handler.handleMessage(request);
this.handler.handleMessage(request);
Message<?> reply = outputChannel.receive(0);
assertThat(reply).isNotNull();
assertThat(reply).matches(new MessagePredicate(request));
@@ -53,15 +55,16 @@ public class BridgeHandlerTests {
public void missingOutputChannelVerifiedAtRuntime() {
Message<?> request = new GenericMessage<>("test");
assertThatThrownBy(() -> handler.handleMessage(request))
.hasCauseInstanceOf(DestinationResolutionException.class);
assertThatExceptionOfType(MessageHandlingException.class)
.isThrownBy(() -> this.handler.handleMessage(request))
.withCauseInstanceOf(DestinationResolutionException.class);
}
@Test(timeout = 1000)
public void missingOutputChannelAllowedForReplyChannelMessages() {
PollableChannel replyChannel = new QueueChannel();
Message<String> request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel).build();
handler.handleMessage(request);
this.handler.handleMessage(request);
assertThat(replyChannel.receive()).matches(new MessagePredicate(request));
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.handler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
@@ -42,6 +42,7 @@ import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;
/**
@@ -211,8 +212,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
.hasCauseInstanceOf(EvaluationException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>("foo")))
.withCauseInstanceOf(EvaluationException.class);
}
@Test
@@ -221,8 +223,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.hasCauseInstanceOf(UnsupportedOperationException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.withCauseInstanceOf(UnsupportedOperationException.class);
}
@Test
@@ -231,8 +234,9 @@ public class ExpressionEvaluatingMessageProcessorTests {
ExpressionEvaluatingMessageProcessor<String> processor =
new ExpressionEvaluatingMessageProcessor<>(expression);
processor.setBeanFactory(mock(BeanFactory.class));
assertThatThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.hasCauseInstanceOf(CheckedException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> processor.processMessage(new GenericMessage<>(new TestPayload())))
.withCauseInstanceOf(CheckedException.class);
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.file.remote;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.BDDMockito.willThrow;
@@ -132,14 +132,14 @@ public class StreamingInboundTests {
}
@Test
public void testExceptionOnFetch() throws Exception {
public void testExceptionOnFetch() {
StringSessionFactory sessionFactory = new StringSessionFactory();
Streamer streamer = new Streamer(new StringRemoteFileTemplate(sessionFactory), null);
streamer.setBeanFactory(mock(BeanFactory.class));
streamer.setRemoteDirectory("/bad");
streamer.afterPropertiesSet();
assertThatThrownBy(streamer::receive)
.isInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(streamer::receive);
}
@SuppressWarnings("unchecked")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@@ -16,6 +16,8 @@
package org.springframework.integration.groovy;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.UUID;
@@ -40,6 +42,7 @@ import groovy.lang.GString;
* @author Artem Bilan
* @author Stefan Reuter
* @author Gary Russell
*
* @since 2.0
*/
public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
@@ -59,7 +62,6 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
/**
* Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator}.
*
* @param scriptVariableGenerator The variable generator.
*/
public GroovyCommandMessageProcessor(ScriptVariableGenerator scriptVariableGenerator) {
@@ -72,20 +74,18 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
* and provided {@link Binding}.
* Provided 'binding' will be used in the {@link BindingOverwriteGroovyObjectCustomizerDecorator} to overwrite
* original Groovy Script 'binding'.
*
* @param binding The binding.
*/
public GroovyCommandMessageProcessor(Binding binding) {
this();
Assert.notNull(binding, "binding must not be null");
this.binding = binding;
}
/**
* Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator} and Binding.
* Creates a {@link GroovyCommandMessageProcessor} that will use the provided {@link ScriptVariableGenerator} and
* Binding.
* Provided 'binding' will be used in the {@link BindingOverwriteGroovyObjectCustomizerDecorator} to overwrite
* original Groovy Script 'binding'.
*
* @param binding The binding.
* @param scriptVariableGenerator The variable generator.
*/
@@ -97,7 +97,6 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
/**
* Sets a {@link GroovyObjectCustomizer} for this processor.
*
* @param customizer The customizer.
*/
public void setCustomizer(GroovyObjectCustomizer customizer) {
@@ -113,26 +112,33 @@ public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessag
}
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Assert.notNull(scriptSource, "scriptSource must not be null");
VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null
? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding)
: new VariableBindingGroovyObjectCustomizerDecorator();
VariableBindingGroovyObjectCustomizerDecorator customizerDecorator =
this.binding != null
? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding)
: new VariableBindingGroovyObjectCustomizerDecorator();
if (this.customizer != null) {
customizerDecorator.setCustomizer(this.customizer);
}
if (!CollectionUtils.isEmpty(variables)) {
customizerDecorator.setVariables(variables);
}
GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator);
GroovyScriptFactory factory = new GroovyScriptFactory(getClass().getSimpleName(), customizerDecorator);
if (this.beanClassLoader != null) {
factory.setBeanClassLoader(this.beanClassLoader);
}
if (this.beanFactory != null) {
factory.setBeanFactory(this.beanFactory);
}
Object result = factory.getScriptedObject(scriptSource);
return (result instanceof GString) ? result.toString() : result;
try {
Object result = factory.getScriptedObject(scriptSource);
return (result instanceof GString) ? result.toString() : result;
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
protected String generateScriptName(Message<?> message) {

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.groovy;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
@@ -57,6 +59,7 @@ import groovy.transform.CompileStatic;
* @author Stefan Reuter
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.0
*/
public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object>
@@ -67,24 +70,24 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
private final Lock scriptLock = new ReentrantLock();
private volatile ScriptSource scriptSource;
private ScriptSource scriptSource;
private volatile GroovyClassLoader groovyClassLoader = AccessController.doPrivileged(
(PrivilegedAction<GroovyClassLoader>) () -> new GroovyClassLoader(ClassUtils.getDefaultClassLoader()));
private volatile Class<?> scriptClass;
private GroovyClassLoader groovyClassLoader =
AccessController.doPrivileged((PrivilegedAction<GroovyClassLoader>)
() -> new GroovyClassLoader(ClassUtils.getDefaultClassLoader()));
private boolean compileStatic;
private CompilerConfiguration compilerConfiguration;
private volatile Class<?> scriptClass;
/**
* Create a processor for the given {@link ScriptSource} that will use a
* DefaultScriptVariableGenerator.
* @param scriptSource The script source.
*/
public GroovyScriptExecutingMessageProcessor(ScriptSource scriptSource) {
super();
this.scriptSource = scriptSource;
}
@@ -140,7 +143,7 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
}
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableListableBeanFactory) {
((ConfigurableListableBeanFactory) this.beanFactory).ignoreDependencyType(MetaClass.class);
}
@@ -155,27 +158,32 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
}
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Assert.notNull(scriptSource, "scriptSource must not be null");
this.parseScriptIfNecessary(scriptSource);
Object result = this.execute(variables);
parseScriptIfNecessary(scriptSource);
Object result = execute(variables);
return (result instanceof GString) ? result.toString() : result;
}
private void parseScriptIfNecessary(ScriptSource scriptSource) throws Exception {
private void parseScriptIfNecessary(ScriptSource scriptSource) {
if (this.scriptClass == null || scriptSource.isModified()) {
this.scriptLock.lockInterruptibly();
this.scriptLock.lock();
try {
// synchronized double check
if (this.scriptClass == null || scriptSource.isModified()) {
String className = scriptSource.suggestedClassName();
if (StringUtils.hasText(className)) {
this.scriptClass =
this.groovyClassLoader.parseClass(scriptSource.getScriptAsString(), className);
try {
String scriptAsString = scriptSource.getScriptAsString();
if (StringUtils.hasText(className)) {
this.scriptClass = this.groovyClassLoader.parseClass(scriptAsString, className);
}
else {
this.scriptClass = this.groovyClassLoader.parseClass(scriptAsString);
}
}
else {
this.scriptClass = this.groovyClassLoader.parseClass(scriptSource.getScriptAsString());
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
@@ -210,13 +218,14 @@ public class GroovyScriptExecutingMessageProcessor extends AbstractScriptExecuti
}
catch (IllegalAccessException ex) {
throw new ScriptCompilationException(
this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(), ex);
this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(),
ex);
}
}
private final class BeanFactoryFallbackBinding extends Binding {
private BeanFactoryFallbackBinding(Map<?, ?> variables) {
BeanFactoryFallbackBinding(Map<?, ?> variables) {
super(variables);
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.jpa.core;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.Mockito.mock;
@@ -282,9 +282,9 @@ public class JpaExecutorTests {
executor.setParameterSourceFactory(parameterSourceFactory);
executor.setJpaParameters(Collections.singletonList(new JpaParameter("firstName", null, "#this")));
assertThatThrownBy(executor::afterPropertiesSet)
.isExactlyInstanceOf(IllegalStateException.class)
.hasMessageStartingWith("The 'jpaParameters' and 'parameterSourceFactory' are mutually exclusive.");
assertThatIllegalStateException()
.isThrownBy(executor::afterPropertiesSet)
.withMessageStartingWith("The 'jpaParameters' and 'parameterSourceFactory' are mutually exclusive.");
}
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.redis.util;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import java.util.Map;
import java.util.UUID;
@@ -347,9 +347,9 @@ public class RedisLockRegistryTests extends RedisAvailableTests {
Lock lock1 = registry.obtain("foo");
assertThat(lock1.tryLock()).isTrue();
waitForExpire("foo");
assertThatThrownBy(lock1::unlock)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("Lock was released in the store due to expiration.");
assertThatIllegalStateException()
.isThrownBy(lock1::unlock)
.withMessageContaining("Lock was released in the store due to expiration.");
}

View File

@@ -56,21 +56,6 @@ public abstract class AbstractScriptExecutingMessageProcessor<T>
}
/**
* Executes the script and returns the result.
*/
@Override
public final T processMessage(Message<?> message) {
try {
ScriptSource source = this.getScriptSource(message);
Map<String, Object> variables = this.scriptVariableGenerator.generateScriptVariables(message);
return executeScript(source, variables);
}
catch (Exception e) {
throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "Failed to execute script.", e);
}
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
@@ -81,6 +66,21 @@ public abstract class AbstractScriptExecutingMessageProcessor<T>
this.beanFactory = beanFactory;
}
/**
* Executes the script and returns the result.
*/
@Override
public final T processMessage(Message<?> message) {
try {
ScriptSource source = getScriptSource(message);
Map<String, Object> variables = this.scriptVariableGenerator.generateScriptVariables(message);
return executeScript(source, variables);
}
catch (Exception e) {
throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message, () -> "Failed to execute script.", e);
}
}
/**
* Subclasses must implement this method to create a script source,
* optionally using the message to locate or create the script.
@@ -92,11 +92,10 @@ public abstract class AbstractScriptExecutingMessageProcessor<T>
/**
* Subclasses must implement this method. In doing so, the execution context
* for the script should be populated with the provided script variables.
*78546 @param scriptSource The script source.
* @param scriptSource The script source.
* @param variables The variables.
* @return The result of the execution.
* @throws Exception Any Exception.
*/
protected abstract T executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception;
protected abstract T executeScript(ScriptSource scriptSource, Map<String, Object> variables);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@@ -28,6 +28,8 @@ import org.springframework.util.Assert;
/**
* @author David Turanski
* @author Artem Bilan
*
* @since 2.1
*/
public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
@@ -40,12 +42,10 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess
/**
* Create a processor for the {@link ScriptSource} using the provided
* {@link ScriptExecutor} using the DefaultScriptVariableGenerator
*
* @param scriptSource The script source.
* @param scriptExecutor The script executor.
*/
public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptExecutor scriptExecutor) {
super();
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
}
@@ -53,13 +53,13 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess
/**
* Create a processor for the {@link ScriptSource} using the provided
* {@link ScriptExecutor}
*
* @param scriptSource The script source.
* @param scriptVariableGenerator The script variable generator.
* @param scriptExecutor The script executor.
*/
public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptVariableGenerator scriptVariableGenerator,
ScriptExecutor scriptExecutor) {
super(scriptVariableGenerator);
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
@@ -68,13 +68,13 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess
/**
* Create a processor for the {@link ScriptSource} using the provided
* {@link ScriptExecutor} using the DefaultScriptVariableGenerator
*
* @param scriptSource The script source.
* @param scriptExecutor The script executor.
* @param variables The variables.
*/
public ScriptExecutingMessageProcessor(ScriptSource scriptSource, ScriptExecutor scriptExecutor,
Map<String, Object> variables) {
super(new DefaultScriptVariableGenerator(variables));
this.scriptSource = scriptSource;
this.scriptExecutor = scriptExecutor;
@@ -87,7 +87,7 @@ public class ScriptExecutingMessageProcessor extends AbstractScriptExecutingMess
}
@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) {
Assert.notNull(scriptSource, "scriptSource must not be null");
return this.scriptExecutor.executeScript(scriptSource, variables);
}

View File

@@ -17,7 +17,7 @@
package org.springframework.integration.ws.config;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.doAnswer;
@@ -51,6 +51,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.MessagingException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.client.WebServiceClientException;
@@ -130,8 +131,9 @@ public class UriVariableTests {
.setHeader("param", "test1 & test2")
.build();
assertThatThrownBy(() -> this.inputHttp.send(message))
.hasCauseInstanceOf(WebServiceIOException.class); // offline
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> this.inputHttp.send(message))
.withCauseInstanceOf(WebServiceIOException.class); // offline
assertThat(uri.get()).isEqualTo("http://localhost/spring-integration?param=test1%20&%20test2");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 @@
package org.springframework.integration.xml;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.io.File;
import java.io.IOException;
@@ -117,8 +117,8 @@ public class DefaultXmlPayloadConverterTests {
@Test
public void testInvalidPayload() {
assertThatThrownBy(() -> converter.convertToSource(12))
.isExactlyInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> converter.convertToSource(12));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 @@
package org.springframework.integration.xml.source;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.io.StringReader;
@@ -74,8 +74,8 @@ public class DomSourceFactoryTests {
@Test
public void testWithUnsupportedPayload() {
assertThatThrownBy(() -> sourceFactory.createSource(12))
.isExactlyInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> sourceFactory.createSource(12));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 @@
package org.springframework.integration.xml.source;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.io.BufferedReader;
@@ -65,8 +65,8 @@ public class StringSourceTests {
public void testWithUnsupportedPayload() {
String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><item>one</item>";
StringBuffer buffer = new StringBuffer(docString);
assertThatThrownBy(() -> sourceFactory.createSource(buffer))
.isExactlyInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> sourceFactory.createSource(buffer));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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,7 @@
package org.springframework.integration.xml.transformer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.util.Properties;
@@ -83,8 +83,8 @@ public class ResultToStringTransformerTests {
@Test
public void testWithUnsupportedSaxResult() {
assertThatThrownBy(() -> this.transformer.transformResult(new SAXResult()))
.isExactlyInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> this.transformer.transformResult(new SAXResult()));
}
}

View File

@@ -17,7 +17,8 @@
package org.springframework.integration.xml.transformer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import java.io.File;
import java.io.IOException;
@@ -163,21 +164,21 @@ public class XsltPayloadTransformerTests {
XsltPayloadTransformer transformer =
new XsltPayloadTransformer(getXslResourceThatOutputsText(), "foo.bar.Baz");
transformer.setBeanFactory(Mockito.mock(BeanFactory.class));
assertThatThrownBy(transformer::afterPropertiesSet)
.isExactlyInstanceOf(IllegalStateException.class)
.hasCauseExactlyInstanceOf(ClassNotFoundException.class);
assertThatIllegalStateException()
.isThrownBy(transformer::afterPropertiesSet)
.withCauseExactlyInstanceOf(ClassNotFoundException.class);
}
@Test
public void testNonXmlString() {
assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test")))
.isExactlyInstanceOf(TransformerException.class);
assertThatExceptionOfType(TransformerException.class)
.isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>("test")));
}
@Test
public void testUnsupportedPayloadType() {
assertThatThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12)))
.isExactlyInstanceOf(MessagingException.class);
assertThatExceptionOfType(MessagingException.class)
.isThrownBy(() -> this.transformer.doTransform(new GenericMessage<>(12)));
}
@Test