source: josm/trunk/test/unit/org/openstreetmap/josm/io/ParseWithChangesetReaderTest.java@ 14219

Last change on this file since 14219 was 14040, checked in by Don-vip, 6 years ago

see #16498 - convert one more unit test to Java, fix code style issues

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.fail;
7
8import java.io.ByteArrayInputStream;
9import java.io.IOException;
10import java.io.InputStream;
11import java.nio.charset.StandardCharsets;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.Node;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
19import org.openstreetmap.josm.testutils.JOSMTestRules;
20import org.openstreetmap.josm.tools.Logging;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23
24/**
25 * Additional unit tests for {@link OsmReader}.
26 */
27public class ParseWithChangesetReaderTest {
28
29 /**
30 * Setup rule
31 */
32 @Rule
33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
34 public JOSMTestRules test = new JOSMTestRules();
35
36 private static DataSet getDataSet(String doc) throws IOException, IllegalDataException {
37 try (InputStream is = new ByteArrayInputStream(doc.getBytes(StandardCharsets.UTF_8))) {
38 return OsmReader.parseDataSet(is, null);
39 }
40 }
41
42 private static void shouldFail(String doc) throws IOException {
43 try {
44 getDataSet(doc);
45 fail("should throw exception");
46 } catch (IllegalDataException e) {
47 Logging.trace(e);
48 }
49 }
50
51 /**
52 * A new node with a changeset id. Ignore it.
53 * @throws Exception never
54 */
55 @Test
56 public void test_1() throws Exception {
57 String doc =
58 "<osm version=\"0.6\">\n" +
59 "<node id=\"-1\" lat=\"0.0\" lon=\"0.0\" changeset=\"1\">\n" +
60 " <tag k=\"external-id\" v=\"-1\"/>\n" +
61 "</node>\n" +
62 "</osm>";
63
64 DataSet ds = getDataSet(doc);
65 Node n = ds.getNodes().stream().filter(x -> "-1".equals(x.get("external-id"))).findFirst().get();
66 assertNotNull(n);
67 assertEquals(0, n.getChangesetId());
68 }
69
70 /**
71 * A new node with an invalid changeset id. Ignore it.
72 * @throws Exception never
73 */
74 @Test
75 public void test_11() throws Exception {
76 String doc =
77 "<osm version=\"0.6\">\n" +
78 "<node id=\"-1\" lat=\"0.0\" lon=\"0.0\" changeset=\"0\">\n" +
79 " <tag k=\"external-id\" v=\"-1\"/>\n" +
80 "</node>\n" +
81 "</osm>";
82
83 DataSet ds = getDataSet(doc);
84 Node n = ds.getNodes().stream().filter(x -> "-1".equals(x.get("external-id"))).findFirst().get();
85 assertNotNull(n);
86 assertEquals(0, n.getChangesetId());
87 }
88
89 /**
90 * A new node with an invalid changeset id. Ignore it.
91 * @throws Exception never
92 */
93 @Test
94 public void test_12() throws Exception {
95 String doc =
96 "<osm version=\"0.6\">\n" +
97 "<node id=\"-1\" lat=\"0.0\" lon=\"0.0\" changeset=\"-1\">\n" +
98 " <tag k=\"external-id\" v=\"-1\"/>\n" +
99 "</node>\n" +
100 "</osm>";
101
102 DataSet ds = getDataSet(doc);
103 Node n = ds.getNodes().stream().filter(x -> "-1".equals(x.get("external-id"))).findFirst().get();
104 assertNotNull(n);
105 assertEquals(0, n.getChangesetId());
106 }
107
108 /**
109 * A new node with an invalid changeset id. Ignore it.
110 * @throws Exception never
111 */
112 @Test
113 public void test_13() throws Exception {
114 String doc =
115 "<osm version=\"0.6\">\n" +
116 "<node id=\"-1\" lat=\"0.0\" lon=\"0.0\" changeset=\"aaa\">\n" +
117 " <tag k=\"external-id\" v=\"-1\"/>\n" +
118 "</node>\n" +
119 "</osm>";
120
121 DataSet ds = getDataSet(doc);
122 Node n = ds.getNodes().stream().filter(x -> "-1".equals(x.get("external-id"))).findFirst().get();
123 assertNotNull(n);
124 assertEquals(0, n.getChangesetId());
125 }
126
127 /**
128 * A new node with a missing changeset id. That's fine. The changeset id
129 * is reset to 0.
130 * @throws Exception never
131 */
132 @Test
133 public void test_14() throws Exception {
134 String doc =
135 "<osm version=\"0.6\">\n" +
136 "<node id=\"-1\" lat=\"0.0\" lon=\"0.0\" >\n" +
137 " <tag k=\"external-id\" v=\"-1\"/>\n" +
138 "</node>\n" +
139 "</osm>";
140
141 DataSet ds = getDataSet(doc);
142 Node n = ds.getNodes().stream().filter(x -> "-1".equals(x.get("external-id"))).findFirst().get();
143 assertNotNull(n);
144 assertEquals(0, n.getChangesetId());
145 }
146
147
148 /**
149 * An existing node with a missing changeset id. That's fine. The changeset id
150 * is reset to 0.
151 * @throws Exception never
152 */
153 @Test
154 public void test_2() throws Exception {
155 String doc =
156 "<osm version=\"0.6\">\n" +
157 "<node id=\"1\" lat=\"0.0\" lon=\"0.0\" version=\"1\"/>\n" +
158 "</osm>";
159
160 DataSet ds = getDataSet(doc);
161 OsmPrimitive n = ds.getPrimitiveById(1, OsmPrimitiveType.NODE);
162 assertNotNull(n);
163 assertEquals(1, n.getUniqueId());
164 assertEquals(0, n.getChangesetId());
165 }
166
167 /**
168 * An existing node with a valid changeset id id. That's fine. The changeset id
169 * is applied.
170 * @throws Exception never
171 */
172 @Test
173 public void test_3() throws Exception {
174 String doc =
175 "<osm version=\"0.6\">\n" +
176 "<node id=\"1\" lat=\"0.0\" lon=\"0.0\" version=\"1\" changeset=\"4\"/>\n" +
177 "</osm>";
178
179 DataSet ds = getDataSet(doc);
180 OsmPrimitive n = ds.getPrimitiveById(1, OsmPrimitiveType.NODE);
181 assertNotNull(n);
182 assertEquals(1, n.getUniqueId());
183 assertEquals(4, n.getChangesetId());
184 }
185
186 /**
187 * An existing node with an invalid changeset id. That's a problem. An exception
188 * is thrown.
189 * @throws IOException never
190 */
191 @Test
192 public void test_4() throws IOException {
193 String doc =
194 "<osm version=\"0.6\">\n" +
195 "<node id=\"1\" lat=\"0.0\" lon=\"0.0\" version=\"1\" changeset=\"-1\"/>\n" +
196 "</osm>";
197
198 shouldFail(doc);
199 }
200
201 /**
202 * An existing node with an invalid changeset id. That's a problem. An exception
203 * is thrown.
204 * @throws IOException never
205 */
206 @Test
207 public void test_5() throws IOException {
208 String doc =
209 "<osm version=\"0.6\">\n" +
210 "<node id=\"1\" lat=\"0.0\" lon=\"0.0\" version=\"1\" changeset=\"1.0\"/>\n" +
211 "</osm>";
212
213 shouldFail(doc);
214 }
215
216 /**
217 * An existing node with an invalid changeset id. That's a problem. An exception
218 * is thrown.
219 * @throws IOException never
220 */
221 @Test
222 public void test_6() throws IOException {
223 String doc =
224 "<osm version=\"0.6\">\n" +
225 "<node id=\"1\" lat=\"0.0\" lon=\"0.0\" version=\"1\" changeset=\"abc\"/>\n" +
226 "</osm>";
227
228 shouldFail(doc);
229 }
230}
Note: See TracBrowser for help on using the repository browser.