Implementing a Mock Provider to use when testing the MetaContactListService.

cusax-fix
Emil Ivov 20 years ago
parent ca3a294d47
commit e92467d85c

@ -1,3 +1,9 @@
/*
* SIP Communicator, 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.contactlist.mockprovider;
import net.java.sip.communicator.service.protocol.*;
@ -15,16 +21,21 @@ public class MockContact
{
private String contactID = null;
private MockProvider parentProvider = null;
private MockContactGroup parentGroup = null;
/**
* Creates an instance of a meta contact with the specified string used
* as a name and identifier.
*
* @param id the identifier of this contact (also used as a name).
* @param parentProvider
* @param parentGroup the group that contains the contact
* @param parentProvider the provider that created us.
*/
public MockContact(String id, MockProvider parentProvider)
public MockContact(String id,
MockProvider parentProvider,
MockContactGroup parentGroup)
{
this.parentGroup = parentGroup;
this.contactID = id;
this.parentProvider = parentProvider;
}
@ -93,6 +104,15 @@ public boolean isLocal()
return false;
}
/**
* Returns the group that contains this contact.
* @return a reference to the MockContactGroup that contains this contact.
*/
public MockContactGroup getParentGroup()
{
return this.parentGroup;
}
/**
* Returns a string representation of this contact, containing most of its
* representative details.

@ -1,4 +1,9 @@
/*
* SIP Communicator, 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.contactlist.mockprovider;
import java.util.*;
@ -42,6 +47,16 @@ public boolean canContainSubgroups()
return true;
}
/**
* Returns the protocol provider that this group belongs to.
* @return a regerence to the ProtocolProviderService instance that this
* ContactGroup belongs to.
*/
public ProtocolProviderService getProtocolProvider()
{
return parentProvider;
}
/**
* Returns an Iterator over all contacts, member of this
* <tt>ContactGroup</tt>.
@ -81,7 +96,7 @@ public int countContacts()
*
* @return the number of subGroups currently added to this group.
*/
public int countSubGroups()
public int countSubgroups()
{
return subGroups.size();
}
@ -95,6 +110,16 @@ public void addSubGroup(MockContactGroup subGroup)
this.subGroups.add(subGroup);
}
/**
* Removes the specified contact group from the this group's subgroups.
* @param subGroup the MockContactGroup subgroup to remove.
*/
public void removeSubGroup(MockContactGroup subGroup)
{
this.subGroups.remove(subGroup);
}
/**
* Returns the <tt>Contact</tt> with the specified index.
*
@ -106,6 +131,31 @@ public Contact getContact(int index)
return (MockContact)contacts.get(index);
}
/**
* Returns the group that is parent of the specified mockGroup or null
* if no parent was found.
* @param mockGroup the group whose parent we're looking for.
* @return the MockContactGroup instance that mockGroup belongs to or null
* if no parent was found.
*/
public MockContactGroup findGroupParent(MockContactGroup mockGroup)
{
if ( subGroups.contains(mockGroup) )
return this;
Iterator subGroupsIter = subGroups();
while (subGroupsIter.hasNext())
{
MockContactGroup subgroup = (MockContactGroup) subGroupsIter.next();
MockContactGroup parent = subgroup.findGroupParent(mockGroup);
if(parent != null)
return parent;
}
return null;
}
/**
* Returns the <tt>Contact</tt> with the specified address or identifier.
*
@ -145,7 +195,7 @@ public ContactGroup getGroup(int index)
*/
public ContactGroup getGroup(String groupName)
{
Iterator groupsIter = contacts();
Iterator groupsIter = subGroups();
while (groupsIter.hasNext())
{
MockContactGroup contactGroup
@ -168,6 +218,15 @@ public String getGroupName()
return this.groupName;
}
/**
* Sets this group a new name.
* @param newGrpName a String containing the new name of this group.
*/
public void setGroupName(String newGrpName)
{
this.groupName = newGrpName;
}
/**
* Returns an iterator over the sub groups that this
* <tt>ContactGroup</tt> contains.
@ -180,6 +239,51 @@ public Iterator subGroups()
return subGroups.iterator();
}
/**
* Removes the specified contact from this group.
* @param contact the MockContact to remove from this group
*/
public void removeContact(MockContact contact)
{
this.contacts.remove(contact);
}
/**
* Returns the contact with the specified id or null if no such contact
* exists.
* @param id the id of the contact we're looking for.
* @return MockContact
*/
public MockContact findContactByID(String id)
{
//first go through the contacts that are direct children.
Iterator contactsIter = contacts();
while(contactsIter.hasNext())
{
MockContact mContact = (MockContact)contactsIter.next();
if( mContact.getAddress().equals(id) )
return mContact;
}
//if we didn't find it here, let's try in the subougroups
Iterator groupsIter = subGroups();
while( groupsIter.hasNext() )
{
MockContactGroup mGroup = (MockContactGroup)groupsIter.next();
MockContact mContact = mGroup.findContactByID(id);
if (mContact != null)
return mContact;
}
return null;
}
/**
* Returns a String representation of this group and the contacts it
* contains (may turn out to be a relatively long string).
@ -189,7 +293,7 @@ public String toString()
{
StringBuffer buff = new StringBuffer(getGroupName());
buff.append(".subGroups=" + countSubGroups() + ":\n");
buff.append(".subGroups=" + countSubgroups() + ":\n");
Iterator subGroups = subGroups();
while (subGroups.hasNext())

@ -1,9 +1,16 @@
/*
* SIP Communicator, 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.contactlist.mockprovider;
import java.util.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.service.protocol.icqconstants.*;
/**
@ -14,15 +21,36 @@
public class MockPersistentPresenceOperationSet
implements OperationSetPersistentPresence
{
private static final Logger logger =
Logger.getLogger(MockPersistentPresenceOperationSet.class);
/**
* A list of listeners registered for <tt>SubscriptionEvent</tt>s.
*/
private Vector subscriptionListeners = new Vector();
/**
* A list of listeners registered for <tt>ServerStoredGroupChangeEvent</tt>s.
*/
private Vector serverStoredGroupListeners = new Vector();
/**
* The root of the mock contact list.
*/
private MockContactGroup contactListRoot = null;
/**
* The provider that created us.
*/
private MockProvider parentProvider = null;
/**
* The currently active status message.
*/
private String statusMessage = null;
public MockPersistentPresenceOperationSet(MockProvider provider)
{
this.parentProvider = provider;
contactListRoot = new MockContactGroup("RootMockGroup", provider);
}
@ -34,8 +62,73 @@ public MockPersistentPresenceOperationSet(MockProvider provider)
public void addContactPresenceStatusListener(ContactPresenceStatusListener
listener)
{
}
/**
* Notifies all registered listeners of the new event.
*
* @param source the contact that has caused the event.
* @param parentGroup the group that contains the source contact.
* @param eventID an identifier of the event to dispatch.
*/
public void fireSubscriptionEvent(MockContact source,
ContactGroup parentGroup,
int eventID)
{
SubscriptionEvent evt = new SubscriptionEvent(source, this.parentProvider,
parentGroup, eventID);
for ( int i = 0; i < subscriptionListeners.size(); i++ )
{
SubscriptionListener listener = (SubscriptionListener)
subscriptionListeners.get(i);
if(eventID == SubscriptionEvent.SUBSCRIPTION_CREATED)
{
listener.subscriptionCreated(evt);
}
else if (eventID == SubscriptionEvent.SUBSCRIPTION_FAILED)
{
listener.subscriptionFailed(evt);
}
else if (eventID == SubscriptionEvent.SUBSCRIPTION_REMOVED)
{
listener.subscriptionRemoved(evt);
}
}
}
/**
* Notifies all registered listeners of the new event.
*
* @param source the contact that has caused the event.
* @param eventID an identifier of the event to dispatch.
*/
public void fireServerStoredGroupEvent(MockContactGroup source,
int eventID)
{
ServerStoredGroupEvent evt = new ServerStoredGroupEvent(
source, eventID, this.parentProvider, this);
for ( int i = 0; i < serverStoredGroupListeners.size(); i++ )
{
ServerStoredGroupListener listener = (ServerStoredGroupListener)
serverStoredGroupListeners.get(i);
if(eventID == ServerStoredGroupEvent.GROUP_CREATED_EVENT)
{
listener.groupCreated(evt);
}
else if(eventID == ServerStoredGroupEvent.GROUP_RENAMED_EVENT)
{
listener.groupNameChanged(evt);
}
else if(eventID == ServerStoredGroupEvent.GROUP_REMOVED_EVENT)
{
listener.groupRemoved(evt);
}
}
}
/**
* Mock implementation of the corresponding ProtocolProviderService method.
*
@ -56,6 +149,7 @@ public void addProviderPresenceStatusListener(
public void addServerStoredGroupChangeListener(ServerStoredGroupListener
listener)
{
serverStoredGroupListeners.add(listener);
}
/**
@ -65,6 +159,7 @@ public void addServerStoredGroupChangeListener(ServerStoredGroupListener
*/
public void addSubsciptionListener(SubscriptionListener listener)
{
this.subscriptionListeners.add( listener );
}
/**
@ -77,18 +172,25 @@ public void addSubsciptionListener(SubscriptionListener listener)
public void createServerStoredContactGroup(ContactGroup parent,
String groupName)
{
/** @todo implement createServerStoredContactGroup() */
MockContactGroup newGroup
= new MockContactGroup(groupName, parentProvider);
((MockContactGroup)parent).addSubGroup(newGroup);
this.fireServerStoredGroupEvent(
newGroup, ServerStoredGroupEvent.GROUP_CREATED_EVENT);
}
/**
* A Mock Provider method to use for fast filling of a contact list.
*
* @param contactGroup the group to add
*/
public void addMockGroup(MockContactGroup contactGroup)
{
contactListRoot.addSubGroup(contactGroup);
}
/**
* Returns a reference to the contact with the specified ID in case we
* have a subscription for it and null otherwise/
@ -101,8 +203,16 @@ public void addMockGroup(MockContactGroup contactGroup)
*/
public Contact findContactByID(String contactID)
{
/** @todo implement findContactByID() */
return null;
return contactListRoot.findContactByID(contactID);
}
/**
* Sets the specified status message.
* @param statusMessage a String containing the new status message.
*/
public void setStatusMessage(String statusMessage)
{
this.statusMessage = statusMessage;
}
/**
@ -114,8 +224,7 @@ public Contact findContactByID(String contactID)
*/
public String getCurrentStatusMessage()
{
/** @todo implement getCurrentStatusMessage() */
return "";
return statusMessage;
}
/**
@ -232,7 +341,6 @@ public PresenceStatus queryContactStatus(String contactIdentifier) throws
public void removeContactPresenceStatusListener(
ContactPresenceStatusListener listener)
{
/** @todo implement removeContactPresenceStatusListener() */
}
/**
@ -247,16 +355,46 @@ public void removeProviderPresenceStatusListener(
/** @todo implement removeProviderPresenceStatusListener() */
}
/**
* Returns the group that is parent of the specified mockGroup or null
* if no parent was found.
* @param mockGroup the group whose parent we're looking for.
* @return the MockContactGroup instance that mockGroup belongs to or null
* if no parent was found.
*/
public MockContactGroup findGroupParent(MockContactGroup mockGroup)
{
return contactListRoot.findGroupParent(mockGroup);
}
/**
* Removes the specified group from the server stored contact list.
*
* @param group the group to remove.
*
* @throws IllegalArgumentException if <tt>group</tt> was not found in this
* protocol's contact list.
*/
public void removeServerStoredContactGroup(ContactGroup group)
throws IllegalArgumentException
{
/** @todo implement removeServerStoredContactGroup() */
MockContactGroup mockGroup = (MockContactGroup)group;
MockContactGroup parent = findGroupParent(mockGroup);
if(parent == null){
throw new IllegalArgumentException(
"group " + group
+ " does not seem to belong to this protocol's contact list.");
}
parent.removeSubGroup(mockGroup);
this.fireServerStoredGroupEvent(
mockGroup, ServerStoredGroupEvent.GROUP_REMOVED_EVENT);
}
/**
* Removes the specified group change listener so that it won't receive
* any further events.
@ -266,7 +404,7 @@ public void removeServerStoredContactGroup(ContactGroup group)
public void removeServerStoredGroupChangeListener(ServerStoredGroupListener
listener)
{
/** @todo implement removeServerStoredGroupChangeListener() */
serverStoredGroupListeners.remove(listener);
}
/**
@ -276,7 +414,7 @@ public void removeServerStoredGroupChangeListener(ServerStoredGroupListener
*/
public void removeSubsciptionListener(SubscriptionListener listener)
{
/** @todo implement removeSubsciptionListener() */
this.subscriptionListeners.remove(listener);
}
/**
@ -288,7 +426,10 @@ public void removeSubsciptionListener(SubscriptionListener listener)
public void renameServerStoredContactGroup(ContactGroup group,
String newName)
{
/** @todo implement renameServerStoredContactGroup() */
((MockContactGroup)group).setGroupName(newName);
this.fireServerStoredGroupEvent(
(MockContactGroup)group, ServerStoredGroupEvent.GROUP_RENAMED_EVENT);
}
/**
@ -326,7 +467,16 @@ public void subscribe(ContactGroup parent, String contactIdentifier) throws
IllegalArgumentException, IllegalStateException,
OperationFailedException
{
/** @todo implement subscribe() */
MockContact contact = new MockContact(contactIdentifier
, parentProvider
, (MockContactGroup)parent);
((MockContactGroup)parent).addContact(contact);
fireSubscriptionEvent(contact,
parent,
SubscriptionEvent.SUBSCRIPTION_CREATED);
}
/**
@ -347,7 +497,16 @@ public void subscribe(String contactIdentifier) throws
IllegalArgumentException, IllegalStateException,
OperationFailedException
{
/** @todo implement subscribe() */
MockContact contact = new MockContact(contactIdentifier
, parentProvider
, contactListRoot);
contactListRoot.addContact(contact);
fireSubscriptionEvent(contact,
contactListRoot,
SubscriptionEvent.SUBSCRIPTION_CREATED);
}
/**
@ -367,6 +526,11 @@ public void subscribe(String contactIdentifier) throws
public void unsubscribe(Contact contact) throws IllegalArgumentException,
IllegalStateException, OperationFailedException
{
/** @todo implement unsubscribe() */
((MockContact)contact).getParentGroup()
.removeContact((MockContact)contact);
fireSubscriptionEvent((MockContact)contact,
((MockContact)contact).getParentGroup(),
SubscriptionEvent.SUBSCRIPTION_REMOVED);
}
}

@ -1,7 +1,6 @@
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/

Loading…
Cancel
Save