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

Last change on this file since 12634 was 12634, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.worker, replace it by gui.MainApplication.worker + code refactoring to make sure only editor packages use it

File size: 5.7 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.Before;
16import org.junit.Rule;
17import org.junit.Test;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.TestUtils;
20import org.openstreetmap.josm.data.Preferences;
21import org.openstreetmap.josm.data.PreferencesUtils;
22import org.openstreetmap.josm.gui.io.CustomConfigurator;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24import org.openstreetmap.josm.tools.Utils;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27import net.trajano.commons.testing.UtilityClassTestUtil;
28
29/**
30 * Unit tests for class {@link CustomConfigurator}.
31 */
32public class CustomConfiguratorTest {
33
34 /**
35 * Setup test.
36 */
37 @Rule
38 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
39 public JOSMTestRules test = new JOSMTestRules().preferences();
40
41 /**
42 * Setup test.
43 */
44 @Before
45 public void setUp() {
46 CustomConfigurator.resetLog();
47 }
48
49 /**
50 * Test method for {@link CustomConfigurator#log}.
51 */
52 @Test
53 public void testLog() {
54 assertEquals("", CustomConfigurator.getLog());
55 CustomConfigurator.log("test");
56 assertEquals("test\n", CustomConfigurator.getLog());
57 CustomConfigurator.log("%d\n", 100);
58 assertEquals("test\n100\n", CustomConfigurator.getLog());
59 CustomConfigurator.log("test");
60 assertEquals("test\n100\ntest\n", CustomConfigurator.getLog());
61 }
62
63 /**
64 * Test method for {@link CustomConfigurator#exportPreferencesKeysToFile}.
65 * @throws IOException if any I/O error occurs
66 */
67 @Test
68 public void testExportPreferencesKeysToFile() throws IOException {
69 File tmp = File.createTempFile("josm.testExportPreferencesKeysToFile.lorem_ipsum", ".xml");
70
71 Main.pref.putCollection("lorem_ipsum", Arrays.asList(
72 "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
73 "Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor.",
74 "Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi.",
75 "Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat.",
76 "Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim.",
77 "Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue.",
78 "Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales.",
79 "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh.",
80 "Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit."));
81 CustomConfigurator.exportPreferencesKeysToFile(tmp.getAbsolutePath(), false, "lorem_ipsum");
82 String xml = Utils.join("\n", Files.readAllLines(tmp.toPath(), StandardCharsets.UTF_8));
83 assertTrue(xml.contains("<preferences operation=\"replace\">"));
84 for (String entry : Main.pref.getCollection("lorem_ipsum")) {
85 assertTrue(entry + "\nnot found in:\n" + xml, xml.contains(entry));
86 }
87
88 Main.pref.putCollection("test", Arrays.asList("11111111", "2222222", "333333333"));
89 CustomConfigurator.exportPreferencesKeysByPatternToFile(tmp.getAbsolutePath(), true, "test");
90 xml = Utils.join("\n", Files.readAllLines(tmp.toPath(), StandardCharsets.UTF_8));
91 assertTrue(xml.contains("<preferences operation=\"append\">"));
92 for (String entry : Main.pref.getCollection("test")) {
93 assertTrue(entry + "\nnot found in:\n" + xml, xml.contains(entry));
94 }
95
96 Utils.deleteFile(tmp);
97 }
98
99 /**
100 * Test method for {@link CustomConfigurator#readXML}.
101 * @throws IOException if any I/O error occurs
102 */
103 @Test
104 public void testReadXML() throws IOException {
105 // Test 1 - read(dir, file) + append
106 Main.pref.putCollection("test", Collections.<String>emptyList());
107 assertTrue(Main.pref.getCollection("test").isEmpty());
108 CustomConfigurator.readXML(TestUtils.getTestDataRoot() + "customconfigurator", "append.xml");
109 String log = CustomConfigurator.getLog();
110 assertFalse(log, log.contains("Error"));
111 assertFalse(Main.pref.getCollection("test").isEmpty());
112
113 // Test 2 - read(file, pref) + replace
114 Preferences pref = new Preferences();
115 // avoid messing up preferences file (that makes all following unit tests fail)
116 pref.enableSaveOnPut(false);
117 pref.putCollection("lorem_ipsum", Arrays.asList("only 1 string"));
118 assertEquals(1, pref.getCollection("lorem_ipsum").size());
119 CustomConfigurator.readXML(new File(TestUtils.getTestDataRoot() + "customconfigurator", "replace.xml"), pref);
120 log = CustomConfigurator.getLog();
121 assertFalse(log, log.contains("Error"));
122 assertEquals(9, pref.getCollection("lorem_ipsum").size());
123 }
124
125 /**
126 * Tests that {@code PreferencesUtils} satisfies utility class criterias.
127 * @throws ReflectiveOperationException if an error occurs
128 */
129 @Test
130 public void testUtilityClass() throws ReflectiveOperationException {
131 UtilityClassTestUtil.assertUtilityClassWellDefined(PreferencesUtils.class);
132 }
133}
Note: See TracBrowser for help on using the repository browser.