Added tests for advice chain with @Poller on a channel-polling consumer (@ServiceActivator).

This commit is contained in:
Mark Fisher
2008-10-09 19:12:23 +00:00
parent 875b0cc02a
commit fc6dc7488a
6 changed files with 316 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/stream
http://www.springframework.org/schema/integration/stream/spring-integration-stream-1.0.xsd">
<message-bus enable-annotations="true"/>
<channel id="input">
<queue capacity="1"/>
</channel>
<channel id="output">
<queue capacity="1"/>
</channel>
<channel id="inboundChannelAdapterOutput">
<queue capacity="5"/>
</channel>
<beans:bean id="service" class="org.springframework.integration.config.PollerAnnotationServiceActivator"/>
<beans:bean id="latch" class="java.util.concurrent.CountDownLatch">
<beans:constructor-arg value="4"/>
</beans:bean>
<beans:bean id="beforeAdvice" class="org.springframework.integration.config.TestBeforeAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
<beans:bean id="afterAdvice" class="org.springframework.integration.config.TestAfterAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
<beans:bean id="aroundAdvice" class="org.springframework.integration.config.TestAroundAdvice">
<beans:constructor-arg ref="latch"/>
</beans:bean>
</beans:beans>

View File

@@ -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());
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}