-
Notifications
You must be signed in to change notification settings - Fork 66
Labb3 #38
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
Labb3 #38
Changes from all commits
af3e38d
ef85750
9902ef7
ddd7bcc
5306c62
2bd68aa
3ab9ef2
6084942
e876398
41702b6
a194036
a08d470
e12ebf0
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,2 +1,4 @@ | ||
| target/ | ||
| /.idea/ | ||
| .env | ||
| *.iml |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,15 +11,22 @@ | |
| <properties> | ||
| <maven.compiler.release>25</maven.compiler.release> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <junit.jupiter.version>6.0.0</junit.jupiter.version> | ||
| <junit.jupiter.version>5.9.2</junit.jupiter.version> | ||
| <assertj.core.version>3.27.6</assertj.core.version> | ||
| <mockito.version>5.20.0</mockito.version> | ||
| <javafx.version>25</javafx.version> | ||
| <testfx.version>4.0.16-alpha</testfx.version> | ||
| </properties> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <artifactId>junit-jupiter-api</artifactId> | ||
| <version>${junit.jupiter.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter-engine</artifactId> | ||
| <version>${junit.jupiter.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
@@ -35,6 +42,18 @@ | |
| <version>${mockito.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.testfx</groupId> | ||
| <artifactId>testfx-junit5</artifactId> | ||
| <version>${testfx.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.testfx</groupId> | ||
| <artifactId>testfx-core</artifactId> | ||
| <version>${testfx.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openjfx</groupId> | ||
| <artifactId>javafx-controls</artifactId> | ||
|
|
@@ -45,8 +64,45 @@ | |
| <artifactId>javafx-fxml</artifactId> | ||
| <version>${javafx.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.openjfx</groupId> | ||
| <artifactId>javafx-base</artifactId> | ||
| <version>${javafx.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>2.17.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>io.github.cdimascio</groupId> | ||
| <artifactId>dotenv-java</artifactId> | ||
| <version>3.1.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.jetbrains</groupId> | ||
| <artifactId>annotations</artifactId> | ||
| <version>24.0.1</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.testng</groupId> | ||
| <artifactId>testng</artifactId> | ||
| <version>RELEASE</version> | ||
| <scope>compile</scope> | ||
| </dependency> | ||
|
Comment on lines
+87
to
+92
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. Fix TestNG dependency configuration. There are two critical issues:
Apply this diff: <dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
- <version>RELEASE</version>
- <scope>compile</scope>
+ <version>7.10.2</version>
+ <scope>test</scope>
</dependency>Additionally, verify if you actually need both JUnit Jupiter and TestNG in the same project, as this is unusual. 🤖 Prompt for AI Agents |
||
| </dependencies> | ||
| <build> | ||
| <resources> | ||
| <resource> | ||
| <directory>src/main/resources</directory> | ||
| <includes> | ||
| <include>**/*.fxml</include> | ||
| <include>**/*.css</include> | ||
| <include>**/*.jpg</include> | ||
| <include>**/*.png</include> | ||
| </includes> | ||
| </resource> | ||
| </resources> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.openjfx</groupId> | ||
|
|
@@ -63,6 +119,17 @@ | |
| <noManPages>true</noManPages> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <version>3.2.5</version> | ||
| <configuration> | ||
| <includes> | ||
| <include>**/*Test.java</include> | ||
| <include>**/*IT.java</include> | ||
| </includes> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.example; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public interface ChatNetworkClient { | ||
| void send(String baseUrl, NtfyMessage message) throws Exception; | ||
|
|
||
| //Returnerar mitt egna Subscription-interface | ||
| Subscription subscribe(String baseUrl, String topic, Consumer<NtfyMessage> messageHandler); | ||
|
|
||
| //Inner interface för Subscription | ||
| interface Subscription extends Closeable { | ||
| @Override | ||
| void close() throws IOException; | ||
| boolean isOpen(); | ||
| } | ||
|
Comment on lines
+7
to
+18
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 chainResolve Subscription type duplication and align the API You now have an inner I recommend consolidating to a single
Also consider narrowing 🏁 Script executed: #!/bin/bash
# Quick check of Subscription usages and potential conflicts
rg -n "Subscription" src/main/java/com/example -C2Length of output: 4355 Resolve critical Subscription type mismatch Verification confirms a type conflict:
This creates a type mismatch: Action required: Choose one
Also narrow 🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,155 @@ | ||||||||||||||||||||||||||||||||||||||
| package com.example; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import javafx.beans.binding.Bindings; | ||||||||||||||||||||||||||||||||||||||
| import javafx.fxml.FXML; | ||||||||||||||||||||||||||||||||||||||
| import javafx.scene.control.Label; | ||||||||||||||||||||||||||||||||||||||
| import javafx.scene.control.ListView; | ||||||||||||||||||||||||||||||||||||||
| import javafx.scene.control.TextField; | ||||||||||||||||||||||||||||||||||||||
| import javafx.scene.control.Button; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||
| import java.util.UUID; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||
| * Controller layer: mediates between the view (FXML) and the model. | ||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||
| public class HelloController { | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private final HelloModel model = new HelloModel(); | ||||||||||||||||||||||||||||||||||||||
| private final HelloModel model; | ||||||||||||||||||||||||||||||||||||||
| private final ChatNetworkClient httpClient; // Abstraktion (Dependency Inversion) | ||||||||||||||||||||||||||||||||||||||
| private final String hostName; | ||||||||||||||||||||||||||||||||||||||
| private ChatNetworkClient.Subscription subscription; | ||||||||||||||||||||||||||||||||||||||
| private long subscriptionStartTime; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+18
to
+22
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. Address thread safety concerns for subscription field. The Consider making the field - private ChatNetworkClient.Subscription subscription;
+ private volatile ChatNetworkClient.Subscription subscription;Additionally, the - private long subscriptionStartTime;
+ private volatile long subscriptionStartTime;🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @FXML | ||||||||||||||||||||||||||||||||||||||
| private Label messageLabel; | ||||||||||||||||||||||||||||||||||||||
| @FXML private Label messageLabel; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| //Nya FXML-komponenter: | ||||||||||||||||||||||||||||||||||||||
| @FXML private ListView<NtfyMessage> messageListView; //Visar meddelanden från HelloModel | ||||||||||||||||||||||||||||||||||||||
| @FXML private TextField messageTextField; //Används för att skriva nya meddelanden | ||||||||||||||||||||||||||||||||||||||
| @FXML private Button sendButton; //Används för att skicka meddelanden | ||||||||||||||||||||||||||||||||||||||
| @FXML private Label connectionStatusLabel; // Status för anslutning | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| //Konstruktor för Dependency Injection | ||||||||||||||||||||||||||||||||||||||
| public HelloController(HelloModel model, ChatNetworkClient httpClient, String hostName) { | ||||||||||||||||||||||||||||||||||||||
| this.model = model; | ||||||||||||||||||||||||||||||||||||||
| this.httpClient = httpClient; | ||||||||||||||||||||||||||||||||||||||
| this.hostName = hostName; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @FXML | ||||||||||||||||||||||||||||||||||||||
| private void initialize() { | ||||||||||||||||||||||||||||||||||||||
| if (messageLabel != null) { | ||||||||||||||||||||||||||||||||||||||
| messageLabel.setText(model.getGreeting()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| //Binder listView till meddelandelistan i modellen | ||||||||||||||||||||||||||||||||||||||
| messageListView.setItems(model.getMessages()); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| //Binder anslutningsstatus till etiketten | ||||||||||||||||||||||||||||||||||||||
| connectionStatusLabel.textProperty().bind( | ||||||||||||||||||||||||||||||||||||||
| Bindings.when(model.connectedProperty()) | ||||||||||||||||||||||||||||||||||||||
| .then("Ansluten: ja") | ||||||||||||||||||||||||||||||||||||||
| .otherwise("Ansluten: nej") | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @FXML | ||||||||||||||||||||||||||||||||||||||
| private void sendMessage() { | ||||||||||||||||||||||||||||||||||||||
| String messageText = messageTextField.getText(); | ||||||||||||||||||||||||||||||||||||||
| if (!messageText.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| NtfyMessage message = createMessage(messageText); | ||||||||||||||||||||||||||||||||||||||
| httpClient.send(hostName, message); | ||||||||||||||||||||||||||||||||||||||
| messageTextField.clear(); | ||||||||||||||||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||||||||||||||||
| handleSendError(e); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private NtfyMessage createMessage(String text) { | ||||||||||||||||||||||||||||||||||||||
| return new NtfyMessage( | ||||||||||||||||||||||||||||||||||||||
| UUID.randomUUID().toString(), | ||||||||||||||||||||||||||||||||||||||
| System.currentTimeMillis(), | ||||||||||||||||||||||||||||||||||||||
| "message", | ||||||||||||||||||||||||||||||||||||||
| "mytopic", | ||||||||||||||||||||||||||||||||||||||
| text | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+69
to
+77
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. Fix time field to use seconds. The Apply this diff: private NtfyMessage createMessage(String text) {
return new NtfyMessage(
UUID.randomUUID().toString(),
- System.currentTimeMillis(),
+ System.currentTimeMillis() / 1000,
"message",
"mytopic",
text
);
}This matches the time comparison logic on line 117 which divides by 1000. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private void handleSendError(Exception e) { | ||||||||||||||||||||||||||||||||||||||
| System.err.println("Fel vid sändning: " + e.getMessage()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @FXML | ||||||||||||||||||||||||||||||||||||||
| private void subscribeToTopic() { | ||||||||||||||||||||||||||||||||||||||
| unsubscribeFromCurrentTopic(); // Stäng den gamla prenumerationen | ||||||||||||||||||||||||||||||||||||||
| model.getMessages().clear(); // Rensa gamla meddelanden | ||||||||||||||||||||||||||||||||||||||
| startNewSubscription(); // Starta ny prenumeration | ||||||||||||||||||||||||||||||||||||||
| model.setConnected(true); // Uppdatera anslutningsstatus | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+84
to
+90
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. 🛠️ Refactor suggestion | 🟠 Major Make topic name configurable. The topic "mytopic" is hard-coded in multiple places (lines 74, 106). Consider making it configurable, either through a TextField in the UI or as a constructor parameter. For example, add a TextField to the FXML: <TextField fx:id="topicTextField" promptText="Enter topic name..." text="mytopic" />Then use it in the controller: private void startNewSubscription() {
String topic = topicTextField.getText();
subscriptionStartTime = System.currentTimeMillis();
subscription = httpClient.subscribe(hostName, topic, this::handleIncomingMessage);
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private void unsubscribeFromCurrentTopic() { | ||||||||||||||||||||||||||||||||||||||
| if (subscription != null) { | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| subscription.close(); | ||||||||||||||||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||||||||||||||||
| System.err.println("Kunde inte stänga gammal prenumeration: " + e.getMessage()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private void startNewSubscription() { | ||||||||||||||||||||||||||||||||||||||
| subscriptionStartTime = System.currentTimeMillis(); // Spara starttiden | ||||||||||||||||||||||||||||||||||||||
| subscription = httpClient.subscribe( | ||||||||||||||||||||||||||||||||||||||
| hostName, | ||||||||||||||||||||||||||||||||||||||
| "mytopic", | ||||||||||||||||||||||||||||||||||||||
| this::handleIncomingMessage | ||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private void handleIncomingMessage(NtfyMessage message) { | ||||||||||||||||||||||||||||||||||||||
| System.out.println("Mottaget meddelande: " + message.id() + ", tid: " + message.time()); | ||||||||||||||||||||||||||||||||||||||
| long messageTimeSeconds = message.time(); | ||||||||||||||||||||||||||||||||||||||
| long currentTimeSeconds = System.currentTimeMillis() / 1000; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Hoppa över gamla meddelanden | ||||||||||||||||||||||||||||||||||||||
| if (messageTimeSeconds < (subscriptionStartTime / 1000)) { | ||||||||||||||||||||||||||||||||||||||
| System.out.println("Hoppar över (gammalt meddelande)."); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Hoppa över dubbletter | ||||||||||||||||||||||||||||||||||||||
| if (isDuplicateMessage(message)) { | ||||||||||||||||||||||||||||||||||||||
| System.out.println("Hoppar över (dublett)."); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // Lägg till meddelandet i modellen | ||||||||||||||||||||||||||||||||||||||
| System.out.println("Lägger till i modellen: " + message.message()); | ||||||||||||||||||||||||||||||||||||||
| model.addMessage(message); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private boolean isDuplicateMessage(NtfyMessage message) { | ||||||||||||||||||||||||||||||||||||||
| return model.getMessages().stream() | ||||||||||||||||||||||||||||||||||||||
| .anyMatch(existingMessage -> existingMessage.id().equals(message.id())); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| @FXML | ||||||||||||||||||||||||||||||||||||||
| private void unsubscribeFromTopic() { | ||||||||||||||||||||||||||||||||||||||
| if (subscription != null) { | ||||||||||||||||||||||||||||||||||||||
| closeSubscription(); | ||||||||||||||||||||||||||||||||||||||
| model.setConnected(false); | ||||||||||||||||||||||||||||||||||||||
| subscription = null; // Rensa referensen | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| private void closeSubscription() { | ||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||
| subscription.close(); | ||||||||||||||||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||||||||||||||||
| System.err.println("Fel vid avprenumeration: " + e.getMessage()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,48 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package com.example; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import io.github.cdimascio.dotenv.Dotenv; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javafx.application.Application; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javafx.fxml.FXMLLoader; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javafx.scene.Parent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javafx.scene.Scene; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javafx.stage.Stage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class HelloFX extends Application { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void start(Stage stage) throws Exception { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FXMLLoader fxmlLoader = new FXMLLoader(HelloFX.class.getResource("hello-view.fxml")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dotenv dotenv = Dotenv.load(); //Ladda .env-filen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String hostName = dotenv.get("HOST_NAME"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //Validering (valfritt) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (hostName == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalStateException( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Kunde inte läsa HOST_NAME från .env-filen. " + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "Kontrollera att filen finns i projektets rotmapp!" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Ansluter till ntfy på: " + hostName); //Debug-logg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+28
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. 🛠️ Refactor suggestion | 🟠 Major Add error handling for .env file loading. The code validates Apply this diff to add proper error handling: - Dotenv dotenv = Dotenv.load(); //Ladda .env-filen
+ Dotenv dotenv = null;
+ try {
+ dotenv = Dotenv.load();
+ } catch (Exception e) {
+ throw new IllegalStateException(
+ "Could not load .env file. Ensure it exists in the project root: " + e.getMessage()
+ );
+ }
String hostName = dotenv.get("HOST_NAME");
- //Validering (valfritt)
if (hostName == null) {
throw new IllegalStateException(
- "Kunde inte läsa HOST_NAME från .env-filen. " +
- "Kontrollera att filen finns i projektets rotmapp!"
+ "Could not read HOST_NAME from .env file. " +
+ "Ensure the variable is defined in the .env file!"
);
}
- System.out.println("Ansluter till ntfy på: " + hostName); //Debug-logg
+ System.out.println("Connecting to ntfy at: " + hostName);Note: I've also translated the Swedish comments to English for consistency. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HelloModel model = new HelloModel(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ChatNetworkClient httpClient = new NtfyHttpClient(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| FXMLLoader fxmlLoader = new FXMLLoader( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HelloFX.class.getResource("/com/example/hello-view.fxml") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fxmlLoader.setControllerFactory(c -> new HelloController(model, httpClient, hostName)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parent root = fxmlLoader.load(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Scene scene = new Scene(root, 640, 480); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stage.setTitle("Hello MVC"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scene.getStylesheets().add( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Objects.requireNonNull(getClass().getResource("/com/example/styles.css")).toExternalForm() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stage.setTitle("Chat App"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stage.setScene(scene); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| stage.show(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🧩 Analysis chain
Consider using a stable TestFX version.
The version
4.0.16-alphais an alpha release, which may contain bugs or instability.🌐 Web query:
💡 Result:
The latest stable TestFX release is 4.0.18. [1][2]
Sources:
[1] TestFX GitHub README (shows 4.0.18).
[2] org.testfx listings / javadoc.io (latest versions: testfx-core/testfx-junit/testfx-junit5 = 4.0.18).
Update TestFX to the latest stable version 4.0.18.
The version
4.0.16-alphais an alpha release and a newer stable version (4.0.18) is available. Updatepom.xmlline 18 to use4.0.18instead.🤖 Prompt for AI Agents