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