@Test public void testChannelInteceptorInnerBean() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("channelInterceptorParserTests.xml", getClass()); PollableChannel channel = (PollableChannel) context.getBean("channelWithInterceptorInnerBean"); channel.send(new GenericMessage<String>("test")); Message<?> transformed = channel.receive(1000); assertEquals("TEST", transformed.getPayload()); context.close(); }
@Override protected Object doInvoke(ExecutionCallback callback, Object target, Message<?> message) throws Exception { adviceChannel().send(message); return callback.execute(); }
@Test public void testSolicitResponse() { PollableChannel channel = (PollableChannel) context.getBean("replyChannel"); channel.send(new GenericMessage<String>("foo")); TestService service = (TestService) context.getBean("solicitResponse"); String result = service.solicitResponse(); assertEquals("foo", result); }
@Test public void autoDetectionWithApplicationContext() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("messageBusTests.xml", this.getClass()); context.start(); PollableChannel sourceChannel = (PollableChannel) context.getBean("sourceChannel"); sourceChannel.send(new GenericMessage<>("test")); PollableChannel targetChannel = (PollableChannel) context.getBean("targetChannel"); Message<?> result = targetChannel.receive(10000); assertEquals("test", result.getPayload()); context.close(); }
@Test public void testSendInAnotherThread() throws Exception { simpleChannel.send(new GenericMessage<String>("test")); Executor otherThreadExecutor = Executors.newSingleThreadExecutor(); final CountDownLatch latch = new CountDownLatch(1); otherThreadExecutor.execute(() -> { simpleChannel.send(new GenericMessage<String>("crap")); latch.countDown(); }); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals("test", simpleChannel.receive(10).getPayload()); // Message sent on another thread is not collected here assertEquals(null, simpleChannel.receive(10)); }
@Test public void testPriorityChannelWithCustomComparator() { PollableChannel channel = this.context.getBean("priorityChannelWithCustomComparator", PollableChannel.class); channel.send(new GenericMessage<>("C")); channel.send(new GenericMessage<>("A")); channel.send(new GenericMessage<>("D")); channel.send(new GenericMessage<>("B")); Message<?> reply1 = channel.receive(0); Message<?> reply2 = channel.receive(0); Message<?> reply3 = channel.receive(0); Message<?> reply4 = channel.receive(0); assertEquals("A", reply1.getPayload()); assertEquals("B", reply2.getPayload()); assertEquals("C", reply3.getPayload()); assertEquals("D", reply4.getPayload()); }
@Test public void testPriorityChannelWithIntegerDatatypeEnforced() { PollableChannel channel = this.context.getBean("integerOnlyPriorityChannel", PollableChannel.class); channel.send(new GenericMessage<>(3)); channel.send(new GenericMessage<>(2)); channel.send(new GenericMessage<>(1)); assertEquals(1, channel.receive(0).getPayload()); assertEquals(2, channel.receive(0).getPayload()); assertEquals(3, channel.receive(0).getPayload()); boolean threwException = false; try { channel.send(new GenericMessage<>("wrong type")); } catch (MessageDeliveryException e) { assertEquals("wrong type", e.getFailedMessage().getPayload()); threwException = true; } assertTrue(threwException); }
@Test public void testReceiveInAnotherThread() throws Exception { simpleChannel.send(new GenericMessage<String>("test-1.1")); simpleChannel.send(new GenericMessage<String>("test-1.2")); simpleChannel.send(new GenericMessage<String>("test-1.3")); channelWithInterceptor.send(new GenericMessage<String>("test-2.1")); channelWithInterceptor.send(new GenericMessage<String>("test-2.2")); Executor otherThreadExecutor = Executors.newSingleThreadExecutor(); final List<Object> otherThreadResults = new ArrayList<Object>(); final CountDownLatch latch = new CountDownLatch(2); otherThreadExecutor.execute(() -> { otherThreadResults.add(simpleChannel.receive(0)); latch.countDown(); }); otherThreadExecutor.execute(() -> { otherThreadResults.add(channelWithInterceptor.receive(0)); latch.countDown(); }); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(2, otherThreadResults.size()); assertNull(otherThreadResults.get(0)); assertNull(otherThreadResults.get(1)); assertEquals("test-1.1", simpleChannel.receive(0).getPayload()); assertEquals("test-1.2", simpleChannel.receive(0).getPayload()); assertEquals("test-1.3", simpleChannel.receive(0).getPayload()); assertNull(simpleChannel.receive(0)); assertEquals("test-2.1", channelWithInterceptor.receive(0).getPayload()); assertEquals("test-2.2", channelWithInterceptor.receive(0).getPayload()); assertNull(channelWithInterceptor.receive(0)); }
@Test public void testChannelInteceptorRef() { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("channelInterceptorParserTests.xml", getClass()); PollableChannel channel = (PollableChannel) context.getBean("channelWithInterceptorRef"); TestChannelInterceptor interceptor = (TestChannelInterceptor) context.getBean("interceptor"); assertEquals(0, interceptor.getSendCount()); channel.send(new GenericMessage<>("test")); assertEquals(1, interceptor.getSendCount()); assertEquals(0, interceptor.getReceiveCount()); channel.receive(); assertEquals(1, interceptor.getReceiveCount()); context.close(); }
@Test public void testPriorityChannelWithDefaultComparator() { PollableChannel channel = this.context.getBean("priorityChannelWithDefaultComparator", PollableChannel.class); Message<String> lowPriorityMessage = MessageBuilder.withPayload("low").setPriority(-14).build(); Message<String> midPriorityMessage = MessageBuilder.withPayload("mid").setPriority(0).build(); Message<String> highPriorityMessage = MessageBuilder.withPayload("high").setPriority(99).build(); channel.send(lowPriorityMessage); channel.send(highPriorityMessage); channel.send(midPriorityMessage); Message<?> reply1 = channel.receive(0); Message<?> reply2 = channel.receive(0); Message<?> reply3 = channel.receive(0); assertEquals("high", reply1.getPayload()); assertEquals("mid", reply2.getPayload()); assertEquals("low", reply3.getPayload()); }
@Test public void propagationNotSupported() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "propagationNotSupportedTests.xml", this.getClass()); TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); PollableChannel input = (PollableChannel) context.getBean("input"); PollableChannel output = (PollableChannel) context.getBean("output"); assertEquals(0, txManager.getCommitCount()); input.send(new GenericMessage<>("test")); Message<?> reply = output.receive(10000); assertNotNull(reply); assertEquals(0, txManager.getCommitCount()); assertNull(txManager.getLastDefinition()); context.close(); }
@Test public void propagationMandatory() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "propagationMandatoryTests.xml", this.getClass()); TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); PollableChannel input = (PollableChannel) context.getBean("input"); PollableChannel output = (PollableChannel) context.getBean("output"); PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel"); assertEquals(0, txManager.getCommitCount()); input.send(new GenericMessage<>("test")); Message<?> errorMessage = errorChannel.receive(10000); assertNotNull(errorMessage); Object payload = errorMessage.getPayload(); assertEquals(MessagingException.class, payload.getClass()); MessagingException messagingException = (MessagingException) payload; assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass()); assertNull(output.receive(0)); assertEquals(0, txManager.getCommitCount()); context.close(); }
@Test public void propagationSupports() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "propagationSupportsTests.xml", this.getClass()); TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); PollableChannel input = (PollableChannel) context.getBean("input"); PollableChannel output = (PollableChannel) context.getBean("output"); assertEquals(0, txManager.getCommitCount()); input.send(new GenericMessage<>("test")); Message<?> reply = output.receive(10000); assertNotNull(reply); assertEquals(0, txManager.getCommitCount()); assertNull(txManager.getLastDefinition()); context.close(); }
@Test public void testInterceptor() { int before = interceptor.getSendCount(); channelWithInterceptor.send(new GenericMessage<String>("test")); assertEquals(before + 1, interceptor.getSendCount()); }
@Test public void pollableChannel() { Message<?> message = new GenericMessage<String>("test1"); this.pollableChannel.send(message); Message<?> reply = this.output1.receive(6000); assertThat(message, sameExceptImmutableHeaders(reply)); }
@Test public void testBarrier() { Message<?> releasing = MessageBuilder.withPayload("bar").setHeader(BARRIER, "foo").build(); this.releaseChannel.send(releasing); Message<?> suspending = MessageBuilder.withPayload("foo").setHeader(BARRIER, "foo").build(); this.barrierFlowInput.send(suspending); Message<?> out = this.barrierResults.receive(10000); assertNotNull(out); assertEquals("bar", out.getPayload()); }
@Test public void propagationRequiresNew() throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "propagationRequiresNewTests.xml", this.getClass()); TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); PollableChannel input = (PollableChannel) context.getBean("input"); PollableChannel output = (PollableChannel) context.getBean("output"); assertEquals(0, txManager.getCommitCount()); input.send(new GenericMessage<>("test")); Message<?> reply = output.receive(10000); assertNotNull(reply); txManager.waitForCompletion(10000); assertEquals(1, txManager.getCommitCount()); assertEquals(Propagation.REQUIRES_NEW.value(), txManager.getLastDefinition().getPropagationBehavior()); context.close(); }
@Test public void propagationRequired() throws InterruptedException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "propagationRequiredTests.xml", this.getClass()); TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager"); PollableChannel input = (PollableChannel) context.getBean("input"); PollableChannel output = (PollableChannel) context.getBean("output"); assertEquals(0, txManager.getCommitCount()); input.send(new GenericMessage<>("test")); Message<?> reply = output.receive(10000); assertNotNull(reply); txManager.waitForCompletion(10000); assertEquals(1, txManager.getCommitCount()); assertEquals(Propagation.REQUIRED.value(), txManager.getLastDefinition().getPropagationBehavior()); context.close(); }
@Test public void testBridge() { GenericMessage<String> message = new GenericMessage<>("test"); this.bridgeFlowInput.send(message); Message<?> reply = this.bridgeFlowOutput.receive(10000); assertNotNull(reply); assertEquals("test", reply.getPayload()); assertTrue(this.beanFactory.containsBean("bridgeFlow2.channel#0")); assertThat(this.beanFactory.getBean("bridgeFlow2.channel#0"), instanceOf(FixedSubscriberChannel.class)); try { this.bridgeFlow2Input.send(message); fail("Expected MessageDispatchingException"); } catch (Exception e) { assertThat(e, instanceOf(MessageDeliveryException.class)); assertThat(e.getCause(), instanceOf(MessageDispatchingException.class)); assertThat(e.getMessage(), containsString("Dispatcher has no subscribers")); } this.controlBus.send("@bridge.start()"); this.bridgeFlow2Input.send(message); reply = this.bridgeFlow2Output.receive(10000); assertNotNull(reply); assertEquals("test", reply.getPayload()); assertTrue(this.delayedAdvice.getInvoked()); }
PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel"); assertEquals(0, txManager.getCommitCount()); inputTxFail.send(new GenericMessage<>("commitFailureTest")); Message<?> errorMessage = errorChannel.receive(20000); assertNotNull(errorMessage); assertEquals(0, txManager.getCommitCount()); inputHandlerFail.send(new GenericMessage<>("handlerFailureTest")); errorMessage = errorChannel.receive(10000); assertNotNull(errorMessage);