From 0a43af66dfacaa844d9ad7ec231dc7305c4e1bd9 Mon Sep 17 00:00:00 2001 From: Philipp Wagner Date: Wed, 18 Nov 2026 19:29:39 +0100 Subject: [PATCH] Report a failed read of the preferences file Written-by: DeepSeek V4 Pro Written-by: Claude Fable 5.2 --- .../kst4contest/model/ChatPreferences.java | 5 +- .../model/ChatPreferencesReadResultTest.java | 60 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java diff --git a/core/src/main/java/kst4contest/model/ChatPreferences.java b/core/src/main/java/kst4contest/model/ChatPreferences.java index 197993bd..4bd4ba12 100644 --- a/core/src/main/java/kst4contest/model/ChatPreferences.java +++ b/core/src/main/java/kst4contest/model/ChatPreferences.java @@ -2601,6 +2601,8 @@ public class ChatPreferences { */ public boolean readPreferencesFromXmlFile() { + boolean readable = true; + System.out.println("[ChatPreferences, Info]: restoring prefs from file " + storeAndRestorePreferencesFileName); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); @@ -3457,13 +3459,14 @@ public class ChatPreferences { } catch (ParserConfigurationException | SAXException | IOException e) { + readable = false; e.printStackTrace(); System.out.println(e.getCause()); System.out.println(e.getMessage()); } initializeActualQtfFromDefaultIfRequired(); - return true; + return readable; } diff --git a/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java b/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java new file mode 100644 index 00000000..6f1e484b --- /dev/null +++ b/core/src/test/java/kst4contest/model/ChatPreferencesReadResultTest.java @@ -0,0 +1,60 @@ +package kst4contest.model; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Pins the return value of {@link ChatPreferences#readPreferencesFromXmlFile()}: it must + * report a failed read as {@code false} so callers such as the setup wizard launcher can + * tell an unreadable profile file from a readable one. + */ +class ChatPreferencesReadResultTest { + + @TempDir + Path temporaryDirectory; + + @Test + void aFileThatIsNotXmlIsReportedAsUnreadable() throws IOException { + Path file = temporaryDirectory.resolve("notxml.xml"); + byte[] original = "this is not xml <<<".getBytes(StandardCharsets.UTF_8); + Files.write(file, original); + + ChatPreferences preferences = new ChatPreferences(file); + assertFalse(preferences.readPreferencesFromXmlFile(), "a non-XML file must be reported as unreadable"); + assertArrayEquals(original, Files.readAllBytes(file), "the unreadable file must be left untouched"); + } + + @Test + void aMissingFileIsReportedAsUnreadable() { + Path file = temporaryDirectory.resolve("missing.xml"); + + ChatPreferences preferences = new ChatPreferences(file); + assertFalse(preferences.readPreferencesFromXmlFile(), "a missing file must be reported as unreadable"); + assertFalse(Files.exists(file), "reading a missing file must not create it"); + } + + @Test + void theBundledTemplateIsReadable() throws IOException { + Path templateCopy = temporaryDirectory.resolve("template.xml"); + try (InputStream template = ChatPreferences.class + .getResourceAsStream(ChatPreferences.PREFERENCE_RESOURCE)) { + assertNotNull(template, "bundled resource " + ChatPreferences.PREFERENCE_RESOURCE); + Files.copy(template, templateCopy, StandardCopyOption.REPLACE_EXISTING); + } + + ChatPreferences fromTemplate = new ChatPreferences(templateCopy); + assertTrue(fromTemplate.readPreferencesFromXmlFile(), "the bundled template must be readable"); + } +}