/** * Test of handleEvent method, of class Conversation. Tests both local and remote side * * @throws UnknownHostException * @throws InterruptedException * @throws IOException */ @Test public void testHandleEvent() throws UnknownHostException, IOException, InterruptedException { // clear the converstion created in setUp() EventPool.getAppPool().removeListener(conversation); // get the local app-pool and let it start EventPool localPool = EventPool.getAppPool(); Thread.sleep(100); // create a remote eventpool and connect it to the local one EventPool remotePool = new EventPool(); Connection remoteConnection = new Connection(InetAddress.getLocalHost(), remotePool, null); Thread.sleep(100); // create a contact out of the local user (as seen from the other side) Account localAccount = ChatApplication.getInstance().getAccount(); Contact localContact = new Contact(localAccount.getUsername(), InetAddress.getLocalHost()); Contact remoteContact = new Contact("Jaspervdj", InetAddress.getLocalHost()); // add both of these contacts to our list, so we can look them up later ContactList contactList = localAccount.getContactList(); contactList.addContact(localContact); contactList.addContact(remoteContact); // conversation manager will contain the conversation with the remote user, as // well as the conversation with the local one ConversationManager conversationManager = ChatApplication.getInstance().getConversationManager(); Conversation localConversation = conversationManager.startConversation(remoteContact, false); Conversation remoteConversation = conversationManager.startConversation(localContact, false); // register the remoteConversation specifically with the remote pool remotePool.addListener(remoteConversation, new ConversationEventFilter(remoteConversation)); // remove remoteListener from localPool localPool.removeListener(remoteConversation); // now everything is setup, and a user can type a message ChatMessage chatMessage = new ChatMessage(localContact.getUsername(), "Dag Javache, jij jij remoteUser!"); ConversationEvent localEvent = new NewChatMessageEvent(localConversation, chatMessage); // raise the event on the local pool, should get sent to remotePool too localPool.raiseNetworkEvent(localEvent); Thread.sleep(100); // let's check the results! assertEquals(1, remoteConversation.getChatMessages(10).length); assertEquals( "Dag Javache, jij jij remoteUser!", remoteConversation.getChatMessages(1)[0].getText()); assertEquals(1, localConversation.getChatMessages(10).length); assertEquals( "Dag Javache, jij jij remoteUser!", localConversation.getChatMessages(1)[0].getText()); }
public static void main(String[] args) { ContactList list = new ContactList("Contacts.txt"); Scanner input = new Scanner(System.in); boolean exit = false; System.out.println("Welcome to the Contact Manager!"); do { System.out.println("-------------------"); System.out.println( "Do you want to:\n1) See all contacts\n2)See a specific contact\n3)Enter a new contact\n4+)Quit"); int userInput = Integer.parseInt(input.nextLine()); switch (userInput) { case 1: System.out.println("-------------------"); System.out.println("All contacts:"); list.listContactNames(); break; case 2: System.out.println("-------------------"); System.out.println("Enter the name of the contact that you wish to see"); String userInput2 = input.nextLine(); System.out.println("-------------------"); list.getContact(userInput2); System.out.println(); break; case 3: System.out.println("-------------------"); System.out.println( "Enter the name, then the email, and the number in order to add the contact"); String name = input.nextLine(); String email = input.nextLine(); String number = input.nextLine(); list.addContact(name, email, number); System.out.println("-------------------"); System.out.println("\n" + name + "'s Contact has been added:\n" + list.getContact(name)); break; default: System.out.println("-------------------"); System.out.println("Goodbye."); exit = true; break; } } while (exit == false); }
public void actionPerformed(ActionEvent e) { if (to == e.getSource()) { toSendWindow(); // Display list for user selection // puts user selection into JTextField to } else if (addContacts == e.getSource()) { addContactWindow(); // Display prompt for information for new contact // Uses information to add to ContactList collection // } else if (removeContacts == e.getSource()) { removeContactWindow(); // Display prompt for removing contacts // Uses information to remove from ContactList collection } else if (editContacts == e.getSource()) { editContactWindow(); // UI for editing contacts } else if (save == e.getSource()) { cList.addContact( new Contact( newContactFields[0].getText(), newContactFields[1].getText(), newContactFields[2].getText())); JOptionPane.showMessageDialog( null, "The contact has been added.", "Success!", JOptionPane.INFORMATION_MESSAGE); addWindow.dispose(); } else if (deleteContact == e.getSource()) { ContactList cDeleted = contactList.getContacts(); for (int i = 0; i < cDeleted.getLength(); i++) { cList.removeContact(cDeleted.getContact(i)); /* PLACEHOLDER FOR REPAINT */ } removeWindow.dispose(); } else if (addSendContacts == e.getSource()) { toText.setText(toText.getText() + contactList.getEmails()); toWindow.dispose(); } }