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

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

see #15182 - checkstyle

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