Report a failed read of the preferences file

Written-by: DeepSeek V4 Pro
Written-by: Claude Fable 5.2
This commit is contained in:
Rsclub2_2 committed 2026-11-18 21:32:10 +01:00
1 parent 3575473fa9
commit 0a43af66df
2 files changed
+64 -1

No files matched your search

@@ -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;
}
@@ -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");
}
}