From 6ccc26abe3974eb1f8c3a25ec491c7858a6927de Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:12:38 +0100 Subject: [PATCH 01/53] class Client Handler --- bin/.project | 23 +++++ bin/README.md | 92 +++++++++++++++++++ bin/build-project.sh | 24 +++++ bin/target/classes/META-INF/MANIFEST.MF | 4 + .../org.progmethoden/java-chat/pom.properties | 7 ++ .../maven/org.progmethoden/java-chat/pom.xml | 6 ++ bin/target/classes/test2 | 0 bin/target/test-classes/test.txt | 0 bin/target/test-classes/text.txt | 0 bin/team.md | 5 + src/main/java/ClientHandler.java | 22 +++++ src/main/java/{server => }/Server.java | 0 12 files changed, 183 insertions(+) create mode 100644 bin/.project create mode 100644 bin/README.md create mode 100644 bin/build-project.sh create mode 100644 bin/target/classes/META-INF/MANIFEST.MF create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties create mode 100644 bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml create mode 100644 bin/target/classes/test2 create mode 100644 bin/target/test-classes/test.txt create mode 100644 bin/target/test-classes/text.txt create mode 100644 bin/team.md create mode 100644 src/main/java/ClientHandler.java rename src/main/java/{server => }/Server.java (100%) diff --git a/bin/.project b/bin/.project new file mode 100644 index 0000000..7caa276 --- /dev/null +++ b/bin/.project @@ -0,0 +1,23 @@ + + + java-chat + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..8a88154 --- /dev/null +++ b/bin/README.md @@ -0,0 +1,92 @@ +# Java Chat + + + +## Getting started + +To make it easy for you to get started with GitLab, here's a list of recommended next steps. + +Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! + +## Add your files + +- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files +- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: + +``` +cd existing_repo +git remote add origin https://gitlab.cs.hs-fulda.de/fdai7332/java-chat.git +git branch -M main +git push -uf origin main +``` + +## Integrate with your tools + +- [ ] [Set up project integrations](https://gitlab.cs.hs-fulda.de/fdai7332/java-chat/-/settings/integrations) + +## Collaborate with your team + +- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) +- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) +- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) +- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) +- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) + +## Test and Deploy + +Use the built-in continuous integration in GitLab. + +- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) +- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) +- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) +- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) +- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) + +*** + +# Editing this README + +When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template. + +## Suggestions for a good README +Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. + +## Name +Choose a self-explaining name for your project. + +## Description +Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. + +## Badges +On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. + +## Visuals +Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. + +## Installation +Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. + +## Usage +Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. + +## Support +Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. + +## Roadmap +If you have ideas for releases in the future, it is a good idea to list them in the README. + +## Contributing +State if you are open to contributions and what your requirements are for accepting them. + +For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. + +You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. + +## Authors and acknowledgment +Show your appreciation to those who have contributed to the project. + +## License +For open source projects, say how it is licensed. + +## Project status +If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/bin/build-project.sh b/bin/build-project.sh new file mode 100644 index 0000000..ed85686 --- /dev/null +++ b/bin/build-project.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Überprüfen, ob Maven installiert ist +command -v mvn >/dev/null 2>&1 || { echo >&2 "Maven ist erforderlich, aber nicht installiert. Bitte installieren Sie Maven."; exit 1; } + +# Projekt übersetzen und Unittests ausführen +mvn clean compile test + +# Überprüfen, ob der Build erfolgreich war +if [ $? -eq 0 ]; then + echo "Build erfolgreich abgeschlossen." +else + echo "Fehler beim Build. Bitte überprüfen Sie Ihre Codebasis." + exit 1 +fi + + + +remove() { + rm -r build + rm main +} + +remove diff --git a/bin/target/classes/META-INF/MANIFEST.MF b/bin/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..b087487 --- /dev/null +++ b/bin/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 17 +Created-By: Maven Integration for Eclipse + diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties new file mode 100644 index 0000000..638f58d --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Fri Feb 02 12:52:10 CET 2024 +m2e.projectLocation=S\:\\backup\\Studium\\1W\\Programmiermethoden und -Werkzeuge\\javachat\\java-chat +m2e.projectName=java-chat +groupId=org.progmethoden +artifactId=java-chat +version=0.0.1-SNAPSHOT diff --git a/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml new file mode 100644 index 0000000..05188ae --- /dev/null +++ b/bin/target/classes/META-INF/maven/org.progmethoden/java-chat/pom.xml @@ -0,0 +1,6 @@ + + 4.0.0 + org.progmethoden + java-chat + 0.0.1-SNAPSHOT + \ No newline at end of file diff --git a/bin/target/classes/test2 b/bin/target/classes/test2 new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/test.txt b/bin/target/test-classes/test.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/target/test-classes/text.txt b/bin/target/test-classes/text.txt new file mode 100644 index 0000000..e69de29 diff --git a/bin/team.md b/bin/team.md new file mode 100644 index 0000000..1e6866c --- /dev/null +++ b/bin/team.md @@ -0,0 +1,5 @@ +- Valentin Spiroski, fdai7332 +- Richard Schmidt, fdai7420 +- Paul Kattenborn, fdai7599 +- Marc Dimmerling, fdai7579 +- Alena Bandarovich, fdai5595 diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java new file mode 100644 index 0000000..6489bb5 --- /dev/null +++ b/src/main/java/ClientHandler.java @@ -0,0 +1,22 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.ServerSocket; + +public class ClientHandler implements Runnable { + private Server server; + private ServerSocket connectionToClient; + private String name; + private BufferedReader fromClientReader; + private PrintWriter toClientWriter; + + @Override + public void run() { + // TODO Auto-generated method stub + + } + + +} diff --git a/src/main/java/server/Server.java b/src/main/java/Server.java similarity index 100% rename from src/main/java/server/Server.java rename to src/main/java/Server.java From 72e5bc47aef532ebab1a48becdcaa315075665cc Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 2 Feb 2024 17:38:25 +0100 Subject: [PATCH 02/53] Class ClientHandler --- src/main/java/ClientHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6489bb5..f3f9b3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -18,5 +18,5 @@ public class ClientHandler implements Runnable { } - + } From 6c037b67d79647779c2fe1c48e759546b46833f9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 22:58:21 +0100 Subject: [PATCH 03/53] ClientHandler Konstruktor --- .gitignore | 1 + src/main/java/ClientHandler.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b83d222..beb9ac5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target/ +/src/main/java/ChatServer \ No newline at end of file diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index f3f9b3c..200dccf 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -4,14 +4,27 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; +import java.net.Socket; + public class ClientHandler implements Runnable { - private Server server; - private ServerSocket connectionToClient; + private ChatServer chatServer; + private Socket connectionToClient; private String name; private BufferedReader fromClientReader; private PrintWriter toClientWriter; + + public ClientHandler(ChatServer chatServer, Socket connectionToClient) { + this.chatServer = chatServer; + this.connectionToClient = connectionToClient; + name = connectionToClient.getInetAddress().getHostAddress(); + new Thread(this).start(); + } + + + + @Override public void run() { // TODO Auto-generated method stub From 851e506d1fedfe913e3f16f25126778a08b81cce Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:17:02 +0100 Subject: [PATCH 04/53] run Method, implement from Runnable --- src/main/java/ClientHandler.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 200dccf..6711e84 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -27,8 +27,14 @@ public class ClientHandler implements Runnable { @Override public void run() { - // TODO Auto-generated method stub - + try { + fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + chatServer.broadcastMessage(name + " connected."); + + } catch (IOException e) { + e.printStackTrace(); + } } From f428b2336f9e85a5e5947aa0d495d8cb2df25be9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:27:42 +0100 Subject: [PATCH 05/53] while loop in run() for waiting for new messages --- src/main/java/ClientHandler.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 6711e84..fe885bd 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,6 +32,11 @@ public class ClientHandler implements Runnable { toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); + String message = fromClientReader.readLine(); + while (message!=null) { + chatServer.broadcastMessage(name + ": " + message); + message = fromClientReader.readLine(); + } } catch (IOException e) { e.printStackTrace(); } From 1da0f046d44176cf85dd1fb58a7be35ee46c6172 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:33:12 +0100 Subject: [PATCH 06/53] sendMessage Method --- src/main/java/ClientHandler.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index fe885bd..c59f1ed 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -42,5 +42,9 @@ public class ClientHandler implements Runnable { } } + public void sendMessage(String message) { + toClientWriter.println(message); + toClientWriter.flush(); + } } From a7daba9dadd2203bca0a5aad363bce89abe943c2 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:45:06 +0100 Subject: [PATCH 07/53] finally in run() --- src/main/java/ClientHandler.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index c59f1ed..5561e3c 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -39,6 +39,20 @@ public class ClientHandler implements Runnable { } } catch (IOException e) { e.printStackTrace(); + } finally { + //chatServer.removeClient(this); + chatServer.broadcastMessage(name + " disconnected."); + + if (fromClientReader != null) { + try { + fromClientReader.close(); + } catch (IOException e){ + e.printStackTrace(); + } + } + if (toClientWriter != null) { + toClientWriter.close(); + } } } From 22f2ff1169144d67a5ae5780e76984ea1a638ccf Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Sun, 4 Feb 2024 23:48:42 +0100 Subject: [PATCH 08/53] small correction --- src/main/java/ClientHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5561e3c..9c52f54 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -41,6 +41,7 @@ public class ClientHandler implements Runnable { e.printStackTrace(); } finally { //chatServer.removeClient(this); + throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From 39d9621062326581d3a3646e7eb6a2e7685a258f Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:39:27 +0100 Subject: [PATCH 09/53] small correction --- src/main/java/ClientHandler.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9c52f54..3b30c32 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,4 +1,6 @@ + import java.io.BufferedReader; + import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -38,10 +40,10 @@ public class ClientHandler implements Runnable { message = fromClientReader.readLine(); } } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); + } finally { //chatServer.removeClient(this); - throw new RuntimeException(e); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { From 4cf5587ccfeeaeb9741d2aaf419bfd23454b276c Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:42:48 +0100 Subject: [PATCH 10/53] refactoring: imports --- src/main/java/ClientHandler.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 3b30c32..bbc6455 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -1,14 +1,6 @@ - -import java.io.BufferedReader; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.net.ServerSocket; +import java.io.*; import java.net.Socket; - public class ClientHandler implements Runnable { private ChatServer chatServer; private Socket connectionToClient; From 7fd739aed7a2ab813db026c545b940896a32a75f Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:45:11 +0100 Subject: [PATCH 11/53] refactoring: indentation --- src/main/java/ClientHandler.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index bbc6455..db9059f 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,16 +8,11 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; - public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start(); - } - - - + new Thread(this).start();} @Override public void run() { From d723aed4499c34e7442b013ae6b1f579b6779cf3 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:51:33 +0100 Subject: [PATCH 12/53] refactoring: identation run() --- src/main/java/ClientHandler.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index db9059f..7b96316 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -16,27 +16,32 @@ public class ClientHandler implements Runnable { @Override public void run() { + try { - fromClientReader = new BufferedReader(new InputStreamReader(connectionToClient.getInputStream())); - toClientWriter = new PrintWriter(new OutputStreamWriter(connectionToClient.getOutputStream())); + fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); + toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); chatServer.broadcastMessage(name + " connected."); String message = fromClientReader.readLine(); while (message!=null) { chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } - } catch (IOException e) { - throw new RuntimeException(e); - - } finally { + } + } + + catch (IOException e) { + throw new RuntimeException(e); + } + + finally { //chatServer.removeClient(this); chatServer.broadcastMessage(name + " disconnected."); if (fromClientReader != null) { try { fromClientReader.close(); - } catch (IOException e){ + } + catch (IOException e){ e.printStackTrace(); } } @@ -50,5 +55,4 @@ public class ClientHandler implements Runnable { toClientWriter.println(message); toClientWriter.flush(); } - } From 448b1fc00468ab6ccd2e90026e27851d83eeb71b Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 13:59:03 +0100 Subject: [PATCH 13/53] refactoring: ClientHandler constructor comments --- src/main/java/ClientHandler.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 7b96316..9f60c96 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -8,12 +8,14 @@ public class ClientHandler implements Runnable { private BufferedReader fromClientReader; private PrintWriter toClientWriter; + // Constructor for ClientHandler public ClientHandler(ChatServer chatServer, Socket connectionToClient) { this.chatServer = chatServer; this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); - new Thread(this).start();} - + name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity + + new Thread(this).start();} // Start a new thread for this client handler + @Override public void run() { From 1a284c164aea8b103f162970ef48d15cfdd69cee Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 14:03:07 +0100 Subject: [PATCH 14/53] refactoring: comments run() try --- src/main/java/ClientHandler.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 9f60c96..b6ae287 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -20,12 +20,16 @@ public class ClientHandler implements Runnable { public void run() { try { + // Set up input and output streams for communication with the client fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); - chatServer.broadcastMessage(name + " connected."); + chatServer.broadcastMessage(name + " connected."); // Broadcast a message when the client is connected + + // Read messages from the client and broadcast them to all clients String message = fromClientReader.readLine(); while (message!=null) { + // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); } From 0daca63078e7b8ed0ac27dbc11585130c21266aa Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:25:53 +0100 Subject: [PATCH 15/53] refactoring: comments in catch, finally in run() --- src/main/java/ClientHandler.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index b6ae287..5d943af 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -32,17 +32,17 @@ public class ClientHandler implements Runnable { // Broadcast the client's message to all connected clients chatServer.broadcastMessage(name + ": " + message); message = fromClientReader.readLine(); - } + } } catch (IOException e) { - throw new RuntimeException(e); + throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception } - finally { //chatServer.removeClient(this); - chatServer.broadcastMessage(name + " disconnected."); - + chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected + + // Close resources in the finally block if (fromClientReader != null) { try { fromClientReader.close(); @@ -54,7 +54,7 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } public void sendMessage(String message) { From e15782a29327bb397a2355b73ee6d500c221eaf9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Mon, 5 Feb 2024 15:28:11 +0100 Subject: [PATCH 16/53] refactoring: comments and identation on sendMessage Method --- src/main/java/ClientHandler.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java index 5d943af..fa4b924 100644 --- a/src/main/java/ClientHandler.java +++ b/src/main/java/ClientHandler.java @@ -54,11 +54,12 @@ public class ClientHandler implements Runnable { if (toClientWriter != null) { toClientWriter.close(); } - } + } } - - public void sendMessage(String message) { - toClientWriter.println(message); - toClientWriter.flush(); + + //Method to send a message to the client + public void sendMessage(String message) { + toClientWriter.println(message); // Send the message to the client + toClientWriter.flush(); // Flush the stream } } From 12e96a7361cdb838efe376f2e594e21bcecba2d5 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 16:05:54 +0100 Subject: [PATCH 17/53] merge issues fix --- .gitignore | 2 - src/main/java/ClientHandler.java | 65 -------------------------------- src/main/java/Server.java | 29 -------------- 3 files changed, 96 deletions(-) delete mode 100644 .gitignore delete mode 100644 src/main/java/ClientHandler.java delete mode 100644 src/main/java/Server.java diff --git a/.gitignore b/.gitignore deleted file mode 100644 index beb9ac5..0000000 --- a/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target/ -/src/main/java/ChatServer \ No newline at end of file diff --git a/src/main/java/ClientHandler.java b/src/main/java/ClientHandler.java deleted file mode 100644 index fa4b924..0000000 --- a/src/main/java/ClientHandler.java +++ /dev/null @@ -1,65 +0,0 @@ -import java.io.*; -import java.net.Socket; - -public class ClientHandler implements Runnable { - private ChatServer chatServer; - private Socket connectionToClient; - private String name; - private BufferedReader fromClientReader; - private PrintWriter toClientWriter; - - // Constructor for ClientHandler - public ClientHandler(ChatServer chatServer, Socket connectionToClient) { - this.chatServer = chatServer; - this.connectionToClient = connectionToClient; - name = connectionToClient.getInetAddress().getHostAddress(); // Use the client's IP address as their name for simplicity - - new Thread(this).start();} // Start a new thread for this client handler - - @Override - public void run() { - - try { - // Set up input and output streams for communication with the client - fromClientReader = new BufferedReader (new InputStreamReader(connectionToClient.getInputStream())); - toClientWriter = new PrintWriter (new OutputStreamWriter(connectionToClient.getOutputStream())); - - chatServer.broadcastMessage(name + " connected."); // Broadcast a message when the client is connected - - // Read messages from the client and broadcast them to all clients - String message = fromClientReader.readLine(); - while (message!=null) { - // Broadcast the client's message to all connected clients - chatServer.broadcastMessage(name + ": " + message); - message = fromClientReader.readLine(); - } - } - - catch (IOException e) { - throw new RuntimeException(e); // Handle exceptions by throwing a runtime exception - } - finally { - //chatServer.removeClient(this); - chatServer.broadcastMessage(name + " disconnected."); // Broadcast a message when the client is disconnected - - // Close resources in the finally block - if (fromClientReader != null) { - try { - fromClientReader.close(); - } - catch (IOException e){ - e.printStackTrace(); - } - } - if (toClientWriter != null) { - toClientWriter.close(); - } - } - } - - //Method to send a message to the client - public void sendMessage(String message) { - toClientWriter.println(message); // Send the message to the client - toClientWriter.flush(); // Flush the stream - } -} diff --git a/src/main/java/Server.java b/src/main/java/Server.java deleted file mode 100644 index 70c24c9..0000000 --- a/src/main/java/Server.java +++ /dev/null @@ -1,29 +0,0 @@ -package server; - -import java.io.IOException; -import java.net.ServerSocket; -import java.net.Socket; - -public class Server { - - private ServerSocket serverSocket; - - public void ChatServer(int port) { - try { - serverSocket = new ServerSocket(port); - System.out.println("Started ChatServer on port " + port); - - Socket connectionToClient = serverSocket.accept(); - System.out.println("Accepted new Client"); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - public static void main(String[] args) { - // TODO Auto-generated method stub - - } - -} \ No newline at end of file From c97d9b0d475ac8a21be12c4973e1b50d7fa91573 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Thu, 8 Feb 2024 19:49:47 +0100 Subject: [PATCH 18/53] test_ClientHandlerConstructorTest --- .../java/ClientHandlerConstructorTest.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/test/java/ClientHandlerConstructorTest.java diff --git a/src/test/java/ClientHandlerConstructorTest.java b/src/test/java/ClientHandlerConstructorTest.java new file mode 100644 index 0000000..7ebe7ce --- /dev/null +++ b/src/test/java/ClientHandlerConstructorTest.java @@ -0,0 +1,27 @@ +import static org.junit.Assert.*; +import org.junit.Test; +import java.io.*; +import java.net.*; + +public class ClientHandlerConstructorTest { + + @Test + + public void testConstructor() { + Socket mockSocket = new Socket(); + + + int port = 12345; + + + ChatServer mockChatServer = new ChatServer(port); + + + ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); + + + + + assertTrue(mockChatServer.getClients().contains(clientHandler)); + } +} From c50fa7d7aea96f3cf09597b98663eb1d29196572 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 14:50:05 +0100 Subject: [PATCH 19/53] test_TestClientHandlerConstructor --- ...tructorTest.java => TestClientHandlerConstructor.java} | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) rename src/test/java/{ClientHandlerConstructorTest.java => TestClientHandlerConstructor.java} (66%) diff --git a/src/test/java/ClientHandlerConstructorTest.java b/src/test/java/TestClientHandlerConstructor.java similarity index 66% rename from src/test/java/ClientHandlerConstructorTest.java rename to src/test/java/TestClientHandlerConstructor.java index 7ebe7ce..0a20691 100644 --- a/src/test/java/ClientHandlerConstructorTest.java +++ b/src/test/java/TestClientHandlerConstructor.java @@ -3,7 +3,7 @@ import org.junit.Test; import java.io.*; import java.net.*; -public class ClientHandlerConstructorTest { +public class TestClientHandlerConstructor { @Test @@ -19,7 +19,11 @@ public class ClientHandlerConstructorTest { ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); - + try { + Thread.sleep(1000); // Adjust the sleep duration as needed + } catch (InterruptedException e) { + e.printStackTrace(); + } assertTrue(mockChatServer.getClients().contains(clientHandler)); From 6a7ab650b9ccebd60097a7abcf8513c666abd5d9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 14:52:18 +0100 Subject: [PATCH 20/53] refactoring: TestClientHandlerConstructor --- .../java/TestClientHandlerConstructor.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/test/java/TestClientHandlerConstructor.java b/src/test/java/TestClientHandlerConstructor.java index 0a20691..eb1c0ff 100644 --- a/src/test/java/TestClientHandlerConstructor.java +++ b/src/test/java/TestClientHandlerConstructor.java @@ -3,29 +3,28 @@ import org.junit.Test; import java.io.*; import java.net.*; -public class TestClientHandlerConstructor { +public class ClientHandlerTest { @Test - public void testConstructor() { - Socket mockSocket = new Socket(); + // Create a mock Socket + Socket mockSocket = new Socket(); + // Create a ChatServer instance + int port = 12345; // Use any available port for testing + ChatServer mockChatServer = new ChatServer(port); - int port = 12345; - - - ChatServer mockChatServer = new ChatServer(port); + // Create a ClientHandler instance using the constructor + ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); - - ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket); + // Wait for a short time to allow the ClientHandler to be added to the ChatServer's list of clients + try { + Thread.sleep(1000); // Adjust the sleep duration as needed + } catch (InterruptedException e) { + e.printStackTrace(); + } - try { - Thread.sleep(1000); // Adjust the sleep duration as needed - } catch (InterruptedException e) { - e.printStackTrace(); - } - - + // Assert that the clientHandler was added to the list of clients in the ChatServer assertTrue(mockChatServer.getClients().contains(clientHandler)); } } From e239eadd0566544b1cd97882fec581b023926b62 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 14:56:38 +0100 Subject: [PATCH 21/53] refactoring: TestClientHandlerConstructor --- src/test/java/TestClientHandlerConstructor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/TestClientHandlerConstructor.java b/src/test/java/TestClientHandlerConstructor.java index eb1c0ff..773b39f 100644 --- a/src/test/java/TestClientHandlerConstructor.java +++ b/src/test/java/TestClientHandlerConstructor.java @@ -3,7 +3,7 @@ import org.junit.Test; import java.io.*; import java.net.*; -public class ClientHandlerTest { +public class TestClientHandlerConstructor { @Test public void testConstructor() { From 99506291fbacf95ab9b430dc47420f4178cc90df Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:15:01 +0100 Subject: [PATCH 22/53] pom.xml dependency for emojis --- pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pom.xml b/pom.xml index 4b4d00b..2988a3c 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,13 @@ test + + com.vdurmont + emoji-java + 5.1.1 + + + From e2e6a702f1fb67d313ddfde95d5ad54a011b4861 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:16:44 +0100 Subject: [PATCH 23/53] EmojiPicker.java --- src/main/java/EmojiPicker.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/EmojiPicker.java diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java new file mode 100644 index 0000000..f4ad1d5 --- /dev/null +++ b/src/main/java/EmojiPicker.java @@ -0,0 +1,27 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + + +public class EmojiPicker extends JPanel { + + private static final String[] emojis = {"😀", "😃", "😄", "😁", "😆", "😅", "😂", "🤣", "😊", "😇", "🙂", "🙃"}; + private JTextField textField; + + public EmojiPicker(JTextField textField) { + + setLayout(new GridLayout(4, 3, 5, 5)); // Adjust grid layout parameters as needed + + + } + + + } + + + + + + + From c0a154c9fbbdcc30be7d5930b7fb82a972def6e0 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:23:51 +0100 Subject: [PATCH 24/53] EmojiACtionListener --- src/main/java/EmojiPicker.java | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index f4ad1d5..89fd7fb 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -11,13 +11,26 @@ public class EmojiPicker extends JPanel { public EmojiPicker(JTextField textField) { - setLayout(new GridLayout(4, 3, 5, 5)); // Adjust grid layout parameters as needed - - + setLayout(new GridLayout(4, 3, 5, 5)); + + + + setPreferredSize(new Dimension(300, 200)); + } + private class EmojiActionListener implements ActionListener { + + private String emoji; - } + @Override + public void actionPerformed(ActionEvent e) { + + System.out.println("Selected emoji: " + emoji); + textField.setText(textField.getText() + emoji); + } + }} + From 5bf82a5684d45bb6bc4343d3549ed886d6fb4c8f Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:24:43 +0100 Subject: [PATCH 25/53] EmojiACtionListener2 --- src/main/java/EmojiPicker.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 89fd7fb..c641c38 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -23,6 +23,10 @@ public class EmojiPicker extends JPanel { private String emoji; + public EmojiActionListener(String emoji) { + this.emoji = emoji; + } + @Override public void actionPerformed(ActionEvent e) { From 4a36e33afa04c5634e3d1eeb397e1f589e7d6a20 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:25:41 +0100 Subject: [PATCH 26/53] for loop in EmojiPicker --- src/main/java/EmojiPicker.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index c641c38..73bb1db 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -13,7 +13,12 @@ public class EmojiPicker extends JPanel { setLayout(new GridLayout(4, 3, 5, 5)); - + for (String emoji : emojis) { + JButton emojiButton = new JButton(emoji); + emojiButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); + emojiButton.addActionListener(new EmojiActionListener(emoji)); + add(emojiButton); + } setPreferredSize(new Dimension(300, 200)); From d9bc03621e4e9c99e747fc236e4ba3f9d29efd90 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 20:42:24 +0100 Subject: [PATCH 27/53] ChatClient update --- src/main/java/ChatClient.java | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 30b486c..7ff74a6 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -1,11 +1,69 @@ import javax.swing.*; +import javax.swing.border.EmptyBorder; import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.*; import java.net.Socket; +import java.text.SimpleDateFormat; +import java.util.Date; public class ChatClient extends JFrame implements KeyListener { + public static class TR extends JFrame { + + + + private JScrollPane jScrollPane; + private JPanel container; + + public TR() throws HeadlessException { + super("Notizen"); + this.jScrollPane = jScrollPane; + + initComponents(); + } + + private void initComponents() { + setVisible(true); + setBounds(1180, 170, 350, 550); + setPreferredSize(new Dimension(300, 500)); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(false); + + JPanel root = new JPanel(); + root.setPreferredSize(new Dimension(300, 500)); + + setContentPane(root); + + jScrollPane = new JScrollPane(container, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + jScrollPane.setPreferredSize(new Dimension(300, 500)); + root.add(jScrollPane); + + container = new JPanel(); + container.setBorder(new EmptyBorder(3, 3, 3, 3)); + container.setBackground(new Color(0x839683)); + container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS)); + } + + private JPanel setupPanel(String header) { + JPanel panel = new JPanel(); + panel.setPreferredSize(new Dimension(270, 150)); + panel.setLayout(new BorderLayout()); + + container.add(panel); + container.add(Box.createVerticalStrut(5)); + + JLabel jLabel = new JLabel(header); + jLabel.setHorizontalAlignment(JLabel.LEFT); + panel.add(jLabel, BorderLayout.NORTH); + + return panel; + } + + + } private String address; private String connectionFailedMessage; private Socket connectionToServer; @@ -16,6 +74,8 @@ public class ChatClient extends JFrame implements KeyListener { private JTextArea outputTextArea; private JTextField inputTextField; private JScrollPane outputScrollPane; + private Timer timer; + private JLabel timeLabel; public ChatClient() { super("Chat"); @@ -26,13 +86,34 @@ public class ChatClient extends JFrame implements KeyListener { } } + private void initTimer() { + timer = new Timer(1000, new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + updateTime(); + } + }); + timer.start(); + } + + private void updateTime() { + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); + String currentTime = sdf.format(new Date()); + timeLabel.setText("Current Time: " + currentTime); + } + private void initGui() { outputTextArea = new JTextArea(); outputTextArea.setEditable(false); outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat")); + outputTextArea.setBackground(Color.lightGray); outputScrollPane = new JScrollPane(outputTextArea); + timeLabel = new JLabel(); + updateTime(); + add(timeLabel, BorderLayout.NORTH); + inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); @@ -52,6 +133,7 @@ public class ChatClient extends JFrame implements KeyListener { toServerWriter = new PrintWriter(new OutputStreamWriter(connectionToServer.getOutputStream())); initGui(); + initTimer(); while (true) { String message = fromServerReader.readLine(); @@ -107,6 +189,8 @@ public class ChatClient extends JFrame implements KeyListener { } public static void main(String[] args) { + SwingUtilities.invokeLater(() -> new TR()); new ChatClient(); } } + From 74851c0fa03c9708d443c7dad40e580af7cfb01a Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:34:17 +0100 Subject: [PATCH 28/53] Emoji in ChatClient --- src/main/java/ChatClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 7ff74a6..bbfc218 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -77,6 +77,10 @@ public class ChatClient extends JFrame implements KeyListener { private Timer timer; private JLabel timeLabel; + private JButton emojiButton; + private EmojiPicker emojiPicker; + + public ChatClient() { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); @@ -118,8 +122,13 @@ public class ChatClient extends JFrame implements KeyListener { inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); + JPanel inputPanel = new JPanel(new BorderLayout()); + inputPanel.add(inputTextField, BorderLayout.CENTER); + emojiPicker = new EmojiPicker(inputTextField); + + add(outputScrollPane, BorderLayout.CENTER); - add(inputTextField, BorderLayout.SOUTH); + add(inputTextField, BorderLayout.PAGE_END); setVisible(true); setSize(Constants.WINDOW_WIDTH, Constants.WINDOW_HEIGHT); From ad72a8166ce70a279f6d09fdbcf3ca0f45b78508 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:36:26 +0100 Subject: [PATCH 29/53] Emoji Button in ChatClient --- src/main/java/ChatClient.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index bbfc218..46bdc1a 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -126,6 +126,11 @@ public class ChatClient extends JFrame implements KeyListener { inputPanel.add(inputTextField, BorderLayout.CENTER); emojiPicker = new EmojiPicker(inputTextField); + JPanel emojiPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + emojiButton = new JButton("😀"); + emojiButton.setPreferredSize(new Dimension(50, 50)); + + inputPanel.add(emojiPanel, BorderLayout.LINE_END); add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.PAGE_END); From df7030b54717afec73cdc2099c00c2c80a2d33a2 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:37:39 +0100 Subject: [PATCH 30/53] shoWEmojiPicker() in ChatClient --- src/main/java/ChatClient.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 46bdc1a..b19c6a0 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -129,7 +129,7 @@ public class ChatClient extends JFrame implements KeyListener { JPanel emojiPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); emojiButton = new JButton("😀"); emojiButton.setPreferredSize(new Dimension(50, 50)); - + emojiButton.addActionListener(e -> showEmojiPicker()); inputPanel.add(emojiPanel, BorderLayout.LINE_END); add(outputScrollPane, BorderLayout.CENTER); @@ -140,6 +140,13 @@ public class ChatClient extends JFrame implements KeyListener { setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); } + + private void showEmojiPicker() { + + + } + + private void receiveMessages() { try { connectionToServer = new Socket(address, Constants.PORT); From ea4be8a1e1e56b71e28aee00a853f77268be4678 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:38:21 +0100 Subject: [PATCH 31/53] JDialog in showEmojiPicker() --- src/main/java/ChatClient.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index b19c6a0..6a3e773 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -142,7 +142,12 @@ public class ChatClient extends JFrame implements KeyListener { } private void showEmojiPicker() { - + JDialog emojiDialog = new JDialog(this, "Emoji Picker", true); + emojiDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE); + emojiDialog.getContentPane().add(emojiPicker); + emojiDialog.pack(); + emojiDialog.setLocationRelativeTo(this); + emojiDialog.setVisible(true); } From d9feac282d944e5c507d9bd08ee9473b899a3a9b Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:48:38 +0100 Subject: [PATCH 32/53] refactoring: Emojis in ChatClient --- src/main/java/ChatClient.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 6a3e773..b4be3a3 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -77,10 +77,10 @@ public class ChatClient extends JFrame implements KeyListener { private Timer timer; private JLabel timeLabel; + // Emoji private JButton emojiButton; private EmojiPicker emojiPicker; - public ChatClient() { super("Chat"); address = JOptionPane.showInputDialog("bitte IP-Adresse"); @@ -121,11 +121,15 @@ public class ChatClient extends JFrame implements KeyListener { inputTextField = new JTextField(); inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben")); inputTextField.addKeyListener(this); - + + // Create a panel to hold input text field and emoji picker JPanel inputPanel = new JPanel(new BorderLayout()); - inputPanel.add(inputTextField, BorderLayout.CENTER); + inputPanel.add(inputTextField, BorderLayout.CENTER); // Add input text field to the center of the panel + + // Initialize EmojiPicker and add it to the input text field emojiPicker = new EmojiPicker(inputTextField); + // Create a panel to hold emoji button JPanel emojiPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); emojiButton = new JButton("😀"); emojiButton.setPreferredSize(new Dimension(50, 50)); From 0e1e2c4ec4de689ff9dbc963fc9c946c585b3033 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:50:06 +0100 Subject: [PATCH 33/53] refactoring: Emojis in ChatClient2 --- src/main/java/ChatClient.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index b4be3a3..3ce3327 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -131,10 +131,12 @@ public class ChatClient extends JFrame implements KeyListener { // Create a panel to hold emoji button JPanel emojiPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - emojiButton = new JButton("😀"); - emojiButton.setPreferredSize(new Dimension(50, 50)); - emojiButton.addActionListener(e -> showEmojiPicker()); - inputPanel.add(emojiPanel, BorderLayout.LINE_END); + emojiButton = new JButton("😀"); // Create emoji button with a smiley face + emojiButton.setPreferredSize(new Dimension(50, 50));// Set preferred size for the emoji button + emojiButton.addActionListener(e -> showEmojiPicker());// Add action listener to show emoji picker when button is clicked + + inputPanel.add(emojiPanel, BorderLayout.LINE_END);// Add emoji panel to the right side of the input panel + add(outputScrollPane, BorderLayout.CENTER); add(inputTextField, BorderLayout.PAGE_END); From 8f6b20f495ade782cb471a1d3a4ccb6dede28083 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:50:46 +0100 Subject: [PATCH 34/53] small correction --- src/main/java/ChatClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 3ce3327..30a7136 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -134,7 +134,7 @@ public class ChatClient extends JFrame implements KeyListener { emojiButton = new JButton("😀"); // Create emoji button with a smiley face emojiButton.setPreferredSize(new Dimension(50, 50));// Set preferred size for the emoji button emojiButton.addActionListener(e -> showEmojiPicker());// Add action listener to show emoji picker when button is clicked - + emojiPanel.add(emojiButton); // Add emoji button to panel inputPanel.add(emojiPanel, BorderLayout.LINE_END);// Add emoji panel to the right side of the input panel From ba3b8fa4e045cd5e3187f67f5e9e7083a2713564 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 21:52:38 +0100 Subject: [PATCH 35/53] refactoring: Emojis in ChatClient3 --- src/main/java/ChatClient.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ChatClient.java b/src/main/java/ChatClient.java index 30a7136..e7df403 100644 --- a/src/main/java/ChatClient.java +++ b/src/main/java/ChatClient.java @@ -148,16 +148,15 @@ public class ChatClient extends JFrame implements KeyListener { } private void showEmojiPicker() { - JDialog emojiDialog = new JDialog(this, "Emoji Picker", true); - emojiDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE); - emojiDialog.getContentPane().add(emojiPicker); - emojiDialog.pack(); - emojiDialog.setLocationRelativeTo(this); - emojiDialog.setVisible(true); - + // Create a dialog to display the emoji picker + JDialog emojiDialog = new JDialog(this, "Emoji Picker", true); // Create a modal dialog with a title "Emoji Picker" + emojiDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Set close operation to dispose the dialog when closed + emojiDialog.getContentPane().add(emojiPicker); // Add the emoji picker component to the content pane of the dialog + emojiDialog.pack(); // Pack the dialog to fit the preferred size of its components + emojiDialog.setLocationRelativeTo(this);// Set the location of the dialog relative to the parent frame + emojiDialog.setVisible(true); // Make the dialog visible } - private void receiveMessages() { try { connectionToServer = new Socket(address, Constants.PORT); From 8fa67d594cec56a4fe0e22b1659887ac2891c26d Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:00:26 +0100 Subject: [PATCH 36/53] refactoring: EmojiPicker --- src/main/java/EmojiPicker.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 73bb1db..ad93e6a 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -3,16 +3,19 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; - public class EmojiPicker extends JPanel { - - private static final String[] emojis = {"😀", "😃", "😄", "😁", "😆", "😅", "😂", "🤣", "😊", "😇", "🙂", "🙃"}; + private JTextField textField; + private static final String[] emojis = {"😀", "😃", "😄", "😁", "😆", "😅", + "😂", "🤣", "😊", "😇", "🙂", "🙃"}; + + + public EmojiPicker(JTextField textField) { - setLayout(new GridLayout(4, 3, 5, 5)); + for (String emoji : emojis) { JButton emojiButton = new JButton(emoji); emojiButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); @@ -21,7 +24,6 @@ public class EmojiPicker extends JPanel { } setPreferredSize(new Dimension(300, 200)); - } private class EmojiActionListener implements ActionListener { @@ -34,11 +36,11 @@ public class EmojiPicker extends JPanel { @Override public void actionPerformed(ActionEvent e) { - System.out.println("Selected emoji: " + emoji); textField.setText(textField.getText() + emoji); + } } - }} +} From 783dcc256c2f8db7164aa41043860e386ce3390e Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:03:15 +0100 Subject: [PATCH 37/53] refactoring: EmojiPicker2 --- src/main/java/EmojiPicker.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index ad93e6a..8d9f330 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -3,15 +3,22 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +/** + * A panel containing a selection of emoji buttons to be used in conjunction with a JTextField. + */ + public class EmojiPicker extends JPanel { - private JTextField textField; + private JTextField textField; // Text field to append selected emoji + // Array containing a selection of emojis private static final String[] emojis = {"😀", "😃", "😄", "😁", "😆", "😅", "😂", "🤣", "😊", "😇", "🙂", "🙃"}; - - + /** + * Constructs an EmojiPicker with a specified JTextField. + * @param textField The JTextField to append selected emojis. + */ public EmojiPicker(JTextField textField) { setLayout(new GridLayout(4, 3, 5, 5)); From edd14a58242d36fb567b12fb23dd1f028cdcf1d1 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:04:25 +0100 Subject: [PATCH 38/53] refactoring: EmojiPicker3 --- src/main/java/EmojiPicker.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 8d9f330..85c1f21 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -20,19 +20,20 @@ public class EmojiPicker extends JPanel { * @param textField The JTextField to append selected emojis. */ public EmojiPicker(JTextField textField) { - setLayout(new GridLayout(4, 3, 5, 5)); - + setLayout(new GridLayout(4, 3, 5, 5)); // Set grid layout for the emoji buttons + // Create and add buttons for each emoji for (String emoji : emojis) { JButton emojiButton = new JButton(emoji); - emojiButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); - emojiButton.addActionListener(new EmojiActionListener(emoji)); - add(emojiButton); + emojiButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); // Set font for emoji buttons + emojiButton.addActionListener(new EmojiActionListener(emoji)); // Add action listener to each button + add(emojiButton); // Add emoji button to the panel } - setPreferredSize(new Dimension(300, 200)); + setPreferredSize(new Dimension(300, 200)); // Set preferred size for the EmojiPicker panel } + private class EmojiActionListener implements ActionListener { private String emoji; From 91279d4835d8bc25ad3215e55426008ea5dea25d Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:05:33 +0100 Subject: [PATCH 39/53] refactoring: EmojiPicker4 --- src/main/java/EmojiPicker.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 85c1f21..dfba42d 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -33,10 +33,17 @@ public class EmojiPicker extends JPanel { setPreferredSize(new Dimension(300, 200)); // Set preferred size for the EmojiPicker panel } - + /** + * ActionListener for handling emoji button clicks. + */ private class EmojiActionListener implements ActionListener { - private String emoji; + private String emoji; // Selected emoji + + /** + * Constructs an EmojiActionListener with the specified emoji. + * @param emoji The emoji associated with this listener. + */ public EmojiActionListener(String emoji) { this.emoji = emoji; @@ -44,7 +51,7 @@ public class EmojiPicker extends JPanel { @Override public void actionPerformed(ActionEvent e) { - System.out.println("Selected emoji: " + emoji); + System.out.println("Selected emoji: " + emoji); textField.setText(textField.getText() + emoji); } } From 894f9ed00889afdd5cfe050d7da9a52d817eb5bf Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:05:56 +0100 Subject: [PATCH 40/53] refactoring: EmojiPicker5 --- src/main/java/EmojiPicker.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index dfba42d..85963eb 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -51,8 +51,8 @@ public class EmojiPicker extends JPanel { @Override public void actionPerformed(ActionEvent e) { - System.out.println("Selected emoji: " + emoji); - textField.setText(textField.getText() + emoji); + System.out.println("Selected emoji: " + emoji); // Print selected emoji to console + textField.setText(textField.getText() + emoji); // Append selected emoji to text field } } } From d8dbed3e10b25782c24dae023488558f1d6ddc42 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:28:02 +0100 Subject: [PATCH 41/53] test_testEmojiPickerConstructor --- src/test/java/TestEmojiPicker.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/test/java/TestEmojiPicker.java diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java new file mode 100644 index 0000000..19b6128 --- /dev/null +++ b/src/test/java/TestEmojiPicker.java @@ -0,0 +1,15 @@ +import org.junit.jupiter.api.Test; +import javax.swing.*; +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestEmojiPicker { + + @Test + public void testEmojiPickerConstructor() { + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); + assertNotNull(emojiPicker); + } +} \ No newline at end of file From 5448721ea12aa8284b496b0b1ffde7fa2135129d Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:29:12 +0100 Subject: [PATCH 42/53] test_testEmojiPickerButtonCount --- src/test/java/TestEmojiPicker.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 19b6128..5595063 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -1,15 +1,38 @@ import org.junit.jupiter.api.Test; import javax.swing.*; + import java.awt.*; + import static org.junit.jupiter.api.Assertions.*; public class TestEmojiPicker { @Test - public void testEmojiPickerConstructor() { - JTextField textField = new JTextField(); + public void testEmojiPickerConstructor() { + + + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); + assertNotNull(emojiPicker); - } + + } + @Test + public void testEmojiPickerButtonCount() { + JTextField textField = new JTextField(); + + EmojiPicker emojiPicker = new EmojiPicker(textField); + + Component[] components = emojiPicker.getComponents(); + + assertEquals(12, components.length); + + } + + + + + } \ No newline at end of file From 11b24c125791ba939686381037068c15492c05e7 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:30:19 +0100 Subject: [PATCH 43/53] test_testEmojiPickerButtonText --- src/test/java/TestEmojiPicker.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 5595063..c2c55c3 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -32,7 +32,23 @@ public class TestEmojiPicker { } - + @Test + public void testEmojiPickerButtonText() { + + + JTextField textField = new JTextField(); + + EmojiPicker emojiPicker = new EmojiPicker(textField); + + Component[] components = emojiPicker.getComponents(); + + for (Component component : components) { + assertTrue(component instanceof JButton); + + JButton button = (JButton) component; + assertTrue(button.getText().matches("[\uD83C-\uDBFF\uDC00-\uDFFF]+")); + } + } } \ No newline at end of file From fbd24f4ee9a4d4cfb8e37a210e7c4e0aba8d49ee Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:32:39 +0100 Subject: [PATCH 44/53] test_testEmojiPickerActionListener --- src/main/java/EmojiPicker.java | 2 +- src/test/java/TestEmojiPicker.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 85963eb..17a7f7a 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -36,7 +36,7 @@ public class EmojiPicker extends JPanel { /** * ActionListener for handling emoji button clicks. */ - private class EmojiActionListener implements ActionListener { + class EmojiActionListener implements ActionListener { private String emoji; // Selected emoji diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index c2c55c3..5fe2c94 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -50,5 +50,24 @@ public class TestEmojiPicker { } } + @Test + public void testEmojiPickerActionListener() { + + JTextField textField = new JTextField(); + + EmojiPicker emojiPicker = new EmojiPicker(textField); + + Component[] components = emojiPicker.getComponents(); + + for (Component component : components) { + assertTrue(component instanceof JButton); + + JButton button = (JButton) component; + assertNotNull(button.getActionListeners()); + + assertEquals(1, button.getActionListeners().length); + assertTrue(button.getActionListeners()[0] instanceof EmojiPicker.EmojiActionListener); + } + } } \ No newline at end of file From 12c80a6668a835025256b85066f5432563a7cd86 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:34:48 +0100 Subject: [PATCH 45/53] test_testEmojiPickerActionListenerFunctionality --- src/test/java/TestEmojiPicker.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 5fe2c94..ad5aa2e 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -70,4 +70,24 @@ public class TestEmojiPicker { } } -} \ No newline at end of file + @Test + public void testEmojiPickerActionListenerFunctionality() { + JTextField textField = new JTextField(); + + EmojiPicker emojiPicker = new EmojiPicker(textField); + + Component[] components = emojiPicker.getComponents(); + for (Component component : components) { + + assertTrue(component instanceof JButton); + JButton button = (JButton) component; + EmojiPicker.EmojiActionListener actionListener = (EmojiPicker.EmojiActionListener) button.getActionListeners()[0]; + + assertNotNull(actionListener); + + actionListener.actionPerformed(null); + assertEquals(button.getText(), textField.getText()); + } + } +} + From caf3336bf594c126332324b7f69a0cb70a4e9d70 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:39:10 +0100 Subject: [PATCH 46/53] refactoring:TestEmojiPicker --- src/test/java/TestEmojiPicker.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index ad5aa2e..997f920 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -1,22 +1,22 @@ import org.junit.jupiter.api.Test; import javax.swing.*; - import java.awt.*; - import static org.junit.jupiter.api.Assertions.*; -public class TestEmojiPicker { +public class TestEmojiPicker { + //Test to ensure the EmojiPicker constructor initializes without errors @Test public void testEmojiPickerConstructor() { - - + // Arrange JTextField textField = new JTextField(); + + // Act + EmojiPicker emojiPicker = new EmojiPicker(textField); - EmojiPicker emojiPicker = new EmojiPicker(textField); - - assertNotNull(emojiPicker); + // Assert + assertNotNull(emojiPicker); } @Test From cea16601835a1188d650b8bfc2f7184c44619f3e Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:41:32 +0100 Subject: [PATCH 47/53] refactoring:testEmojiPickerButtonCount --- src/test/java/TestEmojiPicker.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 997f920..8179e87 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -19,16 +19,18 @@ public class TestEmojiPicker { assertNotNull(emojiPicker); } + //Test to verify the correct number of emoji buttons in the EmojiPicker @Test public void testEmojiPickerButtonCount() { - JTextField textField = new JTextField(); - - EmojiPicker emojiPicker = new EmojiPicker(textField); - - Component[] components = emojiPicker.getComponents(); - - assertEquals(12, components.length); - + // Arrange + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); + + // Act + Component[] components = emojiPicker.getComponents(); + + // Assert + assertEquals(12, components.length); // 12 buttons for 12 emojis } From 5afefbff15141c6c31ef5f696d18bff70cd87569 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:43:29 +0100 Subject: [PATCH 48/53] refactoring:testEmojiPickerButtonText --- src/test/java/TestEmojiPicker.java | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 8179e87..cf7905e 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -33,23 +33,22 @@ public class TestEmojiPicker { assertEquals(12, components.length); // 12 buttons for 12 emojis } - + //Test to check if each emoji button contains valid emoji text @Test public void testEmojiPickerButtonText() { - - - JTextField textField = new JTextField(); - - EmojiPicker emojiPicker = new EmojiPicker(textField); - - Component[] components = emojiPicker.getComponents(); - - for (Component component : components) { - assertTrue(component instanceof JButton); + // Arrange + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); - JButton button = (JButton) component; - assertTrue(button.getText().matches("[\uD83C-\uDBFF\uDC00-\uDFFF]+")); - } + // Act + Component[] components = emojiPicker.getComponents(); + + // Assert + for (Component component : components) { + assertTrue(component instanceof JButton); + JButton button = (JButton) component; + assertTrue(button.getText().matches("[\uD83C-\uDBFF\uDC00-\uDFFF]+")); // Check if button text is an emoji + } } @Test From 4e4755aba5729eafcbf1489e4637855811360d1c Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:44:51 +0100 Subject: [PATCH 49/53] refactoring:testEmojiPickerActionListener --- src/test/java/TestEmojiPicker.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index cf7905e..4843226 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -51,24 +51,24 @@ public class TestEmojiPicker { } } + // Test to ensure each emoji button has an EmojiActionListener attached @Test public void testEmojiPickerActionListener() { + // Arrange + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); - JTextField textField = new JTextField(); - - EmojiPicker emojiPicker = new EmojiPicker(textField); - - Component[] components = emojiPicker.getComponents(); - - for (Component component : components) { - assertTrue(component instanceof JButton); - - JButton button = (JButton) component; - assertNotNull(button.getActionListeners()); - - assertEquals(1, button.getActionListeners().length); - assertTrue(button.getActionListeners()[0] instanceof EmojiPicker.EmojiActionListener); - } + // Act + Component[] components = emojiPicker.getComponents(); + + // Assert + for (Component component : components) { + assertTrue(component instanceof JButton); + JButton button = (JButton) component; + assertNotNull(button.getActionListeners()); + assertEquals(1, button.getActionListeners().length); + assertTrue(button.getActionListeners()[0] instanceof EmojiPicker.EmojiActionListener); + } } @Test From e8da8454c8c18ac879e857b7249f24439d0bb283 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 22:46:51 +0100 Subject: [PATCH 50/53] refactoring:testEmojiPickerActionListenerFunctionality --- src/test/java/TestEmojiPicker.java | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 4843226..1ad2632 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -71,24 +71,23 @@ public class TestEmojiPicker { } } + //Test to verify the functionality of EmojiActionListener for each emoji button @Test public void testEmojiPickerActionListenerFunctionality() { - JTextField textField = new JTextField(); - - EmojiPicker emojiPicker = new EmojiPicker(textField); - - Component[] components = emojiPicker.getComponents(); - for (Component component : components) { - - assertTrue(component instanceof JButton); - JButton button = (JButton) component; + // Arrange + JTextField textField = new JTextField(); + EmojiPicker emojiPicker = new EmojiPicker(textField); + Component[] components = emojiPicker.getComponents(); + + // Act & Assert + for (Component component : components) { + assertTrue(component instanceof JButton); + JButton button = (JButton) component; EmojiPicker.EmojiActionListener actionListener = (EmojiPicker.EmojiActionListener) button.getActionListeners()[0]; - assertNotNull(actionListener); - actionListener.actionPerformed(null); - assertEquals(button.getText(), textField.getText()); - } + assertEquals(button.getText(), textField.getText()); // Check if emoji is appended to the text field + } } } From 8a5d76f9215508e7e11211e381203c0312e17b95 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 23:02:47 +0100 Subject: [PATCH 51/53] test_ChatClientTest.java --- src/test/java/ChatClientTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/test/java/ChatClientTest.java diff --git a/src/test/java/ChatClientTest.java b/src/test/java/ChatClientTest.java new file mode 100644 index 0000000..bd11fa3 --- /dev/null +++ b/src/test/java/ChatClientTest.java @@ -0,0 +1,17 @@ +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import javax.swing.*; +import java.awt.*; +import java.io.*; + +import static org.junit.jupiter.api.Assertions.*; + + public class ChatClientTest { + private ChatClient chatClient; + + @BeforeEach + public void setUp() { + + chatClient = new ChatClient(); + } +} \ No newline at end of file From 027049797c2287742f93030ae0eaa230a5d341a9 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 23:15:07 +0100 Subject: [PATCH 52/53] delete test --- src/test/java/ChatClientTest.java | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/test/java/ChatClientTest.java diff --git a/src/test/java/ChatClientTest.java b/src/test/java/ChatClientTest.java deleted file mode 100644 index bd11fa3..0000000 --- a/src/test/java/ChatClientTest.java +++ /dev/null @@ -1,17 +0,0 @@ -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import javax.swing.*; -import java.awt.*; -import java.io.*; - -import static org.junit.jupiter.api.Assertions.*; - - public class ChatClientTest { - private ChatClient chatClient; - - @BeforeEach - public void setUp() { - - chatClient = new ChatClient(); - } -} \ No newline at end of file From 08935f3889149adb3c29343578a9a419ee0c9788 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 23:16:45 +0100 Subject: [PATCH 53/53] test_EmojiPickerActionListenerFunctionality --- src/main/java/EmojiPicker.java | 1 + src/test/java/TestEmojiPicker.java | 32 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 17a7f7a..dc991e1 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -52,6 +52,7 @@ public class EmojiPicker extends JPanel { @Override public void actionPerformed(ActionEvent e) { System.out.println("Selected emoji: " + emoji); // Print selected emoji to console + System.out.println("textField: " + textField); // Print the value of textField textField.setText(textField.getText() + emoji); // Append selected emoji to text field } } diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 1ad2632..1279081 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -71,23 +71,23 @@ public class TestEmojiPicker { } } - //Test to verify the functionality of EmojiActionListener for each emoji button +//Test to verify the functionality of EmojiActionListener for each emoji button @Test public void testEmojiPickerActionListenerFunctionality() { - // Arrange - JTextField textField = new JTextField(); - EmojiPicker emojiPicker = new EmojiPicker(textField); - Component[] components = emojiPicker.getComponents(); - - // Act & Assert - for (Component component : components) { - assertTrue(component instanceof JButton); - JButton button = (JButton) component; - EmojiPicker.EmojiActionListener actionListener = (EmojiPicker.EmojiActionListener) button.getActionListeners()[0]; - assertNotNull(actionListener); - actionListener.actionPerformed(null); - assertEquals(button.getText(), textField.getText()); // Check if emoji is appended to the text field - } + // Arrange + JTextField textField = new JTextField(); // Initialize JTextField + EmojiPicker emojiPicker = new EmojiPicker(textField); // Pass the initialized JTextField to the EmojiPicker constructor + Component[] components = emojiPicker.getComponents(); + + // Act & Assert + for (Component component : components) { + assertTrue(component instanceof JButton); + JButton button = (JButton) component; + EmojiPicker.EmojiActionListener actionListener = (EmojiPicker.EmojiActionListener) button.getActionListeners()[0]; + assertNotNull(actionListener); + actionListener.actionPerformed(null); + String expectedText = textField.getText() + button.getText(); // Expected text after appending emoji + assertEquals(expectedText, textField.getText(), "Failed for emoji: " + button.getText()); // Check if emoji is appended to the text field + } } } -