Example usage for org.springframework.integration.channel AbstractSubscribableChannel unsubscribe

List of usage examples for org.springframework.integration.channel AbstractSubscribableChannel unsubscribe

Introduction

In this page you can find the example usage for org.springframework.integration.channel AbstractSubscribableChannel unsubscribe.

Prototype

@Override
    public boolean unsubscribe(MessageHandler handle) 

Source Link

Usage

From source file:org.springframework.integration.channel.P2pChannelTests.java

/**
 * @param channel/*w  w w . j  av a 2  s.  c om*/
 */
private void verifySubscriptions(final AbstractSubscribableChannel channel) {
    final Log logger = mock(Log.class);
    when(logger.isInfoEnabled()).thenReturn(true);
    final List<String> logs = new ArrayList<String>();
    doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) throws Throwable {
            logs.add((String) invocation.getArguments()[0]);
            return null;
        }
    }).when(logger).info(Mockito.anyString());
    ReflectionUtils.doWithFields(AbstractMessageChannel.class, new FieldCallback() {
        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if ("logger".equals(field.getName())) {
                field.setAccessible(true);
                field.set(channel, logger);
            }
        }
    });
    String log = "Channel '" + channel.getComponentName() + "' has " + "%d subscriber(s).";

    MessageHandler handler1 = mock(MessageHandler.class);
    channel.subscribe(handler1);
    assertEquals(String.format(log, 1), logs.remove(0));
    MessageHandler handler2 = mock(MessageHandler.class);
    channel.subscribe(handler2);
    assertEquals(String.format(log, 2), logs.remove(0));
    channel.unsubscribe(handler1);
    assertEquals(String.format(log, 1), logs.remove(0));
    channel.unsubscribe(handler1);
    assertEquals(0, logs.size());
    channel.unsubscribe(handler2);
    assertEquals(String.format(log, 0), logs.remove(0));
    verify(logger, times(4)).info(Mockito.anyString());
}