From fe796888c394f6f6977bf626abd948c802ade44f Mon Sep 17 00:00:00 2001 From: Paul Kattenborn Date: Fri, 9 Feb 2024 17:22:51 +0100 Subject: [PATCH] test_TestAddMessage plus Anpassen der anderen Tests auf den Zeitstempel --- src/main/java/ChatGUI.java | 2 +- src/test/java/ChatGUITest.java | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatGUI.java b/src/main/java/ChatGUI.java index 2011bc9..171cd62 100644 --- a/src/main/java/ChatGUI.java +++ b/src/main/java/ChatGUI.java @@ -145,7 +145,7 @@ public class ChatGUI implements ActionListener { } // add-message method with time stamp - private void addMessage(String message) { + void addMessage(String message) { String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); outputTextArea.append("[" + timeStamp + "] " + message + "\n"); outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength()); diff --git a/src/test/java/ChatGUITest.java b/src/test/java/ChatGUITest.java index 47cc055..2931dad 100644 --- a/src/test/java/ChatGUITest.java +++ b/src/test/java/ChatGUITest.java @@ -1,8 +1,11 @@ import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; +import java.text.SimpleDateFormat; +import java.util.Date; import org.junit.Before; import org.junit.Test; @@ -59,9 +62,11 @@ public class ChatGUITest { chatGUI.inputTextField.setText("Testnachricht"); chatGUI.sendButton.doClick(); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); } + @Test public void testInputTextFieldActionPerformed() { @@ -69,12 +74,24 @@ public class ChatGUITest { chatGUI.actionPerformed(new ActionEvent(chatGUI.inputTextField, ActionEvent.ACTION_PERFORMED, "")); - String expectedOutput = "Testnachricht\n"; + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedOutput = "[" + timeStamp + "] Testnachricht\n"; assertEquals(expectedOutput, chatGUI.outputTextArea.getText()); - assertEquals("", chatGUI.inputTextField.getText()); } + @Test + public void testAddMessage() { + + String message = "Testnachricht"; + chatGUI.addMessage(message); + + + String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date()); + String expectedMessage = "[" + timeStamp + "] " + message + "\n"; + + assertTrue(chatGUI.outputTextArea.getText().contains(expectedMessage)); + } }