1 | package org.openstreetmap.josm.test;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.StringReader;
|
---|
5 | import java.io.StringWriter;
|
---|
6 |
|
---|
7 | import junit.framework.TestCase;
|
---|
8 |
|
---|
9 | import org.jdom.Element;
|
---|
10 | import org.jdom.JDOMException;
|
---|
11 | import org.jdom.Namespace;
|
---|
12 | import org.jdom.input.SAXBuilder;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.io.GpxWriter;
|
---|
15 | import org.openstreetmap.josm.test.framework.Bug;
|
---|
16 | import org.openstreetmap.josm.test.framework.DataSetTestCaseHelper;
|
---|
17 |
|
---|
18 | public class GpxWriterTest extends TestCase {
|
---|
19 |
|
---|
20 | private static Namespace GPX = Namespace.getNamespace("http://www.topografix.com/GPX/1/0");
|
---|
21 | private static Namespace JOSM = Namespace.getNamespace("http://wiki.eigenheimstrasse.de/wiki/JOSM");
|
---|
22 |
|
---|
23 | private DataSet ds;
|
---|
24 |
|
---|
25 | private Element root;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Verify that deleted objects that are not uploaded to the server does not show up
|
---|
29 | * in gpx save output at all.
|
---|
30 | */
|
---|
31 | @Bug(47)
|
---|
32 | public void testDeleteNewDoesReallyRemove() throws JDOMException, IOException {
|
---|
33 | ds.waies.iterator().next().setDeleted(true);
|
---|
34 | root = reparse();
|
---|
35 | assertEquals("way has vanished and 3 trk (segments) left", 3, root.getChildren("trk", GPX).size());
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Verify, that new created elements, if and only if they occoure more than once in
|
---|
41 | * the file, have a negative id attached.
|
---|
42 | */
|
---|
43 | @Bug(47)
|
---|
44 | public void testNewCreateAddIdWhenMoreThanOnce() {
|
---|
45 | // the trk with the two trkseg's only occoure once -> no extension id
|
---|
46 | Element realWay = null;
|
---|
47 | for (Object o : root.getChildren("trk", GPX)) {
|
---|
48 | Element e = (Element)o;
|
---|
49 | if (e.getChildren("trkseg", GPX).size() != 2)
|
---|
50 | continue;
|
---|
51 | Element ext = e.getChild("extensions", GPX);
|
---|
52 | if (ext != null)
|
---|
53 | assertEquals("no id for way (used only once)", 0, ext.getChildren("uid", JOSM).size());
|
---|
54 | realWay = e;
|
---|
55 | }
|
---|
56 | assertNotNull("way not found in GPX file", realWay);
|
---|
57 |
|
---|
58 | // the second point of the first segment of the waies has an id
|
---|
59 | Element trkseg = (Element)realWay.getChildren("trkseg", GPX).get(0);
|
---|
60 | Element trkpt = (Element)trkseg.getChildren("trkpt", GPX).get(1);
|
---|
61 | assertEquals("waypoint used twice but has no extensions at all", 1, trkpt.getChildren("extensions", GPX).size());
|
---|
62 | Element ext = trkpt.getChild("extensions", GPX);
|
---|
63 | assertEquals("waypoint used twice but has no id", 1, ext.getChildren("uid", JOSM).size());
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Parse the intern dataset and return the root gpx - element.
|
---|
69 | */
|
---|
70 | private Element reparse() throws IOException, JDOMException {
|
---|
71 | StringWriter out = new StringWriter();
|
---|
72 | GpxWriter writer = new GpxWriter(out, ds);
|
---|
73 | writer.output();
|
---|
74 | Element root = new SAXBuilder().build(new StringReader(out.toString())).getRootElement();
|
---|
75 | return root;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | protected void setUp() throws Exception {
|
---|
81 | super.setUp();
|
---|
82 | ds = DataSetTestCaseHelper.createCommon();
|
---|
83 | root = reparse();
|
---|
84 | }
|
---|
85 | }
|
---|