aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorBananeweizen <bananeweizen@gmx.de>2011-11-01 19:25:40 +0100
committerBananeweizen <bananeweizen@gmx.de>2011-11-01 19:25:40 +0100
commit6a8f40ccb8c7761e75e4041cb7e53c4df4064573 (patch)
treea890ed1a7a2f3d77cb3babe929e3316666333797 /tests/src
parenta5571fe16d358c91b60be1f45085d54f16df800b (diff)
parent96fbeff00e6793eed966c15f5829e3d18e16b326 (diff)
downloadcgeo-6a8f40ccb8c7761e75e4041cb7e53c4df4064573.zip
cgeo-6a8f40ccb8c7761e75e4041cb7e53c4df4064573.tar.gz
cgeo-6a8f40ccb8c7761e75e4041cb7e53c4df4064573.tar.bz2
Merge remote branch 'stephanme/importwpt'
Conflicts: main/src/cgeo/geocaching/files/GPXParser.java main/src/cgeo/geocaching/files/LocParser.java tests/src/cgeo/geocaching/files/LocParserTest.java
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/cgeo/geocaching/cgeogpxesTest.java194
-rw-r--r--tests/src/cgeo/geocaching/files/GPXParserTest.java36
-rw-r--r--tests/src/cgeo/geocaching/files/LocParserTest.java75
3 files changed, 225 insertions, 80 deletions
diff --git a/tests/src/cgeo/geocaching/cgeogpxesTest.java b/tests/src/cgeo/geocaching/cgeogpxesTest.java
index de2cc29..90ce201 100644
--- a/tests/src/cgeo/geocaching/cgeogpxesTest.java
+++ b/tests/src/cgeo/geocaching/cgeogpxesTest.java
@@ -1,25 +1,189 @@
package cgeo.geocaching;
-import cgeo.geocaching.cgeogpxes;
+import cgeo.geocaching.test.R;
-import android.test.AndroidTestCase;
+import android.os.Handler;
+import android.os.Message;
+import android.test.ActivityInstrumentationTestCase2;
-public class cgeogpxesTest extends AndroidTestCase {
- private cgeogpxes cgeogpxes = new cgeogpxes();
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class cgeogpxesTest extends ActivityInstrumentationTestCase2<cgeogpxes> {
+ private int listId;
+ 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();
+
+ tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest");
+ tempDir.mkdir();
+
+ // workaround to get storage initialized
+ cgeoapplication.getInstance().getAllHistoricCachesCount();
+ listId = cgeoapplication.getInstance().createList("cgeogpxesTest");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ cgeoapplication.getInstance().dropStored(listId);
+ cgeoapplication.getInstance().removeList(listId);
+ deleteDirectory(tempDir);
+ super.tearDown();
+ }
+
+
+ private void copyResourceToFile(int resourceId, File file) throws IOException {
+ final InputStream is = getInstrumentation().getContext().getResources().openRawResource(resourceId);
+ final FileOutputStream os = new FileOutputStream(file);
+
+ try {
+ byte[] buffer = new byte[4096];
+ int byteCount;
+ while ((byteCount = is.read(buffer)) >= 0) {
+ os.write(buffer, 0, byteCount);
+ }
+ } finally {
+ os.close();
+ is.close();
+ }
+ }
+
+ private void deleteDirectory(File dir) {
+ for (File f : dir.listFiles()) {
+ if (f.isFile()) {
+ f.delete();
+ } else if (f.isDirectory()) {
+ deleteDirectory(f);
+ }
+ }
+ dir.delete();
+ }
+
+ static class TestHandler extends Handler {
+ List<Message> messages = new ArrayList<Message>();
+
+ @Override
+ public void handleMessage(Message msg) {
+ Message msg1 = new Message();
+ msg1.copyFrom(msg);
+ messages.add(msg1);
+ }
+ }
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"));
+ 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(cgeogpxes.filenameBelongsToList("1234567.gpy"));
- assertFalse(cgeogpxes.filenameBelongsToList("1234567.agpx"));
- assertFalse(cgeogpxes.filenameBelongsToList("1234567"));
- assertFalse(cgeogpxes.filenameBelongsToList(""));
- assertFalse(cgeogpxes.filenameBelongsToList("gpx"));
+ assertFalse(importGpxActivity.filenameBelongsToList("1234567.gpy"));
+ assertFalse(importGpxActivity.filenameBelongsToList("1234567.agpx"));
+ assertFalse(importGpxActivity.filenameBelongsToList("1234567"));
+ assertFalse(importGpxActivity.filenameBelongsToList(""));
+ assertFalse(importGpxActivity.filenameBelongsToList("gpx"));
- assertFalse(cgeogpxes.filenameBelongsToList("1234567-wpts.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 {
+ File gc31j2h = new File(tempDir, "gc31j2h.gpx");
+ copyResourceToFile(R.raw.gc31j2h, gc31j2h);
+
+ cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ 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");
+ assertNotNull(cache);
+
+ // can't assert, for whatever reason the waypoints are remembered in DB
+ // assertNull(cache.waypoints);
+ }
+
+ public void testImportGpxWithWaypoints() throws IOException {
+ File gc31j2h = new File(tempDir, "gc31j2h.gpx");
+ copyResourceToFile(R.raw.gc31j2h, gc31j2h);
+ copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx"));
+
+ cgeogpxes.ImportGpxFileThread importThread = new cgeogpxes.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ 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");
+ assertNotNull(cache);
+ assertEquals(2, cache.getWaypoints().size());
+ }
+
+ public void testImportLoc() throws IOException {
+ File oc5952 = new File(tempDir, "oc5952.loc");
+ copyResourceToFile(R.raw.oc5952_loc, oc5952);
+
+ cgeogpxes.ImportLocFileThread importThread = new cgeogpxes.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler);
+ importThread.run();
+
+ 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 bc795e2..c3e253a 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.getAttributes());
assertEquals(10, cache.getAttributes().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.getGeocode());
@@ -74,7 +73,7 @@ public class GPXParserTest extends InstrumentationTestCase {
assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(cache.getCoords()));
}
- 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.getWaypoints());
}
- 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.getCoords().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 6e74061..6fa8182 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.getGeocode());
- assertEquals("Die Schatzinsel / treasure island", coord.getName());
- assertTrue(new Geopoint(48.85968, 9.18740).isEqualTo(coord.getCoords()));
+ 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.getGeocode());
- assertEquals("Die Schatzinsel / treasure island", coord.getName());
- assertTrue(new Geopoint(48.859683, 9.1874).isEqualTo(coord.getCoords()));
- assertEquals(1.0f, coord.getDifficulty().floatValue());
- assertEquals(5.0f, coord.getTerrain().floatValue());
- assertEquals(CacheSize.MICRO, coord.getSize());
+ 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());
+ }
}