source: josm/trunk/test/unit/org/openstreetmap/josm/io/OsmChangeBuilderTest.groovy@ 2605

Last change on this file since 2605 was 2605, checked in by Gubaer, 15 years ago

Unit tests for r2604

File size: 5.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2
3package org.openstreetmap.josm.io;
4
5import org.junit.Test
6import org.openstreetmap.josm.data.osm.Changeset
7import org.openstreetmap.josm.data.osm.Node
8import org.openstreetmap.josm.data.coor.LatLon
9
10
11import static org.junit.Assert.*;
12
13class OsmChangeBuilderTest {
14
15 /**
16 * Test various constructor invocations
17 */
18 @Test
19 public void testConstructor() {
20 def Changeset cs = new Changeset(1)
21 // should not fail
22 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
23
24 // should not fail either - null allowed
25 builder = new OsmChangeBuilder(null)
26
27 // should not fail
28 builder = new OsmChangeBuilder(cs, "0.5")
29
30 builder = new OsmChangeBuilder(cs, null)
31
32 builder = new OsmChangeBuilder(null, null)
33 }
34
35 /**
36 * Test the sequence of method calls. Should throw IllegalStateException if
37 * the protocol start(),append()*, finish() is violated.
38 */
39 @Test
40 public void testSequenceOfMethodCalls() {
41 def Changeset cs = new Changeset(1)
42 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
43
44 final shouldFail = new GroovyTestCase().&shouldFail
45
46 // should be OK
47 builder.start()
48 Node n = new Node(new LatLon(0,0))
49 builder.append n
50 builder.finish()
51
52 shouldFail(IllegalStateException) {
53 builder = new OsmChangeBuilder(cs)
54 builder.append n
55 }
56
57 shouldFail(IllegalStateException) {
58 builder = new OsmChangeBuilder(cs)
59 builder.append([n])
60 }
61
62 shouldFail(IllegalStateException) {
63 builder = new OsmChangeBuilder(cs)
64 builder.finish()
65 }
66
67 shouldFail(IllegalStateException) {
68 builder = new OsmChangeBuilder(cs)
69 builder.start()
70 builder.start()
71 }
72 }
73
74 @Test
75 public void testDocumentWithNewNode() {
76 def Changeset cs = new Changeset(1)
77 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
78 Node n = new Node(new LatLon(0,0))
79
80 builder.start()
81 builder.append n
82 builder.finish()
83
84 def doc = new XmlParser().parseText(builder.document)
85 assert doc.@version == "0.6"
86 assert doc.@generator == "JOSM"
87 assert doc.name() == "osmChange"
88 assert doc.children().size() == 1
89 def create = doc.create
90 assert create != null
91
92 assert create.size() == 1
93 def nodes = create[0].node
94 assert nodes.size() == 1
95 def node = nodes[0]
96 assert node.@id == n.uniqueId.toString()
97 assert node.@lat != null
98 assert node.@lon != null
99 assert node.@changeset == cs.id.toString()
100 }
101
102 /**
103 * Test building a coument with a modified node
104 */
105 @Test
106 public void testDocumentWithModifiedNode() {
107 def Changeset cs = new Changeset(1)
108 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
109 Node n = new Node(1)
110 n.coor = new LatLon(0,0)
111 n.incomplete = false
112 n.modified = true
113
114 builder.start()
115 builder.append n
116 builder.finish()
117
118 def doc = new XmlParser().parseText(builder.document)
119 assert doc.@version == "0.6"
120 assert doc.@generator == "JOSM"
121 assert doc.name() == "osmChange"
122 assert doc.children().size() == 1
123 def modify = doc.modify
124 assert modify != null
125
126 assert modify.size() == 1
127 def nodes = modify[0].node
128 assert nodes.size() == 1
129 def node = nodes[0]
130 assert node.@id == n.uniqueId.toString()
131 assert node.@lat != null
132 assert node.@lon != null
133 assert node.@changeset == cs.id.toString()
134 }
135
136 /**
137 * Test building a document with a deleted node
138 */
139 @Test
140 public void testDocumentWithDeletedNode() {
141 def Changeset cs = new Changeset(1)
142 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
143 Node n = new Node(1)
144 n.coor = new LatLon(0,0)
145 n.incomplete = false
146 n.deleted = true
147
148 builder.start()
149 builder.append n
150 builder.finish()
151
152 def doc = new XmlParser().parseText(builder.document)
153 assert doc.@version == "0.6"
154 assert doc.@generator == "JOSM"
155 assert doc.name() == "osmChange"
156 assert doc.children().size() == 1
157 def delete = doc.delete
158 assert delete != null
159
160 assert delete.size() == 1
161 def nodes = delete[0].node
162 assert nodes.size() == 1
163 def node = nodes[0]
164 assert node.@id == n.uniqueId.toString()
165 assert node.@lat != null
166 assert node.@lon != null
167 assert node.@changeset == cs.id.toString()
168 }
169
170 /**
171 * Test building a mixed document.
172 *
173 */
174 @Test
175 public void testMixed() {
176 def Changeset cs = new Changeset(1)
177 OsmChangeBuilder builder = new OsmChangeBuilder(cs)
178 Node n1 = new Node(1)
179 n1.coor = new LatLon(0,0)
180 n1.incomplete = false
181 n1.deleted = true
182
183 Node n2 = new Node(new LatLon(0,0))
184
185 Node n3 = new Node(2)
186 n3.coor = new LatLon(0,0)
187 n3.incomplete = false
188 n3.modified = true
189
190 builder.start()
191 builder.append([n1,n2,n3])
192 builder.finish()
193
194 def doc = new XmlParser().parseText(builder.document)
195
196 assert doc.children().size() == 3
197 assert doc.children()[0].name() == "delete"
198 assert doc.children()[1].name() == "create"
199 assert doc.children()[2].name() == "modify"
200
201 def node = doc.children()[0].node[0]
202 assert node.@id == n1.uniqueId.toString()
203
204 node = doc.children()[1].node[0]
205 assert node.@id == n2.uniqueId.toString()
206
207 node = doc.children()[2].node[0]
208 assert node.@id == n3.uniqueId.toString()
209 }
210}
Note: See TracBrowser for help on using the repository browser.