Exemplo n.º 1
0
 /**
  * @param history a message history
  * @param componentName the name of a component to scan for
  * @param startingIndex the index to start scanning
  * @return the properties provided by the named component or null if none available
  */
 public static Properties locateComponentInHistory(
     MessageHistory history, String componentName, int startingIndex) {
   Assert.notNull(history, "'history' must not be null");
   Assert.isTrue(StringUtils.hasText(componentName), "'componentName' must be provided");
   Assert.isTrue(
       startingIndex < history.size(), "'startingIndex' can not be greater then size of history");
   Properties component = null;
   for (int i = startingIndex; i < history.size(); i++) {
     Properties properties = history.get(i);
     if (componentName.equals(properties.get("name"))) {
       component = properties;
       break;
     }
   }
   return component;
 }
  @Test
  public void testWithMessageHistory() throws Exception {
    GemfireMessageStore store = new GemfireMessageStore(this.cache);
    store.afterPropertiesSet();

    store.getMessageGroup(1);

    Message<?> message = new GenericMessage<String>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");

    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    store.addMessageToGroup(1, message);

    message = store.getMessageGroup(1).getMessages().iterator().next();

    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
  }
 @Override
 protected void handleMessage(Message<?> message) {
   if (this.shouldTrack) {
     message = MessageHistory.write(message, this, this.getMessageBuilderFactory());
   }
   try {
     this.messagingTemplate.send(getOutputChannel(), message);
   } catch (Exception e) {
     if (e instanceof MessagingException) {
       throw (MessagingException) e;
     } else {
       throw new MessagingException(message, "Failed to send Message", e);
     }
   }
 }
 @Test
 public void adapterWithJmsTemplate() {
   ClassPathXmlApplicationContext context =
       new ClassPathXmlApplicationContext("jmsInboundWithJmsTemplate.xml", this.getClass());
   PollableChannel output = (PollableChannel) context.getBean("output");
   Message<?> message = output.receive(timeoutOnReceive);
   MessageHistory history = MessageHistory.read(message);
   assertNotNull(history);
   Properties componentHistoryRecord =
       TestUtils.locateComponentInHistory(history, "inboundAdapter", 0);
   assertNotNull(componentHistoryRecord);
   assertEquals("jms:inbound-channel-adapter", componentHistoryRecord.get("type"));
   assertNotNull("message should not be null", message);
   assertEquals("polling-test", message.getPayload());
   context.stop();
 }
 @Test
 public void adapterWithMessageSelector() {
   ClassPathXmlApplicationContext context =
       new ClassPathXmlApplicationContext("jmsInboundWithMessageSelector.xml", this.getClass());
   PollableChannel output = (PollableChannel) context.getBean("output2");
   Message<?> message = output.receive(timeoutOnReceive);
   MessageHistory history = MessageHistory.read(message);
   assertNotNull(history);
   Properties componentHistoryRecord =
       TestUtils.locateComponentInHistory(history, "messageDrivenAdapter", 0);
   assertNotNull(componentHistoryRecord);
   JmsMessageDrivenEndpoint endpoint =
       context.getBean("messageDrivenAdapter", JmsMessageDrivenEndpoint.class);
   assertEquals("jms:message-driven-channel-adapter", componentHistoryRecord.get("type"));
   assertNotNull("message should not be null", message);
   assertEquals("test [with selector: TestProperty = 'foo']", message.getPayload());
   endpoint.stop();
   context.close();
 }
 @Test
 public void testGatewayWithConnectionFactoryAndDestination() {
   ClassPathXmlApplicationContext context =
       new ClassPathXmlApplicationContext(
           "jmsGatewayWithConnectionFactoryAndDestination.xml", this.getClass());
   PollableChannel channel = (PollableChannel) context.getBean("requestChannel");
   JmsMessageDrivenEndpoint gateway = (JmsMessageDrivenEndpoint) context.getBean("jmsGateway");
   assertEquals(JmsMessageDrivenEndpoint.class, gateway.getClass());
   context.start();
   Message<?> message = channel.receive(3000);
   MessageHistory history = MessageHistory.read(message);
   assertNotNull(history);
   Properties componentHistoryRecord =
       TestUtils.locateComponentInHistory(history, "jmsGateway", 0);
   assertNotNull(componentHistoryRecord);
   assertEquals("jms:inbound-gateway", componentHistoryRecord.get("type"));
   assertNotNull("message should not be null", message);
   assertEquals("message-driven-test", message.getPayload());
   context.stop();
 }
  @Test
  public void postRequestWithTextContentOk() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("test".getBytes());

    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but not in Spring
    // 3.0.7.RELEASE
    // Instead use:
    request.addHeader("Content-Type", "text/plain");

    MockHttpServletResponse response = new MockHttpServletResponse();
    postOnlyAdapter.handleRequest(request, response);
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    Message<?> message = requests.receive(0);
    MessageHistory history = MessageHistory.read(message);
    assertNotNull(history);
    Properties componentHistoryRecord =
        TestUtils.locateComponentInHistory(history, "postOnlyAdapter", 0);
    assertNotNull(componentHistoryRecord);
    assertEquals("http:inbound-channel-adapter", componentHistoryRecord.get("type"));
    assertNotNull(message);
    assertEquals("test", message.getPayload());
  }
  @Test
  public void testAnnotatedServiceActivator() throws Exception {
    this.serviceActivatorEndpoint.setReceiveTimeout(10000);
    this.serviceActivatorEndpoint.start();
    assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS));

    assertEquals(
        10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll"));

    Trigger trigger =
        TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    assertTrue(this.annotationTestService.isRunning());
    Log logger =
        spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1);
    doAnswer(
            new Answer<Void>() {

              @Override
              public Void answer(InvocationOnMock invocation) throws Throwable {
                pollerInterruptedLatch.countDown();
                invocation.callRealMethod();
                return null;
              }
            })
        .when(logger)
        .debug("Received no Message during the poll, returning 'false'");
    new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger);

    this.serviceActivatorEndpoint.stop();
    assertFalse(this.annotationTestService.isRunning());

    // wait until the service activator's poller is interrupted.
    assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS));
    this.serviceActivatorEndpoint.start();
    assertTrue(this.annotationTestService.isRunning());

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(100L, TestUtils.getPropertyValue(trigger, "period"));
    assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(CronTrigger.class));
    assertEquals(
        "0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression"));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(11L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));
    assertSame(this.myTrigger, trigger);

    trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class);
    assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class));
    assertEquals(10L, TestUtils.getPropertyValue(trigger, "period"));
    assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class));

    this.input.send(MessageBuilder.withPayload("Foo").build());

    Message<?> interceptedMessage = this.wireTapChannel.receive(10000);
    assertNotNull(interceptedMessage);
    assertEquals("Foo", interceptedMessage.getPayload());

    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals("FOO", receive.getPayload());

    MessageHistory messageHistory =
        receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    String messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.containsString("input"));
    assertThat(
        messageHistoryString,
        Matchers.containsString("annotationTestService.handle.serviceActivator.handler"));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));

    receive = this.publishedChannel.receive(10000);

    assertNotNull(receive);
    assertEquals("foo", receive.getPayload());

    messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class);
    assertNotNull(messageHistory);
    messageHistoryString = messageHistory.toString();
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input")));
    assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output")));
    assertThat(messageHistoryString, Matchers.containsString("publishedChannel"));

    assertNull(this.wireTapChannel.receive(0));
    assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0));
    assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0));

    assertTrue(
        this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source"));
    Object messageSource =
        this.context.getBean("annotationTestService.count.inboundChannelAdapter.source");
    assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class));

    assertNull(this.counterChannel.receive(10));

    SmartLifecycle countSA =
        this.context.getBean(
            "annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class);
    assertFalse(countSA.isAutoStartup());
    assertEquals(23, countSA.getPhase());
    countSA.start();

    for (int i = 0; i < 10; i++) {
      Message<?> message = this.counterChannel.receive(1000);
      assertNotNull(message);
      assertEquals(i + 1, message.getPayload());
    }

    Message<?> message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    message = this.fooChannel.receive(1000);
    assertNotNull(message);
    assertEquals("foo", message.getPayload());
    assertNull(this.fooChannel.receive(10));

    message = this.messageChannel.receive(1000);
    assertNotNull(message);
    assertEquals("bar", message.getPayload());
    assertTrue(message.getHeaders().containsKey("foo"));
    assertEquals("FOO", message.getHeaders().get("foo"));

    MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel);
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()"));
    assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
    this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()"));
    assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class));
  }