Fixes INT-305 - lazy initialization of DefaultChannelFactoryBean.

This commit is contained in:
Marius Bogoevici
2008-07-18 19:59:00 +00:00
parent a9a498f3fc
commit 3a916b223d
5 changed files with 129 additions and 33 deletions

View File

@@ -16,6 +16,10 @@
package org.springframework.integration.channel.config;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -64,7 +68,9 @@ public class ChannelParserTests {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("queueChannelByDefault");
assertEquals(QueueChannel.class, channel.getClass());
//called to initialize the channel instance
channel.getName();
assertEquals(QueueChannel.class, extractProxifiedChannel(channel).getClass());
}
@Test
@@ -207,4 +213,13 @@ public class ChannelParserTests {
assertTrue(threwException);
}
public static MessageChannel extractProxifiedChannel (Object channelProxy) {
InvocationHandler handler = Proxy.getInvocationHandler(channelProxy);
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
AtomicReference<MessageChannel> reference =
(AtomicReference<MessageChannel>) handlerAccessor.getPropertyValue("targetChannelReference");
return reference.get();
}
}

View File

@@ -16,7 +16,7 @@
package org.springframework.integration.channel.config;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@@ -34,7 +34,7 @@ public class DefaultChannelParserTests {
public void testDefaultChannel() {
ApplicationContext context = new ClassPathXmlApplicationContext("defaultChannelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("defaultChannel");
assertTrue(channel instanceof StubChannel);
assertTrue(StubChannel.class.isAssignableFrom(ChannelParserTests.extractProxifiedChannel(channel).getClass()));
}
}

View File

@@ -16,13 +16,13 @@
package org.springframework.integration.channel.factory;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
@@ -33,13 +33,17 @@ import org.springframework.integration.bus.DefaultChannelFactoryBean;
import org.springframework.integration.bus.DefaultMessageBus;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PriorityChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.RendezvousChannel;
import org.springframework.integration.channel.ThreadLocalChannel;
import org.springframework.integration.channel.config.ChannelParserTests;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
import org.springframework.integration.dispatcher.DirectChannel;
import org.springframework.integration.dispatcher.DirectChannelFactory;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
/**
* @author Marius Bogoevici
@@ -49,6 +53,7 @@ public class ChannelFactoryTests {
private final ArrayList<ChannelInterceptor> interceptors = new ArrayList<ChannelInterceptor>();
private final ArrayList<ChannelInterceptor> appliedInterceptors = new ArrayList<ChannelInterceptor>();
@Before
public void initInterceptorsList() {
@@ -112,12 +117,15 @@ public class ChannelFactoryTests {
channelFactoryBean.setBeanName("testChannel");
channelFactoryBean.setApplicationContext(applicationContext);
channelFactoryBean.setInterceptors(interceptors);
StubChannel channel = (StubChannel) channelFactoryBean.getObject();
MessageChannel channel = (MessageChannel) channelFactoryBean.getObject();
channel.getName();
assertEquals(StubChannel.class, ChannelParserTests.extractProxifiedChannel(channel).getClass());
assertEquals("testChannel", channel.getName());
assertInterceptors(channel);
channel.send(MessageBuilder.fromPayload("").build());
assertTrue(appliedInterceptors.get(0) == interceptors.get(0));
assertTrue(appliedInterceptors.get(1) == interceptors.get(1));
}
private void genericChannelFactoryTests(ChannelFactory channelFactory, Class<?> expectedChannelClass) {
assertNotNull(interceptors);
AbstractMessageChannel channel = (AbstractMessageChannel) channelFactory.getChannel("testChannel", interceptors);
@@ -131,12 +139,18 @@ public class ChannelFactoryTests {
Object interceptorsWrapper = new DirectFieldAccessor(channel).getPropertyValue("interceptors");
List<ChannelInterceptor> interceptors = (List<ChannelInterceptor>)
new DirectFieldAccessor(interceptorsWrapper).getPropertyValue("interceptors");
assertTrue(interceptors.get(0) == interceptors.get(0));
assertTrue(interceptors.get(1) == interceptors.get(1));
assertTrue(interceptors.get(0) == this.interceptors.get(0));
assertTrue(interceptors.get(1) == this.interceptors.get(1));
}
private static class TestChannelInterceptor extends ChannelInterceptorAdapter {
private class TestChannelInterceptor extends ChannelInterceptorAdapter {
public Message<?> preSend(Message<?> message, MessageChannel channel) {
appliedInterceptors.add(this);
return message;
}
}
}

View File

@@ -37,7 +37,9 @@ import org.springframework.integration.bus.MessageBusInterceptorTests;
import org.springframework.integration.bus.TestMessageBusAwareImpl;
import org.springframework.integration.bus.TestMessageBusStartInterceptor;
import org.springframework.integration.bus.TestMessageBusStopInterceptor;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.channel.config.ChannelParserTests;
import org.springframework.integration.dispatcher.DirectChannel;
import org.springframework.integration.handler.TestHandlers;
import org.springframework.integration.scheduling.SimpleTaskScheduler;
@@ -137,7 +139,9 @@ public class MessageBusParserTests {
public void testMessageBusWithChannelFactory() {
ApplicationContext context = new ClassPathXmlApplicationContext("messageBusWithChannelFactory.xml",
this.getClass());
assertEquals(DirectChannel.class, context.getBean("defaultTypeChannel").getClass());
((MessageChannel)context.getBean("defaultTypeChannel")).getName();
assertEquals(DirectChannel.class,
ChannelParserTests.extractProxifiedChannel(context.getBean("defaultTypeChannel")).getClass());
assertEquals(QueueChannel.class, context.getBean("specifiedTypeChannel").getClass());
}