Changeset 19719 in josm


Ignore:
Timestamp:
2026-11-29T13:15:13+01:00 (less than one hour ago)
Author:
stoecker
Message:

fix #25147 - Default preferences cache is never saved - patch by wangi

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r19707 r19719  
    388388        // Backup old preferences if there are old preferences
    389389        if (initSuccessful && prefFile.exists() && prefFile.length() > 0) {
    390             checkFileValidity(prefFile, f -> Utils.copyFile(f, backupFile));
     390            checkFileValidity(prefFile, defaults, f -> Utils.copyFile(f, backupFile));
    391391        }
    392392
    … …  
    400400        File tmpFile = new File(prefFile + "_tmp");
    401401        // Only replace the pref file if the _tmp file is valid
    402         checkFileValidity(tmpFile, f -> Files.move(f.toPath(), prefFile.toPath(), StandardCopyOption.REPLACE_EXISTING));
     402        checkFileValidity(tmpFile, defaults, f -> Files.move(f.toPath(), prefFile.toPath(), StandardCopyOption.REPLACE_EXISTING));
    403403
    404404        setCorrectPermissions(prefFile);
    … …  
    409409     * Ensure that a preferences file is "ok" before copying/moving it over another preferences file
    410410     * @param file The file to check
     411     * @param defaults {@code true} if the file is the cache file for default preferences
    411412     * @param consumer The consumer that will perform the copy/move action
    412413     * @throws IOException If there is an issue reading/writing the file
    413414     */
    414     private static void checkFileValidity(File file, ThrowingConsumer<File, IOException> consumer) throws IOException {
     415    private static void checkFileValidity(File file, boolean defaults, ThrowingConsumer<File, IOException> consumer) throws IOException {
    415416        try {
    416417            // But don't back up if the current preferences are invalid.
    417418            // The validations are expensive (~2/3 CPU, ~1/3 memory), but this isn't a "hot" method
    418419            PreferencesReader.validateXML(file);
    419             PreferencesReader reader = new PreferencesReader(file, false);
     420            PreferencesReader reader = new PreferencesReader(file, defaults);
    420421            reader.parse();
    421422            consumer.accept(file);
  • trunk/test/unit/org/openstreetmap/josm/data/PreferencesTest.java

    r19707 r19719  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertFalse;
    56import static org.junit.jupiter.api.Assertions.assertNull;
     7import static org.junit.jupiter.api.Assertions.assertTrue;
    68
    79import java.io.File;
    … …  
    5658            "  <tag key='osm-server.url' value='http://fake.xxx/api'/>%n" +
    5759            "</preferences>%n").getBytes(StandardCharsets.UTF_8));
     60        final Preferences preferences = new Preferences(directories(dir));
     61        preferences.load();
     62
     63        assertNull(preferences.get("oauth.access-token.key", null));
     64        assertNull(preferences.get("oauth.access-token.secret", null));
     65        assertEquals("http://fake.xxx/api", preferences.get("osm-server.url", null));
     66    }
     67
     68    /**
     69     * The cache file for default preferences is saved, and a valid one is backed up before it is replaced.
     70     * <p>
     71     * Non-regression test: since r19179 (#23802) the saved file was validated as a normal preferences file, which
     72     * failed on its {@code preferences-defaults} root element, so it was left as {@code default_preferences.xml_tmp}.
     73     * @param dir a temporary directory for the preferences files
     74     * @throws IOException if the preferences file cannot be written or read
     75     * @throws SAXException if the preferences file is not valid
     76     * @throws XMLStreamException if the preferences file cannot be parsed
     77     */
     78    @Test
     79    void testSaveDefaults(@TempDir Path dir) throws IOException, SAXException, XMLStreamException {
     80        final Preferences preferences = new Preferences(directories(dir));
     81        preferences.initSuccessful = true;
     82        assertEquals("a default", preferences.get("test.default", "a default"));
     83        preferences.saveDefaults();
     84
     85        final File defaults = preferences.getDefaultsCacheFile();
     86        assertTrue(defaults.isFile(), "the defaults cache file was not saved");
     87        assertFalse(new File(defaults + "_tmp").exists(), "the defaults cache file was left as temporary file");
     88
     89        final Preferences reloaded = new Preferences(directories(dir));
     90        reloaded.loadDefaults();
     91        assertEquals("a default", reloaded.getAllDefaults().get("test.default").getValue());
     92
     93        // the next save backs up the valid file
     94        preferences.saveDefaults();
     95        assertTrue(new File(defaults + "_backup").isFile(), "the defaults cache file was not backed up");
     96    }
     97
     98    private static IBaseDirectories directories(Path dir) {
    5899        final File directory = dir.toFile();
    59         final Preferences preferences = new Preferences(new IBaseDirectories() {
     100        return new IBaseDirectories() {
    60101            @Override
    61102            public File getPreferencesDirectory(boolean createIfMissing) {
    … …  
    72113                return directory;
    73114            }
    74         });
    75         preferences.load();
    76 
    77         assertNull(preferences.get("oauth.access-token.key", null));
    78         assertNull(preferences.get("oauth.access-token.secret", null));
    79         assertEquals("http://fake.xxx/api", preferences.get("osm-server.url", null));
     115        };
    80116    }
    81117}
Note: See TracChangeset for help on using the changeset viewer.