INT-28 Initial commit of (new in 2.0) MessagePublishingInterceptor. An AOP interceptor that uses EL to construct a Message payload and then publishes it.

This commit is contained in:
Mark Fisher
2009-08-25 19:26:37 +00:00
parent 1d07bbc3a5
commit 42a1cf0e13
9 changed files with 812 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2002-2009 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.aop;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Method;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.integration.channel.MapBasedChannelResolver;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.Message;
/**
* @author Mark Fisher
* @since 2.0
*/
public class MessagePublishingInterceptorTests {
private final ExpressionSource source = new TestExpressionSource();
private final MapBasedChannelResolver channelResolver = new MapBasedChannelResolver();
private final QueueChannel testChannel = new QueueChannel();
@Before
public void setup() {
channelResolver.setChannelMap(Collections.singletonMap("c", testChannel));
}
@Test
public void returnValue() {
MessagePublishingInterceptor interceptor = new MessagePublishingInterceptor(source);
interceptor.setChannelResolver(channelResolver);
ProxyFactory pf = new ProxyFactory(new TestBeanImpl());
pf.addAdvice(interceptor);
TestBean proxy = (TestBean) pf.getProxy();
proxy.test();
Message<?> message = testChannel.receive(0);
assertNotNull(message);
assertEquals("foo", message.getPayload());
}
static interface TestBean {
String test();
}
static class TestBeanImpl implements TestBean {
public String test() {
return "foo";
}
}
private static class TestExpressionSource implements ExpressionSource {
public String getArgumentMapName(Method method) {
return "m";
}
public String[] getArgumentNames(Method method) {
return new String[] { "a1", "a2" };
}
public String getChannelName(Method method) {
return "c";
}
public String getExceptionName(Method method) {
return "x";
}
public String getExpressionString(Method method) {
return "#r";
}
public String getReturnValueName(Method method) {
return "r";
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2009 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.aop;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* @author Mark Fisher
* @since 2.0
*/
public class MethodAnnotationExpressionSourceTests {
private final MethodAnnotationExpressionSource source = new MethodAnnotationExpressionSource();
@Test
public void defaultBindings() {
Method method = getMethod("methodWithExpressionAnnotationOnly", String.class, int.class);
String expressionString = source.getExpressionString(method);
assertEquals("testExpression1", expressionString);
assertEquals(2, source.getArgumentNames(method).length);
assertEquals("arg1", source.getArgumentNames(method)[0]);
assertEquals("arg2", source.getArgumentNames(method)[1]);
assertEquals(ExpressionSource.DEFAULT_ARGUMENT_MAP_NAME, source.getArgumentMapName(method));
assertEquals(ExpressionSource.DEFAULT_EXCEPTION_NAME, source.getExceptionName(method));
assertEquals(ExpressionSource.DEFAULT_RETURN_VALUE_NAME, source.getReturnValueName(method));
}
@Test
public void annotationBindings() {
Method method = getMethod("methodWithExpressionBinding", String.class, int.class);
String expressionString = source.getExpressionString(method);
assertEquals("testExpression2", expressionString);
assertEquals(2, source.getArgumentNames(method).length);
assertEquals("s", source.getArgumentNames(method)[0]);
assertEquals("i", source.getArgumentNames(method)[1]);
assertEquals("argz", source.getArgumentMapName(method));
assertEquals("x", source.getExceptionName(method));
assertEquals("result", source.getReturnValueName(method));
}
@Test
public void channelName() {
Method method = getMethod("methodWithChannelAndReturnAsPayload");
String channelName = source.getChannelName(method);
assertEquals("foo", channelName);
}
private static Method getMethod(String name, Class<?> ... params) {
try {
return MethodAnnotationExpressionSourceTests.class.getMethod(name, params);
}
catch (Exception e) {
throw new RuntimeException("failed to resolve method", e);
}
}
@Publisher("testExpression1")
public void methodWithExpressionAnnotationOnly(String arg1, int arg2) {
}
@Publisher(value="#return", channel="foo")
public void methodWithChannelAndReturnAsPayload() {
}
@Publisher("testExpression2")
@ExpressionBinding(argNames="s, i", argumentMapName="argz", exceptionName="x", returnValueName="result")
public void methodWithExpressionBinding(String arg1, int arg2) {
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2002-2009 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.aop;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.Message;
/**
* @author Mark Fisher
* @since 2.0
*/
public class PublisherAnnotationAdvisorTests {
private final StaticApplicationContext context = new StaticApplicationContext();
@Before
public void setup() {
context.registerSingleton("testChannel", QueueChannel.class);
}
@Test
public void returnValue() {
PublisherAnnotationAdvisor advisor = new PublisherAnnotationAdvisor();
advisor.setBeanFactory(context);
QueueChannel testChannel = context.getBean("testChannel", QueueChannel.class);
advisor.setDefaultChannel(testChannel);
ProxyFactory pf = new ProxyFactory(new TestBeanImpl());
pf.addAdvisor(advisor);
TestBean proxy = (TestBean) pf.getProxy();
proxy.test();
Message<?> message = testChannel.receive(0);
assertNotNull(message);
assertEquals("foo", message.getPayload());
}
static interface TestBean {
String test();
}
static class TestBeanImpl implements TestBean {
@Publisher("#return")
public String test() {
return "foo";
}
}
}