FileIndexerQueueListener implements MessageListener { @Override public void onMessage(final Message message) { if (message instanceof ObjectMessage) { ObjectMessage objectMessage = (ObjectMessage) message; try { IndexMessage indexMessage = (IndexMessage) objectMessage.getObject(); String fileName = indexMessage.getFileName(); String type = indexMessage.getType(); if (type.equals(IndexingType.ADD.getName())) { luceneEngine.addFileToIndex(fileName); } else if (type.equals(IndexingType.DELETE.getName())) { luceneEngine.deleteFileFromIndex(fileName); } } catch (JMSException e) { throw new SystemException("Message information transfer failed. Reason:" + e.getMessage()); } catch (OutOfMemoryError e) { logger.error("HeapMaxMemory:" + Runtime.getRuntime().maxMemory() + ", HeapTotalMemory:" + Runtime.getRuntime().totalMemory() + ", HeapFreeMemory:" + Runtime.getRuntime().freeMemory() + ", Exception: ", e); shutdown(); } } }  @Override void onMessage(final Message message);  }
@Test public void testOnMessageWhenTypeIsAddShouldAddFileToIndex() throws JMSException { indexMessage = new IndexMessage(FILENAME, IndexingType.ADD.getName()); given(objectMessage.getObject()).willReturn(indexMessage); underTest.onMessage(objectMessage); verify(luceneEngine).addFileToIndex(FILENAME); }
@Test public void testOnMessageWhenTypeIsDeleteShouldAddDeleteFromIndex() throws JMSException { indexMessage = new IndexMessage(FILENAME, IndexingType.DELETE.getName()); given(objectMessage.getObject()).willReturn(indexMessage); underTest.onMessage(objectMessage); verify(luceneEngine).deleteFileFromIndex(FILENAME); }
@Test public void testOnMessageWhenTypeIsNotAddOrDeleteShouldDoNothing() throws JMSException { indexMessage = new IndexMessage(FILENAME, "modify"); given(objectMessage.getObject()).willReturn(indexMessage); underTest.onMessage(objectMessage); verify(luceneEngine, never()).addFileToIndex(FILENAME); verify(luceneEngine, never()).deleteFileFromIndex(FILENAME); }
@Test public void testOnMessageWhenMessageIsNotObjectMessageShouldDoNothing() throws JMSException { indexMessage = new IndexMessage(FILENAME, "modify"); given(objectMessage.getObject()).willReturn(indexMessage); underTest.onMessage(message); verify(luceneEngine, never()).addFileToIndex(FILENAME); verify(luceneEngine, never()).deleteFileFromIndex(FILENAME); }
@Test(expectedExceptions = SystemException.class) public void testOnMessageWhenCannotGetObjectShouldThrowSystemException() throws JMSException { indexMessage = new IndexMessage(FILENAME, IndexingType.ADD.getName()); given(objectMessage.getObject()).willThrow(new JMSException("exception")); underTest.onMessage(objectMessage); }