1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertArrayEquals;
|
---|
5 | import static org.junit.Assert.assertEquals;
|
---|
6 |
|
---|
7 | import java.io.ByteArrayOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.OutputStreamWriter;
|
---|
10 | import java.io.PrintWriter;
|
---|
11 | import java.io.StringWriter;
|
---|
12 | import java.nio.charset.StandardCharsets;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Arrays;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
---|
23 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
24 | import org.openstreetmap.josm.data.osm.UploadPolicy;
|
---|
25 | import org.openstreetmap.josm.data.osm.User;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Unit tests of {@link OsmWriter} class.
|
---|
29 | */
|
---|
30 | public class OsmWriterTest {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unit test of {@link OsmWriter#byIdComparator}.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | public 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 | public 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 | public 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 | public 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='<anonymous>' 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 | }
|
---|