source: josm/trunk/test/unit/org/openstreetmap/josm/data/preferences/PreferencesWriterTest.java@ 17360

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.io.IOException;
7import java.io.PrintWriter;
8import java.io.StringWriter;
9import java.util.Arrays;
10import java.util.HashMap;
11import java.util.Map;
12import java.util.SortedMap;
13import java.util.TreeMap;
14
15import org.junit.jupiter.api.Test;
16import org.openstreetmap.josm.data.Version;
17import org.openstreetmap.josm.spi.preferences.Setting;
18import org.openstreetmap.josm.spi.preferences.ListListSetting;
19import org.openstreetmap.josm.spi.preferences.ListSetting;
20import org.openstreetmap.josm.spi.preferences.AbstractSetting;
21import org.openstreetmap.josm.spi.preferences.StringSetting;
22import org.openstreetmap.josm.spi.preferences.MapListSetting;
23
24/**
25 * Unit tests for class {@link PreferencesWriter}.
26 */
27class PreferencesWriterTest {
28
29 private static <T extends AbstractSetting<?>> T setting(T s, long time) {
30 s.setTime(time);
31 return s;
32 }
33
34 /**
35 * Unit test of {@link PreferencesWriter#visit(ListListSetting)}.
36 * @throws IOException if any I/O error occurs
37 */
38 @Test
39 void testListList() throws IOException {
40 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
41 long time = System.currentTimeMillis() / 1000;
42 w.visit(setting(new ListListSetting(Arrays.asList(Arrays.asList("bar"))), time));
43 assertEquals(String.format(
44 " <lists key='null' time='%d'>%n" +
45 " <list>%n" +
46 " <entry value='bar'/>%n" +
47 " </list>%n" +
48 " </lists>%n", time),
49 out.toString());
50 }
51 }
52
53 /**
54 * Unit test of {@link PreferencesWriter#visit(ListSetting)}.
55 * @throws IOException if any I/O error occurs
56 */
57 @Test
58 void testList() throws IOException {
59 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
60 long time = System.currentTimeMillis() / 1000;
61 w.visit(setting(new ListSetting(Arrays.asList("bar")), time));
62 assertEquals(String.format(
63 " <list key='null' time='%d'>%n" +
64 " <entry value='bar'/>%n" +
65 " </list>%n", time),
66 out.toString());
67 }
68 }
69
70 /**
71 * Unit test of {@link PreferencesWriter#visit(MapListSetting)}.
72 * @throws IOException if any I/O error occurs
73 */
74 @Test
75 void testMapList() throws IOException {
76 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
77 long time = System.currentTimeMillis() / 1000;
78 Map<String, String> map = new HashMap<>();
79 map.put("foo", "bar");
80 w.visit(setting(new MapListSetting(Arrays.asList(map)), time));
81 assertEquals(String.format(
82 " <maps key='null' time='%d'>%n" +
83 " <map>%n" +
84 " <tag key='foo' value='bar'/>%n" +
85 " </map>%n" +
86 " </maps>%n", time),
87 out.toString());
88 }
89 }
90
91 /**
92 * Unit test of {@link PreferencesWriter#visit(StringSetting)}.
93 * @throws IOException if any I/O error occurs
94 */
95 @Test
96 void testString() throws IOException {
97 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
98 long time = System.currentTimeMillis() / 1000;
99 w.visit(setting(new StringSetting("bar"), time));
100 assertEquals(String.format(
101 " <tag key='null' time='%d' value='bar'/>%n", time),
102 out.toString());
103 }
104 }
105
106 /**
107 * Unit test of {@link PreferencesWriter#write(java.util.Collection)}.
108 * @throws IOException if any I/O error occurs
109 */
110 @Test
111 void testWrite() throws IOException {
112 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
113 long time = System.currentTimeMillis() / 1000;
114 Map<String, Setting<?>> map = new HashMap<>();
115 map.put("foo", setting(new StringSetting("bar"), time));
116 w.write(map.entrySet());
117 assertEquals(String.format(
118 // CHECKSTYLE.OFF: LineLength
119 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
120 "<preferences-defaults xmlns='http://josm.openstreetmap.de/preferences-1.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' version='%d'>%n" +
121 " <tag key='foo' time='%d' value='bar'/>%n" +
122 "</preferences-defaults>%n",
123 // CHECKSTYLE.ON: LineLength
124 Version.getInstance().getVersion(), time),
125 out.toString());
126 }
127 }
128
129 /**
130 * Test null value in default preferences.
131 * @throws IOException if any I/O error occurs
132 */
133 @Test
134 void testNullValue() throws IOException {
135 long time = System.currentTimeMillis() / 1000;
136 // CHECKSTYLE.OFF: LineLength
137 String expected = String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n" +
138 "<preferences-defaults xmlns='http://josm.openstreetmap.de/preferences-1.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' version='%d'>%n" +
139 " <list key='foo_list' time='%2$d' xsi:nil='true'/>%n" +
140 " <lists key='foo_listlist' time='%2$d' xsi:nil='true'/>%n" +
141 " <maps key='foo_maplist' time='%2$d' xsi:nil='true'/>%n" +
142 " <tag key='foo_tag' time='%2$d' xsi:nil='true'/>%n" +
143 "</preferences-defaults>%n",
144 Version.getInstance().getVersion(), time);
145 // CHECKSTYLE.ON: LineLength
146 try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
147 SortedMap<String, Setting<?>> map = new TreeMap<>();
148 map.put("foo_tag", setting(new StringSetting(null), time));
149 map.put("foo_list", setting(new ListSetting(null), time));
150 map.put("foo_listlist", setting(new ListListSetting(null), time));
151 map.put("foo_maplist", setting(new MapListSetting(null), time));
152 w.write(map.entrySet());
153 assertEquals(expected, out.toString());
154 }
155 }
156}
Note: See TracBrowser for help on using the repository browser.