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

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

Test cases

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