source: josm/trunk/test/unit/org/openstreetmap/josm/data/CustomConfiguratorTest.java@ 12069

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

improve unit test coverage of utilities classes thanks to https://trajano.github.io/commons-testing

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