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.nio.charset.StandardCharsets;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Arrays;
|
---|
14 | import java.util.Collections;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
---|
19 | import org.openstreetmap.josm.data.osm.NodeData;
|
---|
20 | import org.openstreetmap.josm.data.osm.UploadPolicy;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests of {@link OsmWriter} class.
|
---|
24 | */
|
---|
25 | public class OsmWriterTest {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Unit test of {@link OsmWriter#byIdComparator}.
|
---|
29 | */
|
---|
30 | @Test
|
---|
31 | public void testByIdComparator() {
|
---|
32 |
|
---|
33 | final List<NodeData> ids = new ArrayList<>();
|
---|
34 | for (Long id : Arrays.asList(12L, Long.MIN_VALUE, 65L, -12L, 2L, 0L, -3L, -20L, Long.MAX_VALUE)) {
|
---|
35 | final NodeData n = new NodeData();
|
---|
36 | n.setId(id);
|
---|
37 | ids.add(n);
|
---|
38 | }
|
---|
39 |
|
---|
40 | Collections.sort(ids, OsmWriter.byIdComparator);
|
---|
41 |
|
---|
42 | final long[] longIds = ids.stream().mapToLong(NodeData::getUniqueId).toArray();
|
---|
43 | assertArrayEquals(new long[] {
|
---|
44 | -3, -12, -20, -9223372036854775808L, 0, 2, 12, 65, 9223372036854775807L
|
---|
45 | }, longIds);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Unit test of {@link OsmWriter#header(DownloadPolicy, UploadPolicy)}.
|
---|
50 | * @throws IOException if an I/O error occurs
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | public void testHeader() throws IOException {
|
---|
54 | doTestHeader(null, null,
|
---|
55 | "<osm version='0.6' generator='JOSM'>");
|
---|
56 | doTestHeader(DownloadPolicy.NORMAL, UploadPolicy.NORMAL,
|
---|
57 | "<osm version='0.6' generator='JOSM'>");
|
---|
58 | doTestHeader(DownloadPolicy.BLOCKED, UploadPolicy.BLOCKED,
|
---|
59 | "<osm version='0.6' download='never' upload='never' generator='JOSM'>");
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static void doTestHeader(DownloadPolicy download, UploadPolicy upload, String expected) throws IOException {
|
---|
63 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
---|
64 | try (PrintWriter out = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
|
---|
65 | OsmWriter writer = OsmWriterFactory.createOsmWriter(out, true, OsmWriter.DEFAULT_API_VERSION)) {
|
---|
66 | writer.header(download, upload);
|
---|
67 | }
|
---|
68 | assertEquals("<?xml version='1.0' encoding='UTF-8'?>" + expected,
|
---|
69 | baos.toString("UTF-8").replaceAll("\r", "").replaceAll("\n", ""));
|
---|
70 | }
|
---|
71 | }
|
---|