aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/files/GPXParserTest.java
blob: f37f327f3b6d8194144289d3e0d00eeb7334485e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package cgeo.geocaching.files;

import cgeo.geocaching.LogEntry;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgWaypoint;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.WaypointType;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase;
import cgeo.geocaching.test.R;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class GPXParserTest extends AbstractResourceInstrumentationTestCase {
    private static final SimpleDateFormat LOG_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // 2010-04-20T07:00:00Z

    public void testGPXVersion100() throws Exception {
        testGPXVersion(R.raw.gc1bkp3_gpx100);
    }

    private cgCache testGPXVersion(final int resourceId) throws IOException, ParserException {
        final List<cgCache> caches = readGPX10(resourceId);
        assertNotNull(caches);
        assertEquals(1, caches.size());
        final cgCache cache = caches.get(0);
        assertEquals("GC1BKP3", cache.getGeocode());
        assertEquals("9946f030-a514-46d8-a050-a60e92fd2e1a", cache.getGuid());
        assertEquals(CacheType.TRADITIONAL, cache.getType());
        assertEquals(false, cache.isArchived());
        assertEquals(false, cache.isDisabled());
        assertEquals("Die Schatzinsel / treasure island", cache.getName());
        assertEquals("Die unbesiegbaren Geo - Geparden", cache.getOwner());
        assertEquals(CacheSize.MICRO, cache.getSize());
        assertEquals(1.0f, cache.getDifficulty());
        assertEquals(5.0f, cache.getTerrain());
        assertEquals("Baden-Württemberg, Germany", cache.getLocation());
        assertEquals("Ein alter Kindheitstraum, ein Schatz auf einer unbewohnten Insel.\nA old dream of my childhood, a treasure on a lonely island.", cache.getShortdesc());
        assertEquals(new Geopoint(48.859683, 9.1874), cache.getCoords());
        return cache;
    }

    public void testGPXVersion101() throws IOException, ParserException {
        final cgCache cache = testGPXVersion(R.raw.gc1bkp3_gpx101);
        assertNotNull(cache.getAttributes());
        assertEquals(10, cache.getAttributes().size());
    }

    public void testOC() throws IOException, ParserException {
        final List<cgCache> caches = readGPX10(R.raw.oc5952_gpx);
        final cgCache cache = caches.get(0);
        assertEquals("OC5952", cache.getGeocode());
        assertEquals(CacheType.TRADITIONAL, cache.getType());
        assertEquals(false, cache.isArchived());
        assertEquals(false, cache.isDisabled());
        assertEquals("Die Schatzinsel / treasure island", cache.getName());
        assertEquals("Die unbesiegbaren Geo - Geparden", cache.getOwner());
        assertEquals(CacheSize.SMALL, cache.getSize());
        assertEquals(1.0f, cache.getDifficulty());
        assertEquals(4.0f, cache.getTerrain());
        assertEquals("Baden-Württemberg, Germany", cache.getLocation());
        assertEquals("Ein alter Kindheitstraum, ein Schatz auf einer unbewohnten Insel. A old dream of my childhood, a treasure on a lonely is", cache.getShortdesc());
        assertEquals(new Geopoint(48.85968, 9.18740), cache.getCoords());
        assertTrue(cache.isReliableLatLon());
    }

    public void testGc31j2h() throws IOException, ParserException {
        final List<cgCache> caches = readGPX10(R.raw.gc31j2h);
        assertEquals(1, caches.size());
        final cgCache cache = caches.get(0);

        assertGc31j2h(cache);
        assertSame(cache, caches.get(0));

        // no waypoints without importing waypoint file
        assertEquals(0, cache.getWaypoints().size());
        assertTrue(cache.isReliableLatLon());
    }

    public void testGc31j2hWpts() throws IOException, ParserException {
        List<cgCache> caches = readGPX10(R.raw.gc31j2h, R.raw.gc31j2h_wpts);
        assertEquals(1, caches.size());
        cgCache cache = caches.get(0);
        assertGc31j2h(cache);
        assertGc31j2hWaypoints(cache);
    }

    public void testGc31j2hWptsWithoutCache() throws IOException, ParserException {
        final List<cgCache> caches = readGPX10(R.raw.gc31j2h_wpts);
        assertEquals(0, caches.size());
    }

    public static void testConvertWaypointSym2Type() {
        assertEquals(WaypointType.WAYPOINT, GPXParser.convertWaypointSym2Type("unknown sym"));

        assertEquals(WaypointType.PARKING, GPXParser.convertWaypointSym2Type("Parking area"));
        assertEquals(WaypointType.STAGE, GPXParser.convertWaypointSym2Type("Stages of a multicache"));
        assertEquals(WaypointType.PUZZLE, GPXParser.convertWaypointSym2Type("Question to answer"));
        assertEquals(WaypointType.TRAILHEAD, GPXParser.convertWaypointSym2Type("Trailhead"));
        assertEquals(WaypointType.FINAL, GPXParser.convertWaypointSym2Type("Final location"));
        assertEquals(WaypointType.WAYPOINT, GPXParser.convertWaypointSym2Type("Reference point"));
    }

    private static void assertGc31j2h(final cgCache cache) {
        assertEquals("GC31J2H", cache.getGeocode());
        assertEquals("Hockenheimer City-Brunnen", cache.getName());
        assertTrue("Hockenheimer City-Brunnen by vptsz, Multi-cache (2/1)", cache.getShortDescription().startsWith("Kurzer informativer Multi entlang der Brunnen"));
        assertTrue(cache.getDescription().startsWith("Cachemobile können kostenfrei am Messplatz geparkt werden."));
        assertTrue(cache.hasTrackables());
        assertEquals(2.0f, cache.getDifficulty(), 0.01f);
        assertEquals(1.0f, cache.getTerrain(), 0.01f);
        final Geopoint refCoordinates = new Geopoint("N 49° 19.122", "E 008° 32.739");
        assertEquals(refCoordinates, cache.getCoords());
        assertEquals("vptsz", cache.getOwner());
        assertEquals(CacheSize.SMALL, cache.getSize());
        assertEquals(CacheType.MULTI, cache.getType());
        assertFalse(cache.isArchived());
        assertFalse(cache.isDisabled());
        assertFalse(cache.isEventCache());
        assertFalse(cache.isPremiumMembersOnly());
        assertFalse(cache.isOwn());
        assertTrue(cache.isFound());
        assertEquals("Station3: Der zerbrochene Stein zählt doppelt.\nFinal: Oben neben dem Tor", cache.getHint());
        // logs
        assertEquals(6, cache.getLogs().size());
        final LogEntry log = cache.getLogs().get(5);
        assertEquals("Geoteufel", log.author);
        assertEquals(parseTime("2011-09-11T07:00:00Z"), log.date);
        assertEquals(-1, log.found);
        assertEquals("Sehr schöne Runde und wir haben wieder etwas Neues über Hockenheim gelernt. Super Tarnung.\nTFTC, Geoteufel", log.log);

        // following info is not contained in pocket query gpx file
        assertEquals(0, cache.getAttributes().size());
    }

    private static long parseTime(final String time) {
        try {
            return LOG_DATE_FORMAT.parse(time).getTime();
        } catch (ParseException e) {
            return 0;
        }
    }

    private static void assertGc31j2hWaypoints(final cgCache cache) {
        assertNotNull(cache.getWaypoints());
        assertEquals(2, cache.getWaypoints().size());
        cgWaypoint wp = cache.getWaypoints().get(0);
        assertEquals("GC31J2H", wp.getGeocode());
        assertEquals("00", wp.getPrefix());
        assertEquals("---", wp.getLookup());
        assertEquals("Parkplatz", wp.getName());
        assertEquals("Kostenfreies Parken (je nach Parkreihe Parkscheibe erforderlich)", wp.getNote());
        assertEquals(WaypointType.PARKING, wp.getWaypointType());
        assertEquals(49.317517, wp.getCoords().getLatitude(), 0.000001);
        assertEquals(8.545083, wp.getCoords().getLongitude(), 0.000001);

        wp = cache.getWaypoints().get(1);
        assertEquals("GC31J2H", wp.getGeocode());
        assertEquals("S1", wp.getPrefix());
        assertEquals("---", wp.getLookup());
        assertEquals("Station 1", wp.getName());
        assertEquals("Ein zweiter Wegpunkt, der nicht wirklich existiert sondern nur zum Testen gedacht ist.", wp.getNote());
        assertEquals(WaypointType.STAGE, wp.getWaypointType());
        assertEquals(49.317500, wp.getCoords().getLatitude(), 0.000001);
        assertEquals(8.545100, wp.getCoords().getLongitude(), 0.000001);
    }

    private List<cgCache> readGPX10(int... resourceIds) throws IOException, ParserException {
        final GPX10Parser parser = new GPX10Parser(1);
        return readVersionedGPX(parser, resourceIds);
    }

    private List<cgCache> readGPX11(int... resourceIds) throws IOException, ParserException {
        final GPX11Parser parser = new GPX11Parser(1);
        return readVersionedGPX(parser, resourceIds);
    }

    private List<cgCache> readVersionedGPX(final GPXParser parser, int... resourceIds) throws IOException, ParserException {
        Collection<cgCache> caches = null;
        for (int resourceId : resourceIds) {
            final InputStream instream = getResourceStream(resourceId);
            try {
                caches = parser.parse(instream, null);
                assertNotNull(caches);
            } finally {
                instream.close();
            }
        }

        List<cgCache> cacheList = new ArrayList<cgCache>(caches);
        // TODO: may need to sort by geocode when a test imports more than one cache
        return cacheList;
    }

    public static void testParseDateWithFractionalSeconds() {
        // was experienced in GSAK file
        String dateString = "2011-08-13T02:52:18.103Z";
        try {
            GPXParser.parseDate(dateString);
        } catch (ParseException e) {
            fail();
            e.printStackTrace();
        }
    }

    public static void testParseDateWithHugeFraction() {
        // see issue 821
        String dateString = "2011-11-07T00:00:00.0000000-07:00";
        try {
            GPXParser.parseDate(dateString);
        } catch (ParseException e) {
            fail();
            e.printStackTrace();
        }
    }

    public void testSelfmadeGPXWithoutGeocodes() throws Exception {
        final List<cgCache> caches = readGPX11(R.raw.no_connector);
        assertEquals(13, caches.size());
    }

    public void testTexasChallenge2012() throws Exception {
        final List<cgCache> caches = readGPX10(R.raw.challenge);
        // previously these caches overwrote each other during parsing
        assertEquals(130, caches.size());
    }
}