source: josm/trunk/test/unit/org/openstreetmap/josm/gui/io/CustomConfiguratorTest.java @ 12842

Last change on this file since 12842 was 12842, checked in by bastiK, 13 months ago

see #15229 - fix test deprecations

File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
7
8import java.io.File;
9import java.io.IOException;
10import java.nio.charset.StandardCharsets;
11import java.nio.file.Files;
12import java.util.Arrays;
13import java.util.Collections;
14
15import org.junit.Rule;
16import org.junit.Test;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.data.Preferences;
20import org.openstreetmap.josm.data.PreferencesUtils;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22import org.openstreetmap.josm.tools.Utils;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Unit tests for class {@link CustomConfigurator}.
28 */
29public class CustomConfiguratorTest {
30
31    /**
32     * Setup test.
33     */
34    @Rule
35    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
36    public JOSMTestRules test = new JOSMTestRules().preferences();
37
38    /**
39     * Test method for {@link CustomConfigurator#exportPreferencesKeysToFile}.
40     * @throws IOException if any I/O error occurs
41     */
42    @Test
43    public void testExportPreferencesKeysToFile() throws IOException {
44        File tmp = File.createTempFile("josm.testExportPreferencesKeysToFile.lorem_ipsum", ".xml");
45
46        Main.pref.putList("lorem_ipsum", Arrays.asList(
47                "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
48                "Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.",
49                "Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi.",
50                "Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat.",
51                "Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim.",
52                "Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue.",
53                "Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales.",
54                "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh.",
55                "Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit."));
56        CustomConfigurator.exportPreferencesKeysToFile(tmp.getAbsolutePath(), false, "lorem_ipsum");
57        String xml = Utils.join("\n", Files.readAllLines(tmp.toPath(), StandardCharsets.UTF_8));
58        assertTrue(xml.contains("<preferences operation=\"replace\">"));
59        for (String entry : Main.pref.getList("lorem_ipsum")) {
60            assertTrue(entry + "\nnot found in:\n" + xml, xml.contains(entry));
61        }
62
63        Main.pref.putList("test", Arrays.asList("11111111", "2222222", "333333333"));
64        CustomConfigurator.exportPreferencesKeysByPatternToFile(tmp.getAbsolutePath(), true, "test");
65        xml = Utils.join("\n", Files.readAllLines(tmp.toPath(), StandardCharsets.UTF_8));
66        assertTrue(xml.contains("<preferences operation=\"append\">"));
67        for (String entry : Main.pref.getList("test")) {
68            assertTrue(entry + "\nnot found in:\n" + xml, xml.contains(entry));
69        }
70
71        Utils.deleteFile(tmp);
72    }
73
74    /**
75     * Test method for {@link CustomConfigurator#readXML}.
76     * @throws IOException if any I/O error occurs
77     */
78    @Test
79    public void testReadXML() throws IOException {
80        // Test 1 - read(dir, file) + append
81        Main.pref.putList("test", Collections.<String>emptyList());
82        assertTrue(Main.pref.getList("test").isEmpty());
83        CustomConfigurator.readXML(TestUtils.getTestDataRoot() + "customconfigurator", "append.xml");
84        String log = PreferencesUtils.getLog();
85        assertFalse(log, log.contains("Error"));
86        assertFalse(Main.pref.getList("test").isEmpty());
87
88        // Test 2 - read(file, pref) + replace
89        Preferences pref = new Preferences();
90        // avoid messing up preferences file (that makes all following unit tests fail)
91        pref.enableSaveOnPut(false);
92        pref.putList("lorem_ipsum", Arrays.asList("only 1 string"));
93        assertEquals(1, pref.getList("lorem_ipsum").size());
94        CustomConfigurator.readXML(new File(TestUtils.getTestDataRoot() + "customconfigurator", "replace.xml"), pref);
95        log = PreferencesUtils.getLog();
96        assertFalse(log, log.contains("Error"));
97        assertEquals(9, pref.getList("lorem_ipsum").size());
98    }
99}
Note: See TracBrowser for help on using the repository browser.