aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/files/LocParserTest.java
blob: 6fa81829064763e5f144b8083b49b14862dbe925 (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
package cgeo.geocaching.files;

import cgeo.geocaching.cgCache;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.test.R;

import android.content.res.Resources;
import android.os.Handler;
import android.test.InstrumentationTestCase;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class LocParserTest extends InstrumentationTestCase {
    private List<cgCache> readLoc(int resourceId) throws IOException, ParserException {
        LocParser parser = new LocParser(1);
        Collection<cgCache> caches = null;
        final Resources res = getInstrumentation().getContext().getResources();
        final InputStream instream = res.openRawResource(resourceId);
        try {
            caches = parser.parse(instream, new Handler());
            assertNotNull(caches);
            assertTrue(caches.size() > 0);
        } 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 void testOCLoc() throws IOException, ParserException {
        final List<cgCache> caches = readLoc(R.raw.oc5952_loc);
        assertEquals(1, caches.size());
        final cgCache cache = caches.get(0);
        assertNotNull(cache);
        assertEquals("OC5952", cache.getGeocode());
        assertEquals("Die Schatzinsel / treasure island", cache.getName());
        assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(cache.getCoords()));
    }

    public void testGCLoc() throws IOException, ParserException {
        final List<cgCache> caches = readLoc(R.raw.gc1bkp3_loc);
        assertEquals(1, caches.size());
        final cgCache cache = caches.get(0);
        assertNotNull(cache);
        assertEquals("GC1BKP3", cache.getGeocode());
        assertEquals("Die Schatzinsel / treasure island", cache.getName());
        assertTrue(new Geopoint(48.859683, 9.1874).isEqualTo(cache.getCoords()));
        assertEquals(1.0f, cache.getDifficulty().floatValue());
        assertEquals(5.0f, cache.getTerrain().floatValue());
        assertEquals(CacheSize.MICRO, cache.getSize());
    }
}