diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/res/raw/gc31j2h_err.gpx | 17 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/cgeogpxesTest.java | 87 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/files/GPXParserTest.java | 36 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/files/LocParserTest.java | 75 |
4 files changed, 128 insertions, 87 deletions
diff --git a/tests/res/raw/gc31j2h_err.gpx b/tests/res/raw/gc31j2h_err.gpx new file mode 100644 index 0000000..2c41bb2 --- /dev/null +++ b/tests/res/raw/gc31j2h_err.gpx @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?>
+<gpx xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" creator="Groundspeak Pocket Query" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0">
+ <name>Testdata</name>
+ <desc>Geocache file generated by Groundspeak</desc>
+ <author>Groundspeak</author>
+ <email>contact@groundspeak.com</email>
+ <time>2011-09-16T21:13:25.1723847Z</time>
+ <keywords>cache, geocache, groundspeak</keywords>
+ <bounds minlat="49.3187" minlon="8.54565" maxlat="49.3187" maxlon="8.54565" />
+ <wpt lat="49.3187" lon="8.54565">
+ <time>2011-08-05T07:00:00Z</time>
+ <name>GC31J2H</name>
+ <desc>Hockenheimer City-Brunnen by vptsz, Multi-cache (2/1)</desc>
+ <url>http://www.geocaching.com/seek/cache_details.aspx?guid=0eba627b-bc4c-4e37-9bae-80dad5b97815</url>
+ <urlname>Hockenheimer City-Brunnen</urlname>
+ <sym>Geocache Found</sym>
+ <type>Geocache|Multi-cache</type>
diff --git a/tests/src/cgeo/geocaching/cgeogpxesTest.java b/tests/src/cgeo/geocaching/cgeogpxesTest.java index 4441a09..7d9a0de 100644 --- a/tests/src/cgeo/geocaching/cgeogpxesTest.java +++ b/tests/src/cgeo/geocaching/cgeogpxesTest.java @@ -4,7 +4,7 @@ import cgeo.geocaching.test.R; import android.os.Handler; import android.os.Message; -import android.test.InstrumentationTestCase; +import android.test.ActivityInstrumentationTestCase2; import java.io.File; import java.io.FileOutputStream; @@ -14,13 +14,17 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class cgeogpxesTest extends InstrumentationTestCase { +public class cgeogpxesTest extends ActivityInstrumentationTestCase2<cgeogpxes> { private int listId; - private cgeogpxes cgeogpxes = new cgeogpxes(); + private cgeogpxes importGpxActivity = new cgeogpxes(); private File tempDir; private TestHandler importStepHandler = new TestHandler(); private TestHandler progressHandler = new TestHandler(); + public cgeogpxesTest() { + super("cgeo.geocaching", cgeogpxes.class); + } + @Override protected void setUp() throws Exception { super.setUp(); @@ -28,6 +32,8 @@ public class cgeogpxesTest extends InstrumentationTestCase { tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest"); tempDir.mkdir(); + // workaround to get storage initialized + cgeoapplication.getInstance().getAllHistoricCachesCount(); listId = cgeoapplication.getInstance().createList("cgeogpxesTest"); } @@ -79,19 +85,33 @@ public class cgeogpxesTest extends InstrumentationTestCase { } public void testFileNameMatches() { - assertTrue(cgeogpxes.filenameBelongsToList("1234567.gpx")); - assertTrue(cgeogpxes.filenameBelongsToList("1234567.GPX")); - assertTrue(cgeogpxes.filenameBelongsToList(".gpx")); - assertTrue(cgeogpxes.filenameBelongsToList("1234567.loc")); - assertTrue(cgeogpxes.filenameBelongsToList("1234567.LOC")); - - assertFalse(cgeogpxes.filenameBelongsToList("1234567.gpy")); - assertFalse(cgeogpxes.filenameBelongsToList("1234567.agpx")); - assertFalse(cgeogpxes.filenameBelongsToList("1234567")); - assertFalse(cgeogpxes.filenameBelongsToList("")); - assertFalse(cgeogpxes.filenameBelongsToList("gpx")); - - assertFalse(cgeogpxes.filenameBelongsToList("1234567-wpts.gpx")); + assertTrue(importGpxActivity.filenameBelongsToList("1234567.gpx")); + assertTrue(importGpxActivity.filenameBelongsToList("1234567.GPX")); + assertTrue(importGpxActivity.filenameBelongsToList(".gpx")); + assertTrue(importGpxActivity.filenameBelongsToList("1234567.loc")); + assertTrue(importGpxActivity.filenameBelongsToList("1234567.LOC")); + + assertFalse(importGpxActivity.filenameBelongsToList("1234567.gpy")); + assertFalse(importGpxActivity.filenameBelongsToList("1234567.agpx")); + assertFalse(importGpxActivity.filenameBelongsToList("1234567")); + assertFalse(importGpxActivity.filenameBelongsToList("")); + assertFalse(importGpxActivity.filenameBelongsToList("gpx")); + + assertFalse(importGpxActivity.filenameBelongsToList("1234567-wpts.gpx")); + } + + public void testGetWaypointsFileForGpx() { + assertEquals(new File("1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.gpx"))); + assertEquals(new File("/mnt/sdcard/1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/1234567.gpx"))); + assertEquals(new File("/mnt/sdcard/1-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/1.gpx"))); + assertEquals(new File("/mnt/sd.card/1-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sd.card/1.gpx"))); + assertEquals(new File("1234567.9-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.9.gpx"))); + assertEquals(new File("1234567-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("1234567.GPX"))); + assertEquals(new File("gpx.gpx-wpts.gpx"), cgeogpxes.getWaypointsFileForGpx(new File("gpx.gpx.gpx"))); + assertNull(cgeogpxes.getWaypointsFileForGpx(new File("123.gpy"))); + assertNull(cgeogpxes.getWaypointsFileForGpx(new File("gpx"))); + assertNull(cgeogpxes.getWaypointsFileForGpx(new File(".gpx"))); + assertNull(cgeogpxes.getWaypointsFileForGpx(new File("/mnt/sdcard/.gpx"))); } public void testImportGpx() throws IOException { @@ -101,8 +121,11 @@ public class cgeogpxesTest extends InstrumentationTestCase { cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); importThread.run(); - assertEquals(1, importStepHandler.messages.size()); - cgSearch search = (cgSearch) importStepHandler.messages.get(0).obj; + assertEquals(3, importStepHandler.messages.size()); + assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); + assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what); + assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what); + cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj; assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); @@ -120,8 +143,12 @@ public class cgeogpxesTest extends InstrumentationTestCase { cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); importThread.run(); - assertEquals(1, importStepHandler.messages.size()); - cgSearch search = (cgSearch) importStepHandler.messages.get(0).obj; + assertEquals(4, importStepHandler.messages.size()); + assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); + assertEquals(cgeogpxes.IMPORT_STEP_READ_WPT_FILE, importStepHandler.messages.get(1).what); + assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(2).what); + assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(3).what); + cgSearch search = (cgSearch) importStepHandler.messages.get(3).obj; assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes()); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H"); @@ -136,11 +163,27 @@ public class cgeogpxesTest extends InstrumentationTestCase { cgeogpxes.ImportLocFileThread importThread = new cgeogpxes.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler); importThread.run(); - assertEquals(1, importStepHandler.messages.size()); - cgSearch search = (cgSearch) importStepHandler.messages.get(0).obj; + assertEquals(3, importStepHandler.messages.size()); + assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); + assertEquals(cgeogpxes.IMPORT_STEP_STORE_CACHES, importStepHandler.messages.get(1).what); + assertEquals(cgeogpxes.IMPORT_STEP_FINISHED, importStepHandler.messages.get(2).what); + cgSearch search = (cgSearch) importStepHandler.messages.get(2).obj; assertEquals(Collections.singletonList("OC5952"), search.getGeocodes()); cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952"); assertNotNull(cache); } + + public void testImportGpxError() throws IOException { + File gc31j2h = new File(tempDir, "gc31j2h.gpx"); + copyResourceToFile(R.raw.gc31j2h_err, gc31j2h); + + cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); + importThread.run(); + + assertEquals(2, importStepHandler.messages.size()); + assertEquals(cgeogpxes.IMPORT_STEP_READ_FILE, importStepHandler.messages.get(0).what); + assertEquals(cgeogpxes.IMPORT_STEP_FINISHED_WITH_ERROR, importStepHandler.messages.get(1).what); + } + } diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index 8a229ef..8fed2ae 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -14,7 +14,6 @@ import android.content.res.Resources; import android.os.Handler; import android.test.InstrumentationTestCase; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; @@ -30,7 +29,7 @@ public class GPXParserTest extends InstrumentationTestCase { testGPXVersion(R.raw.gc1bkp3_gpx100); } - private cgCache testGPXVersion(final int resourceId) throws IOException { + private cgCache testGPXVersion(final int resourceId) throws IOException, ParserException { final List<cgCache> caches = readGPX(resourceId); assertNotNull(caches); assertEquals(1, caches.size()); @@ -51,13 +50,13 @@ public class GPXParserTest extends InstrumentationTestCase { return cache; } - public void testGPXVersion101() throws IOException { + public void testGPXVersion101() throws IOException, ParserException { final cgCache cache = testGPXVersion(R.raw.gc1bkp3_gpx101); assertNotNull(cache.attributes); assertEquals(10, cache.attributes.size()); } - public void testOC() throws IOException { + public void testOC() throws IOException, ParserException { final List<cgCache> caches = readGPX(R.raw.oc5952_gpx); final cgCache cache = caches.get(0); assertEquals("OC5952", cache.geocode); @@ -74,7 +73,7 @@ public class GPXParserTest extends InstrumentationTestCase { assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(cache.coords)); } - public void testGc31j2h() throws IOException { + public void testGc31j2h() throws IOException, ParserException { final List<cgCache> caches = readGPX(R.raw.gc31j2h); assertEquals(1, caches.size()); final cgCache cache = caches.get(0); @@ -86,7 +85,7 @@ public class GPXParserTest extends InstrumentationTestCase { assertNull(cache.waypoints); } - public void testGc31j2hWpts() throws IOException { + public void testGc31j2hWpts() throws IOException, ParserException { List<cgCache> caches = readGPX(R.raw.gc31j2h, R.raw.gc31j2h_wpts); assertEquals(1, caches.size()); cgCache cache = caches.get(0); @@ -94,7 +93,7 @@ public class GPXParserTest extends InstrumentationTestCase { assertGc31j2hWaypoints(cache); } - public void testGc31j2hWptsWithoutCache() throws IOException { + public void testGc31j2hWptsWithoutCache() throws IOException, ParserException { final List<cgCache> caches = readGPX(R.raw.gc31j2h_wpts); assertEquals(0, caches.size()); } @@ -175,33 +174,20 @@ public class GPXParserTest extends InstrumentationTestCase { assertEquals(8.545100, wp.coords.getLongitude(), 0.000001); } - public static void testGetWaypointsFileForGpx() { - assertEquals(new File("1234567-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("1234567.gpx"))); - assertEquals(new File("/mnt/sdcard/1234567-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("/mnt/sdcard/1234567.gpx"))); - assertEquals(new File("/mnt/sdcard/1-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("/mnt/sdcard/1.gpx"))); - assertEquals(new File("/mnt/sd.card/1-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("/mnt/sd.card/1.gpx"))); - assertEquals(new File("1234567.9-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("1234567.9.gpx"))); - assertEquals(new File("1234567-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("1234567.GPX"))); - assertEquals(new File("gpx.gpx-wpts.gpx"), GPXParser.getWaypointsFileForGpx(new File("gpx.gpx.gpx"))); - assertNull(GPXParser.getWaypointsFileForGpx(new File("123.gpy"))); - assertNull(GPXParser.getWaypointsFileForGpx(new File("gpx"))); - assertNull(GPXParser.getWaypointsFileForGpx(new File(".gpx"))); - assertNull(GPXParser.getWaypointsFileForGpx(new File("/mnt/sdcard/.gpx"))); - } - - private List<cgCache> readGPX(int... resourceIds) throws IOException { + private List<cgCache> readGPX(int... resourceIds) throws IOException, ParserException { final GPX10Parser parser = new GPX10Parser(1); + Collection<cgCache> caches = null; for (int resourceId : resourceIds) { final Resources res = getInstrumentation().getContext().getResources(); final InputStream instream = res.openRawResource(resourceId); try { - assertTrue(parser.parse(instream, new Handler())); + caches = parser.parse(instream, new Handler()); + assertNotNull(caches); } finally { instream.close(); } } - Collection<cgCache> caches = parser.getParsedCaches(); - assertNotNull(caches); + List<cgCache> cacheList = new ArrayList<cgCache>(caches); // TODO: may need to sort by geocode when a test imports more than one cache return cacheList; diff --git a/tests/src/cgeo/geocaching/files/LocParserTest.java b/tests/src/cgeo/geocaching/files/LocParserTest.java index 9d90ef4..9b633e3 100644 --- a/tests/src/cgeo/geocaching/files/LocParserTest.java +++ b/tests/src/cgeo/geocaching/files/LocParserTest.java @@ -1,64 +1,59 @@ package cgeo.geocaching.files; -import cgeo.geocaching.cgCoord; +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.Map; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; public class LocParserTest extends InstrumentationTestCase { - private Map<String, cgCoord> readLoc(int resourceId) { - Map<String, cgCoord> caches = null; + 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 { - final StringBuilder buffer = new StringBuilder(); - int ch; - while ((ch = instream.read()) != -1) { - buffer.append((char) ch); - } - caches = LocParser.parseCoordinates(buffer.toString()); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + caches = parser.parse(instream, new Handler()); + assertNotNull(caches); + assertTrue(caches.size() > 0); } finally { - try { - instream.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + instream.close(); } - assertNotNull(caches); - assertTrue(caches.size() > 0); - return caches; - } - public void testOCLoc() { - final Map<String, cgCoord> coords = readLoc(R.raw.oc5952_loc); - final cgCoord coord = coords.get("OC5952"); - assertNotNull(coord); - assertEquals("OC5952", coord.geocode); - assertEquals("Die Schatzinsel / treasure island", coord.name); - assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(coord.coords)); + 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 testGCLoc() { - final Map<String, cgCoord> coords = readLoc(R.raw.gc1bkp3_loc); - final cgCoord coord = coords.get("GC1BKP3"); - assertNotNull(coord); - assertEquals("GC1BKP3", coord.geocode); - assertEquals("Die Schatzinsel / treasure island", coord.name); - assertTrue(new Geopoint(48.859683, 9.1874).isEqualTo(coord.coords)); - assertEquals(1.0f, coord.difficulty.floatValue()); - assertEquals(5.0f, coord.terrain.floatValue()); - assertEquals(CacheSize.MICRO, coord.size); + 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.geocode); + assertEquals("Die Schatzinsel / treasure island", cache.name); + assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(cache.coords)); } + 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.geocode); + assertEquals("Die Schatzinsel / treasure island", cache.name); + assertTrue(new Geopoint(48.859683, 9.1874).isEqualTo(cache.coords)); + assertEquals(1.0f, cache.difficulty.floatValue()); + assertEquals(5.0f, cache.terrain.floatValue()); + assertEquals(CacheSize.MICRO, cache.size); + } } |
