1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
---|
6 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
7 |
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.data.osm.Node;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
23 | import org.openstreetmap.josm.io.DiffResultProcessor.DiffResultEntry;
|
---|
24 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
27 |
|
---|
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests of {@link DiffResultProcessor}
|
---|
32 | */
|
---|
33 | class DiffResultProcessorTest {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Setup rule
|
---|
37 | */
|
---|
38 | @RegisterExtension
|
---|
39 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
40 | public JOSMTestRules test = new JOSMTestRules();
|
---|
41 |
|
---|
42 | private static void shouldFail(String s) {
|
---|
43 | try {
|
---|
44 | new DiffResultProcessor(null).parse(s, NullProgressMonitor.INSTANCE);
|
---|
45 | fail("should throw exception");
|
---|
46 | } catch (IllegalArgumentException | XmlParsingException e) {
|
---|
47 | Logging.trace(e);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Unit test of {@link DiffResultProcessor#DiffResultProcessor}.
|
---|
53 | */
|
---|
54 | @Test
|
---|
55 | void testConstructor() {
|
---|
56 | Node n = new Node(1);
|
---|
57 | // these calls should not fail
|
---|
58 | //
|
---|
59 | new DiffResultProcessor(null);
|
---|
60 | new DiffResultProcessor(Collections.emptyList());
|
---|
61 | new DiffResultProcessor(Collections.singleton(n));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Test invalid cases.
|
---|
66 | */
|
---|
67 | @Test
|
---|
68 | void testParse_NOK_Cases() {
|
---|
69 | shouldFail(null);
|
---|
70 | shouldFail("");
|
---|
71 | shouldFail("<x></x>");
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Test valid cases.
|
---|
76 | * @throws XmlParsingException never
|
---|
77 | */
|
---|
78 | @Test
|
---|
79 | void testParse_OK_Cases() throws XmlParsingException {
|
---|
80 | DiffResultProcessor processor = new DiffResultProcessor(null);
|
---|
81 | String doc =
|
---|
82 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
83 | " <node old_id=\"-1\" new_id=\"1\" new_version=\"1\"/>\n" +
|
---|
84 | " <way old_id=\"-2\" new_id=\"2\" new_version=\"1\"/>\n" +
|
---|
85 | " <relation old_id=\"-3\" new_id=\"3\" new_version=\"1\"/>\n" +
|
---|
86 | "</diffResult>";
|
---|
87 |
|
---|
88 | processor.parse(doc, null);
|
---|
89 | assertEquals(3, processor.getDiffResults().size());
|
---|
90 | SimplePrimitiveId id = new SimplePrimitiveId(-1, OsmPrimitiveType.NODE);
|
---|
91 | DiffResultEntry entry = processor.getDiffResults().get(id);
|
---|
92 | assertNotNull(entry);
|
---|
93 | assertEquals(1, entry.newId);
|
---|
94 | assertEquals(1, entry.newVersion);
|
---|
95 |
|
---|
96 | id = new SimplePrimitiveId(-2, OsmPrimitiveType.WAY);
|
---|
97 | entry = processor.getDiffResults().get(id);
|
---|
98 | assertNotNull(entry);
|
---|
99 | assertEquals(2, entry.newId);
|
---|
100 | assertEquals(1, entry.newVersion);
|
---|
101 |
|
---|
102 | id = new SimplePrimitiveId(-3, OsmPrimitiveType.RELATION);
|
---|
103 | entry = processor.getDiffResults().get(id);
|
---|
104 | assertNotNull(entry);
|
---|
105 | assertEquals(3, entry.newId);
|
---|
106 | assertEquals(1, entry.newVersion);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Test {@link DiffResultProcessor#postProcess}
|
---|
111 | * @throws XmlParsingException never
|
---|
112 | */
|
---|
113 | @Test
|
---|
114 | void testPostProcess_Invocation_Variants() throws XmlParsingException {
|
---|
115 | DiffResultProcessor processor = new DiffResultProcessor(null);
|
---|
116 | String doc =
|
---|
117 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
118 | " <node old_id=\"-1\" new_id=\"1\" new_version=\"1\"/>\n" +
|
---|
119 | " <way old_id=\"-2\" new_id=\"2\" new_version=\"1\"/>\n" +
|
---|
120 | " <relation old_id=\"-3\" new_id=\"3\" new_version=\"1\"/>\n" +
|
---|
121 | "</diffResult>";
|
---|
122 |
|
---|
123 | processor.parse(doc, null);
|
---|
124 |
|
---|
125 | // should all be ok
|
---|
126 | //
|
---|
127 | processor.postProcess(null, null);
|
---|
128 | processor.postProcess(null, NullProgressMonitor.INSTANCE);
|
---|
129 | processor.postProcess(new Changeset(1), null);
|
---|
130 | processor.postProcess(new Changeset(1), NullProgressMonitor.INSTANCE);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Test {@link DiffResultProcessor#postProcess}
|
---|
135 | * @throws XmlParsingException never
|
---|
136 | */
|
---|
137 | @Test
|
---|
138 | void testPostProcess_OK() throws XmlParsingException {
|
---|
139 |
|
---|
140 | Node n = new Node();
|
---|
141 | Way w = new Way();
|
---|
142 | Relation r = new Relation();
|
---|
143 |
|
---|
144 | String doc =
|
---|
145 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
146 | " <node old_id=\""+n.getUniqueId()+"\" new_id=\"1\" new_version=\"10\"/>\n" +
|
---|
147 | " <way old_id=\""+w.getUniqueId()+"\" new_id=\"2\" new_version=\"11\"/>\n" +
|
---|
148 | " <relation old_id=\""+r.getUniqueId()+"\" new_id=\"3\" new_version=\"12\"/>\n" +
|
---|
149 | "</diffResult>";
|
---|
150 |
|
---|
151 | DiffResultProcessor processor = new DiffResultProcessor(Arrays.asList(n, w, r));
|
---|
152 | processor.parse(doc, null);
|
---|
153 | Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null);
|
---|
154 | assertEquals(3, processed.size());
|
---|
155 | n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get();
|
---|
156 | assertEquals(5, n.getChangesetId());
|
---|
157 | assertEquals(10, n.getVersion());
|
---|
158 |
|
---|
159 | w = (Way) processed.stream().filter(x -> x.getUniqueId() == 2).findFirst().get();
|
---|
160 | assertEquals(5, w.getChangesetId());
|
---|
161 | assertEquals(11, w.getVersion());
|
---|
162 |
|
---|
163 | r = (Relation) processed.stream().filter(x -> x.getUniqueId() == 3).findFirst().get();
|
---|
164 | assertEquals(5, r.getChangesetId());
|
---|
165 | assertEquals(12, r.getVersion());
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Test {@link DiffResultProcessor#postProcess}
|
---|
170 | * @throws XmlParsingException never
|
---|
171 | */
|
---|
172 | @Test
|
---|
173 | void testPostProcess_ForCreatedElement() throws XmlParsingException {
|
---|
174 |
|
---|
175 | Node n = new Node();
|
---|
176 | String doc =
|
---|
177 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
178 | " <node old_id=\""+n.getUniqueId()+"\" new_id=\"1\" new_version=\"1\"/>\n" +
|
---|
179 | "</diffResult>";
|
---|
180 |
|
---|
181 | DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n));
|
---|
182 | processor.parse(doc, null);
|
---|
183 | Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null);
|
---|
184 | assertEquals(1, processed.size());
|
---|
185 | n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get();
|
---|
186 | assertEquals(5, n.getChangesetId());
|
---|
187 | assertEquals(1, n.getVersion());
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Test {@link DiffResultProcessor#postProcess}
|
---|
192 | * @throws XmlParsingException never
|
---|
193 | */
|
---|
194 | @Test
|
---|
195 | void testPostProcess_ForModifiedElement() throws XmlParsingException {
|
---|
196 |
|
---|
197 | Node n = new Node(1);
|
---|
198 | n.setCoor(new LatLon(1, 1));
|
---|
199 | n.setOsmId(1, 2);
|
---|
200 | n.setModified(true);
|
---|
201 | String doc =
|
---|
202 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
203 | " <node old_id=\""+n.getUniqueId()+"\" new_id=\""+n.getUniqueId()+"\" new_version=\"3\"/>\n" +
|
---|
204 | "</diffResult>";
|
---|
205 |
|
---|
206 | DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n));
|
---|
207 | processor.parse(doc, null);
|
---|
208 | Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null);
|
---|
209 | assertEquals(1, processed.size());
|
---|
210 | n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get();
|
---|
211 | assertEquals(5, n.getChangesetId());
|
---|
212 | assertEquals(3, n.getVersion());
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Test {@link DiffResultProcessor#postProcess}
|
---|
217 | * @throws XmlParsingException never
|
---|
218 | */
|
---|
219 | @Test
|
---|
220 | void testPostProcess_ForDeletedElement() throws XmlParsingException {
|
---|
221 |
|
---|
222 | Node n = new Node(1);
|
---|
223 | n.setCoor(new LatLon(1, 1));
|
---|
224 | n.setOsmId(1, 2);
|
---|
225 | n.setDeleted(true);
|
---|
226 | String doc =
|
---|
227 | "<diffResult version=\"0.6\" generator=\"Test Data\">\n" +
|
---|
228 | " <node old_id=\""+n.getUniqueId()+"\"/>\n" +
|
---|
229 | "</diffResult>";
|
---|
230 |
|
---|
231 | DiffResultProcessor processor = new DiffResultProcessor(Collections.singleton(n));
|
---|
232 | processor.parse(doc, null);
|
---|
233 | Set<OsmPrimitive> processed = processor.postProcess(new Changeset(5), null);
|
---|
234 | assertEquals(1, processed.size());
|
---|
235 | n = (Node) processed.stream().filter(x -> x.getUniqueId() == 1).findFirst().get();
|
---|
236 | assertEquals(5, n.getChangesetId());
|
---|
237 | assertEquals(2, n.getVersion());
|
---|
238 | }
|
---|
239 | }
|
---|