public EventDrivenConsumer buildContext(String routerDef) { appContext = TestXmlApplicationContextHelper.getTestAppContext(channelConfig + routerDef); appContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); EventDrivenConsumer consumer = (EventDrivenConsumer) appContext.getBean("router"); consumer.start(); return consumer; }
@Test public void testtCPOrder() { this.outGateway.start(); this.testOutTcpNio.start(); @SuppressWarnings("unchecked") Set<MessageHandler> handlers = (Set<MessageHandler>) TestUtils .getPropertyValue( TestUtils.getPropertyValue(this.tcpChannel, "dispatcher"), "handlers"); Iterator<MessageHandler> iterator = handlers.iterator(); assertSame(this.tcpNewOut2, iterator.next()); //15 assertSame(this.tcpOutboundGateway, iterator.next()); //24 assertSame(this.tcpNewOut1, iterator.next()); //25 assertSame(this.tcpOut, iterator.next()); //35 }
@Test public void splitWithMassiveReplyMessages_allMessagesSent() { final int messageQuantity = 100000; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); final AtomicInteger receivedMessageCounter = new AtomicInteger(0); new EventDrivenConsumer(replyChannel, message -> { assertThat("Failure with msg: " + message, message.getPayload(), is(notNullValue())); receivedMessageCounter.incrementAndGet(); }).start(); splitter.handleMessage(this.message); assertThat(receivedMessageCounter.get(), is(messageQuantity)); }
@Test public void splitToIterator_allMessagesContainSequenceNumber() { final int messageQuantity = 5; MethodInvokingSplitter splitter = new MethodInvokingSplitter(new IteratorTestBean(messageQuantity)); splitter.setBeanFactory(mock(BeanFactory.class)); DirectChannel replyChannel = new DirectChannel(); splitter.setOutputChannel(replyChannel); splitter.afterPropertiesSet(); new EventDrivenConsumer(replyChannel, message -> assertThat("Failure with msg: " + message, message.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Integer.class), is(Integer.valueOf((String) message.getPayload())))).start(); splitter.handleMessage(this.message); }
@Test public void filterAcceptsWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> true); filter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertNotNull(reply); assertEquals(message.getPayload(), reply.getPayload()); }
@Test public void testSimpleStringExpressionWithCreateDocuments() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); TestXmlApplicationContext ctx = TestXmlApplicationContextHelper .getTestAppContext(channelDefinitions + "<si-xml:xpath-splitter id='splitter' input-channel='test-input' output-channel='test-output' create-documents='true'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>"); EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter"); consumer.start(); ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); inputChannel.send(docMessage); assertEquals("Wrong number of split messages ", 2, outputChannel.getQueueSize()); assertTrue("Splitter failed to create documents ", ((Message<?>) outputChannel.receive(1000)).getPayload() instanceof Document); assertTrue("Splitter failed to create documents ", ((Message<?>) outputChannel.receive(1000)).getPayload() instanceof Document); }
@Test public void testGateway() throws Exception { this.whichTest = 0; GatewayProxyFactoryBean gpfb = new GatewayProxyFactoryBean(Foo.class); gpfb.setBeanFactory(mock(BeanFactory.class)); DirectChannel input = new DirectChannel(); gpfb.setDefaultRequestChannel(input); gpfb.setDefaultReplyTimeout(10000L); gpfb.afterPropertiesSet(); Foo foo = (Foo) gpfb.getObject(); this.handler.setOutputChannel(null); EventDrivenConsumer consumer = new EventDrivenConsumer(input, this.handler); consumer.afterPropertiesSet(); consumer.start(); this.latch.countDown(); String result = foo.exchange("foo"); assertEquals("reply", result); }
@Test(expected = TestException.class) public void testCheckedExceptionRethrownAsIs() throws Exception { GatewayProxyFactoryBean proxyFactory = new GatewayProxyFactoryBean(); DirectChannel channel = new DirectChannel(); EventDrivenConsumer consumer = new EventDrivenConsumer(channel, new MessageHandler() { @Override public void handleMessage(Message<?> message) { Method method = ReflectionUtils.findMethod( GatewayProxyFactoryBeanTests.class, "throwTestException"); ReflectionUtils.invokeMethod(method, this); } }); consumer.start(); 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"); }
@Test(expected = MessageRejectedException.class) public void filterThrowsExceptionWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setThrowExceptionOnRejection(true); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); }
@Test public void correlationIdCopiedFromMessageId() { Message<String> message = MessageBuilder.withPayload("test").build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); DefaultMessageSplitter splitter = new DefaultMessageSplitter(); splitter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, splitter); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void jdkProxy() { DirectChannel input = new DirectChannel(); QueueChannel output = new QueueChannel(); GreetingService testBean = new GreetingBean(); ProxyFactory proxyFactory = new ProxyFactory(testBean); proxyFactory.setProxyTargetClass(true); testBean = (GreetingService) proxyFactory.getProxy(); MethodInvokingMessageGroupProcessor aggregator = new MethodInvokingMessageGroupProcessor(testBean); AggregatingMessageHandler handler = new AggregatingMessageHandler(aggregator); handler.setReleaseStrategy(new MessageCountReleaseStrategy()); handler.setOutputChannel(output); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler); endpoint.start(); Message<?> message = MessageBuilder.withPayload("proxy").setCorrelationId("abc").build(); input.send(message); assertEquals("hello proxy", output.receive(0).getPayload()); }
@Test public void filterRejectsSilentlyWithChannels() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); assertNull(outputChannel.receive(0)); }
@Test public void cglibProxy() { DirectChannel input = new DirectChannel(); QueueChannel output = new QueueChannel(); GreetingService testBean = new GreetingBean(); ProxyFactory proxyFactory = new ProxyFactory(testBean); proxyFactory.setProxyTargetClass(true); testBean = (GreetingService) proxyFactory.getProxy(); MethodInvokingMessageGroupProcessor aggregator = new MethodInvokingMessageGroupProcessor(testBean); AggregatingMessageHandler handler = new AggregatingMessageHandler(aggregator); handler.setReleaseStrategy(new MessageCountReleaseStrategy()); handler.setOutputChannel(output); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler); endpoint.start(); Message<?> message = MessageBuilder.withPayload("proxy").setCorrelationId("abc").build(); input.send(message); assertEquals("hello proxy", output.receive(0).getPayload()); }
@Test public void testCorrelationNotCopiedFromRequestMessgeIdIfAlreadySetByHandler() throws Exception { Message<?> message = new GenericMessage<String>("test"); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testSimpleStringExpression() throws Exception { Document doc = XmlTestUtil.getDocumentForString("<names><name>Bob</name><name>John</name></names>"); GenericMessage<Document> docMessage = new GenericMessage<Document>(doc); TestXmlApplicationContext ctx = TestXmlApplicationContextHelper .getTestAppContext(channelDefinitions + "<si-xml:xpath-splitter id='splitter' " + "order='2' send-timeout='123' auto-startup='false' phase='-1' " + "input-channel='test-input' output-channel='test-output'><si-xml:xpath-expression expression='//name'/></si-xml:xpath-splitter>"); EventDrivenConsumer consumer = (EventDrivenConsumer) ctx.getBean("splitter"); assertEquals(2, TestUtils.getPropertyValue(consumer, "handler.order")); assertEquals(123L, TestUtils.getPropertyValue(consumer, "handler.messagingTemplate.sendTimeout")); assertEquals(-1, TestUtils.getPropertyValue(consumer, "phase")); assertFalse(TestUtils.getPropertyValue(consumer, "autoStartup", Boolean.class)); consumer.start(); ctx.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false); inputChannel.send(docMessage); assertEquals("Wrong number of split messages ", 2, outputChannel.getQueueSize()); }
@Test public void testCorrelationIdPassedIfAvailable() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(correlationId, new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationNotPassedFromRequestHeaderIfAlreadySetByHandler() { Object correlationId = "123-ABC"; Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId(correlationId).build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "createMessage"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals("456-XYZ", new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void testCorrelationIdCopiedFromMessageCorrelationIdIfAvailable() { Message<String> message = MessageBuilder.withPayload("test") .setCorrelationId("correlationId").build(); DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(1); ServiceActivatingHandler serviceActivator = new ServiceActivatingHandler(new TestBean(), "upperCase"); serviceActivator.setOutputChannel(outputChannel); serviceActivator.setBeanFactory(mock(BeanFactory.class)); serviceActivator.afterPropertiesSet(); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, serviceActivator); endpoint.start(); assertTrue(inputChannel.send(message)); Message<?> reply = outputChannel.receive(0); assertEquals(new IntegrationMessageHeaderAccessor(message).getCorrelationId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId()); }
@Test public void filterDiscardsMessage() { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); QueueChannel discardChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setDiscardChannel(discardChannel); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); assertTrue(inputChannel.send(message)); Message<?> reply = discardChannel.receive(0); assertNotNull(reply); assertEquals(message, reply); assertNull(outputChannel.receive(0)); }
@Test(expected = MessageRejectedException.class) public void filterDiscardsMessageAndThrowsException() throws Exception { DirectChannel inputChannel = new DirectChannel(); QueueChannel outputChannel = new QueueChannel(); QueueChannel discardChannel = new QueueChannel(); MessageFilter filter = new MessageFilter(message -> false); filter.setOutputChannel(outputChannel); filter.setDiscardChannel(discardChannel); filter.setThrowExceptionOnRejection(true); EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, filter); endpoint.start(); Message<?> message = new GenericMessage<String>("test"); try { assertTrue(inputChannel.send(message)); } catch (Exception e) { throw e; } finally { Message<?> reply = discardChannel.receive(0); assertNotNull(reply); assertEquals(message, reply); assertNull(outputChannel.receive(0)); } }