Paul Kattenborn
11 months ago
3 changed files with 53 additions and 44 deletions
-
52src/main/java/ChatGUI.java
-
1src/main/java/ClientHandler.java
-
44src/test/java/ChatServerTest.java
@ -0,0 +1,52 @@ |
|||
import java.awt.BorderLayout; |
|||
import java.awt.Dimension; |
|||
import java.awt.Toolkit; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
|
|||
import javax.swing.JFrame; |
|||
import javax.swing.JScrollPane; |
|||
import javax.swing.JTextArea; |
|||
import javax.swing.JTextField; |
|||
|
|||
public class ChatGUI implements ActionListener { |
|||
|
|||
JTextField inputTextField = new JTextField(); |
|||
JTextArea outputTextArea = new JTextArea(); |
|||
|
|||
JFrame gui; |
|||
|
|||
public ChatGUI() { |
|||
gui = new JFrame(); |
|||
|
|||
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
gui.setTitle("java-chat"); |
|||
|
|||
gui.setLayout(new BorderLayout()); |
|||
|
|||
|
|||
JScrollPane outputScrollPane = new JScrollPane(outputTextArea); |
|||
gui.add(outputScrollPane, BorderLayout.CENTER); |
|||
|
|||
inputTextField.setPreferredSize(new Dimension(0, 30)); |
|||
gui.add(inputTextField, BorderLayout.SOUTH); |
|||
|
|||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|||
int quarterWidth = screenSize.width / 2; |
|||
int quarterHeight = screenSize.height / 2; |
|||
gui.setSize(quarterWidth, quarterHeight); |
|||
gui.setVisible(true); |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
|
|||
new ChatGUI(); |
|||
} |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
// TODO Auto-generated method stub |
|||
|
|||
} |
|||
|
|||
|
|||
} |
@ -1,44 +0,0 @@ |
|||
import static org.junit.jupiter.api.Assertions.*; |
|||
|
|||
import java.util.List; |
|||
|
|||
import org.junit.jupiter.api.Test; |
|||
|
|||
class ChatServerTest { |
|||
|
|||
@Test |
|||
public void testConstructor() { |
|||
// Arrange |
|||
ChatServer chatServer = null; |
|||
|
|||
// Act |
|||
try { |
|||
chatServer = new ChatServer(1234); |
|||
} catch (Exception e) { |
|||
fail("Exception occurred: " + e.getMessage()); |
|||
} |
|||
|
|||
// Assert |
|||
assertNotNull(chatServer); |
|||
} |
|||
@Test |
|||
public void testBroadcastMessage() { |
|||
// Arrange |
|||
ChatServer chatServer = new ChatServer(1234); |
|||
ClientHandler client1 = new ClientHandler(chatServer, null); |
|||
ClientHandler client2 = new ClientHandler(chatServer, null); |
|||
chatServer.getClients().add(client1); |
|||
chatServer.getClients().add(client2); |
|||
|
|||
// Act |
|||
chatServer.broadcastMessage("Test message"); |
|||
|
|||
// Assert |
|||
List<ClientHandler> clients = chatServer.getClients(); |
|||
for (ClientHandler client : clients) { |
|||
assertEquals("Test message", client.getLastMessage()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue