예제 #1
0
  @Test
  public void testReceiveMessageWithDestinationName() throws JMSException {
    JmsMessageReceiver receiver = new JmsMessageReceiver();
    receiver.setConnectionFactory(connectionFactory);

    receiver.setDestinationName("myDestination");

    Map<String, Object> controlHeaders = new HashMap<String, Object>();
    final Message<String> controlMessage =
        MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>")
            .copyHeaders(controlHeaders)
            .build();

    Map<String, String> headers = new HashMap<String, String>();

    reset(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    expect(connectionFactory.createConnection()).andReturn(connection).once();
    expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session).once();
    expect(session.getTransacted()).andReturn(false).once();
    expect(session.getAcknowledgeMode()).andReturn(Session.AUTO_ACKNOWLEDGE).once();

    expect(session.createQueue("myDestination")).andReturn(destinationQueue).once();

    expect(session.createConsumer(destinationQueue, null)).andReturn(messageConsumer).once();

    expect(messageConsumer.receive(5000L))
        .andReturn(
            new TextMessageImpl(
                "<TestRequest><Message>Hello World!</Message></TestRequest>", headers))
        .once();

    connection.start();
    expectLastCall().once();

    replay(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    Message<?> receivedMessage = receiver.receive();
    Assert.assertEquals(receivedMessage.getPayload(), controlMessage.getPayload());

    verify(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);
  }