@Before
 public void setUp() throws Exception {
   app = new ImApp();
   con = new XmppConnection(app);
   roster = createMock(Roster.class);
   smackCon = createMock(MyXMPPConnection.class);
   expect(smackCon.getRoster()).andStubReturn(roster);
   Contact user = new Contact(new XmppConnection.XmppAddress(TEST_CONTACT), TEST_CONTACT_NAME);
   con.initConnection(smackCon, user, XmppConnection.LOGGED_IN);
   contactListManager = con.getContactListManager();
   subscriptionRequestListener = createMock(SubscriptionRequestListener.class);
   contactListManager.setSubscriptionRequestListener(subscriptionRequestListener);
   listener = createMock(ContactListListener.class);
   contactListManager.addContactListListener(listener);
   expect(smackCon.isConnected()).andStubReturn(true);
 }
 @Test
 public void testApproveSubscription() throws Exception {
   expectInitialListCreation();
   Capture<RosterListener> listenerCapture = new Capture<RosterListener>();
   roster.addRosterListener(capture(listenerCapture));
   expectLastCall();
   subscriptionRequestListener.onSubscriptionApproved(TEST_CONTACT);
   expectLastCall().times(2);
   smackCon.sendPacket(anyObject(Packet.class));
   expectLastCall().times(2);
   expect(roster.getGroups()).andReturn(new ArrayList<RosterGroup>());
   expect(roster.getUnfiledEntryCount()).andStubReturn(0);
   roster.createEntry(
       eq(TEST_CONTACT), eq(TEST_CONTACT_NAME), aryEq(new String[] {DEFAULT_GROUP_NAME}));
   expectLastCall().times(2);
   listener.onContactChange(
       eq(ContactListListener.LIST_CONTACT_ADDED),
       anyObject(ContactList.class),
       anyObject(Contact.class));
   expectLastCall();
   replayAll();
   contactListManager.listenToRoster(roster);
   contactListManager.loadContactLists();
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   // Second time should not call notifyContactListUpdated, since contact
   // already exists
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   assertEquals(1, contactListManager.getContactLists().size());
   assertNotNull(contactListManager.getContactList(DEFAULT_GROUP_NAME));
   assertTrue(con.joinGracefully());
   verifyAll();
 }
 // Approve a subscription while the server already has a Buddies group
 @Test
 public void testApproveSubscription_serverBuddies() throws Exception {
   expectInitialListCreation();
   Capture<RosterListener> listenerCapture = new Capture<RosterListener>();
   roster.addRosterListener(capture(listenerCapture));
   expectLastCall();
   subscriptionRequestListener.onSubscriptionApproved(TEST_CONTACT);
   expectLastCall();
   smackCon.sendPacket(anyObject(Packet.class));
   expectLastCall();
   final ArrayList<RosterGroup> groups = new ArrayList<RosterGroup>();
   RosterGroup buddiesGroup = createNiceMock(RosterGroup.class);
   expect(buddiesGroup.getName()).andStubReturn(DEFAULT_GROUP_NAME);
   expect(buddiesGroup.getEntries()).andStubReturn(new ArrayList<RosterEntry>());
   groups.add(buddiesGroup);
   expect(roster.getGroups()).andReturn(groups);
   expect(roster.getUnfiledEntryCount()).andStubReturn(0);
   roster.createEntry(
       eq(TEST_CONTACT), eq(TEST_CONTACT_NAME), aryEq(new String[] {DEFAULT_GROUP_NAME}));
   expectLastCall();
   listener.onContactChange(
       eq(ContactListListener.LIST_CONTACT_ADDED),
       anyObject(ContactList.class),
       anyObject(Contact.class));
   expectLastCall();
   replayAll();
   contactListManager.listenToRoster(roster);
   contactListManager.loadContactLists();
   contactListManager.approveSubscriptionRequest(TEST_CONTACT);
   assertEquals(1, contactListManager.getContactLists().size());
   assertNotNull(contactListManager.getContactList(DEFAULT_GROUP_NAME));
   assertTrue(con.joinGracefully());
   verifyAll();
 }