/** * Receive from the {@link MessageSource} and send immediately to the input channel, so that the call that we are * intercepting always a message to receive. * * @see ChannelInterceptor#preReceive(MessageChannel) */ @Override public boolean preReceive(MessageChannel channel) { Message<?> message = source.receive(); if (message != null) { if (this.channel != null) { channel = this.channel; } channel.send(message); if (logger.isDebugEnabled()) { logger.debug("Sent " + message + " to channel " + channel); } return true; } return true; }
@Override protected Message<?> receiveMessage() { return this.source.receive(); }
/** * Build a mock for the {@link MessageSource} based on the provided message. * The returned instance is ordinary Mockito mock that is capable of * recording interactions with it and further verification. * @param message the message to return by mocked {@link MessageSource} * @return the mocked {@link MessageSource} * @see Mockito#mock(Class) */ @SuppressWarnings("rawtypes") public static MessageSource<?> mockMessageSource(Message<?> message) { MessageSource messageSource = Mockito.mock(MessageSource.class); BDDMockito.given(messageSource.receive()) .willReturn(message); return messageSource; }
/** * Build a mock for the {@link MessageSource} based on the provided messages. * The returned instance is ordinary Mockito mock that is capable of * recording interactions with it and further verification. * @param message the first message to return by mocked {@link MessageSource} * @param messages the next messages to return by mocked {@link MessageSource} * @return the mocked {@link MessageSource} * @see Mockito#mock(Class) */ @SuppressWarnings("rawtypes") public static MessageSource<?> mockMessageSource(Message<?> message, Message<?>... messages) { MessageSource messageSource = Mockito.mock(MessageSource.class); BDDMockito.given(messageSource.receive()) .willReturn(message, messages); return messageSource; }
@Override public boolean poll(MessageHandler handler) { Assert.notNull(handler, "'handler' cannot be null"); Message<?> message = this.source.receive(); if (message != null) { AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message); try { handler.handleMessage(message); AckUtils.autoAck(ackCallback); } catch (Exception e) { AckUtils.autoNack(ackCallback); throw new MessageHandlingException(message, e); } return true; } return false; }
@Override protected Message<?> receiveMessage() { return this.source.receive(); }
Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding()); assertEquals(Charset.forName("UTF-8"), readerCharset); Message<?> message = source.receive(); assertNotNull(message); assertEquals("foo", message.getPayload());
@Test public void adapterWithDefaultCharset() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "consoleInboundChannelAdapterParserTests.xml", ConsoleInboundChannelAdapterParserTests.class); SourcePollingChannelAdapter adapter = context.getBean("adapterWithDefaultCharset.adapter", SourcePollingChannelAdapter.class); MessageSource<?> source = (MessageSource<?>) new DirectFieldAccessor(adapter).getPropertyValue("source"); assertTrue(source instanceof NamedComponent); assertEquals("adapterWithDefaultCharset.adapter", adapter.getComponentName()); assertEquals("stream:stdin-channel-adapter(character)", adapter.getComponentType()); assertEquals("stream:stdin-channel-adapter(character)", ((NamedComponent) source).getComponentType()); DirectFieldAccessor sourceAccessor = new DirectFieldAccessor(source); Reader bufferedReader = (Reader) sourceAccessor.getPropertyValue("reader"); assertEquals(BufferedReader.class, bufferedReader.getClass()); DirectFieldAccessor bufferedReaderAccessor = new DirectFieldAccessor(bufferedReader); Reader reader = (Reader) bufferedReaderAccessor.getPropertyValue("in"); assertEquals(InputStreamReader.class, reader.getClass()); Charset readerCharset = Charset.forName(((InputStreamReader) reader).getEncoding()); assertEquals(Charset.defaultCharset(), readerCharset); Message<?> message = source.receive(); assertNotNull(message); assertEquals("foo", message.getPayload()); adapter = context.getBean("pipedAdapterNoCharset.adapter", SourcePollingChannelAdapter.class); source = adapter.getMessageSource(); assertTrue(TestUtils.getPropertyValue(source, "blockToDetectEOF", Boolean.class)); context.close(); }
@Test public void ensurePollerTaskStopsForAdapter() throws Exception { final CountDownLatch latch = new CountDownLatch(1); QueueChannel channel = new QueueChannel(); SourcePollingChannelAdapterFactoryBean adapterFactory = new SourcePollingChannelAdapterFactoryBean(); PollerMetadata pollerMetadata = new PollerMetadata(); pollerMetadata.setTrigger(new PeriodicTrigger(2000)); adapterFactory.setPollerMetadata(pollerMetadata); //Has to be an explicit implementation - Mockito cannot mock/spy lambdas MessageSource<String> source = spy(new MessageSource<String>() { @Override public Message<String> receive() { latch.countDown(); return new GenericMessage<>("hello"); } }); adapterFactory.setSource(source); adapterFactory.setOutputChannel(channel); adapterFactory.setBeanFactory(mock(ConfigurableBeanFactory.class)); SourcePollingChannelAdapter adapter = adapterFactory.getObject(); adapter.setTaskScheduler(this.taskScheduler); adapter.afterPropertiesSet(); adapter.start(); assertTrue(latch.await(20, TimeUnit.SECONDS)); assertNotNull(channel.receive(100)); adapter.stop(); assertNull(channel.receive(10)); Mockito.verify(source, times(1)).receive(); }
@SuppressWarnings("unchecked") @Override public Message<T> receive() { Message<?> orig = source.receive(); if (orig != null) { return (Message<T>) transformer.transform(orig); } return null; } }
@Override public int read() throws IOException { if (currentPayload == null || index >= currentPayload.length) { Message<byte[]> msg = source.receive(); if (msg != null) { currentPayload = msg.getPayload(); index = 0; } else { return -1; } } return (currentPayload[index++] & 0xff); }
this.channel.send(message); try { this.channel.send(this.source.receive()); fail("Expected exception");
/** * Build a mock for the {@link MessageSource} based on the provided message. * The returned instance is ordinary Mockito mock that is capable of * recording interactions with it and further verification. * @param message the message to return by mocked {@link MessageSource} * @return the mocked {@link MessageSource} * @see Mockito#mock(Class) */ @SuppressWarnings("rawtypes") public static MessageSource<?> mockMessageSource(Message<?> message) { MessageSource messageSource = Mockito.mock(MessageSource.class); BDDMockito.given(messageSource.receive()) .willReturn(message); return messageSource; }
/** * Build a mock for the {@link MessageSource} based on the provided messages. * The returned instance is ordinary Mockito mock that is capable of * recording interactions with it and further verification. * @param message the first message to return by mocked {@link MessageSource} * @param messages the next messages to return by mocked {@link MessageSource} * @return the mocked {@link MessageSource} * @see Mockito#mock(Class) */ @SuppressWarnings("rawtypes") public static MessageSource<?> mockMessageSource(Message<?> message, Message<?>... messages) { MessageSource messageSource = Mockito.mock(MessageSource.class); BDDMockito.given(messageSource.receive()) .willReturn(message, messages); return messageSource; }
@Override public boolean poll(MessageHandler handler) { Assert.notNull(handler, "'handler' cannot be null"); Message<?> message = this.source.receive(); if (message != null) { AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message); try { handler.handleMessage(message); AckUtils.autoAck(ackCallback); } catch (Exception e) { AckUtils.autoNack(ackCallback); throw new MessageHandlingException(message, e); } return true; } return false; }
/** * Receives Message from the source and converts its payload to a provided type. * Can return null */ private Message<?> receive(ParameterizedTypeReference<?> type) { Message<?> message = this.source.receive(); if (message != null && type != null && this.messageConverter != null) { Class<?> targetType = type == null ? Object.class : type.getType() instanceof Class ? (Class<?>) type.getType() : Object.class; Object payload = this.messageConverter.fromMessage(message, targetType, type); if (payload == null) { throw new MessageConversionException(message, "No converter could convert Message"); } message = MessageBuilder.withPayload(payload) .copyHeaders(message.getHeaders()) .build(); } return message; }