1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.io.StringWriter;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.data.Version;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Unit tests for class {@link PreferencesWriter}.
|
---|
18 | */
|
---|
19 | public class PreferencesWriterTest {
|
---|
20 |
|
---|
21 | private static <T extends AbstractSetting<?>> T setting(T s, long time) {
|
---|
22 | s.setTime(time);
|
---|
23 | return s;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit test of {@link PreferencesWriter#visit(ListListSetting)}.
|
---|
28 | * @throws IOException if any I/O error occurs
|
---|
29 | */
|
---|
30 | @Test
|
---|
31 | public void testListList() throws IOException {
|
---|
32 | try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
|
---|
33 | long time = System.currentTimeMillis() / 1000;
|
---|
34 | w.visit(setting(new ListListSetting(Arrays.asList(Arrays.asList("bar"))), time));
|
---|
35 | assertEquals(String.format(
|
---|
36 | " <lists key='null' time='%d'>%n <list>%n <entry value='bar'/>%n </list>%n </lists>%n", time),
|
---|
37 | out.toString());
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Unit test of {@link PreferencesWriter#visit(ListSetting)}.
|
---|
43 | * @throws IOException if any I/O error occurs
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | public void testList() throws IOException {
|
---|
47 | try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
|
---|
48 | long time = System.currentTimeMillis() / 1000;
|
---|
49 | w.visit(setting(new ListSetting(Arrays.asList("bar")), time));
|
---|
50 | assertEquals(String.format(
|
---|
51 | " <list key='null' time='%d'>%n <entry value='bar'/>%n </list>%n", time),
|
---|
52 | out.toString());
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Unit test of {@link PreferencesWriter#visit(MapListSetting)}.
|
---|
58 | * @throws IOException if any I/O error occurs
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void testMapList() throws IOException {
|
---|
62 | try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
|
---|
63 | long time = System.currentTimeMillis() / 1000;
|
---|
64 | Map<String, String> map = new HashMap<>();
|
---|
65 | map.put("foo", "bar");
|
---|
66 | w.visit(setting(new MapListSetting(Arrays.asList(map)), time));
|
---|
67 | assertEquals(String.format(
|
---|
68 | " <maps key='null' time='%d'>%n <map>%n <tag key='foo' value='bar'/>%n </map>%n </maps>%n", time),
|
---|
69 | out.toString());
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Unit test of {@link PreferencesWriter#visit(StringSetting)}.
|
---|
75 | * @throws IOException if any I/O error occurs
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | public void testString() throws IOException {
|
---|
79 | try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
|
---|
80 | long time = System.currentTimeMillis() / 1000;
|
---|
81 | w.visit(setting(new StringSetting("bar"), time));
|
---|
82 | assertEquals(String.format(
|
---|
83 | " <tag key='null' time='%d' value='bar'/>%n", time),
|
---|
84 | out.toString());
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Unit test of {@link PreferencesWriter#write(java.util.Collection)}.
|
---|
90 | * @throws IOException if any I/O error occurs
|
---|
91 | */
|
---|
92 | @Test
|
---|
93 | public void testWrite() throws IOException {
|
---|
94 | try (StringWriter out = new StringWriter(); PreferencesWriter w = new PreferencesWriter(new PrintWriter(out), true, true)) {
|
---|
95 | long time = System.currentTimeMillis() / 1000;
|
---|
96 | Map<String, Setting<?>> map = new HashMap<>();
|
---|
97 | map.put("foo", setting(new StringSetting("bar"), time));
|
---|
98 | w.write(map.entrySet());
|
---|
99 | assertEquals(String.format(
|
---|
100 | // CHECKSTYLE.OFF: LineLength
|
---|
101 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>%n<preferences-defaults xmlns='http://josm.openstreetmap.de/preferences-1.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' version='%d'>%n <tag key='foo' time='%d' value='bar'/>%n</preferences-defaults>%n",
|
---|
102 | // CHECKSTYLE.ON: LineLength
|
---|
103 | Version.getInstance().getVersion(), time),
|
---|
104 | out.toString());
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|