diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/ExpressionControlBusFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/config/ExpressionControlBusFactoryBean.java index 179f9b86b3..9cedaf14c4 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/ExpressionControlBusFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/ExpressionControlBusFactoryBean.java @@ -1,11 +1,11 @@ /* * Copyright 2002-2011 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. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. @@ -31,7 +31,7 @@ import org.springframework.util.ReflectionUtils; /** * FactoryBean for creating {@link MessageHandler} instances to handle a message as a SpEL expression. - * + * * @author Dave Syer * @author Mark Fisher * @author Oleg Zhurakousky @@ -48,9 +48,10 @@ public class ExpressionControlBusFactoryBean extends AbstractSimpleMessageHandle this.sendTimeout = sendTimeout; } + @Override protected MessageHandler createHandler() { - ExpressionCommandMessageProcessor processor = new ExpressionCommandMessageProcessor(this.methodFilter); - processor.setBeanFactory(this.getBeanFactory()); + ExpressionCommandMessageProcessor processor = + new ExpressionCommandMessageProcessor(this.methodFilter, this.getBeanFactory()); ServiceActivatingHandler handler = new ServiceActivatingHandler(processor); if (this.sendTimeout != null) { handler.setSendTimeout(this.sendTimeout); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/expression/ExpressionUtils.java b/spring-integration-core/src/main/java/org/springframework/integration/expression/ExpressionUtils.java index a6ab8b166c..5f1e925668 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/expression/ExpressionUtils.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/expression/ExpressionUtils.java @@ -20,6 +20,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanFactory; +import org.springframework.context.expression.BeanFactoryResolver; import org.springframework.context.expression.MapAccessor; import org.springframework.core.convert.ConversionService; import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -45,12 +46,16 @@ public abstract class ExpressionUtils { * @param conversionService the conversion service. * @return the evaluation context. */ - private static StandardEvaluationContext createStandardEvaluationContext(ConversionService conversionService) { + private static StandardEvaluationContext createStandardEvaluationContext(ConversionService conversionService, + BeanFactory beanFactory) { StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); evaluationContext.addPropertyAccessor(new MapAccessor()); if (conversionService != null) { evaluationContext.setTypeConverter(new StandardTypeConverter(conversionService)); } + if (beanFactory != null) { + evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory)); + } return evaluationContext; } @@ -85,7 +90,7 @@ public abstract class ExpressionUtils { if (beanFactory != null) { conversionService = IntegrationContextUtils.getConversionService(beanFactory); } - evaluationContext = createStandardEvaluationContext(conversionService); + evaluationContext = createStandardEvaluationContext(conversionService, beanFactory); } return evaluationContext; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionCommandMessageProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionCommandMessageProcessor.java index 1b0b1ca8ab..5a373c50bf 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionCommandMessageProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionCommandMessageProcessor.java @@ -1,11 +1,11 @@ /* - * Copyright 2002-2010 the original author or authors. - * + * Copyright 2002-2013 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. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.springframework.beans.factory.BeanFactory; import org.springframework.core.convert.TypeDescriptor; import org.springframework.expression.AccessException; import org.springframework.expression.EvaluationContext; @@ -32,9 +33,10 @@ import org.springframework.integration.Message; /** * A MessageProcessor implementation that expects an Expression or expressionString * as the Message payload. When processing, it simply evaluates that expression. - * + * * @author Dave Syer * @author Mark Fisher + * @author Gary Russell * @since 2.0 */ public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor { @@ -43,9 +45,16 @@ public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor< } public ExpressionCommandMessageProcessor(MethodFilter methodFilter) { + this(methodFilter, null); + } + + public ExpressionCommandMessageProcessor(MethodFilter methodFilter, BeanFactory beanFactory) { + if (beanFactory != null) { + this.setBeanFactory(beanFactory); + } if (methodFilter != null) { MethodResolver methodResolver = new ExpressionCommandMethodResolver(methodFilter); - this.getEvaluationContext().setMethodResolvers(Collections.singletonList(methodResolver)); + this.getEvaluationContext(false).setMethodResolvers(Collections.singletonList(methodResolver)); } } @@ -54,6 +63,7 @@ public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor< * Evaluates the Message payload expression as a command. * @throws IllegalArgumentException if the payload is not an Exception or String */ + @Override public Object processMessage(Message message) { Object expression = message.getPayload(); if (expression instanceof Expression) { @@ -72,10 +82,11 @@ public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor< private ExpressionCommandMethodResolver(MethodFilter methodFilter) { - this.methodFilter = methodFilter; + this.methodFilter = methodFilter; } + @Override public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name, List argumentTypes) throws AccessException { this.validateMethod(targetObject, name, (argumentTypes != null ? argumentTypes.size() : 0)); @@ -96,7 +107,7 @@ public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor< } List supportedMethods = this.methodFilter.filter(candidates); if (supportedMethods.size() == 0) { - String methodDescription = (candidates.size() > 0) ? candidates.get(0).toString() : name; + String methodDescription = (candidates.size() > 0) ? candidates.get(0).toString() : name; throw new EvaluationException("The method '" + methodDescription + "' is not supported by this command processor. " + "If using the Control Bus, consider adding @ManagedOperation or @ManagedAttribute."); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingCorrelationStrategyTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingCorrelationStrategyTests.java index b9dfdbd2ef..20b34d471f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingCorrelationStrategyTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingCorrelationStrategyTests.java @@ -13,13 +13,15 @@ package org.springframework.integration.aggregator; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; @@ -35,6 +37,7 @@ import org.springframework.integration.support.MessageBuilder; * @author Alex Peters * @author Oleg Zhurakousky * @author Gunnar Hillert + * @author Gary Russell */ public class ExpressionEvaluatingCorrelationStrategyTests { @@ -57,6 +60,8 @@ public class ExpressionEvaluatingCorrelationStrategyTests { ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression expression = parser.parseExpression("payload.substring(0,1)"); strategy = new ExpressionEvaluatingCorrelationStrategy(expression); + strategy.setBeanFactory(mock(BeanFactory.class)); + strategy.afterPropertiesSet(); Object correlationKey = strategy.getCorrelationKey(new GenericMessage("bla")); assertThat(correlationKey, is(instanceOf(String.class))); assertThat((String) correlationKey, is("b")); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingMessageGroupProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingMessageGroupProcessorTests.java index 6332503f55..e7b787f284 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingMessageGroupProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingMessageGroupProcessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,7 @@ package org.springframework.integration.aggregator; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -30,19 +31,21 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.store.MessageGroup; import org.springframework.integration.support.MessageBuilder; /** * @author Alex Peters - * @author Mark Fisher + * @author Mark Fisher + * @author Gary Russell */ @RunWith(MockitoJUnitRunner.class) public class ExpressionEvaluatingMessageGroupProcessorTests { private ExpressionEvaluatingMessageGroupProcessor processor; - + @Mock private MessageGroup group; @@ -62,6 +65,7 @@ public class ExpressionEvaluatingMessageGroupProcessorTests { public void testProcessAndSendWithSizeExpressionEvaluated() throws Exception { when(group.getMessages()).thenReturn(messages); processor = new ExpressionEvaluatingMessageGroupProcessor("#root.size()"); + processor.setBeanFactory(mock(BeanFactory.class)); Object result = processor.processMessageGroup(group); assertTrue(result instanceof Message); Message resultMessage = (Message) result; @@ -72,7 +76,9 @@ public class ExpressionEvaluatingMessageGroupProcessorTests { public void testProcessAndCheckHeaders() throws Exception { when(group.getMessages()).thenReturn(messages); processor = new ExpressionEvaluatingMessageGroupProcessor("#root"); + processor.setBeanFactory(mock(BeanFactory.class)); Object result = processor.processMessageGroup(group); + processor.setBeanFactory(mock(BeanFactory.class)); assertTrue(result instanceof Message); Message resultMessage = (Message) result; assertEquals("bar", resultMessage.getHeaders().get("foo")); @@ -82,6 +88,7 @@ public class ExpressionEvaluatingMessageGroupProcessorTests { public void testProcessAndSendWithProjectionExpressionEvaluated() throws Exception { when(group.getMessages()).thenReturn(messages); processor = new ExpressionEvaluatingMessageGroupProcessor("![payload]"); + processor.setBeanFactory(mock(BeanFactory.class)); Object result = processor.processMessageGroup(group); assertTrue(result instanceof Message); Message resultMessage = (Message) result; @@ -99,6 +106,7 @@ public class ExpressionEvaluatingMessageGroupProcessorTests { public void testProcessAndSendWithFilterAndProjectionExpressionEvaluated() throws Exception { when(group.getMessages()).thenReturn(messages); processor = new ExpressionEvaluatingMessageGroupProcessor("?[payload>2].![payload]"); + processor.setBeanFactory(mock(BeanFactory.class)); Object result = processor.processMessageGroup(group); assertTrue(result instanceof Message); Message resultMessage = (Message) result; @@ -115,6 +123,7 @@ public class ExpressionEvaluatingMessageGroupProcessorTests { when(group.getMessages()).thenReturn(messages); processor = new ExpressionEvaluatingMessageGroupProcessor(String.format("T(%s).sum(?[payload>2].![payload])", getClass().getName())); + processor.setBeanFactory(mock(BeanFactory.class)); Object result = processor.processMessageGroup(group); assertTrue(result instanceof Message); Message resultMessage = (Message) result; diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingReleaseStrategyTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingReleaseStrategyTests.java index 63426ac290..c3d3a00407 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingReleaseStrategyTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aggregator/ExpressionEvaluatingReleaseStrategyTests.java @@ -1,24 +1,42 @@ +/* + * Copyright 2002-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.integration.aggregator; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.store.SimpleMessageGroup; /** * @author Alex Peters * @author Dave Syer - * + * @author Gary Russell + * */ public class ExpressionEvaluatingReleaseStrategyTests { private ExpressionEvaluatingReleaseStrategy strategy; - private SimpleMessageGroup messages = new SimpleMessageGroup("foo"); + private final SimpleMessageGroup messages = new SimpleMessageGroup("foo"); @Before @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -31,18 +49,21 @@ public class ExpressionEvaluatingReleaseStrategyTests { @Test public void testCompletedWithSizeSpelEvaluated() throws Exception { strategy = new ExpressionEvaluatingReleaseStrategy("#root.size()==5"); + strategy.setBeanFactory(mock(BeanFactory.class)); assertThat(strategy.canRelease(messages), is(true)); } @Test public void testCompletedWithFilterSpelEvaluated() throws Exception { strategy = new ExpressionEvaluatingReleaseStrategy("!?[payload==5].empty"); + strategy.setBeanFactory(mock(BeanFactory.class)); assertThat(strategy.canRelease(messages), is(true)); } @Test public void testCompletedWithFilterSpelReturnsNotCompleted() throws Exception { strategy = new ExpressionEvaluatingReleaseStrategy("!?[payload==6].empty"); + strategy.setBeanFactory(mock(BeanFactory.class)); assertThat(strategy.canRelease(messages), is(false)); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java index 5ff32d7a6a..06eaebf4ef 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aop/MessagePublishingInterceptorTests.java @@ -25,6 +25,7 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; + import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.integration.Message; @@ -43,10 +44,11 @@ public class MessagePublishingInterceptorTests { private final QueueChannel testChannel = new QueueChannel(); + private DefaultListableBeanFactory beanFactory; @Before public void setup() { - DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + beanFactory = new DefaultListableBeanFactory(); channelResolver = new BeanFactoryChannelResolver(beanFactory); beanFactory.registerSingleton("c", testChannel); } @@ -55,6 +57,7 @@ public class MessagePublishingInterceptorTests { public void returnValue() { PublisherMetadataSource metadataSource = new TestPublisherMetadataSource(); MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(metadataSource); + interceptor.setBeanFactory(beanFactory); interceptor.setChannelResolver(channelResolver); ProxyFactory pf = new ProxyFactory(new TestBeanImpl()); pf.addAdvice(interceptor); @@ -82,6 +85,7 @@ public class MessagePublishingInterceptorTests { metadataSource.setHeaderExpressionMap(headerExpressionMap); MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(metadataSource); + interceptor.setBeanFactory(beanFactory); interceptor.setChannelResolver(channelResolver); ProxyFactory pf = new ProxyFactory(new TestBeanImpl()); pf.addAdvice(interceptor); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceIntegrationTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceIntegrationTests.java index 88817f08b0..9102368b91 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceIntegrationTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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,6 +17,7 @@ package org.springframework.integration.endpoint; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import java.util.ArrayList; import java.util.HashMap; @@ -26,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -38,6 +40,7 @@ import org.springframework.util.ErrorHandler; /** * @author Mark Fisher + * @author Gary Russell * @since 2.0 */ public class ExpressionEvaluatingMessageSourceIntegrationTests { @@ -58,6 +61,7 @@ public class ExpressionEvaluatingMessageSourceIntegrationTests { factoryBean.afterPropertiesSet(); Expression expression = factoryBean.getObject(); ExpressionEvaluatingMessageSource source = new ExpressionEvaluatingMessageSource(expression, Object.class); + source.setBeanFactory(mock(BeanFactory.class)); source.setHeaderExpressions(headerExpressions); SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); adapter.setSource(source); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceTests.java index 6ba15229ad..c7ab2e78cf 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/ExpressionEvaluatingMessageSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -18,8 +18,11 @@ package org.springframework.integration.endpoint; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.core.convert.ConversionFailedException; import org.springframework.expression.Expression; import org.springframework.expression.common.LiteralExpression; @@ -27,6 +30,7 @@ import org.springframework.integration.Message; /** * @author Mark Fisher + * @author Gary Russell * @since 2.0 */ public class ExpressionEvaluatingMessageSourceTests { @@ -37,6 +41,7 @@ public class ExpressionEvaluatingMessageSourceTests { Expression expression = new LiteralExpression("foo"); ExpressionEvaluatingMessageSource source = new ExpressionEvaluatingMessageSource(expression, String.class); + source.setBeanFactory(mock(BeanFactory.class)); Message message = source.receive(); assertNotNull(message); assertEquals("foo", message.getPayload()); @@ -47,6 +52,7 @@ public class ExpressionEvaluatingMessageSourceTests { Expression expression = new LiteralExpression("foo"); ExpressionEvaluatingMessageSource source = new ExpressionEvaluatingMessageSource(expression, Integer.class); + source.setBeanFactory(mock(BeanFactory.class)); source.receive(); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PseudoTransactionalMessageSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PseudoTransactionalMessageSourceTests.java index 4c4dbc7e48..d3aed36e8e 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PseudoTransactionalMessageSourceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/endpoint/PseudoTransactionalMessageSourceTests.java @@ -18,12 +18,15 @@ package org.springframework.integration.endpoint; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; import java.lang.reflect.Method; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Ignore; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; import org.springframework.integration.channel.QueueChannel; @@ -57,6 +60,7 @@ public class PseudoTransactionalMessageSourceTests { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); + syncProcessor.setBeanFactory(mock(BeanFactory.class)); PollableChannel queueChannel = new QueueChannel(); syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix")); syncProcessor.setBeforeCommitChannel(queueChannel); @@ -101,6 +105,7 @@ public class PseudoTransactionalMessageSourceTests { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); + syncProcessor.setBeanFactory(mock(BeanFactory.class)); PollableChannel queueChannel = new QueueChannel(); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); @@ -142,6 +147,7 @@ public class PseudoTransactionalMessageSourceTests { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); + syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setBeforeCommitExpression(new SpelExpressionParser().parseExpression("#bix")); syncProcessor.setBeforeCommitChannel(queueChannel); syncProcessor.setAfterCommitChannel(queueChannel); @@ -187,6 +193,7 @@ public class PseudoTransactionalMessageSourceTests { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); + syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); @@ -230,6 +237,7 @@ public class PseudoTransactionalMessageSourceTests { SourcePollingChannelAdapter adapter = new SourcePollingChannelAdapter(); ExpressionEvaluatingTransactionSynchronizationProcessor syncProcessor = new ExpressionEvaluatingTransactionSynchronizationProcessor(); + syncProcessor.setBeanFactory(mock(BeanFactory.class)); syncProcessor.setAfterRollbackChannel(queueChannel); syncProcessor.setAfterRollbackExpression(new SpelExpressionParser().parseExpression("#baz")); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/expression/ExpressionUtilsTests.java b/spring-integration-core/src/test/java/org/springframework/integration/expression/ExpressionUtilsTests.java index 010491f8b3..8782284d5c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/expression/ExpressionUtilsTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/expression/ExpressionUtilsTests.java @@ -75,7 +75,7 @@ public class ExpressionUtilsTests { new RootBeanDefinition(ConversionServiceFactoryBean.class)); context.refresh(); StandardEvaluationContext evalContext = ExpressionUtils.createStandardEvaluationContext(context); - assertNull(evalContext.getBeanResolver()); + assertNotNull(evalContext.getBeanResolver()); TypeConverter typeConverter = evalContext.getTypeConverter(); assertNotNull(typeConverter); assertNotSame(TestUtils.getPropertyValue(typeConverter, "defaultConversionService"), diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/AsyncGatewayTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/AsyncGatewayTests.java index dd20662677..c21fa01040 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/AsyncGatewayTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/AsyncGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -18,12 +18,14 @@ package org.springframework.integration.gateway; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; import org.springframework.integration.channel.QueueChannel; @@ -33,12 +35,13 @@ import org.springframework.integration.message.GenericMessage; /** * @author Mark Fisher * @author Oleg Zhurakousky + * @author Gary Russell * @since 2.0 */ public class AsyncGatewayTests { // TODO: changed from 0 because of recurrent failure: is this right? - private long safety = 100; + private final long safety = 100; @Test public void futureWithMessageReturned() throws Exception { @@ -48,6 +51,7 @@ public class AsyncGatewayTests { proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setServiceInterface(TestEchoService.class); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestEchoService service = (TestEchoService) proxyFactory.getObject(); Future> f = service.returnMessage("foo"); @@ -67,6 +71,7 @@ public class AsyncGatewayTests { proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setServiceInterface(TestEchoService.class); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestEchoService service = (TestEchoService) proxyFactory.getObject(); Future f = service.returnString("foo"); @@ -87,6 +92,7 @@ public class AsyncGatewayTests { proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setServiceInterface(TestEchoService.class); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestEchoService service = (TestEchoService) proxyFactory.getObject(); Future f = service.returnSomething("foo"); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapperToMessageTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapperToMessageTests.java index b0b093f95b..e076aa4b1a 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapperToMessageTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayMethodInboundMessageMapperToMessageTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,7 @@ package org.springframework.integration.gateway; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import java.lang.reflect.Method; import java.util.HashMap; @@ -25,6 +26,7 @@ import java.util.Map; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -36,6 +38,7 @@ import org.springframework.integration.support.MessageBuilder; /** * @author Mark Fisher + * @author Gary Russell */ public class GatewayMethodInboundMessageMapperToMessageTests { @@ -43,6 +46,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithPayload() throws Exception { Method method = TestService.class.getMethod("sendPayload", String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test" }); assertEquals("test", message.getPayload()); } @@ -51,6 +55,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithTooManyParameters() throws Exception { Method method = TestService.class.getMethod("sendPayload", String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.toMessage(new Object[] { "test" , "oops" }); } @@ -58,6 +63,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithEmptyParameterArray() throws Exception { Method method = TestService.class.getMethod("sendPayload", String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.toMessage(new Object[] {}); } @@ -66,6 +72,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndHeader", String.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test", "bar" }); assertEquals("test", message.getPayload()); assertEquals("bar", message.getHeaders().get("foo")); @@ -76,6 +83,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndHeader", String.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.toMessage(new Object[] { "test", null }); } @@ -84,6 +92,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndOptionalHeader", String.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test", "bar" }); assertEquals("test", message.getPayload()); assertEquals("bar", message.getHeaders().get("foo")); @@ -94,6 +103,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndOptionalHeader", String.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test", null }); assertEquals("test", message.getPayload()); assertNull(message.getHeaders().get("foo")); @@ -104,6 +114,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndHeadersMap", String.class, Map.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Map headers = new HashMap(); headers.put("abc", 123); headers.put("def", 456); @@ -118,6 +129,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndHeadersMap", String.class, Map.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test", null }); assertEquals("test", message.getPayload()); } @@ -127,6 +139,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { Method method = TestService.class.getMethod( "sendPayloadAndHeadersMap", String.class, Map.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Map headers = new HashMap(); headers.put(123, "abc"); mapper.toMessage(new Object[] { "test", headers }); @@ -136,6 +149,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithMessageParameter() throws Exception { Method method = TestService.class.getMethod("sendMessage", Message.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message inputMessage = MessageBuilder.withPayload("test message").build(); Message message = mapper.toMessage(new Object[] { inputMessage }); assertEquals("test message", message.getPayload()); @@ -145,6 +159,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithMessageParameterAndHeader() throws Exception { Method method = TestService.class.getMethod("sendMessageAndHeader", Message.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message inputMessage = MessageBuilder.withPayload("test message").build(); Message message = mapper.toMessage(new Object[] { inputMessage, "bar" }); assertEquals("test message", message.getPayload()); @@ -155,6 +170,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithMessageParameterAndRequiredHeaderButNullValue() throws Exception { Method method = TestService.class.getMethod("sendMessageAndHeader", Message.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message inputMessage = MessageBuilder.withPayload("test message").build(); mapper.toMessage(new Object[] { inputMessage, null }); } @@ -163,6 +179,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithMessageParameterAndOptionalHeaderWithValue() throws Exception { Method method = TestService.class.getMethod("sendMessageAndOptionalHeader", Message.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message inputMessage = MessageBuilder.withPayload("test message").build(); Message message = mapper.toMessage(new Object[] { inputMessage, "bar" }); assertEquals("test message", message.getPayload()); @@ -173,6 +190,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void toMessageWithMessageParameterAndOptionalHeaderWithNull() throws Exception { Method method = TestService.class.getMethod("sendMessageAndOptionalHeader", Message.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message inputMessage = MessageBuilder.withPayload("test message").build(); Message message = mapper.toMessage(new Object[] { inputMessage, null }); assertEquals("test message", message.getPayload()); @@ -183,6 +201,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void noArgs() throws Exception { Method method = TestService.class.getMethod("noArgs", new Class[] {}); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.toMessage(new Object[] {}); } @@ -190,6 +209,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { public void onlyHeaders() throws Exception { Method method = TestService.class.getMethod("onlyHeaders", String.class, String.class); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.toMessage(new Object[] { "abc", "def" }); } @@ -201,6 +221,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { headers.put("bar", new SpelExpressionParser().parseExpression("6 * 7")); headers.put("baz", new LiteralExpression("hello")); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method, headers); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { "test" }); assertEquals("test", message.getPayload()); assertEquals("foo", message.getHeaders().get("foo")); @@ -214,6 +235,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { map.put(1, "One"); map.put(2, "Two"); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.setPayloadExpression("'hello'"); Message message = mapper.toMessage(new Object[] { map }); assertEquals("hello", message.getPayload()); @@ -226,6 +248,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { map.put(1, "One"); map.put(2, "Two"); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.setPayloadExpression("#args[0]"); Message message = mapper.toMessage(new Object[] { map }); assertEquals(map, message.getPayload()); @@ -238,6 +261,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { map.put(1, "One"); map.put(2, "Two"); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); Message message = mapper.toMessage(new Object[] { map }); assertEquals(map, message.getPayload()); } @@ -252,6 +276,7 @@ public class GatewayMethodInboundMessageMapperToMessageTests { mapB.put("1", "ONE"); mapB.put("2", "TWO"); GatewayMethodInboundMessageMapper mapper = new GatewayMethodInboundMessageMapper(method); + mapper.setBeanFactory(mock(BeanFactory.class)); mapper.setPayloadExpression("#args[0]"); Message message = mapper.toMessage(new Object[] { mapA, mapB }); assertEquals(mapA, message.getPayload()); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java index a6469785b2..1260680e02 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java @@ -18,6 +18,7 @@ package org.springframework.integration.gateway; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; import java.lang.reflect.Method; import java.util.Random; @@ -28,6 +29,8 @@ import java.util.concurrent.TimeUnit; import org.junit.Test; import org.mockito.Mockito; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -49,6 +52,7 @@ import org.springframework.util.ReflectionUtils; * @author Mark Fisher * @author Oleg Zhurakousky * @author Gunnar Hillert + * @author Gary Russell */ public class GatewayProxyFactoryBeanTests { @@ -59,6 +63,7 @@ public class GatewayProxyFactoryBeanTests { GatewayProxyFactoryBean proxyFactory = new GatewayProxyFactoryBean(); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setServiceInterface(TestService.class); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.setBeanName("testGateway"); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); @@ -100,6 +105,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setServiceInterface(TestService.class); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); service.oneWay("test"); @@ -138,6 +144,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setServiceInterface(TestService.class); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); Integer result = service.requestReplyWithIntegers(123); @@ -207,6 +214,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setServiceInterface(TestService.class); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); String result = service.requestReplyWithMessageParameter(new GenericMessage("foo")); @@ -221,6 +229,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setServiceInterface(TestService.class); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); String result = service.requestReplyWithPayloadAnnotation(); @@ -241,6 +250,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setServiceInterface(TestService.class); proxyFactory.setDefaultRequestChannel(requestChannel); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestService service = (TestService) proxyFactory.getObject(); Message result = service.requestReplyWithMessageReturnValue("foo"); @@ -269,6 +279,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setDefaultRequestChannel(new DirectChannel()); proxyFactory.setServiceInterface(TestService.class); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); Object proxy = proxyFactory.getObject(); String expected = "gateway proxy for"; @@ -290,6 +301,7 @@ public class GatewayProxyFactoryBeanTests { proxyFactory.setDefaultRequestChannel(channel); proxyFactory.setServiceInterface(TestExceptionThrowingInterface.class); proxyFactory.setBeanName("testGateway"); + proxyFactory.setBeanFactory(mock(BeanFactory.class)); proxyFactory.afterPropertiesSet(); TestExceptionThrowingInterface proxy = (TestExceptionThrowingInterface) proxyFactory.getObject(); proxy.throwCheckedException("test"); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/DelayHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/DelayHandlerTests.java index 8238c63156..185550cde5 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/DelayHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/DelayHandlerTests.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.Date; import java.util.concurrent.CountDownLatch; @@ -31,6 +32,7 @@ import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.support.StaticApplicationContext; import org.springframework.expression.Expression; @@ -71,7 +73,7 @@ public class DelayHandlerTests { private DelayHandler delayHandler; - private ResultHandler resultHandler = new ResultHandler(); + private final ResultHandler resultHandler = new ResultHandler(); @Before public void setup() { @@ -81,6 +83,7 @@ public class DelayHandlerTests { taskScheduler.afterPropertiesSet(); delayHandler = new DelayHandler(DELAYER_MESSAGE_GROUP_ID, taskScheduler); delayHandler.setOutputChannel(output); + delayHandler.setBeanFactory(mock(BeanFactory.class)); input.subscribe(delayHandler); output.subscribe(resultHandler); } @@ -399,6 +402,7 @@ public class DelayHandlerTests { this.delayHandler.setOutputChannel(output); this.delayHandler.setDefaultDelay(200); this.delayHandler.setMessageStore(messageGroupStore); + this.delayHandler.setBeanFactory(mock(BeanFactory.class)); this.startDelayerHandler(); assertTrue(this.latch.await(10, TimeUnit.SECONDS)); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java index 4d6e067dd5..f21c648dae 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessorTests.java @@ -15,6 +15,7 @@ package org.springframework.integration.handler; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.Arrays; @@ -26,6 +27,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -49,6 +51,7 @@ import org.springframework.integration.test.util.TestUtils; * @author Dave Syer * @author Mark Fisher * @author Gunnar Hillert + * @author Gary Russell * @since 2.0 */ public class ExpressionEvaluatingMessageProcessorTests { @@ -67,6 +70,7 @@ public class ExpressionEvaluatingMessageProcessorTests { public void testProcessMessage() { Expression expression = expressionParser.parseExpression("payload"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); assertEquals("foo", processor.processMessage(new GenericMessage("foo"))); } @@ -81,6 +85,7 @@ public class ExpressionEvaluatingMessageProcessorTests { } Expression expression = expressionParser.parseExpression("#target.stringify(payload)"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); processor.afterPropertiesSet(); EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class); evaluationContext.setVariable("target", new TestTarget()); @@ -97,6 +102,7 @@ public class ExpressionEvaluatingMessageProcessorTests { } Expression expression = expressionParser.parseExpression("#target.ping(payload)"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); processor.afterPropertiesSet(); EvaluationContext evaluationContext = TestUtils.getPropertyValue(processor, "evaluationContext", EvaluationContext.class); evaluationContext.setVariable("target", new TestTarget()); @@ -133,6 +139,7 @@ public class ExpressionEvaluatingMessageProcessorTests { public void testProcessMessageWithDollarInBrackets() { Expression expression = expressionParser.parseExpression("headers['$foo_id']"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); Message message = MessageBuilder.withPayload("foo").setHeader("$foo_id", "abc").build(); assertEquals("abc", processor.processMessage(message)); } @@ -142,6 +149,7 @@ public class ExpressionEvaluatingMessageProcessorTests { public void testProcessMessageWithDollarPropertyAccess() { Expression expression = expressionParser.parseExpression("headers.$foo_id"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); Message message = MessageBuilder.withPayload("foo").setHeader("$foo_id", "xyz").build(); assertEquals("xyz", processor.processMessage(message)); } @@ -151,6 +159,7 @@ public class ExpressionEvaluatingMessageProcessorTests { public void testProcessMessageWithStaticKey() { Expression expression = expressionParser.parseExpression("headers[headers.ID]"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); GenericMessage message = new GenericMessage("foo"); assertEquals(message.getHeaders().getId(), processor.processMessage(message)); } @@ -205,6 +214,7 @@ public class ExpressionEvaluatingMessageProcessorTests { }); Expression expression = expressionParser.parseExpression("payload.fixMe()"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); assertEquals("foo", processor.processMessage(new GenericMessage("foo"))); } @@ -225,6 +235,7 @@ public class ExpressionEvaluatingMessageProcessorTests { }); Expression expression = expressionParser.parseExpression("payload.throwRuntimeException()"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); assertEquals("foo", processor.processMessage(new GenericMessage(new TestPayload()))); } @@ -245,6 +256,7 @@ public class ExpressionEvaluatingMessageProcessorTests { }); Expression expression = expressionParser.parseExpression("payload.throwCheckedException()"); ExpressionEvaluatingMessageProcessor processor = new ExpressionEvaluatingMessageProcessor(expression); + processor.setBeanFactory(mock(BeanFactory.class)); assertEquals("foo", processor.processMessage(new GenericMessage(new TestPayload()))); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/AdvisedMessageHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/AdvisedMessageHandlerTests.java index 3a5abba80b..04a86db5c0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/AdvisedMessageHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/advice/AdvisedMessageHandlerTests.java @@ -49,6 +49,7 @@ import org.mockito.stubbing.Answer; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.AopUtils; import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.MessageHandlingException; import org.springframework.integration.MessagingException; @@ -105,6 +106,7 @@ public class AdvisedMessageHandlerTests { PollableChannel successChannel = new QueueChannel(); PollableChannel failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); + advice.setBeanFactory(mock(BeanFactory.class)); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("'foo'"); @@ -183,6 +185,7 @@ public class AdvisedMessageHandlerTests { PollableChannel successChannel = new QueueChannel(); PollableChannel failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); + advice.setBeanFactory(mock(BeanFactory.class)); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("1/0"); @@ -657,6 +660,7 @@ public class AdvisedMessageHandlerTests { List adviceChain = new ArrayList(); ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice(); + expressionAdvice.setBeanFactory(mock(BeanFactory.class)); // MessagingException / RuntimeException expressionAdvice.setOnFailureExpression("#exception.cause.message"); expressionAdvice.setReturnFailureExpressionResult(true); @@ -704,6 +708,7 @@ public class AdvisedMessageHandlerTests { List adviceChain = new ArrayList(); ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice(); + expressionAdvice.setBeanFactory(mock(BeanFactory.class)); expressionAdvice.setOnFailureExpression("#exception.message"); expressionAdvice.setFailureChannel(errors); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java index 4c639266df..37cebae97c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java @@ -1 +1 @@ -/* * Copyright 2002-2011 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.integration.message; import org.junit.Before; import org.junit.Test; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.core.MessageHandler; import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; import java.util.HashMap; import static org.junit.Assert.assertEquals; /** * @author Artem Bilan * @since 2.1 */ public class ExpressionEvaluatingMessageHandlerTests { private ExpressionParser parser; @Before public void setup() { parser = new SpelExpressionParser(); } @Test public void validExpression() { Expression expression = parser.parseExpression("T(System).out.println(payload)"); MessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithNoArgs() { Expression expression = parser.parseExpression("T(System).out.println()"); MessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithSomeArgs() { Expression expression = parser.parseExpression("T(System).out.write(payload.bytes, 0, headers.offset)"); MessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); HashMap headers = new HashMap(); headers.put("offset", 4); handler.handleMessage(new GenericMessage("testtest", headers)); } @Test(expected = MessagingException.class) public void expressionWithReturnValue() { Message message = new GenericMessage(.1f); try { Expression expression = parser.parseExpression("T(System).out.printf('$%4.2f', payload)"); MessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.handleMessage(message); } catch (MessagingException e) { assertEquals(e.getFailedMessage(), message); throw e; } } } \ No newline at end of file +/* * Copyright 2002-2013 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. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.integration.message; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import java.util.HashMap; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; /** * @author Artem Bilan * @author Gary Russell * @since 2.1 */ public class ExpressionEvaluatingMessageHandlerTests { private ExpressionParser parser; @Before public void setup() { parser = new SpelExpressionParser(); } @Test public void validExpression() { Expression expression = parser.parseExpression("T(System).out.println(payload)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithNoArgs() { Expression expression = parser.parseExpression("T(System).out.println()"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithSomeArgs() { Expression expression = parser.parseExpression("T(System).out.write(payload.bytes, 0, headers.offset)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); HashMap headers = new HashMap(); headers.put("offset", 4); handler.handleMessage(new GenericMessage("testtest", headers)); } @Test(expected = MessagingException.class) public void expressionWithReturnValue() { Message message = new GenericMessage(.1f); try { Expression expression = parser.parseExpression("T(System).out.printf('$%4.2f', payload)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(message); } catch (MessagingException e) { assertEquals(e.getFailedMessage(), message); throw e; } } } \ No newline at end of file diff --git a/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java index bfee7ee3ed..500bafbffe 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/message/MethodInvokingMessageSourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -19,11 +19,14 @@ package org.springframework.integration.message; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import java.util.HashMap; import java.util.Map; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -33,6 +36,7 @@ import org.springframework.integration.endpoint.MethodInvokingMessageSource; /** * @author Mark Fisher + * @author Gary Russell */ public class MethodInvokingMessageSourceTests { @@ -53,6 +57,7 @@ public class MethodInvokingMessageSourceTests { headerExpressions.put("foo", new LiteralExpression("abc")); headerExpressions.put("bar", new SpelExpressionParser().parseExpression("new Integer(123)")); MethodInvokingMessageSource source = new MethodInvokingMessageSource(); + source.setBeanFactory(mock(BeanFactory.class)); source.setObject(new TestBean()); source.setMethodName("validMethod"); source.setHeaderExpressions(headerExpressions); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java b/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java index 63c7d3752e..8d5f4cbe7d 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/transformer/ContentEnricherTests.java @@ -54,6 +54,7 @@ import org.springframework.scheduling.support.PeriodicTrigger; * @author Mark Fisher * @author Gunnar Hillert * @author Artem Bilan + * @author Gary Russell * * @since 2.1 */ @@ -106,6 +107,7 @@ public class ContentEnricherTests { enricher.setPropertyExpressions(expressions); enricher.setRequiresReply(true); enricher.setBeanName("Enricher"); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); final AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() { @@ -161,6 +163,7 @@ public class ContentEnricherTests { ContentEnricher enricher = new ContentEnricher(); enricher.setRequestChannel(requestChannel); enricher.setRequestTimeout(requestTimeout); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); Target target = new Target("replace me"); @@ -194,6 +197,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); Target target = new Target("replace me"); @@ -210,6 +214,7 @@ public class ContentEnricherTests { ContentEnricher enricher = new ContentEnricher(); enricher.setReplyChannel(replyChannel); + enricher.setBeanFactory(mock(BeanFactory.class)); try { enricher.afterPropertiesSet(); @@ -225,6 +230,7 @@ public class ContentEnricherTests { public void setNullReplyTimeout() { ContentEnricher enricher = new ContentEnricher(); + enricher.setBeanFactory(mock(BeanFactory.class)); try { enricher.setReplyTimeout(null); @@ -240,6 +246,7 @@ public class ContentEnricherTests { public void setNullRequestTimeout() { ContentEnricher enricher = new ContentEnricher(); + enricher.setBeanFactory(mock(BeanFactory.class)); try { enricher.setRequestTimeout(null); @@ -259,6 +266,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("'just a static string'")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); Target target = new Target("replace me"); Message requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build(); @@ -272,6 +280,7 @@ public class ContentEnricherTests { ContentEnricher enricher = new ContentEnricher(); enricher.setReplyChannel(new QueueChannel()); + enricher.setBeanFactory(mock(BeanFactory.class)); try { enricher.afterPropertiesSet(); @@ -300,6 +309,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("child.name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); Target target = new Target("test"); @@ -329,6 +339,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); Target target = new Target("replace me"); @@ -358,6 +369,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); TargetUser target = new TargetUser(); @@ -390,6 +402,7 @@ public class ContentEnricherTests { Map propertyExpressions = new HashMap(); propertyExpressions.put("name", parser.parseExpression("payload.lastName + ', ' + payload.firstName")); enricher.setPropertyExpressions(propertyExpressions); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); UncloneableTargetUser target = new UncloneableTargetUser(); @@ -411,6 +424,7 @@ public class ContentEnricherTests { @Test public void testLifeCycleMethodsWithoutRequestChannel() { ContentEnricher enricher = new ContentEnricher(); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); @@ -432,6 +446,7 @@ public class ContentEnricherTests { ContentEnricher enricher = new ContentEnricher(); enricher.setRequestChannel(requestChannel); + enricher.setBeanFactory(mock(BeanFactory.class)); enricher.afterPropertiesSet(); diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java index 48b785a9b0..35fe06a475 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/FileWritingMessageHandler.java @@ -26,6 +26,7 @@ import java.nio.charset.Charset; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.expression.Expression; import org.springframework.expression.common.LiteralExpression; import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -82,6 +83,8 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand private volatile FileNameGenerator fileNameGenerator = new DefaultFileNameGenerator(); + private volatile boolean fileNameGeneratorSet; + private volatile StandardEvaluationContext evaluationContext; private final Expression destinationDirectoryExpression; @@ -190,6 +193,7 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand public void setFileNameGenerator(FileNameGenerator fileNameGenerator) { Assert.notNull(fileNameGenerator, "FileNameGenerator must not be null"); this.fileNameGenerator = fileNameGenerator; + this.fileNameGeneratorSet = true; } /** @@ -226,6 +230,9 @@ public class FileWritingMessageHandler extends AbstractReplyProducingMessageHand validateDestinationDirectory(directory, this.autoCreateDirectory); } + if (!this.fileNameGeneratorSet && this.fileNameGenerator instanceof BeanFactoryAware) { + ((BeanFactoryAware) this.fileNameGenerator).setBeanFactory(this.getBeanFactory()); + } } private void validateDestinationDirectory(File destinationDirectory, boolean autoCreateDirectory) { diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index 6163954459..b9a5fa8c51 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -285,6 +285,7 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } if (this.getBeanFactory() != null) { this.fileNameProcessor.setBeanFactory(this.getBeanFactory()); + this.renameProcessor.setBeanFactory(this.getBeanFactory()); } } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/AutoCreateDirectoryTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/AutoCreateDirectoryTests.java index d36fb087ac..3eacfea3ba 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/AutoCreateDirectoryTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/AutoCreateDirectoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2013 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,16 +16,20 @@ package org.springframework.integration.file; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.io.File; + import org.junit.After; import org.junit.Before; import org.junit.Test; -import java.io.File; - -import static org.junit.Assert.assertTrue; +import org.springframework.beans.factory.BeanFactory; /** * @author Mark Fisher + * @author Gary Russell * @since 1.0.3 */ public class AutoCreateDirectoryTests { @@ -58,6 +62,7 @@ public class AutoCreateDirectoryTests { public void autoCreateForInboundEnabledByDefault() { FileReadingMessageSource source = new FileReadingMessageSource(); source.setDirectory(new File(INBOUND_PATH)); + source.setBeanFactory(mock(BeanFactory.class)); source.afterPropertiesSet(); assertTrue(new File(INBOUND_PATH).exists()); } @@ -67,6 +72,7 @@ public class AutoCreateDirectoryTests { FileReadingMessageSource source = new FileReadingMessageSource(); source.setDirectory(new File(INBOUND_PATH)); source.setAutoCreateDirectory(false); + source.setBeanFactory(mock(BeanFactory.class)); source.afterPropertiesSet(); } @@ -74,6 +80,7 @@ public class AutoCreateDirectoryTests { public void autoCreateForOutboundEnabledByDefault() { FileWritingMessageHandler handler = new FileWritingMessageHandler( new File(OUTBOUND_PATH)); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); assertTrue(new File(OUTBOUND_PATH).exists()); } @@ -82,6 +89,7 @@ public class AutoCreateDirectoryTests { public void autoCreateForOutboundDisabled() { FileWritingMessageHandler handler = new FileWritingMessageHandler( new File(OUTBOUND_PATH)); + handler.setBeanFactory(mock(BeanFactory.class)); handler.setAutoCreateDirectory(false); handler.afterPropertiesSet(); } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java index 31a0132cfa..c2f1f4d7af 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2013 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,24 +17,27 @@ package org.springframework.integration.file; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import java.io.File; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.support.MessageBuilder; /** * @author Mark Fisher + * @author Gary Russell */ public class DefaultFileNameGeneratorTests { @Test public void defaultHeaderNamePresent() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); - Message message = MessageBuilder.withPayload("test") - .setHeader(FileHeaders.FILENAME, "foo").build(); + generator.setBeanFactory(mock(BeanFactory.class)); + Message message = MessageBuilder.withPayload("test").setHeader(FileHeaders.FILENAME, "foo").build(); String filename = generator.generateFileName(message); assertEquals("foo", filename); } @@ -42,6 +45,7 @@ public class DefaultFileNameGeneratorTests { @Test public void defaultHeaderNameNotPresent() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); Message message = MessageBuilder.withPayload("test").build(); String filename = generator.generateFileName(message); assertEquals(message.getHeaders().getId() + ".msg", filename); @@ -50,8 +54,9 @@ public class DefaultFileNameGeneratorTests { @Test public void defaultHeaderNameNotString() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); - Message message = MessageBuilder.withPayload("test") - .setHeader(FileHeaders.FILENAME, new Integer(123)).build(); + generator.setBeanFactory(mock(BeanFactory.class)); + Message message = MessageBuilder.withPayload("test").setHeader(FileHeaders.FILENAME, new Integer(123)) + .build(); String filename = generator.generateFileName(message); assertEquals(message.getHeaders().getId() + ".msg", filename); } @@ -59,9 +64,9 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNamePresent() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); - Message message = MessageBuilder.withPayload("test") - .setHeader("foo", "bar").build(); + Message message = MessageBuilder.withPayload("test").setHeader("foo", "bar").build(); String filename = generator.generateFileName(message); assertEquals("bar", filename); } @@ -69,6 +74,7 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNameNotPresent() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); Message message = MessageBuilder.withPayload("test").build(); String filename = generator.generateFileName(message); @@ -78,9 +84,9 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNameNotString() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); - Message message = MessageBuilder.withPayload("test") - .setHeader("foo", new Integer(123)).build(); + Message message = MessageBuilder.withPayload("test").setHeader("foo", new Integer(123)).build(); String filename = generator.generateFileName(message); assertEquals(message.getHeaders().getId() + ".msg", filename); } @@ -88,18 +94,19 @@ public class DefaultFileNameGeneratorTests { @Test public void filePayloadPresent() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); File payload = new File("/some/path/foo"); Message message = MessageBuilder.withPayload(payload).build(); String filename = generator.generateFileName(message); - assertEquals("foo", filename); + assertEquals("foo", filename); } @Test public void defaultHeaderNameTakesPrecedenceOverFilePayload() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); File payload = new File("/some/path/ignore"); - Message message = MessageBuilder.withPayload(payload) - .setHeader(FileHeaders.FILENAME, "foo").build(); + Message message = MessageBuilder.withPayload(payload).setHeader(FileHeaders.FILENAME, "foo").build(); String filename = generator.generateFileName(message); assertEquals("foo", filename); } @@ -107,10 +114,10 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNameTakesPrecedenceOverFilePayload() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); File payload = new File("/some/path/ignore"); - Message message = MessageBuilder.withPayload(payload) - .setHeader("foo", "bar").build(); + Message message = MessageBuilder.withPayload(payload).setHeader("foo", "bar").build(); String filename = generator.generateFileName(message); assertEquals("bar", filename); } @@ -118,9 +125,9 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNameTakesPrecedenceOverDefault() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); - Message message = MessageBuilder.withPayload("test") - .setHeader(FileHeaders.FILENAME, "ignore") + Message message = MessageBuilder.withPayload("test").setHeader(FileHeaders.FILENAME, "ignore") .setHeader("foo", "bar").build(); String filename = generator.generateFileName(message); assertEquals("bar", filename); @@ -129,10 +136,10 @@ public class DefaultFileNameGeneratorTests { @Test public void customHeaderNameTakesPrecedenceOverFilePayloadAndDefault() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); generator.setHeaderName("foo"); File payload = new File("/some/path/ignore1"); - Message message = MessageBuilder.withPayload(payload) - .setHeader(FileHeaders.FILENAME, "ignore2") + Message message = MessageBuilder.withPayload(payload).setHeader(FileHeaders.FILENAME, "ignore2") .setHeader("foo", "bar").build(); String filename = generator.generateFileName(message); assertEquals("bar", filename); diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/FileOutboundChannelAdapterIntegrationTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/FileOutboundChannelAdapterIntegrationTests.java index 499e9835cc..fcad325a99 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/FileOutboundChannelAdapterIntegrationTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/FileOutboundChannelAdapterIntegrationTests.java @@ -19,12 +19,12 @@ package org.springframework.integration.file; import java.io.File; import java.io.FileOutputStream; -import org.junit.Assert; - import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.Message; import org.springframework.integration.MessageChannel; @@ -38,6 +38,7 @@ import org.springframework.util.FileCopyUtils; /** * @author Gunnar Hillert * @author Artem Bilan + * @author Gary Russell */ @ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java index f132515da4..276b6b725e 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/FileWritingMessageHandlerTests.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.io.File; import java.io.FileOutputStream; @@ -35,6 +36,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.MessageHandlingException; import org.springframework.integration.channel.NullChannel; @@ -66,6 +68,8 @@ public class FileWritingMessageHandlerTests { super.create(); outputDirectory = temp.newFolder("outputDirectory"); handler = new FileWritingMessageHandler(outputDirectory); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); sourceFile = temp.newFile("sourceFile"); FileCopyUtils.copy(SAMPLE_CONTENT.getBytes(DEFAULT_ENCODING), new FileOutputStream(sourceFile, false)); diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java index 7b13d3c9ef..80213429b7 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/remote/gateway/RemoteFileOutboundGatewayTests.java @@ -39,6 +39,8 @@ import java.util.concurrent.atomic.AtomicReference; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.file.FileHeaders; @@ -120,6 +122,7 @@ public class RemoteFileOutboundGatewayTests { Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/x/")).thenReturn(files); @@ -303,6 +306,7 @@ public class RemoteFileOutboundGatewayTests { SessionFactory sessionFactory = mock(SessionFactory.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "mv", "payload"); + gw.afterPropertiesSet(); Session session = mock(Session.class); final AtomicReference args = new AtomicReference(); doAnswer(new Answer() { @@ -329,6 +333,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "mv", "payload"); gw.setRenameExpression("payload.substring(1)"); + gw.afterPropertiesSet(); Session session = mock(Session.class); final AtomicReference args = new AtomicReference(); doAnswer(new Answer() { @@ -353,6 +358,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "mv", "payload"); gw.setRenameExpression("'foo/bar/baz'"); + gw.afterPropertiesSet(); Session session = mock(Session.class); final AtomicReference args = new AtomicReference(); doAnswer(new Answer() { @@ -404,6 +410,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-f"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/x/")).thenReturn(files); @@ -423,6 +430,7 @@ public class RemoteFileOutboundGatewayTests { Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = new TestLsEntry[0]; when(session.list("testremote/")).thenReturn(files); @@ -439,6 +447,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-1"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -457,6 +466,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-1 -f"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -475,6 +485,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-1 -dirs"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -494,6 +505,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-1 -dirs -links"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -514,6 +526,7 @@ public class RemoteFileOutboundGatewayTests { TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "ls", "payload"); gw.setOptions("-1 -a -f -dirs -links"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -537,6 +550,7 @@ public class RemoteFileOutboundGatewayTests { (sessionFactory, "ls", "payload"); gw.setOptions("-1 -a -f -dirs -links"); gw.setFilter(new TestPatternFilter("*4")); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); TestLsEntry[] files = fileList(); when(session.list("testremote/")).thenReturn(files); @@ -721,6 +735,7 @@ public class RemoteFileOutboundGatewayTests { Session session = mock(Session.class); TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway (sessionFactory, "rm", "payload"); + gw.afterPropertiesSet(); when(sessionFactory.getSession()).thenReturn(session); when(session.remove("testremote/x/f1")).thenReturn(Boolean.TRUE); @SuppressWarnings("unchecked") @@ -742,6 +757,7 @@ class TestRemoteFileOutboundGateway extends AbstractRemoteFileOutboundGateway handler = new FileTransferringMessageHandler(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("''")); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); @@ -98,6 +100,7 @@ public class FileTransferringMessageHandlerTests { FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sf); handler.setRemoteDirectoryExpression(new LiteralExpression("foo")); handler.setTemporaryRemoteDirectoryExpression(new LiteralExpression("bar")); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello")); verify(session, times(1)).write(Mockito.any(InputStream.class), Mockito.anyString()); @@ -122,6 +125,7 @@ public class FileTransferringMessageHandlerTests { ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message message = MessageBuilder.withPayload("hello").setHeader("path", null).build(); handler.handleMessage(message); @@ -136,6 +140,7 @@ public class FileTransferringMessageHandlerTests { when(sf.getSession()).thenReturn(session); ExpressionParser parser = new SpelExpressionParser(); FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sf); + handler.setBeanFactory(mock(BeanFactory.class)); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); handler.setTemporaryFileSuffix(null); handler.onInit(); @@ -153,6 +158,7 @@ public class FileTransferringMessageHandlerTests { FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sf); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['path']")); handler.setUseTemporaryFileName(false); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message message = MessageBuilder.withPayload("hello").setHeader("path", null).build(); handler.handleMessage(message); @@ -166,6 +172,7 @@ public class FileTransferringMessageHandlerTests { SessionFactory sf = mock(SessionFactory.class); CachingSessionFactory csf = new CachingSessionFactory(sf, 2); FileTransferringMessageHandler handler = new FileTransferringMessageHandler(csf); + handler.setBeanFactory(mock(BeanFactory.class)); Session session1 = newSession(); Session session2 = newSession(); Session session3 = newSession(); diff --git a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpOutboundTests.java b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpOutboundTests.java index 2008fe0e6f..cb088f65da 100644 --- a/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpOutboundTests.java +++ b/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/outbound/FtpOutboundTests.java @@ -26,7 +26,11 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.List; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; @@ -35,6 +39,8 @@ import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.expression.common.LiteralExpression; @@ -53,6 +59,7 @@ import org.springframework.util.FileCopyUtils; * @author Oleg Zhurakousky * @author Artem Bilan * @author Gunnar Hillert + * @author Gary Russell */ public class FtpOutboundTests { @@ -83,6 +90,7 @@ public class FtpOutboundTests { return "handlerContent.test"; } }); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello")); assertTrue(file.exists()); @@ -102,6 +110,7 @@ public class FtpOutboundTests { return "handlerContent.test"; } }); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello".getBytes())); assertTrue(file.exists()); @@ -119,6 +128,7 @@ public class FtpOutboundTests { return ((File)message.getPayload()).getName() + ".test"; } }); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); File srcFile = File.createTempFile("testHandleFileMessage", ".tmp"); diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java index 71b8041dd3..949f16e8eb 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java @@ -19,8 +19,11 @@ package org.springframework.integration.gemfire.inbound; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.RegionAttributesFactoryBean; import org.springframework.data.gemfire.RegionFactoryBean; @@ -52,6 +55,7 @@ public class CacheListeningMessageProducerTests { CacheListeningMessageProducer producer = new CacheListeningMessageProducer(region); producer.setPayloadExpression("key + '=' + newValue"); producer.setOutputChannel(channel); + producer.setBeanFactory(mock(BeanFactory.class)); producer.afterPropertiesSet(); producer.start(); assertNull(channel.receive(0)); @@ -77,6 +81,7 @@ public class CacheListeningMessageProducerTests { CacheListeningMessageProducer producer = new CacheListeningMessageProducer(region); producer.setPayloadExpression("newValue"); producer.setOutputChannel(channel); + producer.setBeanFactory(mock(BeanFactory.class)); producer.afterPropertiesSet(); producer.start(); assertNull(channel.receive(0)); @@ -107,6 +112,7 @@ public class CacheListeningMessageProducerTests { producer.setSupportedEventTypes(EventType.DESTROYED); producer.setPayloadExpression("oldValue"); producer.setOutputChannel(channel); + producer.setBeanFactory(mock(BeanFactory.class)); producer.afterPropertiesSet(); producer.start(); assertNull(channel.receive(0)); @@ -135,6 +141,7 @@ public class CacheListeningMessageProducerTests { producer.setSupportedEventTypes(EventType.INVALIDATED); producer.setPayloadExpression("key + ' was ' + oldValue"); producer.setOutputChannel(channel); + producer.setBeanFactory(mock(BeanFactory.class)); producer.afterPropertiesSet(); producer.start(); assertNull(channel.receive(0)); diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingControllerTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingControllerTests.java index df935be8d2..4ee71f2da7 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingControllerTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingControllerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -30,6 +30,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.http.HttpStatus; @@ -57,6 +59,7 @@ public class HttpRequestHandlingControllerTests { public void sendOnly() throws Exception { QueueChannel requestChannel = new QueueChannel(); HttpRequestHandlingController controller = new HttpRequestHandlingController(false); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -80,6 +83,7 @@ public class HttpRequestHandlingControllerTests { public void sendOnlyViewExpression() throws Exception { QueueChannel requestChannel = new QueueChannel(); HttpRequestHandlingController controller = new HttpRequestHandlingController(false); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); Expression viewExpression = new SpelExpressionParser().parseExpression("'baz'"); controller.setViewExpression(viewExpression); @@ -111,6 +115,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -142,6 +147,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']"); controller.setViewExpression(viewExpression); @@ -171,6 +177,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']"); controller.setViewExpression(viewExpression); @@ -198,6 +205,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); controller.setReplyKey("myReply"); @@ -229,6 +237,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); controller.setExtractReplyPayload(false); @@ -259,6 +268,7 @@ public class HttpRequestHandlingControllerTests { } }; HttpRequestHandlingController controller = new HttpRequestHandlingController(false); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); @@ -295,6 +305,7 @@ public class HttpRequestHandlingControllerTests { }; requestChannel.subscribe(handler); final HttpRequestHandlingController controller = new HttpRequestHandlingController(true); + controller.setBeanFactory(mock(BeanFactory.class)); controller.setRequestChannel(requestChannel); controller.setViewName("foo"); final MockHttpServletRequest request = new MockHttpServletRequest(); diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayTests.java index 9d87c1fe39..61bece4264 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,7 @@ package org.springframework.integration.http.inbound; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; import java.io.IOException; import java.io.PrintWriter; @@ -27,6 +28,8 @@ import java.util.Arrays; import java.util.List; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; @@ -59,6 +62,7 @@ public class HttpRequestHandlingMessagingGatewayTests { public void getRequestGeneratesMapPayload() throws Exception { QueueChannel requestChannel = new QueueChannel(); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestChannel(requestChannel); MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); @@ -77,6 +81,7 @@ public class HttpRequestHandlingMessagingGatewayTests { public void stringExpectedWithoutReply() throws Exception { QueueChannel requestChannel = new QueueChannel(); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestPayloadType(String.class); gateway.setRequestChannel(requestChannel); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -105,6 +110,7 @@ public class HttpRequestHandlingMessagingGatewayTests { } }); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestPayloadType(String.class); gateway.setRequestChannel(requestChannel); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -131,6 +137,7 @@ public class HttpRequestHandlingMessagingGatewayTests { } }); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestPayloadType(String.class); gateway.setRequestChannel(requestChannel); MockHttpServletRequest request = new MockHttpServletRequest(); @@ -155,6 +162,7 @@ public class HttpRequestHandlingMessagingGatewayTests { } }; HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestChannel(requestChannel); gateway.setConvertExceptions(true); gateway.setMessageConverters(Arrays.>asList(new TestHttpMessageConverter())); @@ -171,6 +179,7 @@ public class HttpRequestHandlingMessagingGatewayTests { public void multiValueParameterMap() throws Exception { QueueChannel channel = new QueueChannel(); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestChannel(channel); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test"); request.setParameter("foo", "123"); @@ -197,6 +206,7 @@ public class HttpRequestHandlingMessagingGatewayTests { public void serializableRequestBody() throws Exception { QueueChannel channel = new QueueChannel(); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(false); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setRequestPayloadType(TestBean.class); gateway.setRequestChannel(channel); @@ -251,6 +261,7 @@ public class HttpRequestHandlingMessagingGatewayTests { messageConverters.add(messageConverter); final HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setMessageConverters(messageConverters); gateway.setRequestChannel(requestChannel); @@ -272,7 +283,7 @@ public class HttpRequestHandlingMessagingGatewayTests { private class ContentTypeCheckingMockHttpServletResponse extends MockHttpServletResponse { - private List contentTypeList = new ArrayList(); + private final List contentTypeList = new ArrayList(); @Override public void addHeader(String name, String value) { diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayWithPathMappingTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayWithPathMappingTests.java index 0b81582d12..83c792c1db 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayWithPathMappingTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/inbound/HttpRequestHandlingMessagingGatewayWithPathMappingTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -18,10 +18,13 @@ package org.springframework.integration.http.inbound; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.Map; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; @@ -36,6 +39,7 @@ import org.springframework.mock.web.MockHttpServletResponse; * @author Oleg Zhurakousky * @author Gary Russell * @author Gunnar Hillert + * @author Gary Russell */ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests { @@ -63,6 +67,7 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests { request.setRequestURI("/fname/bill/lname/clinton"); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setPath("/fname/{f}/lname/{l}"); gateway.setRequestChannel(echoChannel); @@ -93,6 +98,7 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests { request.setRequestURI("/fname/bill/lname/clinton"); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setPath("/fname/{f}/lname/{l}"); gateway.setRequestChannel(echoChannel); gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables.f")); @@ -124,6 +130,7 @@ public class HttpRequestHandlingMessagingGatewayWithPathMappingTests { request.setRequestURI("/fname/bill/lname/clinton"); HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.setPath("/fname/{f}/lname/{l}"); gateway.setRequestChannel(echoChannel); gateway.setPayloadExpression(PARSER.parseExpression("#pathVariables")); diff --git a/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/UriVariableExpressionTests.java b/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/UriVariableExpressionTests.java index 976d6695ab..d4c978174c 100644 --- a/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/UriVariableExpressionTests.java +++ b/spring-integration-http/src/test/java/org/springframework/integration/http/outbound/UriVariableExpressionTests.java @@ -17,6 +17,7 @@ package org.springframework.integration.http.outbound; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import java.io.IOException; import java.net.URI; @@ -27,6 +28,7 @@ import java.util.concurrent.atomic.AtomicReference; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.http.HttpMethod; @@ -39,6 +41,7 @@ import org.springframework.integration.message.GenericMessage; * @author Dave Syer * @author Mark Fisher * @author Wallace Wadge + * @author Gary Russell * @since 2.0 */ public class UriVariableExpressionTests { @@ -56,6 +59,7 @@ public class UriVariableExpressionTests { throw new RuntimeException("intentional"); } }); + handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); Message message = new GenericMessage("bar"); Exception exception = null; @@ -86,6 +90,8 @@ public class UriVariableExpressionTests { throw new RuntimeException("intentional"); } }); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); Message message = new GenericMessage("bar"); Exception exception = null; try { diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java index 76a1790b1b..5f4fb4c314 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. You may obtain a copy of the License at @@ -20,6 +20,7 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; + import org.springframework.expression.ExpressionException; import org.springframework.integration.util.AbstractExpressionEvaluator; import org.springframework.jdbc.core.namedparam.AbstractSqlParameterSource; @@ -31,6 +32,7 @@ import org.springframework.jdbc.core.namedparam.SqlParameterSource; * * @author Dave Syer * @author Oleg Zhurakousky + * @author Gary Russell * @since 2.0 */ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpressionEvaluator implements @@ -58,7 +60,6 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre */ public void setStaticParameters(Map staticParameters) { this.staticParameters = staticParameters; - getEvaluationContext().setVariable("staticParameters", staticParameters); } /** @@ -106,6 +107,12 @@ public class ExpressionEvaluatingSqlParameterSourceFactory extends AbstractExpre return toReturn; } + @Override + public void afterPropertiesSet() throws Exception { + super.afterPropertiesSet(); + this.getEvaluationContext().setVariable("staticParameters", this.staticParameters); + } + private final class ExpressionEvaluatingSqlParameterSource extends AbstractSqlParameterSource { private final Object input; diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcOutboundGateway.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcOutboundGateway.java index 1a0639fb08..cd884207d3 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcOutboundGateway.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcOutboundGateway.java @@ -45,6 +45,8 @@ public class JdbcOutboundGateway extends AbstractReplyProducingMessageHandler im private volatile SqlParameterSourceFactory sqlParameterSourceFactory = new ExpressionEvaluatingSqlParameterSourceFactory(); + private volatile boolean sqlParameterSourceFactorySet; + private volatile boolean keysGenerated; private volatile Integer maxRowsPerPoll; @@ -114,9 +116,14 @@ public class JdbcOutboundGateway extends AbstractReplyProducingMessageHandler im } if (this.handler!= null) { + handler.setBeanFactory(this.getBeanFactory()); handler.afterPropertiesSet(); } + if (!this.sqlParameterSourceFactorySet && this.getBeanFactory() != null) { + ((ExpressionEvaluatingSqlParameterSourceFactory) this.sqlParameterSourceFactory) + .setBeanFactory(this.getBeanFactory()); + } } @Override @@ -168,11 +175,13 @@ public class JdbcOutboundGateway extends AbstractReplyProducingMessageHandler im } public void setRequestSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) { - handler.setSqlParameterSourceFactory(sqlParameterSourceFactory); + Assert.notNull(this.handler); + this.handler.setSqlParameterSourceFactory(sqlParameterSourceFactory); } public void setReplySqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) { this.sqlParameterSourceFactory = sqlParameterSourceFactory; + this.sqlParameterSourceFactorySet = true; } public void setRowMapper(RowMapper rowMapper) { diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java index f48587f40a..3cef0fe9d3 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapter.java @@ -62,6 +62,8 @@ public class JdbcPollingChannelAdapter extends IntegrationObjectSupport implemen private volatile SqlParameterSourceFactory sqlParameterSourceFactory = new ExpressionEvaluatingSqlParameterSourceFactory(); + private volatile boolean sqlParameterSourceFactorySet; + private volatile int maxRowsPerPoll = 0; /** @@ -102,6 +104,7 @@ public class JdbcPollingChannelAdapter extends IntegrationObjectSupport implemen public void setUpdateSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) { this.sqlParameterSourceFactory = sqlParameterSourceFactory; + this.sqlParameterSourceFactorySet = true; } /** @@ -124,6 +127,15 @@ public class JdbcPollingChannelAdapter extends IntegrationObjectSupport implemen this.maxRowsPerPoll = maxRows; } + @Override + protected void onInit() throws Exception { + super.onInit(); + if (!this.sqlParameterSourceFactorySet && this.getBeanFactory() != null) { + ((ExpressionEvaluatingSqlParameterSourceFactory)this.sqlParameterSourceFactory) + .setBeanFactory(this.getBeanFactory()); + } + } + /** * Executes the query. If a query result set contains one or more rows, the * Message payload will contain either a List of Maps for each row or, if a @@ -201,6 +213,7 @@ public class JdbcPollingChannelAdapter extends IntegrationObjectSupport implemen return payload; } + @Override public String getComponentType(){ return "jdbc:inbound-channel-adapter"; } diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java index e2f1e898db..bac5241659 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/StoredProcExecutor.java @@ -171,9 +171,9 @@ public class StoredProcExecutor implements BeanFactoryAware, InitializingBean { ExpressionEvaluatingSqlParameterSourceFactory expressionSourceFactory = new ExpressionEvaluatingSqlParameterSourceFactory(); + expressionSourceFactory.setBeanFactory(this.beanFactory); expressionSourceFactory.setStaticParameters(ProcedureParameter.convertStaticParameters(procedureParameters)); expressionSourceFactory.setParameterExpressions(ProcedureParameter.convertExpressions(procedureParameters)); - expressionSourceFactory.setBeanFactory(this.beanFactory); this.sqlParameterSourceFactory = expressionSourceFactory; diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java index b7547b125a..8a858ab13e 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/ExpressionEvaluatingSqlParameterSourceFactoryTests.java @@ -17,38 +17,47 @@ package org.springframework.integration.jdbc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.Arrays; import java.util.Collections; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.jdbc.core.namedparam.SqlParameterSource; /** * @author Dave Syer - * + * */ public class ExpressionEvaluatingSqlParameterSourceFactoryTests { - private ExpressionEvaluatingSqlParameterSourceFactory factory = new ExpressionEvaluatingSqlParameterSourceFactory(); + private final ExpressionEvaluatingSqlParameterSourceFactory factory = new ExpressionEvaluatingSqlParameterSourceFactory(); @Test - public void testSetStaticParameters() { + public void testSetStaticParameters() throws Exception { factory.setStaticParameters(Collections.singletonMap("foo", "bar")); + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(null); assertTrue(source.hasValue("foo")); assertEquals("bar", source.getValue("foo")); } @Test - public void testMapInput() { + public void testMapInput() throws Exception { + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar")); assertTrue(source.hasValue("foo")); assertEquals("bar", source.getValue("foo")); } @Test - public void testListOfMapsInput() { + public void testListOfMapsInput() throws Exception { + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); @SuppressWarnings("unchecked") SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"), Collections.singletonMap("foo", "bucket"))); @@ -58,7 +67,9 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests { } @Test - public void testMapInputWithExpression() { + public void testMapInputWithExpression() throws Exception { + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar")); // This is an illegal parameter name in Spring JDBC so we'd never get this as input assertTrue(source.hasValue("foo.toUpperCase()")); @@ -66,25 +77,31 @@ public class ExpressionEvaluatingSqlParameterSourceFactoryTests { } @Test - public void testMapInputWithMappedExpression() { + public void testMapInputWithMappedExpression() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("foo", "bar")); assertTrue(source.hasValue("spam")); assertEquals("BAR", source.getValue("spam")); } @Test - public void testMapInputWithMappedExpressionResolveStatic() { + public void testMapInputWithMappedExpressionResolveStatic() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "#staticParameters['foo'].toUpperCase()")); factory.setStaticParameters(Collections.singletonMap("foo", "bar")); + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); SqlParameterSource source = factory.createParameterSource(Collections.singletonMap("crap", "bucket")); assertTrue(source.hasValue("spam")); assertEquals("BAR", source.getValue("spam")); } @Test - public void testListOfMapsInputWithExpression() { + public void testListOfMapsInputWithExpression() throws Exception { factory.setParameterExpressions(Collections.singletonMap("spam", "foo.toUpperCase()")); + factory.setBeanFactory(mock(BeanFactory.class)); + factory.afterPropertiesSet(); @SuppressWarnings("unchecked") SqlParameterSource source = factory.createParameterSource(Arrays.asList(Collections.singletonMap("foo", "bar"), Collections.singletonMap("foo", "bucket"))); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java index 11dab33b28..fa2b9de2f2 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/JdbcPollingChannelAdapterIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -19,6 +19,7 @@ package org.springframework.integration.jdbc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.sql.ResultSet; import java.sql.SQLException; @@ -31,6 +32,8 @@ import org.apache.commons.logging.LogFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.Message; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; @@ -41,9 +44,10 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; /** * @author Jonas Partner + * @author Gary Russell */ public class JdbcPollingChannelAdapterIntegrationTests { - + private static Log logger = LogFactory.getLog(JdbcPollingChannelAdapterIntegrationTests.class); private EmbeddedDatabase embeddedDatabase; @@ -143,6 +147,8 @@ public class JdbcPollingChannelAdapterIntegrationTests { adapter .setUpdateSql("update item set status = 10 where id in (:id)"); adapter.setRowMapper(new ItemRowMapper()); + adapter.setBeanFactory(mock(BeanFactory.class)); + adapter.afterPropertiesSet(); this.jdbcTemplate.update("insert into item values(1,2)"); this.jdbcTemplate.update("insert into item values(2,2)"); @@ -177,6 +183,8 @@ public class JdbcPollingChannelAdapterIntegrationTests { adapter.setUpdateSql("update item set status = 10 where id = :id"); adapter.setUpdatePerRow(true); adapter.setRowMapper(new ItemRowMapper()); + adapter.setBeanFactory(mock(BeanFactory.class)); + adapter.afterPropertiesSet(); this.jdbcTemplate.update("insert into item values(1,2)"); this.jdbcTemplate.update("insert into item values(2,2)"); @@ -212,6 +220,8 @@ public class JdbcPollingChannelAdapterIntegrationTests { adapter.setUpdatePerRow(true); adapter.setMaxRowsPerPoll(1); adapter.setRowMapper(new ItemRowMapper()); + adapter.setBeanFactory(mock(BeanFactory.class)); + adapter.afterPropertiesSet(); this.jdbcTemplate.update("insert into item values(1,2)"); this.jdbcTemplate.update("insert into item values(2,2)"); @@ -249,6 +259,8 @@ public class JdbcPollingChannelAdapterIntegrationTests { adapter.setUpdatePerRow(true); adapter.setMaxRowsPerPoll(1); adapter.setRowMapper(new ItemRowMapper()); + adapter.setBeanFactory(mock(BeanFactory.class)); + adapter.afterPropertiesSet(); this.jdbcTemplate.update("insert into item values(1,2)"); this.jdbcTemplate.update("insert into item values(2,2)"); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java index 8d3e5945b7..389ac97750 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcExecutorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -35,6 +35,8 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.integration.config.ExpressionFactoryBean; import org.springframework.integration.jdbc.storedproc.ProcedureParameter; @@ -71,6 +73,7 @@ public class StoredProcExecutorTests { try { StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); } catch (IllegalArgumentException e) { assertEquals("You must either provide a " @@ -108,6 +111,7 @@ public class StoredProcExecutorTests { final Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); assertEquals("headers['stored_procedure_name']", storedProcExecutor.getStoredProcedureNameExpressionAsString()); @@ -120,6 +124,7 @@ public class StoredProcExecutorTests { StoredProcExecutor storedProcExecutor = new StoredProcExecutor(datasource); storedProcExecutor.setStoredProcedureName("123"); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); assertEquals("123", storedProcExecutor.getStoredProcedureName()); @@ -322,6 +327,7 @@ public class StoredProcExecutorTests { final Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); @@ -365,6 +371,7 @@ public class StoredProcExecutorTests { final Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); @@ -403,6 +410,7 @@ public class StoredProcExecutorTests { final Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); diff --git a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java index c34d8b949f..eba659b822 100644 --- a/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java +++ b/spring-integration-jdbc/src/test/java/org/springframework/integration/jdbc/StoredProcMessageHandlerDerbyIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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,6 +17,7 @@ package org.springframework.integration.jdbc; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; import java.sql.SQLException; import java.util.ArrayList; @@ -26,6 +27,8 @@ import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.integration.config.ExpressionFactoryBean; import org.springframework.integration.jdbc.storedproc.ProcedureParameter; @@ -38,6 +41,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; /** * @author Gunnar Hillert + * @author Gary Russell */ public class StoredProcMessageHandlerDerbyIntegrationTests { @@ -66,6 +70,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { StoredProcMessageHandler messageHandler = new StoredProcMessageHandler(storedProcExecutor); storedProcExecutor.setStoredProcedureName("CREATE_USER"); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); messageHandler.afterPropertiesSet(); @@ -92,6 +97,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { final Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); messageHandler.afterPropertiesSet(); @@ -119,6 +125,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { Expression expression = efb.getObject(); storedProcExecutor.setStoredProcedureNameExpression(expression); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); messageHandler.afterPropertiesSet(); @@ -149,6 +156,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { procedureParameters.add(new ProcedureParameter("email", null, "payload.email.toUpperCase()")); storedProcExecutor.setProcedureParameters(procedureParameters); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); messageHandler.afterPropertiesSet(); @@ -180,6 +188,7 @@ public class StoredProcMessageHandlerDerbyIntegrationTests { procedureParameters.add(new ProcedureParameter("email", "static_email" , null)); storedProcExecutor.setProcedureParameters(procedureParameters); + storedProcExecutor.setBeanFactory(mock(BeanFactory.class)); storedProcExecutor.afterPropertiesSet(); messageHandler.afterPropertiesSet(); diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaExecutor.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaExecutor.java index 7c70efb899..f2b7ab5755 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaExecutor.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/core/JpaExecutor.java @@ -21,6 +21,9 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; @@ -57,7 +60,7 @@ import org.springframework.util.Assert; * @since 2.2 * */ -public class JpaExecutor implements InitializingBean { +public class JpaExecutor implements InitializingBean, BeanFactoryAware { private volatile JpaOperations jpaOperations; private volatile List jpaParameters; @@ -88,6 +91,8 @@ public class JpaExecutor implements InitializingBean { */ private volatile Boolean usePayloadAsParameterSource = null; + private volatile BeanFactory beanFactory; + /** * Constructor taking an {@link EntityManagerFactory} from which the * {@link EntityManager} can be obtained. @@ -132,6 +137,11 @@ public class JpaExecutor implements InitializingBean { this.jpaOperations = jpaOperations; } + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } + /** * * Verifies and sets the parameters. E.g. initializes the to be used @@ -144,7 +154,7 @@ public class JpaExecutor implements InitializingBean { if (this.parameterSourceFactory == null) { ExpressionEvaluatingParameterSourceFactory expressionSourceFactory = - new ExpressionEvaluatingParameterSourceFactory(); + new ExpressionEvaluatingParameterSourceFactory(this.beanFactory); expressionSourceFactory.setParameters(jpaParameters); this.parameterSourceFactory = expressionSourceFactory; diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapter.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapter.java index e8e90b388d..2fd8697061 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapter.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/inbound/JpaPollingChannelAdapter.java @@ -70,6 +70,7 @@ public class JpaPollingChannelAdapter extends IntegrationObjectSupport implement @Override protected void onInit() throws Exception { super.onInit(); + this.jpaExecutor.setBeanFactory(this.getBeanFactory()); } /** diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGateway.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGateway.java index 28fbce0be7..97aeba4571 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGateway.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGateway.java @@ -65,6 +65,7 @@ public class JpaOutboundGateway extends AbstractReplyProducingMessageHandler { @Override protected void onInit() { super.onInit(); + this.jpaExecutor.setBeanFactory(this.getBeanFactory()); } @Override diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGatewayFactoryBean.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGatewayFactoryBean.java index f3c39fb9b7..92df61c498 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGatewayFactoryBean.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/outbound/JpaOutboundGatewayFactoryBean.java @@ -19,6 +19,7 @@ package org.springframework.integration.jpa.outbound; import java.util.List; import org.aopalliance.aop.Advice; + import org.springframework.aop.framework.ProxyFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.AbstractFactoryBean; @@ -152,6 +153,7 @@ public class JpaOutboundGatewayFactoryBean extends AbstractFactoryBean parameters; - private ParameterExpressionEvaluator expressionEvaluator = new ParameterExpressionEvaluator(); + + private final ParameterExpressionEvaluator expressionEvaluator = new ParameterExpressionEvaluator(); public ExpressionEvaluatingParameterSourceFactory() { + this(null); + } + + public ExpressionEvaluatingParameterSourceFactory(BeanFactory beanFactory) { this.parameters = Collections.unmodifiableList(new ArrayList()); + this.expressionEvaluator.setBeanFactory(beanFactory); } /** diff --git a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/AbstractJpaOperationsTests.java b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/AbstractJpaOperationsTests.java index c962e5456f..137d9464c0 100644 --- a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/AbstractJpaOperationsTests.java +++ b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/AbstractJpaOperationsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. You may obtain a copy of the License at @@ -14,6 +14,7 @@ package org.springframework.integration.jpa.core; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -23,6 +24,8 @@ import java.util.List; import javax.persistence.EntityManager; import org.junit.Assert; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.jpa.support.parametersource.ExpressionEvaluatingParameterSourceFactory; import org.springframework.integration.jpa.support.parametersource.ParameterSource; @@ -38,6 +41,7 @@ import org.springframework.transaction.support.DefaultTransactionDefinition; /** * @author Gunnar Hillert + * @author Gary Russell * @since 2.2 * */ @@ -87,7 +91,8 @@ public class AbstractJpaOperationsTests { List students = jpaOperations.getResultListForClass(StudentDomain.class, 0); Assert.assertTrue(students.size() == 3); - ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(); + ParameterSourceFactory requestParameterSourceFactory = + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); ParameterSource source = requestParameterSourceFactory.createParameterSource(student); int updatedRecords = jpaOperations.executeUpdate("update Student s set s.lastName = :lastName, s.lastUpdated = :lastUpdated " @@ -108,7 +113,8 @@ public class AbstractJpaOperationsTests { final StudentDomain student = JpaTestUtils.getTestStudent(); - ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(); + ParameterSourceFactory requestParameterSourceFactory = + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); ParameterSource source = requestParameterSourceFactory.createParameterSource(student); int updatedRecords = jpaOperations.executeUpdateWithNamedQuery("updateStudent", source); @@ -127,7 +133,8 @@ public class AbstractJpaOperationsTests { final StudentDomain student = JpaTestUtils.getTestStudent(); - ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(); + ExpressionEvaluatingParameterSourceFactory requestParameterSourceFactory = + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); ParameterSource source = requestParameterSourceFactory.createParameterSource(student); int updatedRecords = jpaOperations.executeUpdateWithNativeQuery("update Student set lastName = :lastName, lastUpdated = :lastUpdated " @@ -197,7 +204,8 @@ public class AbstractJpaOperationsTests { final StudentDomain student = JpaTestUtils.getTestStudent(); - ParameterSourceFactory requestParameterSourceFactory = new ExpressionEvaluatingParameterSourceFactory(); + ParameterSourceFactory requestParameterSourceFactory = + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); ParameterSource source = requestParameterSourceFactory.createParameterSource(student); int updatedRecords = jpaOperations.executeUpdateWithNamedQuery("updateStudentNativeQuery", source); diff --git a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java index 004e9320d9..efa3839baf 100644 --- a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java +++ b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/core/JpaExecutorTests.java @@ -20,9 +20,10 @@ import java.util.Map; import javax.persistence.EntityManager; import org.junit.Assert; - import org.junit.Test; import org.junit.runner.RunWith; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.Message; import org.springframework.integration.jpa.support.JpaParameter; @@ -38,6 +39,7 @@ import org.springframework.transaction.annotation.Transactional; * * @author Gunnar Hillert * @author Amol Nayak + * @author Gary Russell * @since 2.2 * */ @@ -177,7 +179,7 @@ public class JpaExecutorTests { private JpaExecutor getJpaExecutorForMessageAsParamSource(String query) { JpaExecutor executor = new JpaExecutor(entityManager); ExpressionEvaluatingParameterSourceFactory factory = - new ExpressionEvaluatingParameterSourceFactory(); + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); factory.setParameters( Collections.singletonList(new JpaParameter("firstName", null, "payload['firstName']"))); executor.setParameterSourceFactory(factory); @@ -195,7 +197,7 @@ public class JpaExecutorTests { private JpaExecutor getJpaExecutorForPayloadAsParamSource(String query) { JpaExecutor executor = new JpaExecutor(entityManager); ExpressionEvaluatingParameterSourceFactory factory = - new ExpressionEvaluatingParameterSourceFactory(); + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); factory.setParameters( Collections.singletonList(new JpaParameter("firstName", null, "#this"))); executor.setParameterSourceFactory(factory); diff --git a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactoryTests.java b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactoryTests.java index ddbde980dd..902fb74908 100644 --- a/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactoryTests.java +++ b/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/support/parametersource/ExpressionEvaluatingParameterSourceFactoryTests.java @@ -17,6 +17,7 @@ package org.springframework.integration.jpa.support.parametersource; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.ArrayList; import java.util.Arrays; @@ -24,6 +25,8 @@ import java.util.Collections; import java.util.List; import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.jpa.support.JpaParameter; /** @@ -34,7 +37,8 @@ import org.springframework.integration.jpa.support.JpaParameter; */ public class ExpressionEvaluatingParameterSourceFactoryTests { - private ExpressionEvaluatingParameterSourceFactory factory = new ExpressionEvaluatingParameterSourceFactory(); + private final ExpressionEvaluatingParameterSourceFactory factory = + new ExpressionEvaluatingParameterSourceFactory(mock(BeanFactory.class)); @Test public void testSetStaticParameters() { diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java index 48f7d933d0..b69634a8cd 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java @@ -56,6 +56,7 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; @@ -101,6 +102,7 @@ public class ImapMailReceiverTests { Message msg2) throws NoSuchFieldException, IllegalAccessException, MessagingException { ((ImapMailReceiver)receiver).setShouldMarkMessagesAsRead(true); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); @@ -172,6 +174,7 @@ public class ImapMailReceiverTests { ((ImapMailReceiver)receiver).setShouldMarkMessagesAsRead(true); receiver.setShouldDeleteMessages(true); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); @@ -216,6 +219,7 @@ public class ImapMailReceiverTests { AbstractMailReceiver receiver = new ImapMailReceiver(); ((ImapMailReceiver)receiver).setShouldMarkMessagesAsRead(false); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); @@ -256,6 +260,7 @@ public class ImapMailReceiverTests { ((ImapMailReceiver)receiver).setShouldDeleteMessages(true); ((ImapMailReceiver)receiver).setShouldMarkMessagesAsRead(false); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); @@ -300,6 +305,7 @@ public class ImapMailReceiverTests { public void receiveAndIgnoreMarkAsReadDontDelete() throws Exception{ AbstractMailReceiver receiver = new ImapMailReceiver(); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); @@ -347,6 +353,7 @@ public class ImapMailReceiverTests { AbstractMailReceiver receiver = new ImapMailReceiver(); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); @@ -414,6 +421,7 @@ public class ImapMailReceiverTests { AbstractMailReceiver receiver = new ImapMailReceiver(); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); @@ -472,6 +480,7 @@ public class ImapMailReceiverTests { ImapMailReceiver receiver = new ImapMailReceiver("imap:foo"); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); final IMAPFolder folder = mock(IMAPFolder.class); @@ -556,6 +565,7 @@ public class ImapMailReceiverTests { ImapMailReceiver receiver = new ImapMailReceiver("imap:foo"); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); final IMAPFolder folder = mock(IMAPFolder.class); @@ -656,6 +666,7 @@ public class ImapMailReceiverTests { DirectFieldAccessor df = new DirectFieldAccessor(receiver); df.setPropertyValue("store", store); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); new Thread(new Runnable() { @@ -700,6 +711,7 @@ public class ImapMailReceiverTests { when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); DirectFieldAccessor df = new DirectFieldAccessor(receiver); df.setPropertyValue("store", store); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); doAnswer(new Answer () { diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailSearchTermsTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailSearchTermsTests.java index 0031219c50..84516b7341 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailSearchTermsTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailSearchTermsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -33,10 +33,12 @@ import javax.mail.search.SearchTerm; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; import org.springframework.util.ReflectionUtils; /** * @author Oleg Zhurakousky + * @author Gary Russell * */ public class ImapMailSearchTermsTests { @@ -45,13 +47,14 @@ public class ImapMailSearchTermsTests { public void validateSearchTermsWhenShouldMarkAsReadNoExistingFlags() throws Exception { ImapMailReceiver receiver = new ImapMailReceiver(); receiver.setShouldMarkMessagesAsRead(true); - + receiver.setBeanFactory(mock(BeanFactory.class)); + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class); compileSearchTerms.setAccessible(true); Flags flags = new Flags(); @@ -66,14 +69,15 @@ public class ImapMailSearchTermsTests { public void validateSearchTermsWhenShouldMarkAsReadWithExistingFlags() throws Exception { ImapMailReceiver receiver = new ImapMailReceiver(); receiver.setShouldMarkMessagesAsRead(true); - + receiver.setBeanFactory(mock(BeanFactory.class)); + receiver.afterPropertiesSet(); Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class); compileSearchTerms.setAccessible(true); Flags flags = new Flags(); @@ -90,19 +94,20 @@ public class ImapMailSearchTermsTests { siFlags.add(AbstractMailReceiver.SI_USER_FLAG); assertTrue(((FlagTerm)notTerm.getTerm()).getFlags().contains(siFlags)); } - + @Test public void validateSearchTermsWhenShouldNotMarkAsReadNoExistingFlags() throws Exception { ImapMailReceiver receiver = new ImapMailReceiver(); receiver.setShouldMarkMessagesAsRead(false); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); - + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Method compileSearchTerms = ReflectionUtils.findMethod(receiver.getClass(), "compileSearchTerms", Flags.class); compileSearchTerms.setAccessible(true); Flags flags = new Flags(); diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/Pop3MailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/Pop3MailReceiverTests.java index cf62e56d91..088f742557 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/Pop3MailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/Pop3MailReceiverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -25,8 +25,8 @@ import static org.mockito.Mockito.when; import java.lang.reflect.Field; -import javax.mail.Flags.Flag; import javax.mail.Flags; +import javax.mail.Flags.Flag; import javax.mail.Folder; import javax.mail.Message; import javax.mail.internet.MimeMessage; @@ -34,10 +34,13 @@ import javax.mail.internet.MimeMessage; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + import org.springframework.beans.DirectFieldAccessor; +import org.springframework.beans.factory.BeanFactory; /** * @author Oleg Zhurakousky + * @author Gary Russell * */ public class Pop3MailReceiverTests { @@ -46,14 +49,15 @@ public class Pop3MailReceiverTests { AbstractMailReceiver receiver = new Pop3MailReceiver(); ((Pop3MailReceiver)receiver).setShouldDeleteMessages(true); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); - + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Message msg1 = mock(MimeMessage.class); Message msg2 = mock(MimeMessage.class); final Message[] messages = new Message[]{msg1, msg2}; @@ -67,13 +71,13 @@ public class Pop3MailReceiverTests { return null; } }).when(receiver).openFolder(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return messages; } }).when(receiver).searchForNewMessages(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return null; @@ -89,14 +93,15 @@ public class Pop3MailReceiverTests { AbstractMailReceiver receiver = new Pop3MailReceiver(); ((Pop3MailReceiver)receiver).setShouldDeleteMessages(false); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); - + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Message msg1 = mock(MimeMessage.class); Message msg2 = mock(MimeMessage.class); final Message[] messages = new Message[]{msg1, msg2}; @@ -105,13 +110,13 @@ public class Pop3MailReceiverTests { return null; } }).when(receiver).openFolder(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return messages; } }).when(receiver).searchForNewMessages(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return null; @@ -126,14 +131,15 @@ public class Pop3MailReceiverTests { public void receiveAndDontSetDeleteWithUrl() throws Exception{ AbstractMailReceiver receiver = new Pop3MailReceiver("pop3://some.host"); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); - + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Message msg1 = mock(MimeMessage.class); Message msg2 = mock(MimeMessage.class); final Message[] messages = new Message[]{msg1, msg2}; @@ -142,13 +148,13 @@ public class Pop3MailReceiverTests { return null; } }).when(receiver).openFolder(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return messages; } }).when(receiver).searchForNewMessages(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return null; @@ -163,14 +169,15 @@ public class Pop3MailReceiverTests { public void receiveAndDontSetDeleteWithoutUrl() throws Exception{ AbstractMailReceiver receiver = new Pop3MailReceiver(); receiver = spy(receiver); + receiver.setBeanFactory(mock(BeanFactory.class)); receiver.afterPropertiesSet(); - + Field folderField = AbstractMailReceiver.class.getDeclaredField("folder"); folderField.setAccessible(true); Folder folder = mock(Folder.class); when(folder.getPermanentFlags()).thenReturn(new Flags(Flags.Flag.USER)); folderField.set(receiver, folder); - + Message msg1 = mock(MimeMessage.class); Message msg2 = mock(MimeMessage.class); final Message[] messages = new Message[]{msg1, msg2}; @@ -179,13 +186,13 @@ public class Pop3MailReceiverTests { return null; } }).when(receiver).openFolder(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return messages; } }).when(receiver).searchForNewMessages(); - + doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { return null; diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java index a3a02c44d8..8e1b8ec108 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/outbound/SftpOutboundTests.java @@ -16,9 +16,9 @@ package org.springframework.integration.sftp.outbound; -import static org.mockito.Matchers.anyString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -35,6 +35,8 @@ import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.expression.common.LiteralExpression; @@ -74,8 +76,11 @@ public class SftpOutboundTests { FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setRemoteDirectoryExpression(new LiteralExpression(targetDir.getName())); DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator(); + fGenerator.setBeanFactory(mock(BeanFactory.class)); fGenerator.setExpression("payload + '.test'"); handler.setFileNameGenerator(fGenerator); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); File srcFile = File.createTempFile("testHandleFileMessage", ".tmp", new File(".")); srcFile.deleteOnExit(); @@ -96,9 +101,12 @@ public class SftpOutboundTests { SessionFactory sessionFactory = new TestSftpSessionFactory(); FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator(); + fGenerator.setBeanFactory(mock(BeanFactory.class)); fGenerator.setExpression("'foo.txt'"); handler.setFileNameGenerator(fGenerator); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello")); assertTrue(new File("remote-target-dir", "foo.txt").exists()); @@ -113,9 +121,12 @@ public class SftpOutboundTests { SessionFactory sessionFactory = new TestSftpSessionFactory(); FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); DefaultFileNameGenerator fGenerator = new DefaultFileNameGenerator(); + fGenerator.setBeanFactory(mock(BeanFactory.class)); fGenerator.setExpression("'foo.txt'"); handler.setFileNameGenerator(fGenerator); handler.setRemoteDirectoryExpression(new LiteralExpression("remote-target-dir")); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("hello".getBytes())); assertTrue(new File("remote-target-dir", "foo.txt").exists()); @@ -173,6 +184,8 @@ public class SftpOutboundTests { FileTransferringMessageHandler handler = new FileTransferringMessageHandler(sessionFactory); handler.setAutoCreateDirectory(true); handler.setRemoteDirectoryExpression(new LiteralExpression("/foo/bar/baz")); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); final List madeDirs = new ArrayList(); doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { diff --git a/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceOutboundGatewayTests.java b/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceOutboundGatewayTests.java index c1c8551c30..e9cecda011 100644 --- a/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceOutboundGatewayTests.java +++ b/spring-integration-ws/src/test/java/org/springframework/integration/ws/SimpleWebServiceOutboundGatewayTests.java @@ -19,6 +19,7 @@ package org.springframework.integration.ws; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; import java.io.ByteArrayInputStream; import java.io.IOException; @@ -29,10 +30,11 @@ import javax.xml.transform.TransformerException; import org.hamcrest.Matchers; import org.junit.Test; - import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.integration.Message; @@ -81,6 +83,7 @@ public class SimpleWebServiceOutboundGatewayTests { soapActionFromCallback.set(soapMessage.getSoapAction()); } }); + gateway.setBeanFactory(mock(BeanFactory.class)); gateway.afterPropertiesSet(); String soapActionHeaderValue = "testAction"; String request = "foo";