JmsMessageListener implements MessageListener { @Override public void onMessage(Message msg) { logger.debug("received new message from queue '{}'", queue); String text = Exceptions.sneak().get(() -> extractTextFromMessage(msg)); SimpleMessage message = new SimpleMessage(text, queue, getMessageProperties(msg)); storage.addMessage(message); } JmsMessageListener(String queue, LocalMessageStorage storage, List<String> propertiesToExtract,
			String encoding); @Override void onMessage(Message msg); Map<String, String> getMessageProperties(Message msg); static final String JMS_IBM_CHARACTER_SET; }
@Test public void shouldAddNewMessageToStorage() throws JMSException { TextMessage message = new MockTextMessage("my awesome message"); sut.onMessage(message); Collection<SimpleMessage> resultInStorage = storage.getMessagesAfter(0); assertThat(resultInStorage).hasSize(1); SimpleMessage first = resultInStorage.iterator().next(); assertThat(first.getText()).isEqualTo("my awesome message"); assertThat(first.getProps()).isEmpty(); }
@Test public void shouldAddNewUTF8BytesMessageToStorage() throws JMSException { MockBytesMessage message = new MockBytesMessage(); message.writeBytes("my awesome message \uF609".getBytes(Charset.forName("UTF-8"))); message.reset(); sut.onMessage(message); Collection<SimpleMessage> resultInStorage = storage.getMessagesAfter(0); assertThat(resultInStorage).hasSize(1); SimpleMessage first = resultInStorage.iterator().next(); assertThat(first.getText()).isEqualTo("my awesome message \uF609"); assertThat(first.getProps()).isEmpty(); }
@Test public void shouldAddNewIso885915BytesMessageToStorage() throws JMSException { MockBytesMessage message = new MockBytesMessage(); message.writeBytes("my awesome message üüü".getBytes(Charset.forName("ISO-8859-15"))); message.setStringProperty(JMS_IBM_CHARACTER_SET, "ISO-8859-15"); message.reset(); sut.onMessage(message); Collection<SimpleMessage> resultInStorage = storage.getMessagesAfter(0); assertThat(resultInStorage).hasSize(1); SimpleMessage first = resultInStorage.iterator().next(); assertThat(first.getText()).isEqualTo("my awesome message üüü"); assertThat(first.getProps()).isEmpty(); }
@Test public void shouldAddNewMapMessageToStorage() throws JMSException { MockMapMessage message = new MockMapMessage(); message.setString("myText", "my awesome message \uF609"); sut.onMessage(message); Collection<SimpleMessage> resultInStorage = storage.getMessagesAfter(0); assertThat(resultInStorage).hasSize(1); SimpleMessage first = resultInStorage.iterator().next(); assertThat(first.getText()).isNotNull(); assertThat(first.getProps()).isEmpty(); }
@Test public void shouldExtractOnlySpecifiedProperties() throws JMSException { TextMessage message = new MockTextMessage("test"); message.setStringProperty("propA", "valA"); message.setStringProperty("propB", "valB"); sut.onMessage(message); Collection<SimpleMessage> resultInStorage = storage.getMessagesAfter(0); assertThat(resultInStorage).hasSize(1); SimpleMessage first = resultInStorage.iterator().next(); assertThat(first.getText()).isEqualTo("test"); assertThat(first.getQueue()).isEqualTo("myQueue"); assertThat(first.getProps()).hasSize(1).contains(entry("propA", "valA")); }