1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.*
|
---|
5 |
|
---|
6 | import org.junit.Test
|
---|
7 | import org.openstreetmap.josm.data.coor.LatLon
|
---|
8 | import org.openstreetmap.josm.data.osm.Changeset
|
---|
9 | import org.openstreetmap.josm.data.osm.Node
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation
|
---|
12 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId
|
---|
13 | import org.openstreetmap.josm.data.osm.Way
|
---|
14 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor
|
---|
15 | import org.openstreetmap.josm.tools.XmlParsingException
|
---|
16 |
|
---|
17 | class DiffResultProcessorTest {
|
---|
18 |
|
---|
19 | @Test
|
---|
20 | public void testConstructor() {
|
---|
21 | Node n = new Node(1)
|
---|
22 | // these calls should not fail
|
---|
23 | //
|
---|
24 | def DiffResultProcessor processor = new DiffResultProcessor(null)
|
---|
25 | processor = new DiffResultProcessor([])
|
---|
26 | processor = new DiffResultProcessor([n])
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Test
|
---|
30 | public void testParse_NOK_Cases() {
|
---|
31 | def DiffResultProcessor processor = new DiffResultProcessor([])
|
---|
32 |
|
---|
33 | final shouldFail = new GroovyTestCase().&shouldFail
|
---|
34 |
|
---|
35 | shouldFail(IllegalArgumentException) {
|
---|
36 | processor.parse null, NullProgressMonitor.INSTANCE
|
---|
37 | }
|
---|
38 |
|
---|
39 | shouldFail(XmlParsingException) {
|
---|
40 | processor.parse "", NullProgressMonitor.INSTANCE
|
---|
41 | }
|
---|
42 |
|
---|
43 | shouldFail(XmlParsingException) {
|
---|
44 | processor.parse "<x></x>", NullProgressMonitor.INSTANCE
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Test
|
---|
49 | public void testParse_OK_Cases() {
|
---|
50 | def DiffResultProcessor processor = new DiffResultProcessor([])
|
---|
51 | String doc = """\
|
---|
52 | <diffResult version="0.6" generator="Test Data">
|
---|
53 | <node old_id="-1" new_id="1" new_version="1"/>
|
---|
54 | <way old_id="-2" new_id="2" new_version="1"/>
|
---|
55 | <relation old_id="-3" new_id="3" new_version="1"/>
|
---|
56 | </diffResult>
|
---|
57 | """
|
---|
58 |
|
---|
59 | processor.parse doc, null
|
---|
60 | assert processor.@diffResults.size() == 3
|
---|
61 | SimplePrimitiveId id = new SimplePrimitiveId(-1, OsmPrimitiveType.NODE)
|
---|
62 | def entry = processor.@diffResults[id]
|
---|
63 | assert entry != null
|
---|
64 | assert entry.newId == 1
|
---|
65 | assert entry.newVersion == 1
|
---|
66 |
|
---|
67 | id = new SimplePrimitiveId(-2, OsmPrimitiveType.WAY)
|
---|
68 | entry = processor.@diffResults[id]
|
---|
69 | assert entry != null
|
---|
70 | assert entry.newId == 2
|
---|
71 | assert entry.newVersion == 1
|
---|
72 |
|
---|
73 | id = new SimplePrimitiveId(-3, OsmPrimitiveType.RELATION)
|
---|
74 | entry = processor.@diffResults[id]
|
---|
75 | assert entry != null
|
---|
76 | assert entry.newId == 3
|
---|
77 | assert entry.newVersion == 1
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Test
|
---|
81 | public void testPostProcess_Invocation_Variants() {
|
---|
82 | def DiffResultProcessor processor = new DiffResultProcessor([])
|
---|
83 | String doc = """\
|
---|
84 | <diffResult version="0.6" generator="Test Data">
|
---|
85 | <node old_id="-1" new_id="1" new_version="1"/>
|
---|
86 | <way old_id="-2" new_id="2" new_version="1"/>
|
---|
87 | <relation old_id="-3" new_id="3" new_version="1"/>
|
---|
88 | </diffResult>
|
---|
89 | """
|
---|
90 |
|
---|
91 | processor.parse doc, null
|
---|
92 |
|
---|
93 | // should all be ok
|
---|
94 | //
|
---|
95 | processor.postProcess null, null
|
---|
96 | processor.postProcess null, NullProgressMonitor.INSTANCE
|
---|
97 | processor.postProcess new Changeset(1), null
|
---|
98 | processor.postProcess new Changeset(1), NullProgressMonitor.INSTANCE
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Test
|
---|
102 | public void testPostProcess_OK() {
|
---|
103 |
|
---|
104 | Node n = new Node()
|
---|
105 | Way w = new Way()
|
---|
106 | Relation r = new Relation()
|
---|
107 |
|
---|
108 | String doc = """\
|
---|
109 | <diffResult version="0.6" generator="Test Data">
|
---|
110 | <node old_id="${n.uniqueId}" new_id="1" new_version="10"/>
|
---|
111 | <way old_id="${w.uniqueId}" new_id="2" new_version="11"/>
|
---|
112 | <relation old_id="${r.uniqueId}" new_id="3" new_version="12"/>
|
---|
113 | </diffResult>
|
---|
114 | """
|
---|
115 |
|
---|
116 | def DiffResultProcessor processor = new DiffResultProcessor([n,w,r])
|
---|
117 | processor.parse doc, null
|
---|
118 | def processed = processor.postProcess(new Changeset(5), null)
|
---|
119 | assert processed.size() == 3
|
---|
120 | n = processed.find {it.uniqueId == 1}
|
---|
121 | assert n.changesetId == 5
|
---|
122 | assert n.version == 10
|
---|
123 |
|
---|
124 | w = processed.find {it.uniqueId == 2}
|
---|
125 | assert w.changesetId == 5
|
---|
126 | assert w.version == 11
|
---|
127 |
|
---|
128 | r = processed.find {it.uniqueId == 3}
|
---|
129 | assert r.changesetId == 5
|
---|
130 | assert r.version == 12
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Test
|
---|
134 | public void testPostProcess_ForCreatedElement() {
|
---|
135 |
|
---|
136 | Node n = new Node()
|
---|
137 | String doc = """\
|
---|
138 | <diffResult version="0.6" generator="Test Data">
|
---|
139 | <node old_id="${n.uniqueId}" new_id="1" new_version="1"/>
|
---|
140 | </diffResult>
|
---|
141 | """
|
---|
142 |
|
---|
143 | def DiffResultProcessor processor = new DiffResultProcessor([n])
|
---|
144 | processor.parse doc, null
|
---|
145 | def processed = processor.postProcess(new Changeset(5), null)
|
---|
146 | assert processed.size() == 1
|
---|
147 | n = processed.find {it.uniqueId == 1}
|
---|
148 | assert n.changesetId == 5
|
---|
149 | assert n.version == 1
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Test
|
---|
153 | public void testPostProcess_ForModifiedElement() {
|
---|
154 |
|
---|
155 | Node n = new Node(1)
|
---|
156 | n.coor = new LatLon(1,1)
|
---|
157 | n.setOsmId 1, 2
|
---|
158 | n.modified = true
|
---|
159 | String doc = """\
|
---|
160 | <diffResult version="0.6" generator="Test Data">
|
---|
161 | <node old_id="${n.uniqueId}" new_id="${n.uniqueId}" new_version="3"/>
|
---|
162 | </diffResult>
|
---|
163 | """
|
---|
164 |
|
---|
165 | def DiffResultProcessor processor = new DiffResultProcessor([n])
|
---|
166 | processor.parse doc, null
|
---|
167 | def processed = processor.postProcess(new Changeset(5), null)
|
---|
168 | assert processed.size() == 1
|
---|
169 | n = processed.find {it.uniqueId == 1}
|
---|
170 | assert n.changesetId == 5
|
---|
171 | assert n.version == 3
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Test
|
---|
175 | public void testPostProcess_ForDeletedElement() {
|
---|
176 |
|
---|
177 | Node n = new Node(1)
|
---|
178 | n.coor = new LatLon(1,1)
|
---|
179 | n.setOsmId 1, 2
|
---|
180 | n.deleted = true
|
---|
181 | String doc = """\
|
---|
182 | <diffResult version="0.6" generator="Test Data">
|
---|
183 | <node old_id="${n.uniqueId}"/>
|
---|
184 | </diffResult>
|
---|
185 | """
|
---|
186 |
|
---|
187 | def DiffResultProcessor processor = new DiffResultProcessor([n])
|
---|
188 | processor.parse doc, null
|
---|
189 | def processed = processor.postProcess(new Changeset(5), null)
|
---|
190 | assert processed.size() == 1
|
---|
191 | n = processed.find {it.uniqueId == 1}
|
---|
192 | assert n.changesetId == 5
|
---|
193 | assert n.version == 2
|
---|
194 | }
|
---|
195 | }
|
---|