-
Notifications
You must be signed in to change notification settings - Fork 66
Tests added #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Tests added #43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,63 @@ | ||
| package com.example; | ||
|
|
||
| import javafx.fxml.FXML; | ||
| import javafx.scene.control.Label; | ||
| import javafx.scene.control.Button; | ||
| import javafx.scene.control.ListView; | ||
| import javafx.scene.control.TextField; | ||
| import javafx.application.Platform; | ||
|
|
||
| /** | ||
| * Controller layer: mediates between the view (FXML) and the model. | ||
| */ | ||
| public class HelloController { | ||
|
|
||
| // Kopplas till FXML-filen | ||
| @FXML private TextField inputField; | ||
| @FXML private Button sendBtn; | ||
| @FXML private ListView<String> messages; | ||
|
|
||
| // Skapar modellen som sköter nätverksdelen (skicka/ta emot) | ||
| private final HelloModel model = new HelloModel(); | ||
|
|
||
| // Körs automatiskt när appen startar | ||
| @FXML | ||
| private Label messageLabel; | ||
| public void initialize() { | ||
| // Starttext när programmet öppnas | ||
| messages.getItems().add("Ansluten till: " + model.info()); | ||
| messages.getItems().add("Skriv ett meddelande och tryck Skicka!"); | ||
|
|
||
| @FXML | ||
| private void initialize() { | ||
| if (messageLabel != null) { | ||
| messageLabel.setText(model.getGreeting()); | ||
| // Börja lyssna efter inkommande meddelanden | ||
| startListeningToMessages(); | ||
|
|
||
| // När man trycker på Skicka-knappen | ||
| sendBtn.setOnAction(_ -> sendMessage()); | ||
| } | ||
|
|
||
| // Skickar ett meddelande till chatten | ||
| private void sendMessage() { | ||
| String text = inputField.getText(); | ||
|
|
||
| if (text == null || text.isBlank()) { | ||
| return; // Skicka inte tomt meddelande | ||
| } | ||
|
|
||
| // Visa direkt i listan | ||
| messages.getItems().add("Jag: " + text); | ||
|
|
||
| // Skicka till servern | ||
| model.sendMessage(text); | ||
|
|
||
| // Töm textfältet | ||
| inputField.clear(); | ||
| } | ||
|
|
||
| // Lyssnar efter nya meddelanden i bakgrunden | ||
| private void startListeningToMessages() { | ||
| model.startListening( | ||
| // När ett nytt meddelande kommer | ||
| msg -> Platform.runLater(() ->messages.getItems().add("Inkommande: " + msg) | ||
| ), | ||
|
|
||
| //Om ett fel inträffar | ||
| err -> Platform.runLater(() ->messages.getItems().add("Fel: " + err.getMessage()) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,65 @@ | ||
| package com.example; | ||
|
|
||
| /** | ||
| * Model layer: encapsulates application data and business logic. | ||
| */ | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URL; | ||
| import java.io.BufferedReader; | ||
| import java.io.InputStreamReader; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public class HelloModel { | ||
| /** | ||
| * Returns a greeting based on the current Java and JavaFX versions. | ||
| */ | ||
| public String getGreeting() { | ||
| String javaVersion = System.getProperty("java.version"); | ||
| String javafxVersion = System.getProperty("javafx.version"); | ||
| return "Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "."; | ||
| private final String baseUrl = "https://ntfy.sh"; | ||
| private final String topic = "java25-erika-chat"; | ||
|
|
||
| // Skickar ett textmeddelande till ntfy | ||
| public void sendMessage(String message) { | ||
| try { | ||
| var url = new URL(baseUrl + "/" + topic); | ||
| var conn = (HttpURLConnection) url.openConnection(); | ||
|
|
||
| conn.setRequestMethod("POST"); | ||
| conn.setDoOutput(true); | ||
|
|
||
| conn.getOutputStream().write(message.getBytes()); | ||
|
|
||
| System.out.println("Skickat till " + url + ": " + message); | ||
| conn.disconnect(); | ||
| } catch (Exception e) { | ||
| System.out.println("Något gick fel: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| // Startar en tråd som lyssnar efter nya meddelanden | ||
| public void startListening(Consumer<String> onMessage, Consumer<Throwable> onError) { | ||
| new Thread(() -> { | ||
| try { | ||
| var url = new URL(baseUrl + "/" + topic + "/json"); | ||
| var conn = (HttpURLConnection) url.openConnection(); | ||
| conn.setRequestMethod("GET"); | ||
|
|
||
| var reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | ||
| String line; | ||
|
|
||
| while ((line = reader.readLine()) != null) { | ||
| // Plocka ut texten mellan "message":" och nästa " | ||
| int start = line.indexOf("\"message\":\""); | ||
| if (start != -1) { | ||
| start += 11; | ||
| int end = line.indexOf("\"", start); | ||
| if (end != -1) { | ||
| String msg = line.substring(start, end); | ||
| if (onMessage != null) onMessage.accept(msg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } catch (Throwable ex) { | ||
| if (onError != null) onError.accept(ex); | ||
| } | ||
| }).start(); | ||
| } | ||
|
|
||
| // Visar vilken topic och url som används | ||
| public String info() { | ||
| return baseUrl + "/" + topic; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,30 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <?import javafx.scene.layout.StackPane?> | ||
| <?import javafx.scene.control.Label?> | ||
|
|
||
| <StackPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.HelloController"> | ||
| <children> | ||
| <Label fx:id="messageLabel" text="Hello, JavaFX!" /> | ||
| </children> | ||
| </StackPane> | ||
|
|
||
| <!-- Enkel chattvy med lista, textfält och knapp --> | ||
| <?import javafx.scene.layout.BorderPane?> | ||
| <?import javafx.scene.layout.HBox?> | ||
| <?import javafx.scene.control.*?> | ||
| <?import javafx.geometry.Insets?> | ||
|
|
||
| <BorderPane xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml" | ||
| fx:controller="com.example.HelloController"> | ||
|
|
||
| <!-- Meddelandelista --> | ||
| <center> | ||
| <ListView fx:id="messages"/> | ||
| </center> | ||
|
|
||
| <!-- Textfält och knapp --> | ||
| <bottom> | ||
| <HBox spacing="8" alignment="CENTER_LEFT"> | ||
| <TextField fx:id="inputField" promptText="Skriv ett meddelande..." HBox.hgrow="ALWAYS"/> | ||
| <Button fx:id="sendBtn" text="Skicka" defaultButton="true"/> | ||
| </HBox> | ||
| </bottom> | ||
|
|
||
| <!-- Luft runt hela layouten --> | ||
| <padding> | ||
| <Insets topRightBottomLeft="8"/> | ||
| </padding> | ||
|
|
||
| </BorderPane> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
|
|
||
| public class HelloModelTest { | ||
|
|
||
| @Test | ||
| void info_returnsCorrectUrl() { | ||
| HelloModel model = new HelloModel(); | ||
| assertEquals("https://ntfy.sh/java25-erika-chat", model.info()); | ||
| } | ||
|
|
||
| @Test | ||
| void sendMessage_doesNotThrow() { | ||
| HelloModel model = new HelloModel(); | ||
| assertDoesNotThrow(() -> model.sendMessage("test")); | ||
| } | ||
|
|
||
| @Test | ||
| void startListening_doesNotThrow() { | ||
| HelloModel model = new HelloModel(); | ||
| assertDoesNotThrow(() -> model.startListening(msg -> {}, err -> {})); | ||
| } | ||
|
Comment on lines
+21
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain
Consider either:
I'll verify the review comment by examining the codebase to understand the actual implementation of Length of output: 170 🏁 Script executed: #!/bin/bash
# Search for startListening method definition and usage
rg -n "startListening" --type=java -C 5Length of output: 3648 🏁 Script executed: #!/bin/bash
# Check for test lifecycle methods in HelloModelTest.java
rg -n "@Before|@After|@BeforeEach|@AfterEach" src/test/java/com/example/HelloModelTest.java -C 3Length of output: 0 🏁 Script executed: #!/bin/bash
# Look for thread creation and daemon status
rg -n "new Thread|setDaemon|Thread\(" --type=java -C 4Length of output: 786 🏁 Script executed: #!/bin/bash
# Check HelloModelTest.java structure for any listeners or cleanup
cat src/test/java/com/example/HelloModelTest.javaLength of output: 730 I need to see the complete Length of output: 2241 Fix the thread leak in The issue is confirmed: Recommended fixes:
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Verification inconclusive
Lambda parameter
_is not valid in modern Java versionsOn Line 30,
sendBtn.setOnAction(_ -> sendMessage());uses_as the lambda parameter name. In Java 9+_is a reserved identifier and this will fail to compile. Use a conventional name instead, e.g.:Lambda parameter
_is not valid in modern Java versionsOn Line 30,
sendBtn.setOnAction(_ -> sendMessage());uses_as the lambda parameter name. In Java 9+, it is a compile-time error if a lambda parameter has the name_. This restriction was introduced in Java 9. Use a conventional name instead, e.g.:🤖 Prompt for AI Agents