1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Collections;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
18 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests for Session writing.
|
---|
25 | */
|
---|
26 | public class SessionWriterTest {
|
---|
27 |
|
---|
28 | private static class OsmHeadlessJosExporter extends OsmDataSessionExporter {
|
---|
29 | public OsmHeadlessJosExporter(OsmDataLayer layer) {
|
---|
30 | super(layer);
|
---|
31 | }
|
---|
32 | @Override
|
---|
33 | public boolean requiresZip() {
|
---|
34 | return false;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private static class OsmHeadlessJozExporter extends OsmDataSessionExporter {
|
---|
39 | public OsmHeadlessJozExporter(OsmDataLayer layer) {
|
---|
40 | super(layer);
|
---|
41 | }
|
---|
42 | @Override
|
---|
43 | public boolean requiresZip() {
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private static class GpxHeadlessJosExporter extends GpxTracksSessionExporter {
|
---|
49 | public GpxHeadlessJosExporter(GpxLayer layer) {
|
---|
50 | super(layer);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public boolean requiresZip() {
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private static class GpxHeadlessJozExporter extends GpxTracksSessionExporter {
|
---|
60 | public GpxHeadlessJozExporter(GpxLayer layer) {
|
---|
61 | super(layer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public boolean requiresZip() {
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Setup tests.
|
---|
72 | */
|
---|
73 | @BeforeClass
|
---|
74 | public static void setUpBeforeClass() {
|
---|
75 | Main.initApplicationPreferences();
|
---|
76 | Main.determinePlatformHook();
|
---|
77 | Main.preConstructorInit(new HashMap<MainApplication.Option, Collection<String>>());
|
---|
78 | new MainApplication();
|
---|
79 | Main.main.createMapFrame(null, null);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void testWrite(List<Layer> layers, final boolean zip) throws IOException {
|
---|
83 | Map<Layer, SessionLayerExporter> exporters = new HashMap<>();
|
---|
84 | if (zip) {
|
---|
85 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJozExporter.class);
|
---|
86 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJozExporter.class);
|
---|
87 | } else {
|
---|
88 | SessionWriter.registerSessionLayerExporter(OsmDataLayer.class, OsmHeadlessJosExporter.class);
|
---|
89 | SessionWriter.registerSessionLayerExporter(GpxLayer.class, GpxHeadlessJosExporter.class);
|
---|
90 | }
|
---|
91 | for (final Layer l : layers) {
|
---|
92 | exporters.put(l, SessionWriter.getSessionLayerExporter(l));
|
---|
93 | }
|
---|
94 | SessionWriter sw = new SessionWriter(layers, -1, exporters, new MultiMap<Layer, Layer>(), zip);
|
---|
95 | File file = new File(System.getProperty("java.io.tmpdir"), getClass().getName()+(zip?".joz":".jos"));
|
---|
96 | try {
|
---|
97 | sw.write(file);
|
---|
98 | } finally {
|
---|
99 | if (file.exists()) {
|
---|
100 | file.delete();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | private OsmDataLayer createOsmLayer() {
|
---|
106 | OsmDataLayer layer = new OsmDataLayer(new DataSet(), null, null);
|
---|
107 | layer.setAssociatedFile(new File("data.osm"));
|
---|
108 | return layer;
|
---|
109 | }
|
---|
110 |
|
---|
111 | private GpxLayer createGpxLayer() {
|
---|
112 | GpxLayer layer = new GpxLayer(new GpxData());
|
---|
113 | layer.setAssociatedFile(new File("data.gpx"));
|
---|
114 | return layer;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Tests to write an empty .jos file.
|
---|
119 | * @throws IOException if any I/O error occurs
|
---|
120 | */
|
---|
121 | @Test
|
---|
122 | public void testWriteEmptyJos() throws IOException {
|
---|
123 | testWrite(Collections.<Layer>emptyList(), false);
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Tests to write an empty .joz file.
|
---|
128 | * @throws IOException if any I/O error occurs
|
---|
129 | */
|
---|
130 | @Test
|
---|
131 | public void testWriteEmptyJoz() throws IOException {
|
---|
132 | testWrite(Collections.<Layer>emptyList(), true);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Tests to write a .jos file containing OSM data.
|
---|
137 | * @throws IOException if any I/O error occurs
|
---|
138 | */
|
---|
139 | @Test
|
---|
140 | public void testWriteOsmJos() throws IOException {
|
---|
141 | testWrite(Collections.<Layer>singletonList(createOsmLayer()), false);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Tests to write a .joz file containing OSM data.
|
---|
146 | * @throws IOException if any I/O error occurs
|
---|
147 | */
|
---|
148 | @Test
|
---|
149 | public void testWriteOsmJoz() throws IOException {
|
---|
150 | testWrite(Collections.<Layer>singletonList(createOsmLayer()), true);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Tests to write a .jos file containing GPX data.
|
---|
155 | * @throws IOException if any I/O error occurs
|
---|
156 | */
|
---|
157 | @Test
|
---|
158 | public void testWriteGpxJos() throws IOException {
|
---|
159 | testWrite(Collections.<Layer>singletonList(createGpxLayer()), false);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Tests to write a .joz file containing GPX data.
|
---|
164 | * @throws IOException if any I/O error occurs
|
---|
165 | */
|
---|
166 | @Test
|
---|
167 | public void testWriteGpxJoz() throws IOException {
|
---|
168 | testWrite(Collections.<Layer>singletonList(createGpxLayer()), true);
|
---|
169 | }
|
---|
170 | }
|
---|