diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests-context.xml b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests-context.xml new file mode 100644 index 0000000000..e919f9429d --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests-context.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests.java new file mode 100644 index 0000000000..18d4b94a1f --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationConsumerAdviceChainTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-2008 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.config; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.integration.channel.MessageChannel; +import org.springframework.integration.channel.PollableChannel; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class PollerAnnotationConsumerAdviceChainTests { + + @Autowired @Qualifier("input") + private MessageChannel input; + + @Autowired @Qualifier("output") + private PollableChannel output; + + @Autowired + private CountDownLatch latch; + + @Autowired + private TestBeforeAdvice beforeAdvice; + + @Autowired + private TestAfterAdvice afterAdvice; + + @Autowired + private TestAroundAdvice aroundAdvice; + + + @Test + public void testAdviceChain() throws InterruptedException { + input.send(new StringMessage("test")); + Message reply = output.receive(1000); + assertEquals("TEST", reply.getPayload()); + latch.await(1, TimeUnit.SECONDS); + assertEquals(0, latch.getCount()); + assertEquals(4, beforeAdvice.getLatchCount()); + assertEquals(3, aroundAdvice.getPreCount()); + assertEquals(2, afterAdvice.getLatchCount()); + assertEquals(1, aroundAdvice.getPostCount()); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationServiceActivator.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationServiceActivator.java new file mode 100644 index 0000000000..64bcc2c282 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/PollerAnnotationServiceActivator.java @@ -0,0 +1,38 @@ +/* + * Copyright 2002-2008 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.config; + +import java.util.concurrent.TimeUnit; + +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.annotation.Poller; +import org.springframework.integration.annotation.ServiceActivator; + +/** + * @author Mark Fisher + */ +@MessageEndpoint +public class PollerAnnotationServiceActivator { + + @ServiceActivator(inputChannel="input", outputChannel="output") + @Poller(interval=5000, timeUnit=TimeUnit.SECONDS, maxMessagesPerPoll=1, + adviceChain="beforeAdvice ,aroundAdvice, afterAdvice ") // spacing intentional + public String testMethod(String input) { + return input.toUpperCase(); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAfterAdvice.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAfterAdvice.java new file mode 100644 index 0000000000..963493ba76 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAfterAdvice.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2008 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.config; + +import java.lang.reflect.Method; +import java.util.concurrent.CountDownLatch; + +import org.springframework.aop.AfterReturningAdvice; + +/** + * @author Mark Fisher + */ +public class TestAfterAdvice implements AfterReturningAdvice { + + private final CountDownLatch latch; + + private volatile long latchCount; + + + public TestAfterAdvice(CountDownLatch latch) { + this.latch = latch; + } + + + public long getLatchCount() { + return this.latchCount; + } + + public void afterReturning(Object returnValue, Method method, + Object[] args, Object target) throws Throwable { + this.latchCount = this.latch.getCount(); + this.latch.countDown(); + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAroundAdvice.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAroundAdvice.java new file mode 100644 index 0000000000..368a3ccab8 --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestAroundAdvice.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2008 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.config; + +import java.util.concurrent.CountDownLatch; + +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +/** + * @author Mark Fisher + */ +public class TestAroundAdvice implements MethodInterceptor { + + private final CountDownLatch latch; + + private volatile long preCount; + + private volatile long postCount; + + + public TestAroundAdvice(CountDownLatch latch) { + this.latch = latch; + } + + + public long getPreCount() { + return this.preCount; + } + + public long getPostCount() { + return this.postCount; + } + + public Object invoke(MethodInvocation invocation) throws Throwable { + try { + this.preCount = this.latch.getCount(); + this.latch.countDown(); + return invocation.proceed(); + } + finally { + this.postCount = this.latch.getCount(); + this.latch.countDown(); + } + } + +} diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/config/TestBeforeAdvice.java b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestBeforeAdvice.java new file mode 100644 index 0000000000..4cf8eaa3ce --- /dev/null +++ b/org.springframework.integration/src/test/java/org/springframework/integration/config/TestBeforeAdvice.java @@ -0,0 +1,48 @@ +/* + * Copyright 2002-2008 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.config; + +import java.lang.reflect.Method; +import java.util.concurrent.CountDownLatch; + +import org.springframework.aop.MethodBeforeAdvice; + +/** + * @author Mark Fisher + */ +public class TestBeforeAdvice implements MethodBeforeAdvice { + + private final CountDownLatch latch; + + private volatile long latchCount; + + + public TestBeforeAdvice(CountDownLatch latch) { + this.latch = latch; + } + + + public long getLatchCount() { + return this.latchCount; + } + + public void before(Method method, Object[] args, Object target) throws Throwable { + this.latchCount = latch.getCount(); + latch.countDown(); + } + +}