source: josm/trunk/test/unit/org/openstreetmap/josm/io/OsmWriterTest.java@ 17275

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 4.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6
7import java.io.ByteArrayOutputStream;
8import java.io.IOException;
9import java.io.OutputStreamWriter;
10import java.io.PrintWriter;
11import java.io.StringWriter;
12import java.nio.charset.StandardCharsets;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Collections;
16import java.util.List;
17
18import org.junit.jupiter.api.Test;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.data.osm.Changeset;
21import org.openstreetmap.josm.data.osm.DataSet;
22import org.openstreetmap.josm.data.osm.DownloadPolicy;
23import org.openstreetmap.josm.data.osm.NodeData;
24import org.openstreetmap.josm.data.osm.UploadPolicy;
25import org.openstreetmap.josm.data.osm.User;
26
27/**
28 * Unit tests of {@link OsmWriter} class.
29 */
30class OsmWriterTest {
31
32 /**
33 * Unit test of {@link OsmWriter#byIdComparator}.
34 */
35 @Test
36 void testByIdComparator() {
37
38 final List<NodeData> ids = new ArrayList<>();
39 for (Long id : Arrays.asList(12L, Long.MIN_VALUE, 65L, -12L, 2L, 0L, -3L, -20L, Long.MAX_VALUE)) {
40 final NodeData n = new NodeData();
41 n.setId(id);
42 ids.add(n);
43 }
44
45 Collections.sort(ids, OsmWriter.byIdComparator);
46
47 final long[] longIds = ids.stream().mapToLong(NodeData::getUniqueId).toArray();
48 assertArrayEquals(new long[] {
49 -3, -12, -20, -9223372036854775808L, 0, 2, 12, 65, 9223372036854775807L
50 }, longIds);
51 }
52
53 /**
54 * Unit test of {@link OsmWriter#header(DownloadPolicy, UploadPolicy)}.
55 * @throws IOException if an I/O error occurs
56 */
57 @Test
58 void testHeader() throws IOException {
59 doTestHeader(null, null,
60 "<osm version='0.6' generator='JOSM'>");
61 doTestHeader(DownloadPolicy.NORMAL, UploadPolicy.NORMAL,
62 "<osm version='0.6' generator='JOSM'>");
63 doTestHeader(DownloadPolicy.BLOCKED, UploadPolicy.BLOCKED,
64 "<osm version='0.6' download='never' upload='never' generator='JOSM'>");
65 }
66
67 private static void doTestHeader(DownloadPolicy download, UploadPolicy upload, String expected) throws IOException {
68 ByteArrayOutputStream baos = new ByteArrayOutputStream();
69 try (PrintWriter out = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
70 OsmWriter writer = OsmWriterFactory.createOsmWriter(out, true, OsmWriter.DEFAULT_API_VERSION)) {
71 writer.header(download, upload);
72 }
73 assertEquals("<?xml version='1.0' encoding='UTF-8'?>" + expected,
74 new String(baos.toByteArray(), StandardCharsets.UTF_8)
75 .replaceAll("\r", "")
76 .replaceAll("\n", ""));
77 }
78
79 /**
80 * Unit test of {@link OsmWriter#write} with dataset locked.
81 * @throws IOException if an I/O error occurs
82 */
83 @Test
84 void testWriteLock() throws IOException {
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 try (PrintWriter out = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
87 OsmWriter writer = OsmWriterFactory.createOsmWriter(out, true, OsmWriter.DEFAULT_API_VERSION)) {
88 DataSet ds = new DataSet();
89 ds.lock();
90 writer.write(ds);
91 }
92 assertEquals("<?xml version='1.0' encoding='UTF-8'?><osm version='0.6' locked='true' generator='JOSM'></osm>",
93 new String(baos.toByteArray(), StandardCharsets.UTF_8)
94 .replaceAll("\r", "")
95 .replaceAll("\n", ""));
96 }
97
98 /**
99 * Unit test of {@link OsmWriter#visit(Changeset)}.
100 * @throws IOException if an I/O error occurs
101 */
102 @Test
103 void testChangeset() throws IOException {
104 Changeset cs = new Changeset();
105 cs.setUser(User.getAnonymous());
106 cs.setId(38038262);
107 cs.setMin(new LatLon(12., 34.));
108 cs.setMax(new LatLon(56., 78.));
109 try (StringWriter stringWriter = new StringWriter();
110 OsmWriter osmWriter = OsmWriterFactory.createOsmWriter(new PrintWriter(stringWriter), true, OsmWriter.DEFAULT_API_VERSION)) {
111 osmWriter.visit(cs);
112 assertEquals(" <changeset id='38038262' user='&lt;anonymous&gt;' uid='-1' open='false' " +
113 "min_lon='34.0' min_lat='12.0' max_lon='78.0' max_lat='56.0'>\n </changeset>\n",
114 stringWriter.toString().replace("\r", ""));
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.