MessageHistory Tests

cusax-fix
Damian Minkov 20 years ago
parent 2a32cfd5e0
commit da1d9c3c24

@ -0,0 +1,199 @@
package net.java.sip.communicator.impl.protocol.mock;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import java.util.Vector;
import java.util.Date;
import java.util.*;
public class MockBasicInstantMessaging
implements OperationSetBasicInstantMessaging
{
private Vector messageListeners = new Vector();
private MockPersistentPresenceOperationSet opSetPersPresence = null;
public MockBasicInstantMessaging(MockProvider provider,
MockPersistentPresenceOperationSet opSetPersPresence)
{
this.opSetPersPresence = opSetPersPresence;
}
/**
* Registeres a MessageListener with this operation set so that it gets
* notifications of successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to register.
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetBasicInstantMessaging
* method
*/
public void addMessageListener(MessageListener listener)
{
messageListeners.add(listener);
}
/**
* Create a Message instance for sending arbitrary MIME-encoding content.
*
* @param content content value
* @param contentType the MIME-type for <tt>content</tt>
* @param contentEncoding encoding used for <tt>content</tt>
* @param subject a <tt>String</tt> subject or <tt>null</tt> for now
* subject.
* @return the newly created message.
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetBasicInstantMessaging
* method
*/
public Message createMessage(byte[] content, String contentType,
String contentEncoding, String subject)
{
return new MessageImpl(new String(content), contentType
, contentEncoding, subject);
}
/**
* Create a Message instance for sending a simple text messages with
* default (text/plain) content type and encoding.
*
* @param messageText the string content of the message.
* @return Message the newly created message
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetBasicInstantMessaging
* method
*/
public Message createMessage(String messageText)
{
return new MessageImpl(messageText, DEFAULT_MIME_TYPE,
DEFAULT_MIME_ENCODING, null);
}
/**
* Unregisteres <tt>listener</tt> so that it won't receive any further
* notifications upon successful message delivery, failure or reception
* of incoming messages..
*
* @param listener the <tt>MessageListener</tt> to unregister.
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetBasicInstantMessaging
* method
*/
public void removeMessageListener(MessageListener listener)
{
messageListeners.remove(listener);
}
/**
* Sends the <tt>message</tt> to the destination indicated by the
* <tt>to</tt> contact.
*
* @param to the <tt>Contact</tt> to send <tt>message</tt> to
* @param message the <tt>Message</tt> to send.
* @throws IllegalStateException if the underlying ICQ stack is not
* registered and initialized.
* @throws IllegalArgumentException if <tt>to</tt> is not an instance
* belonging to the underlying implementation.
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetBasicInstantMessaging
* method
*/
public void sendInstantMessage(Contact to, Message message) throws
IllegalStateException, IllegalArgumentException
{
MessageDeliveredEvent msgDeliveredEvt
= new MessageDeliveredEvent(
message, to, new Date());
Iterator iter = messageListeners.iterator();
while (iter.hasNext())
{
MessageListener l = (MessageListener)iter.next();
l.messageDelivered(msgDeliveredEvt);
}
}
/**
* Methods for manipulating mock operation set as
* deliver(receive) messageop
*/
public void deliverMessage(String to, Message msg)
{
Contact sourceContact = opSetPersPresence.findContactByID(to);
MessageReceivedEvent msgReceivedEvt
= new MessageReceivedEvent(
msg, sourceContact , new Date());
Iterator iter = messageListeners.iterator();
while (iter.hasNext())
{
MessageListener l = (MessageListener)iter.next();
l.messageReceived(msgReceivedEvt);
}
}
public class MessageImpl
implements Message
{
private String textContent = null;
private String contentType = null;
private String contentEncoding = null;
private String messageUID = null;
private String subject = null;
public MessageImpl(String content,
String contentType,
String contentEncoding,
String subject)
{
this.textContent = content;
this.contentType = contentType;
this.contentEncoding = contentEncoding;
this.subject = subject;
//generate the uid
this.messageUID = String.valueOf( System.currentTimeMillis())
+ String.valueOf(hashCode());
}
public String getContent()
{
return textContent;
}
public String getContentType()
{
return contentType;
}
public String getEncoding()
{
return contentEncoding;
}
public String getMessageUID()
{
return messageUID;
}
public byte[] getRawData()
{
return getContent().getBytes();
}
public int getSize()
{
return getContent().length();
}
public String getSubject()
{
return subject;
}
}
}

@ -6,25 +6,49 @@
*/
package net.java.sip.communicator.slick.msghistory;
import java.util.Hashtable;
import java.util.*;
import junit.framework.TestSuite;
import net.java.sip.communicator.util.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.*;
import junit.framework.*;
import net.java.sip.communicator.impl.protocol.mock.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.service.msghistory.MessageHistoryService;
/**
* This class launches the bundle of which test the history bundle. this bundle
* is a set of (j)unit tests. It should be launched by the cruisecontrol module.
*
* @author Alexander Pelov
* @author Damian Minkov
*/
public class MsgHistoryServiceLick extends TestSuite implements BundleActivator {
private static Logger logger = Logger.getLogger(MsgHistoryServiceLick.class);
protected static BundleContext bc = null;
static final String TEST_CONTACT_NAME = "Mincho_Penchev";
/**
* 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;
/**
* A reference to the registration of the first mock provider.
*/
public static ServiceRegistration mockPrServiceRegistration = null;
private static ServiceReference msgHistoryServiceRef = null;
public static MessageHistoryService msgHistoryService = null;
/**
* Start the History Sevice Implementation Compatibility Kit.
*
@ -43,16 +67,86 @@ public void start(BundleContext bundleContext) throws Exception {
bundleContext.registerService(getClass().getName(), this, properties);
logger.debug("Successfully registered " + getClass().getName());
MockProvider provider = new MockProvider("SlickMockUser");
//store thre presence op set of the new provider into the fixture
Map supportedOperationSets =
provider.getSupportedOperationSets();
//get the operation set presence here.
MsgHistoryServiceLick.mockPresOpSet =
(MockPersistentPresenceOperationSet) supportedOperationSets.get(
OperationSetPersistentPresence.class.getName());
MsgHistoryServiceLick.mockBImOpSet =
(MockBasicInstantMessaging) supportedOperationSets.get(
OperationSetBasicInstantMessaging.class.getName());
// fill in a contact to comunicate with
MockContactGroup root =
(MockContactGroup) MsgHistoryServiceLick.mockPresOpSet
.getServerStoredContactListRoot();
root.addContact(new MockContact(TEST_CONTACT_NAME, provider));
MsgHistoryServiceLick.mockPrServiceRegistration
= registerMockProviderService(provider);
//store the created mock provider for later reference
MsgHistoryServiceLick.mockProvider = provider;
msgHistoryServiceRef =
bundleContext.getServiceReference(MessageHistoryService.class.getName());
msgHistoryService = (MessageHistoryService) bundleContext.getService(
msgHistoryServiceRef);
}
/**
* stop
*
* @param bundlecontext
* BundleContext
* @param bundlecontext BundleContext
* @throws Exception
*/
public void stop(BundleContext bundlecontext) throws Exception {
BundleContext context = MsgHistoryServiceLick.bc;
context.ungetService(this.msgHistoryServiceRef);
if (MsgHistoryServiceLick.mockPrServiceRegistration != null)
MsgHistoryServiceLick.mockPrServiceRegistration.unregister();
this.msgHistoryService = null;
this.msgHistoryServiceRef = null;
}
/**
* Registers the specified mock provider as an implementation of the
* ProtocolProviderService in the currently valid bundle context.
*
* @param provider the protocol provider we'd like to export as an OSGI
* service.
* @return the ServiceRegistration reference returned when registering
* the specified provider.
*/
public static ServiceRegistration registerMockProviderService(
MockProvider provider)
{
ServiceRegistration osgiRegistration = null;
Hashtable mockProvProperties = new Hashtable();
mockProvProperties.put(ProtocolProviderFactory.
PROTOCOL_PROPERTY_NAME,
provider.getProtocolName());
osgiRegistration
= MsgHistoryServiceLick.bc.registerService(
ProtocolProviderService.class.getName(),
provider,
mockProvProperties);
logger.debug("Registered a mock protocol provider!");
return osgiRegistration;
}
}

@ -7,95 +7,244 @@
*/
package net.java.sip.communicator.slick.msghistory;
import org.osgi.framework.*;
import junit.framework.*;
import net.java.sip.communicator.service.msghistory.MessageHistoryService;
import net.java.sip.communicator.service.contactlist.MetaContactListService;
import java.util.*;
import net.java.sip.communicator.util.Logger;
import net.java.sip.communicator.service.contactlist.MetaContact;
import net.java.sip.communicator.service.history.QueryResultSet;
import org.osgi.framework.*;
import junit.framework.*;
import net.java.sip.communicator.service.contactlist.*;
import net.java.sip.communicator.service.history.*;
import net.java.sip.communicator.service.history.records.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
/**
* 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 TestMsgHistoryService
extends TestCase
{
private static final Logger logger = Logger.getLogger(TestMsgHistoryService.class);
private ServiceReference msgHistoryServiceRef = null;
private MessageHistoryService msgHistoryService = null;
public TestMsgHistoryService(String name) throws Exception
{
super(name);
logger.info("111111111111111111111111111111111111111111111111111");
private ServiceReference metaCLref = null;
private MetaContactListService metaClService = null;
private MetaContact testMetaContact = null;
ServiceReference ref = MsgHistoryServiceLick.bc.getServiceReference(
MetaContactListService.class.getName());
MetaContactListService metaClService
= (MetaContactListService)MsgHistoryServiceLick.bc.getService(ref);
Message[] messagesToSend = null;
Iterator iter = metaClService.getRoot().getChildContacts();
while (iter.hasNext())
{
Object item = (Object) iter.next();
logger.debug("item --------- " + item);
}
public TestMsgHistoryService(String name) throws Exception
{
super(name);
}
protected void setUp() throws Exception
{
BundleContext context = MsgHistoryServiceLick.bc;
metaCLref = MsgHistoryServiceLick.bc.getServiceReference(
MetaContactListService.class.getName());
metaClService = (MetaContactListService)MsgHistoryServiceLick.bc.getService(metaCLref);
msgHistoryServiceRef =
context.getServiceReference(MessageHistoryService.class.getName());
testMetaContact =
metaClService.getRoot().
getMetaContact(
MsgHistoryServiceLick.mockProvider,
MsgHistoryServiceLick.TEST_CONTACT_NAME);
msgHistoryService = (MessageHistoryService)context.getService(msgHistoryServiceRef);
messagesToSend = new Message[]
{
MsgHistoryServiceLick.mockBImOpSet.createMessage("test message word1"),
MsgHistoryServiceLick.mockBImOpSet.createMessage("test message word2"),
MsgHistoryServiceLick.mockBImOpSet.createMessage("test message word3"),
MsgHistoryServiceLick.mockBImOpSet.createMessage("test message word4"),
MsgHistoryServiceLick.mockBImOpSet.createMessage("test message word5")
};
}
protected void tearDown() throws Exception
{
BundleContext context = MsgHistoryServiceLick.bc;
context.ungetService(this.msgHistoryServiceRef);
context.ungetService(this.metaCLref);
this.msgHistoryService = null;
this.msgHistoryServiceRef = null;
this.metaClService = null;
this.metaCLref = null;
}
public void testWriteRecords()
/**
* First send the messages then tests all read methods (finders)
*/
public void testReadRecords()
{
ServiceReference ref = MsgHistoryServiceLick.bc.getServiceReference(
MetaContactListService.class.getName());
MetaContactListService metaClService
= (MetaContactListService)MsgHistoryServiceLick.bc.getService(ref);
// First deliver message, so they are stored by the message history service
MsgHistoryServiceLick.mockBImOpSet.deliverMessage(
MsgHistoryServiceLick.TEST_CONTACT_NAME, messagesToSend[0]);
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, c.get(Calendar.YEAR) - 10);
Date date = c.getTime();
Date controlDate1 = new Date();
Iterator iter = metaClService.getRoot().getChildContacts();
while (iter.hasNext())
{
MetaContact item = (MetaContact) iter.next();
Object lock = new Object();
synchronized(lock){
// wait a moment
try{lock.wait(200);}
catch (InterruptedException ex){}
}
MsgHistoryServiceLick.mockBImOpSet.deliverMessage(
MsgHistoryServiceLick.TEST_CONTACT_NAME, messagesToSend[1]);
MsgHistoryServiceLick.mockBImOpSet.deliverMessage(
MsgHistoryServiceLick.TEST_CONTACT_NAME, messagesToSend[2]);
Date controlDate2 = new Date();
synchronized(lock){
// wait a moment
try{lock.wait(200);}
catch (InterruptedException ex){}
}
MsgHistoryServiceLick.mockBImOpSet.deliverMessage(
MsgHistoryServiceLick.TEST_CONTACT_NAME, messagesToSend[3]);
MsgHistoryServiceLick.mockBImOpSet.deliverMessage(
MsgHistoryServiceLick.TEST_CONTACT_NAME, messagesToSend[4]);
/**
* This matches all written messages, they are minimum 5
*/
QueryResultSet rs = MsgHistoryServiceLick.msgHistoryService.findByKeyword(
testMetaContact, "test");
assertTrue("Nothing found findByKeyword ", rs.hasNext());
logger.debug("item --------- " + item);
QueryResultSet rs =
msgHistoryService.findByStartDate(item, date);
Iterator iter1 = rs;
while (iter1.hasNext())
Vector msgs = getMessages(rs);
assertTrue("Messages too few - findByKeyword", msgs.size() >= 5);
/**
* This must match also many messages, as tests are run many times
* but the minimum is 3
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByEndDate(
testMetaContact, controlDate2);
assertTrue("Nothing found findByEndDate", rs.hasNext());
msgs = getMessages(rs);
assertTrue("Messages too few - findByEndDate", msgs.size() >= 3);
/**
* This must find also many messages but atleast one
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByKeywords(
testMetaContact, new String[]{"test", "word2"});
assertTrue("Nothing found findByKeywords", rs.hasNext());
msgs = getMessages(rs);
assertTrue("Messages too few - findByKeywords", msgs.size() >= 1);
/**
* Nothing to be found
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByKeywords(
testMetaContact, new String[]{"test1", "word2"});
assertFalse("Something found findByKeywords", rs.hasNext());
/**
* must find 2 messages
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByPeriod(
testMetaContact, controlDate1, controlDate2);
assertTrue("Nothing found findByPeriod", rs.hasNext());
msgs = getMessages(rs);
assertEquals("Messages must be 2", msgs.size(), 2);
assertTrue("Message no found", msgs.contains(messagesToSend[1].getContent()));
assertTrue("Message no found", msgs.contains(messagesToSend[2].getContent()));
/**
* must find 1 record
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByPeriod(
testMetaContact, controlDate1, controlDate2, new String[]{"word2"});
assertTrue("Nothing found findByPeriod", rs.hasNext());
msgs = getMessages(rs);
assertEquals("Messages must be 1", msgs.size(), 1);
assertTrue("Message no found", msgs.contains(messagesToSend[1].getContent()));
/**
* must find 2 records
*/
rs = MsgHistoryServiceLick.msgHistoryService.findByStartDate(
testMetaContact, controlDate2);
assertTrue("Nothing found findByStartDate", rs.hasNext());
msgs = getMessages(rs);
assertEquals("Messages must be 2", msgs.size(), 2);
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 = MsgHistoryServiceLick.msgHistoryService.findLast(
testMetaContact, 3);
assertTrue("Nothing found 8", rs.hasNext());
msgs = getMessages(rs);
assertEquals("Messages must be 3", msgs.size(), 3);
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()));
}
private Vector getMessages(QueryResultSet rs)
{
Vector result = new Vector();
while (rs.hasNext())
{
HistoryRecord hr = (HistoryRecord)rs.next();
for (int i = 0; i < hr.getPropertyNames().length; i++)
{
logger.debug("222222222222222222222 " + iter1.next());
if(hr.getPropertyNames()[i].equals("msg"))
{
result.add(hr.getPropertyValues()[i]);
break;
}
}
}
return result;
}
public void testReadRecords()
private void dumpResult(QueryResultSet rs)
{
while (rs.hasNext())
{
HistoryRecord hr = (HistoryRecord)rs.next();
logger.info("----------------------");
}
for (int i = 0; i < hr.getPropertyNames().length; i++)
{
logger.info(hr.getPropertyNames()[i] + " => " + hr.getPropertyValues()[i]);
}
logger.info("----------------------");
}
}
}

@ -10,6 +10,8 @@ Import-Package: junit.framework,
net.java.sip.communicator.service.contactlist,
net.java.sip.communicator.service.msghistory,
net.java.sip.communicator.service.configuration,
net.java.sip.communicator.impl.protocol.mock,
net.java.sip.communicator.service.protocol,
org.osgi.framework,
org.w3c.dom,
javax.xml.parsers,

Loading…
Cancel
Save