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

Last change on this file since 7081 was 7081, checked in by Don-vip, 10 years ago

fixes for unit tests

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