1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 |
|
---|
8 | import java.io.ByteArrayInputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.util.Collection;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.Segment;
|
---|
17 | import org.openstreetmap.josm.data.osm.Way;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
19 | import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
|
---|
20 | import org.openstreetmap.josm.testframework.MainMock;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | public class BoundingBoxDownloaderTest extends MainMock {
|
---|
24 |
|
---|
25 | private class StringDownloader extends BoundingBoxDownloader {
|
---|
26 | ByteArrayInputStream stream = in == null ? null : new ByteArrayInputStream(in.getBytes());
|
---|
27 | private StringDownloader() {super(1,2,3,4);}
|
---|
28 | @Override protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws IOException {
|
---|
29 | ByteArrayInputStream oldStream = stream;
|
---|
30 | stream = null;
|
---|
31 | return oldStream;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | private BoundingBoxDownloader bbox;
|
---|
36 | private String in;
|
---|
37 | private Collection<Collection<GpsPoint>> gps;
|
---|
38 | private Collection<GpsPoint> trk;
|
---|
39 | private DataSet ds;
|
---|
40 |
|
---|
41 | private void parseRaw() throws IOException, SAXException {
|
---|
42 | bbox = new StringDownloader();
|
---|
43 | gps = bbox.parseRawGps();
|
---|
44 | if (gps != null && gps.size() > 0)
|
---|
45 | trk = gps.iterator().next();
|
---|
46 | }
|
---|
47 |
|
---|
48 | private void parseOsm() throws IOException, SAXException {
|
---|
49 | bbox = new StringDownloader();
|
---|
50 | ds = bbox.parseOsm();
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | @Test public void parseRawGpsEmptyDataReturnsEmptyList() throws Exception {
|
---|
55 | in = "<gpx></gpx>";
|
---|
56 | parseRaw();
|
---|
57 | assertEquals(0, gps.size());
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test public void parseRawGpsOneTrack() throws Exception {
|
---|
61 | in = "<gpx><trk><trkseg><trkpt lat='1' lon='2'/></trkseg></trk></gpx>";
|
---|
62 | parseRaw();
|
---|
63 | assertEquals(1, gps.size());
|
---|
64 | assertEquals(1, trk.size());
|
---|
65 | assertEquals(1.0, trk.iterator().next().latlon.lat());
|
---|
66 | assertEquals(2.0, trk.iterator().next().latlon.lon());
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Test public void parseRawGpsMultipleTracksReturnStillOneTrack() throws Exception {
|
---|
70 | in = "<gpx>" +
|
---|
71 | "<trk>" +
|
---|
72 | "<trkseg><trkpt lat='23' lon='42'/></trkseg>" +
|
---|
73 | "<trkseg><trkpt lat='12' lon='34'/></trkseg>" +
|
---|
74 | "</trk>" +
|
---|
75 | "<trk><trkseg><trkpt lat='3' lon='4'/></trkseg></trk>" +
|
---|
76 | "</gpx>";
|
---|
77 | parseRaw();
|
---|
78 | assertEquals(1, gps.size());
|
---|
79 | assertEquals(3, trk.size());
|
---|
80 | assertEquals(42.0, trk.iterator().next().latlon.lon());
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test public void parseOsmReturnNullIfNullInputStream() throws Exception {
|
---|
84 | in = null;
|
---|
85 | parseOsm();
|
---|
86 | assertNull(ds);
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test public void parseOsmEmpty() throws Exception {
|
---|
90 | in = "<osm version='0.4'></osm>";
|
---|
91 | parseOsm();
|
---|
92 | assertEquals(0, ds.nodes.size());
|
---|
93 | assertEquals(0, ds.segments.size());
|
---|
94 | assertEquals(0, ds.ways.size());
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Test public void parseOsmSimpleNode() throws Exception {
|
---|
98 | in = "<osm version='0.4'><node id='123' lat='12' lon='23'/></osm>";
|
---|
99 | parseOsm();
|
---|
100 | assertEquals(1, ds.nodes.size());
|
---|
101 | Node node = ds.nodes.iterator().next();
|
---|
102 | assertEquals(123, node.id);
|
---|
103 | assertEquals(12.0, node.coor.lat());
|
---|
104 | assertEquals(23.0, node.coor.lon());
|
---|
105 | assertNull(node.keys);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Test public void parseOsmComplexWay() throws Exception {
|
---|
109 | in = "<osm version='0.4'>" +
|
---|
110 | "<way id='1'>" +
|
---|
111 | "<seg id='2' />" +
|
---|
112 | "</way>" +
|
---|
113 | "<segment id='2' from='3' to='3' />" +
|
---|
114 | "<node id='3' lat='1' lon='2'><tag k='foo' v='bar' /></node>" +
|
---|
115 | "</osm>";
|
---|
116 |
|
---|
117 | parseOsm();
|
---|
118 |
|
---|
119 | assertEquals(1, ds.nodes.size());
|
---|
120 | assertEquals(1, ds.segments.size());
|
---|
121 | assertEquals(1, ds.ways.size());
|
---|
122 |
|
---|
123 | Node node = ds.nodes.iterator().next();
|
---|
124 | Segment segment = ds.segments.iterator().next();
|
---|
125 | Way way = ds.ways.iterator().next();
|
---|
126 |
|
---|
127 | assertNotNull(node.keys);
|
---|
128 | assertEquals("bar", node.get("foo"));
|
---|
129 | assertEquals(1.0, node.coor.lat());
|
---|
130 | assertEquals(node, segment.from);
|
---|
131 | assertEquals(1, way.segments.size());
|
---|
132 | assertEquals(segment, way.segments.iterator().next());
|
---|
133 | }
|
---|
134 | }
|
---|