aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Merker <merker.stephan@googlemail.com>2011-10-29 00:30:59 +0200
committerStephan Merker <merker.stephan@googlemail.com>2011-10-30 13:18:11 +0100
commit5687446f0dd3c3b2db5f7dc18f95ee02f94f2ccc (patch)
tree973823a9f446eef7bd3173ecc8e39cd291d42622
parent6c74f71960125b5fefc6fbeb4b3d510ae2f921e8 (diff)
downloadcgeo-5687446f0dd3c3b2db5f7dc18f95ee02f94f2ccc.zip
cgeo-5687446f0dd3c3b2db5f7dc18f95ee02f94f2ccc.tar.gz
cgeo-5687446f0dd3c3b2db5f7dc18f95ee02f94f2ccc.tar.bz2
Added tests for importing GPX and LOC files
- refactored import classes in cgeogpxes so that they can be tested
-rw-r--r--main/src/cgeo/geocaching/cgeogpxes.java54
-rw-r--r--tests/src/cgeo/geocaching/cgeogpxesTest.java127
2 files changed, 163 insertions, 18 deletions
diff --git a/main/src/cgeo/geocaching/cgeogpxes.java b/main/src/cgeo/geocaching/cgeogpxes.java
index 865b9e7..ed8c526 100644
--- a/main/src/cgeo/geocaching/cgeogpxes.java
+++ b/main/src/cgeo/geocaching/cgeogpxes.java
@@ -112,12 +112,16 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
public void importGPX(final File file) {
createProgressDialog((int) file.length());
- new ImportFileThread(file).start();
+ if (StringUtils.endsWithIgnoreCase(file.getName(), GPXParser.GPX_FILE_EXTENSION)) {
+ new ImportGpxFileThread(file, listId, loadCachesHandler, changeParseDialogHandler).start();
+ } else {
+ new ImportLocFileThread(file, listId, loadCachesHandler, changeParseDialogHandler).start();
+ }
}
public void importGPX(final InputStream stream) {
createProgressDialog(-1);
- new ImportStreamThread(stream).start();
+ new ImportStreamThread(stream, listId, loadCachesHandler, changeParseDialogHandler).start();
}
private void createProgressDialog(int maxBytes) {
@@ -130,45 +134,65 @@ public class cgeogpxes extends FileList<cgGPXListAdapter> {
parseDialog.show();
}
- private abstract class ImportThread extends Thread {
+ static abstract class ImportThread extends Thread {
+ final int listId;
+ final Handler importStepHandler;
+ final Handler progressHandler;
+
+ public ImportThread(int listId, Handler importStepHandler, Handler progressHandler) {
+ this.listId = listId;
+ this.importStepHandler = importStepHandler;
+ this.progressHandler = progressHandler;
+ }
@Override
public void run() {
final cgSearch search = doImport();
- loadCachesHandler.sendMessage(loadCachesHandler.obtainMessage(0, cgeoapplication.getCount(search), 0));
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(0, cgeoapplication.getCount(search), 0, search));
}
protected abstract cgSearch doImport();
}
- private class ImportFileThread extends ImportThread {
+ static class ImportGpxFileThread extends ImportThread {
private final File file;
- public ImportFileThread(final File file) {
+ public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, Handler progressHandler) {
+ super(listId, importStepHandler, progressHandler);
this.file = file;
}
@Override
protected cgSearch doImport() {
- if (StringUtils.endsWithIgnoreCase(file.getName(), GPXParser.GPX_FILE_EXTENSION)) {
- return GPXParser.importGPX(file, listId, changeParseDialogHandler);
- }
- else {
- return LocParser.parseLoc(file, listId, changeParseDialogHandler);
- }
+ return GPXParser.importGPX(file, listId, progressHandler);
+ }
+ }
+
+ static class ImportLocFileThread extends ImportThread {
+ private final File file;
+
+ public ImportLocFileThread(final File file, int listId, Handler importStepHandler, Handler progressHandler) {
+ super(listId, importStepHandler, progressHandler);
+ this.file = file;
+ }
+
+ @Override
+ protected cgSearch doImport() {
+ return LocParser.parseLoc(file, listId, progressHandler);
}
}
- private class ImportStreamThread extends ImportThread {
+ static class ImportStreamThread extends ImportThread {
private final InputStream stream;
- public ImportStreamThread(InputStream stream) {
+ public ImportStreamThread(InputStream stream, int listId, Handler importStepHandler, Handler progressHandler) {
+ super(listId, importStepHandler, progressHandler);
this.stream = stream;
}
@Override
protected cgSearch doImport() {
- return GPXParser.importGPX(stream, listId, changeParseDialogHandler);
+ return GPXParser.importGPX(stream, listId, progressHandler);
}
}
diff --git a/tests/src/cgeo/geocaching/cgeogpxesTest.java b/tests/src/cgeo/geocaching/cgeogpxesTest.java
index de2cc29..4441a09 100644
--- a/tests/src/cgeo/geocaching/cgeogpxesTest.java
+++ b/tests/src/cgeo/geocaching/cgeogpxesTest.java
@@ -1,11 +1,82 @@
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.InstrumentationTestCase;
-public class cgeogpxesTest extends AndroidTestCase {
+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 InstrumentationTestCase {
+ private int listId;
private cgeogpxes cgeogpxes = new cgeogpxes();
+ private File tempDir;
+ private TestHandler importStepHandler = new TestHandler();
+ private TestHandler progressHandler = new TestHandler();
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ tempDir = new File(System.getProperty("java.io.tmpdir"), "cgeogpxesTest");
+ tempDir.mkdir();
+
+ 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"));
@@ -22,4 +93,54 @@ public class cgeogpxesTest extends AndroidTestCase {
assertFalse(cgeogpxes.filenameBelongsToList("1234567-wpts.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(1, importStepHandler.messages.size());
+ cgSearch search = (cgSearch) importStepHandler.messages.get(0).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(1, importStepHandler.messages.size());
+ cgSearch search = (cgSearch) importStepHandler.messages.get(0).obj;
+ assertEquals(Collections.singletonList("GC31J2H"), search.getGeocodes());
+
+ cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("GC31J2H");
+ assertNotNull(cache);
+ assertEquals(2, cache.waypoints.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(1, importStepHandler.messages.size());
+ cgSearch search = (cgSearch) importStepHandler.messages.get(0).obj;
+ assertEquals(Collections.singletonList("OC5952"), search.getGeocodes());
+
+ cgCache cache = cgeoapplication.getInstance().getCacheByGeocode("OC5952");
+ assertNotNull(cache);
+ }
}