From b3f722b8d4894c79f1953d53891102d6f7dcca38 Mon Sep 17 00:00:00 2001 From: Ingo Bauersachs Date: Thu, 27 Nov 2014 16:03:46 +0100 Subject: Make the history "unit" tests a little more independent from each other --- .../slick/callhistory/TestCallHistoryService.java | 13 +- .../slick/metahistory/TestMetaHistoryService.java | 17 +- .../slick/msghistory/MsgHistoryServiceLick.java | 1 + .../slick/msghistory/TestMsgHistoryService.java | 273 +----------- .../msghistory/TestMsgHistoryServiceMultiChat.java | 464 +++++++++++++++++++++ 5 files changed, 497 insertions(+), 271 deletions(-) create mode 100644 test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryServiceMultiChat.java (limited to 'test') diff --git a/test/net/java/sip/communicator/slick/callhistory/TestCallHistoryService.java b/test/net/java/sip/communicator/slick/callhistory/TestCallHistoryService.java index 59c84c8..9a433d8 100644 --- a/test/net/java/sip/communicator/slick/callhistory/TestCallHistoryService.java +++ b/test/net/java/sip/communicator/slick/callhistory/TestCallHistoryService.java @@ -65,10 +65,6 @@ public class TestCallHistoryService { TestSuite suite = new TestSuite(); suite.addTest( - new TestCallHistoryService("setupContact")); - suite.addTest( - new TestCallHistoryService("writeRecords")); - suite.addTest( new TestCallHistoryService("readRecords")); suite.addTest( new TestCallHistoryService("checkRecordCompleteness")); @@ -79,6 +75,9 @@ public class TestCallHistoryService @Override protected void setUp() throws Exception { + setupContact(); + callHistoryService.eraseLocallyStoredHistory(); + writeRecords(); } @Override @@ -86,7 +85,7 @@ public class TestCallHistoryService { } - public void setupContact() + private void setupContact() { // changes the history service target derictory System.setProperty("HistoryServiceDirectory", "test-callhistory"); @@ -131,18 +130,20 @@ public class TestCallHistoryService /** * First create calls */ - public void writeRecords() + private void writeRecords() { logger.info("write records "); generateCall(participantAddresses.get(0)); + waitSeconds(1); controlDate1 = new Date(); generateCall(participantAddresses.get(1)); generateCall(participantAddresses.get(2)); + waitSeconds(1); controlDate2 = new Date(); generateCall(participantAddresses.get(3)); diff --git a/test/net/java/sip/communicator/slick/metahistory/TestMetaHistoryService.java b/test/net/java/sip/communicator/slick/metahistory/TestMetaHistoryService.java index ba35f7d..f87e380 100644 --- a/test/net/java/sip/communicator/slick/metahistory/TestMetaHistoryService.java +++ b/test/net/java/sip/communicator/slick/metahistory/TestMetaHistoryService.java @@ -14,6 +14,7 @@ import net.java.sip.communicator.impl.protocol.mock.*; import net.java.sip.communicator.service.callhistory.*; import net.java.sip.communicator.service.contactlist.*; import net.java.sip.communicator.service.filehistory.*; +import net.java.sip.communicator.service.history.*; import net.java.sip.communicator.service.metahistory.*; import net.java.sip.communicator.service.msghistory.*; import net.java.sip.communicator.service.protocol.*; @@ -94,10 +95,6 @@ public class TestMetaHistoryService { TestSuite suite = new TestSuite(); suite.addTest( - new TestMetaHistoryService("setupContact")); - suite.addTest( - new TestMetaHistoryService("writeRecords")); - suite.addTest( new TestMetaHistoryService("messageTests")); suite.addTest( new TestMetaHistoryService("callTests")); @@ -105,8 +102,6 @@ public class TestMetaHistoryService new TestMetaHistoryService("fileTests")); suite.addTest( new TestMetaHistoryService("metaTests")); - suite.addTest( - new TestMetaHistoryService("testPurgeLocalContactListCopy")); return suite; } @@ -114,11 +109,18 @@ public class TestMetaHistoryService @Override protected void setUp() throws Exception { + setupContact(); + ServiceUtils.getService(MetaHistoryServiceLick.bc, + MessageHistoryService.class).eraseLocallyStoredHistory(); + ServiceUtils.getService(MetaHistoryServiceLick.bc, + HistoryService.class).purgeLocallyCachedHistories(); + writeRecords(); } @Override protected void tearDown() throws Exception { + testPurgeLocalContactListCopy(); } public void setupContact() @@ -252,6 +254,7 @@ public class TestMetaHistoryService mockBImOpSet.deliverMessage(TEST_CONTACT_NAME_1, messagesToSend[0]); waitSeconds(200); mockBImOpSet.deliverMessage(TEST_CONTACT_NAME_2, messagesToSend[1]); + waitSeconds(200); controlDate1 = new Date(); @@ -427,7 +430,7 @@ public class TestMetaHistoryService new String[]{MessageHistoryService.class.getName()}, testMetaContact, controlDate1, controlDate2); - assertTrue("Nothing found findByPeriod", !rs.isEmpty()); + assertFalse("Nothing found findByPeriod", rs.isEmpty()); msgs = getMessages(rs); diff --git a/test/net/java/sip/communicator/slick/msghistory/MsgHistoryServiceLick.java b/test/net/java/sip/communicator/slick/msghistory/MsgHistoryServiceLick.java index b1b0ed5..0e547f4 100644 --- a/test/net/java/sip/communicator/slick/msghistory/MsgHistoryServiceLick.java +++ b/test/net/java/sip/communicator/slick/msghistory/MsgHistoryServiceLick.java @@ -39,6 +39,7 @@ public class MsgHistoryServiceLick extends TestSuite implements BundleActivator properties.put("service.pid", getName()); addTest(TestMsgHistoryService.suite()); + addTest(TestMsgHistoryServiceMultiChat.suite()); bundleContext.registerService(getClass().getName(), this, properties); logger.debug("Successfully registered " + getClass().getName()); diff --git a/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java index d78a912..3850290 100644 --- a/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java +++ b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryService.java @@ -86,21 +86,11 @@ public class TestMsgHistoryService { TestSuite suite = new TestSuite(); suite.addTest( - new TestMsgHistoryService("setupContact")); - suite.addTest( - new TestMsgHistoryService("writeRecords")); - suite.addTest( new TestMsgHistoryService("readRecords")); suite.addTest( new TestMsgHistoryService("specialChars")); suite.addTest( new TestMsgHistoryService("insertRecords")); - suite.addTest( - new TestMsgHistoryService("writeRecordsToMultiChat")); - suite.addTest( - new TestMsgHistoryService("readRecordsFromMultiChat")); - suite.addTest( - new TestMsgHistoryService("testPurgeLocalContactListCopy")); return suite; } @@ -108,11 +98,16 @@ public class TestMsgHistoryService @Override protected void setUp() throws Exception { + setupContact(); + msgHistoryService.eraseLocallyStoredHistory(); + historyService.purgeLocallyCachedHistories(); + writeRecords(); } @Override protected void tearDown() throws Exception { + metaClService.purgeLocallyStoredContactListCopy(); } public void setupContact() @@ -247,7 +242,7 @@ public class TestMsgHistoryService public void readRecords() { /** - * This matches all written messages, they are minimum 5 + * This matches all written messages, they are minimum 3 */ Collection rs = msgHistoryService.findByKeyword(testMetaContact, "test"); @@ -256,7 +251,7 @@ public class TestMsgHistoryService List msgs = getMessages(rs); - assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); + assertTrue("Messages too few - findByKeyword", msgs.size() >= 3); /** * Will test case sensitive and insensitive search @@ -269,7 +264,7 @@ public class TestMsgHistoryService msgs = getMessages(rs); - assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); + assertTrue("Messages too few - findByKeyword", msgs.size() >= 3); rs = msgHistoryService.findByKeyword(testMetaContact, "Test", true); @@ -315,7 +310,7 @@ public class TestMsgHistoryService rs = msgHistoryService.findByPeriod( testMetaContact, controlDate1, controlDate2); - assertTrue("Nothing found findByPeriod", !rs.isEmpty()); + assertFalse("Nothing found findByPeriod", rs.isEmpty()); msgs = getMessages(rs); @@ -405,36 +400,25 @@ public class TestMsgHistoryService public void specialChars() { mockBImOpSet.deliverMessage(TEST_CONTACT_NAME_1, messagesToSend[5]); - waitWrite(500); mockBImOpSet.deliverMessage(TEST_CONTACT_NAME_1, messagesToSend[6]); - waitWrite(500); - historyService.purgeLocallyCachedHistories(); - - /** - * Must return exactly the last 4 messages - */ + // Must return exactly the last 2 messages Collection rs - = msgHistoryService.findLast(testMetaContact, 4); + = msgHistoryService.findLast(testMetaContact, 2); - assertTrue("Nothing found 8", !rs.isEmpty()); List msgs = getMessages(rs); - assertEquals("Messages must be 4", 4, msgs.size()); - assertTrue("Message not found", - msgs.contains(messagesToSend[3].getContent())); - assertTrue("Message not found", - msgs.contains(messagesToSend[4].getContent())); + assertEquals("Sent messages must be available", 2, msgs.size()); // For now we are stripping in history the special content chars // in order to avoid breaking the history records in the xml - assertTrue("Message not found", + assertTrue("Message " + messagesToSend[5].getContent() + " not found", msgs.contains(XmlEscapers.xmlContentEscaper().escape( messagesToSend[5].getContent()))); - assertTrue("Message not found", + assertTrue("Message " + messagesToSend[6].getContent() + " not found", msgs.contains(messagesToSend[6].getContent())); } @@ -455,7 +439,7 @@ public class TestMsgHistoryService = msgHistoryService.findByPeriod( testMetaContact, controlDate1, controlDate2); List msgs = getMessages(rs); - assertEquals("Messages must be 3", 3, msgs.size()); + assertEquals("Messages must be found", 3, msgs.size()); } private static void waitWrite(long timeout) @@ -473,233 +457,6 @@ public class TestMsgHistoryService } } - public void writeRecordsToMultiChat() - { - try - { - ChatRoom room = mockMultiChat.createChatRoom("test_room", null); - room.join(); - -// ChatRoom room = mockMultiChat.findRoom(TEST_ROOM_NAME); -// room.joinAs(TEST_CONTACT_NAME); - - // First deliver message, so they are stored by the message history service - room.sendMessage(messagesToSend[0]); - - waitWrite(100); - - TestMsgHistoryService.controlDate1 = new Date(); - logger.info("controlDate1:" + controlDate1.getTime()); - - waitWrite(100); - - room.sendMessage(messagesToSend[1]); - - waitWrite(100); - - room.sendMessage(messagesToSend[2]); - - waitWrite(100); - - TestMsgHistoryService.controlDate2 = new Date(); - logger.info("controlDate2:" + controlDate2.getTime()); - - waitWrite(100); - - room.sendMessage(messagesToSend[3]); - - waitWrite(100); - - room.sendMessage(messagesToSend[4]); - - waitWrite(100); - } - catch(OperationFailedException ex) - { - fail("Failed to create room : " + ex.getMessage()); - logger.error("Failed to create room", ex); - } - catch(OperationNotSupportedException ex) - { - fail("Failed to create room : " + ex.getMessage()); - logger.error("Failed to create room", ex); - } - } - - /** - * tests all read methods (finders) - */ - public void readRecordsFromMultiChat() - { - ChatRoom room = null; - - try - { - room = mockMultiChat.findRoom(TEST_ROOM_NAME); - - }catch(Exception ex) - { - fail("Cannot find room!" + ex.getMessage()); - } - - /** - * This matches all written messages, they are minimum 5 - */ - Collection rs - = msgHistoryService.findByKeyword(room, "test"); - - assertTrue("Nothing found findByKeyword ", !rs.isEmpty()); - - List msgs = getChatMessages(rs); - - assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); - - /** - * Will test case sensitive and insensitive search - */ - rs = msgHistoryService.findByKeyword(room, "Test", false); - - assertTrue("Nothing found findByKeyword caseINsensitive search", !rs.isEmpty()); - - msgs = getChatMessages(rs); - - assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); - - rs = msgHistoryService.findByKeyword(room, "Test", true); - - assertFalse("Something found by findByKeyword casesensitive search", !rs.isEmpty()); - - /** - * This must match also many messages, as tests are run many times - * but the minimum is 3 - */ - rs = msgHistoryService.findByEndDate(room, controlDate2); - - assertTrue("Nothing found findByEndDate", !rs.isEmpty()); - - msgs = getChatMessages(rs); - - assertTrue("Messages too few - findByEndDate", msgs.size() >= 3); - - /** - * This must find also many messages but atleast one - */ - rs = msgHistoryService.findByKeywords( - room, - new String[]{"test", "word2"}); - - assertTrue("Nothing found findByKeywords", !rs.isEmpty()); - msgs = getChatMessages(rs); - assertTrue("Messages too few - findByKeywords", msgs.size() >= 1); - - /** - * Nothing to be found - */ - rs = msgHistoryService.findByKeywords( - room, - new String[]{"test1", "word2"}); - - assertFalse("Something found findByKeywords", !rs.isEmpty()); - - /** - * must find 2 messages - */ - rs = msgHistoryService.findByPeriod( - room, controlDate1, controlDate2); - - assertTrue("Nothing found findByPeriod", !rs.isEmpty()); - - msgs = getChatMessages(rs); - - assertEquals("Messages must be 2", 2, msgs.size()); - - assertTrue("Message no found", - msgs.contains(messagesToSend[1].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[2].getContent())); - - /** - * must find 1 record - */ - rs = msgHistoryService.findByPeriod( - room, controlDate1, controlDate2, new String[]{"word2"}); - - assertTrue("Nothing found findByPeriod", !rs.isEmpty()); - - msgs = getChatMessages(rs); - - assertEquals("Messages must be 1", 1, msgs.size()); - assertTrue("Message no found", - msgs.contains(messagesToSend[1].getContent())); - - /** - * must find 2 records - */ - rs = msgHistoryService.findByStartDate(room, controlDate2); - - assertTrue("Nothing found findByStartDate", !rs.isEmpty()); - msgs = getChatMessages(rs); - assertEquals("Messages must be 2", 2, msgs.size()); - assertTrue("Message no found", - msgs.contains(messagesToSend[3].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[4].getContent())); - - /** - * Must return exactly the last 3 messages - */ - rs = msgHistoryService.findLast(room, 3); - - assertTrue("Nothing found 8", !rs.isEmpty()); - msgs = getChatMessages(rs); - assertEquals("Messages must be 3", 3, msgs.size()); - assertTrue("Message no found", - msgs.contains(messagesToSend[2].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[3].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[4].getContent())); - - /** - * Must return exactly the 3 messages after controlDate1 - */ - rs = msgHistoryService.findFirstMessagesAfter(room, controlDate1, 3); - - assertTrue("Nothing found 9", !rs.isEmpty()); - msgs = getChatMessages(rs); - assertEquals("Messages must be 3", 3, msgs.size()); - assertTrue("Message no found", - msgs.contains(messagesToSend[1].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[2].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[3].getContent())); - - /** - * Must return exactly the 3 messages before controlDate2 - */ - rs = msgHistoryService.findLastMessagesBefore(room, controlDate2, 3); - - assertTrue("Nothing found 10", !rs.isEmpty()); - msgs = getChatMessages(rs); - assertEquals("Messages must be 3", 3, msgs.size()); - assertTrue("Message no found", - msgs.contains(messagesToSend[0].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[1].getContent())); - assertTrue("Message no found", - msgs.contains(messagesToSend[2].getContent())); - } - - /** - * Removes the locally stored contact list copy. The purpose of this is to - * leave the local list empty for a next round of testing. - */ - public void testPurgeLocalContactListCopy() - { - metaClService.purgeLocallyStoredContactListCopy(); - } - private List getMessages(Collection rs) { List result = new Vector(); diff --git a/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryServiceMultiChat.java b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryServiceMultiChat.java new file mode 100644 index 0000000..0bddc8f --- /dev/null +++ b/test/net/java/sip/communicator/slick/msghistory/TestMsgHistoryServiceMultiChat.java @@ -0,0 +1,464 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package net.java.sip.communicator.slick.msghistory; + +import java.util.*; + +import junit.framework.*; +import net.java.sip.communicator.impl.protocol.mock.*; +import net.java.sip.communicator.service.contactlist.*; +import net.java.sip.communicator.service.history.*; +import net.java.sip.communicator.service.msghistory.*; +import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.service.protocol.event.*; +import net.java.sip.communicator.util.*; + +import org.osgi.framework.*; + +/** + * Tests message history. + * First installs the MoxkProtocolProvider to be able to send some messages + * The message history service stores them + * and then tests the verious find methods - does they find the messsages we have + * already sent + * + * @author Damian Minkov + */ +public class TestMsgHistoryServiceMultiChat + extends TestCase +{ + private static final Logger logger + = Logger.getLogger(TestMsgHistoryServiceMultiChat.class); + + static final String TEST_CONTACT_NAME_1 = "Mincho_Penchev_the_fisrt"; + static final String TEST_CONTACT_NAME_2 = "Mincho_Penchev_the_second"; + + static final String TEST_ROOM_NAME = "test_room"; + + /** + * The provider that we use to make a dummy server-stored contactlist + * used for testing. The mockProvider is instantiated and registered + * by the metacontactlist slick activator. + */ + public static MockProvider mockProvider = null; + /** + * The persistent presence operation set of the default mock provider. + */ + public static MockPersistentPresenceOperationSet mockPresOpSet = null; + public static MockBasicInstantMessaging mockBImOpSet = null; + public static MockMultiUserChat mockMultiChat = null; + + private static ServiceReference msgHistoryServiceRef = null; + public static MessageHistoryService msgHistoryService = null; + + public static HistoryService historyService = null; + + private static MockContact testContact = null; + + private static ServiceReference metaCLref = null; + private static MetaContactListService metaClService = null; + + private static MetaContact testMetaContact = null; + + /** + * A reference to the registration of the first mock provider. + */ + public static ServiceRegistration mockPrServiceRegistration = null; + + private static Message[] messagesToSend = null; + + private static Date controlDate1 = null; + private static Date controlDate2 = null; + + private static Object lock = new Object(); + + public TestMsgHistoryServiceMultiChat(String name) + { + super(name); + } + + public static Test suite() + { + TestSuite suite = new TestSuite(); + suite.addTest( + new TestMsgHistoryServiceMultiChat("readRecordsFromMultiChat")); + + return suite; + } + + @Override + protected void setUp() throws Exception + { + setupContact(); + msgHistoryService.eraseLocallyStoredHistory(); + writeRecordsToMultiChat(); + } + + @Override + protected void tearDown() throws Exception + { + metaClService.purgeLocallyStoredContactListCopy(); + } + + public void setupContact() + { + // changes the history service target derictory + System.setProperty("HistoryServiceDirectory", "test-msghistory"); + + mockProvider = new MockProvider("MessageHistoryMockUser"); + + //store thre presence op set of the new provider into the fixture + Map supportedOperationSets = + mockProvider.getSupportedOperationSets(); + + //get the operation set presence here. + mockPresOpSet = + (MockPersistentPresenceOperationSet) supportedOperationSets.get( + OperationSetPersistentPresence.class.getName()); + + mockBImOpSet = + (MockBasicInstantMessaging) supportedOperationSets.get( + OperationSetBasicInstantMessaging.class.getName()); + + mockMultiChat = + (MockMultiUserChat) supportedOperationSets.get( + OperationSetMultiUserChat.class.getName()); + + msgHistoryServiceRef = + MsgHistoryServiceLick.bc. + getServiceReference(MessageHistoryService.class.getName()); + + msgHistoryService = + (MessageHistoryService)MsgHistoryServiceLick.bc. + getService(msgHistoryServiceRef); + + ServiceReference historyServiceRef = + MsgHistoryServiceLick.bc. + getServiceReference(HistoryService.class.getName()); + + historyService = + (HistoryService)MsgHistoryServiceLick.bc. + getService(historyServiceRef); + + // fill in a contact to comunicate with + MockContactGroup root = + (MockContactGroup)mockPresOpSet.getServerStoredContactListRoot(); + + testContact = new MockContact(TEST_CONTACT_NAME_1, mockProvider); + root.addContact(testContact); + + metaCLref = MsgHistoryServiceLick.bc.getServiceReference( + MetaContactListService.class.getName()); + + metaClService = + (MetaContactListService)MsgHistoryServiceLick.bc.getService(metaCLref); + + System.setProperty(MetaContactListService.PROVIDER_MASK_PROPERTY, "1"); + + Hashtable mockProvProperties = new Hashtable(); + mockProvProperties.put(ProtocolProviderFactory.PROTOCOL + , mockProvider.getProtocolName()); + mockProvProperties.put(MetaContactListService.PROVIDER_MASK_PROPERTY, + "1"); + + mockPrServiceRegistration = + MsgHistoryServiceLick.bc.registerService( + ProtocolProviderService.class.getName(), + mockProvider, + mockProvProperties); + logger.debug("Registered a mock protocol provider! "); + + testMetaContact = metaClService.getRoot(). + getMetaContact(mockProvider, TEST_CONTACT_NAME_1); + + // add one more contact as specific problems may happen only when + // more than one contact is in the metacontact + metaClService.addNewContactToMetaContact( + mockProvider, testMetaContact, TEST_CONTACT_NAME_2); + + messagesToSend = new Message[] + { + mockBImOpSet.createMessage("test message word1-" + Math.random()), + mockBImOpSet.createMessage("test message word2" + Math.random()), + mockBImOpSet.createMessage("test message word3" + Math.random()), + mockBImOpSet.createMessage("test message word4" + Math.random()), + mockBImOpSet.createMessage("test message word5" + Math.random()), + mockBImOpSet.createMessage("Hello \u0002World\u0002!"), + mockBImOpSet.createMessage("less than < this, greater than > and an ampersand &") + }; + } + + private static void waitWrite(long timeout) + { + synchronized (lock) + { + // wait a moment + try + { + lock.wait(timeout); + } + catch (InterruptedException ex) + { + } + } + } + + public void writeRecordsToMultiChat() + { + try + { + ChatRoom room = mockMultiChat.createChatRoom("test_room", null); + room.join(); + +// ChatRoom room = mockMultiChat.findRoom(TEST_ROOM_NAME); +// room.joinAs(TEST_CONTACT_NAME); + + // First deliver message, so they are stored by the message history service + room.sendMessage(messagesToSend[0]); + + waitWrite(1000); + + TestMsgHistoryServiceMultiChat.controlDate1 = new Date(); + logger.info("controlDate1:" + controlDate1.getTime()); + + waitWrite(1000); + + room.sendMessage(messagesToSend[1]); + + waitWrite(100); + + room.sendMessage(messagesToSend[2]); + + waitWrite(1000); + + TestMsgHistoryServiceMultiChat.controlDate2 = new Date(); + logger.info("controlDate2:" + controlDate2.getTime()); + + waitWrite(1000); + + room.sendMessage(messagesToSend[3]); + + waitWrite(1000); + + room.sendMessage(messagesToSend[4]); + + waitWrite(1000); + } + catch(OperationFailedException ex) + { + fail("Failed to create room : " + ex.getMessage()); + logger.error("Failed to create room", ex); + } + catch(OperationNotSupportedException ex) + { + fail("Failed to create room : " + ex.getMessage()); + logger.error("Failed to create room", ex); + } + } + + /** + * tests all read methods (finders) + */ + public void readRecordsFromMultiChat() + { + ChatRoom room = null; + + try + { + room = mockMultiChat.findRoom(TEST_ROOM_NAME); + + }catch(Exception ex) + { + fail("Cannot find room!" + ex.getMessage()); + } + + /** + * This matches all written messages, they are minimum 5 + */ + Collection rs + = msgHistoryService.findByKeyword(room, "test"); + + assertTrue("Nothing found findByKeyword ", !rs.isEmpty()); + + List msgs = getChatMessages(rs); + + assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); + + /** + * Will test case sensitive and insensitive search + */ + rs = msgHistoryService.findByKeyword(room, "Test", false); + + assertTrue("Nothing found findByKeyword caseINsensitive search", !rs.isEmpty()); + + msgs = getChatMessages(rs); + + assertTrue("Messages too few - findByKeyword", msgs.size() >= 5); + + rs = msgHistoryService.findByKeyword(room, "Test", true); + + assertFalse("Something found by findByKeyword casesensitive search", !rs.isEmpty()); + + /** + * This must match also many messages, as tests are run many times + * but the minimum is 3 + */ + rs = msgHistoryService.findByEndDate(room, controlDate2); + + assertTrue("Nothing found findByEndDate", !rs.isEmpty()); + + msgs = getChatMessages(rs); + + assertTrue("Messages too few - findByEndDate", msgs.size() >= 3); + + /** + * This must find also many messages but atleast one + */ + rs = msgHistoryService.findByKeywords( + room, + new String[]{"test", "word2"}); + + assertTrue("Nothing found findByKeywords", !rs.isEmpty()); + msgs = getChatMessages(rs); + assertTrue("Messages too few - findByKeywords", msgs.size() >= 1); + + /** + * Nothing to be found + */ + rs = msgHistoryService.findByKeywords( + room, + new String[]{"test1", "word2"}); + + assertFalse("Something found findByKeywords", !rs.isEmpty()); + + /** + * must find 2 messages + */ + rs = msgHistoryService.findByPeriod( + room, controlDate1, controlDate2); + + assertTrue("Nothing found findByPeriod", !rs.isEmpty()); + + msgs = getChatMessages(rs); + + assertEquals("Messages must be 2", 2, msgs.size()); + + assertTrue("Message no found", + msgs.contains(messagesToSend[1].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[2].getContent())); + + /** + * must find 1 record + */ + rs = msgHistoryService.findByPeriod( + room, controlDate1, controlDate2, new String[]{"word2"}); + + assertTrue("Nothing found findByPeriod", !rs.isEmpty()); + + msgs = getChatMessages(rs); + + assertEquals("Messages must be 1", 1, msgs.size()); + assertTrue("Message no found", + msgs.contains(messagesToSend[1].getContent())); + + /** + * must find 2 records + */ + rs = msgHistoryService.findByStartDate(room, controlDate2); + + assertTrue("Nothing found findByStartDate", !rs.isEmpty()); + msgs = getChatMessages(rs); + assertEquals("Messages must be 2", 2, msgs.size()); + assertTrue("Message no found", + msgs.contains(messagesToSend[3].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[4].getContent())); + + /** + * Must return exactly the last 3 messages + */ + rs = msgHistoryService.findLast(room, 3); + + assertTrue("Nothing found 8", !rs.isEmpty()); + msgs = getChatMessages(rs); + assertEquals("Messages must be 3", 3, msgs.size()); + assertTrue("Message no found", + msgs.contains(messagesToSend[2].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[3].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[4].getContent())); + + /** + * Must return exactly the 3 messages after controlDate1 + */ + rs = msgHistoryService.findFirstMessagesAfter(room, controlDate1, 3); + + assertTrue("Nothing found 9", !rs.isEmpty()); + msgs = getChatMessages(rs); + assertEquals("Messages must be 3", 3, msgs.size()); + assertTrue("Message no found", + msgs.contains(messagesToSend[1].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[2].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[3].getContent())); + + /** + * Must return exactly the 3 messages before controlDate2 + */ + rs = msgHistoryService.findLastMessagesBefore(room, controlDate2, 3); + + assertTrue("Nothing found 10", !rs.isEmpty()); + msgs = getChatMessages(rs); + assertEquals("Messages must be 3", 3, msgs.size()); + assertTrue("Message no found", + msgs.contains(messagesToSend[0].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[1].getContent())); + assertTrue("Message no found", + msgs.contains(messagesToSend[2].getContent())); + } + + private List getMessages(Collection rs) + { + List result = new Vector(); + + for (EventObject item : rs) + { + if(item instanceof MessageDeliveredEvent) + result.add( + ((MessageDeliveredEvent)item) + .getSourceMessage().getContent()); + else if(item instanceof MessageReceivedEvent) + result.add( + ((MessageReceivedEvent)item) + .getSourceMessage().getContent()); + } + + return result; + } + + private List getChatMessages(Collection rs) + { + List result = new Vector(); + + for (EventObject item : rs) + { + if(item instanceof ChatRoomMessageDeliveredEvent) + result.add(((ChatRoomMessageDeliveredEvent)item). + getMessage().getContent()); + else + if(item instanceof ChatRoomMessageReceivedEvent) + result.add(((ChatRoomMessageReceivedEvent)item). + getMessage().getContent()); + } + + return result; + } +} -- cgit v1.1