diff options
12 files changed, 143 insertions, 108 deletions
diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 491f8ad..26ddc2c 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -2437,9 +2437,7 @@ public class cgData { } public void removeCache(final String geocode, EnumSet<LoadFlags.RemoveFlag> removeFlags) { - Set<String> geocodes = new HashSet<String>(); - geocodes.add(geocode); - removeCaches(geocodes, removeFlags); + removeCaches(Collections.singleton(geocode), removeFlags); } /** @@ -2477,7 +2475,11 @@ public class cgData { database.delete(dbTableLogs, baseWhereClause, null); database.delete(dbTableLogCount, baseWhereClause, null); database.delete(dbTableLogsOffline, baseWhereClause, null); - database.delete(dbTableWaypoints, baseWhereClause + " and type <> 'own'", null); + String wayPointClause = baseWhereClause; + if (!removeFlags.contains(RemoveFlag.REMOVE_OWN_WAYPOINTS_ONLY_FOR_TESTING)) { + wayPointClause += " and type <> 'own'"; + } + database.delete(dbTableWaypoints, wayPointClause, null); database.delete(dbTableTrackables, baseWhereClause, null); database.setTransactionSuccessful(); } finally { diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 8a2e984..572420d 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -61,7 +61,10 @@ public class cgeoapplication extends Application { @Override public void onLowMemory() { Log.i("Cleaning applications cache."); + removeAllFromCache(); + } + public void removeAllFromCache() { storage.removeAllFromCache(); } diff --git a/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java b/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java new file mode 100644 index 0000000..377785a --- /dev/null +++ b/main/src/cgeo/geocaching/connector/ox/OXGPXParser.java @@ -0,0 +1,23 @@ +package cgeo.geocaching.connector.ox; + +import cgeo.geocaching.cgCache; +import cgeo.geocaching.files.GPX10Parser; + +public class OXGPXParser extends GPX10Parser { + + private final boolean isDetailed; + + public OXGPXParser(int listIdIn, boolean isDetailed) { + super(listIdIn); + this.isDetailed = isDetailed; + } + + @Override + protected void afterParsing(cgCache cache) { + cache.setUpdated(System.currentTimeMillis()); + if (isDetailed) { + cache.setDetailedUpdate(cache.getUpdated()); + cache.setDetailed(true); + } + } +} diff --git a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java index 7ed6073..17be540 100644 --- a/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java +++ b/main/src/cgeo/geocaching/connector/ox/OpenCachingApi.java @@ -2,9 +2,6 @@ package cgeo.geocaching.connector.ox; import cgeo.geocaching.StoredList; import cgeo.geocaching.cgCache; -import cgeo.geocaching.cgeoapplication; -import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; -import cgeo.geocaching.files.GPX10Parser; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.geopoint.GeopointFormatter; import cgeo.geocaching.network.Network; @@ -13,11 +10,11 @@ import cgeo.geocaching.utils.CryptUtils; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; + import org.apache.commons.collections.CollectionUtils; import java.util.Collection; import java.util.Collections; -import java.util.EnumSet; public class OpenCachingApi { @@ -43,21 +40,11 @@ public class OpenCachingApi { } Collection<cgCache> caches; try { - caches = new GPX10Parser(StoredList.STANDARD_LIST_ID).parse(response.getEntity().getContent(), null); + caches = new OXGPXParser(StoredList.STANDARD_LIST_ID, isDetailed).parse(response.getEntity().getContent(), null); } catch (Exception e) { Log.e("Error importing from OpenCaching.com", e); return Collections.emptyList(); } - for (cgCache cache : caches) { - cache.setUpdated(System.currentTimeMillis()); - if (isDetailed) { - cache.setDetailedUpdate(cache.getUpdated()); - cache.setDetailed(true); - } - - // save full detailed caches - cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); - } return caches; } diff --git a/main/src/cgeo/geocaching/enumerations/LoadFlags.java b/main/src/cgeo/geocaching/enumerations/LoadFlags.java index c56a7ba..fb894ac 100644 --- a/main/src/cgeo/geocaching/enumerations/LoadFlags.java +++ b/main/src/cgeo/geocaching/enumerations/LoadFlags.java @@ -37,9 +37,10 @@ public interface LoadFlags { public enum RemoveFlag { REMOVE_CACHE, // save only to CacheCache - REMOVE_DB // includes removing from CacheCache + REMOVE_DB, // includes removing from CacheCache + REMOVE_OWN_WAYPOINTS_ONLY_FOR_TESTING // only to be used in unit testing (as we never delete own waypoints) } - public final static EnumSet<RemoveFlag> REMOVE_ALL = EnumSet.allOf(RemoveFlag.class); + public final static EnumSet<RemoveFlag> REMOVE_ALL = EnumSet.of(RemoveFlag.REMOVE_CACHE, RemoveFlag.REMOVE_DB); }
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/files/FileParser.java b/main/src/cgeo/geocaching/files/FileParser.java index 090573a..0308d81 100644 --- a/main/src/cgeo/geocaching/files/FileParser.java +++ b/main/src/cgeo/geocaching/files/FileParser.java @@ -16,11 +16,11 @@ import java.util.concurrent.CancellationException; public abstract class FileParser { /** * Parses caches from input stream. - * + * * @param stream * @param progressHandler * for reporting parsing progress (in bytes read from input stream) - * @return collection of parsed caches + * @return collection of caches * @throws IOException * if the input stream can't be read * @throws ParserException diff --git a/main/src/cgeo/geocaching/files/GPX10Parser.java b/main/src/cgeo/geocaching/files/GPX10Parser.java index 85884de..0ca2606 100644 --- a/main/src/cgeo/geocaching/files/GPX10Parser.java +++ b/main/src/cgeo/geocaching/files/GPX10Parser.java @@ -2,7 +2,7 @@ package cgeo.geocaching.files; import android.sax.Element; -public final class GPX10Parser extends GPXParser { +public class GPX10Parser extends GPXParser { public GPX10Parser(int listIdIn) { super(listIdIn, "http://www.topografix.com/GPX/1/0", "1.0"); diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index 1bb3c3f..8369161 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -9,7 +9,6 @@ import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.activity.IAbstractActivity;
import cgeo.geocaching.activity.Progress;
import cgeo.geocaching.enumerations.LoadFlags;
-import cgeo.geocaching.enumerations.LoadFlags.SaveFlag;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.Log;
@@ -32,7 +31,6 @@ import java.io.InputStream; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipEntry;
@@ -42,7 +40,6 @@ public class GPXImporter { static final int IMPORT_STEP_START = 0;
static final int IMPORT_STEP_READ_FILE = 1;
static final int IMPORT_STEP_READ_WPT_FILE = 2;
- static final int IMPORT_STEP_STORE_CACHES = 3;
static final int IMPORT_STEP_STORE_STATIC_MAPS = 4;
static final int IMPORT_STEP_FINISHED = 5;
static final int IMPORT_STEP_FINISHED_WITH_ERROR = 6;
@@ -139,11 +136,13 @@ public class GPXImporter { try {
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_START));
caches = doImport();
-
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_CACHES, R.string.gpx_import_storing, caches.size()));
- SearchResult search = storeParsedCaches(caches);
Log.i("Imported successfully " + caches.size() + " caches.");
+ final SearchResult search = new SearchResult();
+ for (cgCache cache : caches) {
+ search.addCache(cache);
+ }
+
if (Settings.isStoreOfflineMaps() || Settings.isStoreOfflineWpMaps()) {
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_STATIC_MAPS, R.string.gpx_import_store_static_maps, search.getCount()));
boolean finishedWithoutCancel = importStaticMaps(search);
@@ -171,24 +170,6 @@ public class GPXImporter { protected abstract Collection<cgCache> doImport() throws IOException, ParserException;
- private SearchResult storeParsedCaches(Collection<cgCache> caches) {
- final SearchResult search = new SearchResult();
- final cgeoapplication app = cgeoapplication.getInstance();
- int storedCaches = 0;
- for (cgCache cache : caches) {
- search.addCache(cache);
- if (app.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB))) {
- storedCaches++;
- }
-
- if (progressHandler.isCancelled()) {
- throw new CancellationException();
- }
- progressHandler.sendMessage(progressHandler.obtainMessage(0, storedCaches, 0));
- }
- return search;
- }
-
private boolean importStaticMaps(final SearchResult importedCaches) {
final cgeoapplication app = cgeoapplication.getInstance();
int storedCacheMaps = 0;
@@ -398,13 +379,6 @@ public class GPXImporter { progress.setMaxProgressAndReset(msg.arg2);
break;
- case IMPORT_STEP_STORE_CACHES:
- progress.setProgressDivider(1);
- progress.setMessage(res.getString(msg.arg1));
- progress.setMaxProgressAndReset(msg.arg2);
- showProgressAfterCancel = true;
- break;
-
case IMPORT_STEP_STORE_STATIC_MAPS:
progress.dismiss();
Message skipMessage = importStepHandler.obtainMessage(IMPORT_STEP_STATIC_MAPS_SKIPPED, msg.arg2, 0);
diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 454c494..ea26a66 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -10,6 +10,9 @@ import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.LoadFlag; +import cgeo.geocaching.enumerations.LoadFlags.SaveFlag; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; @@ -33,10 +36,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -89,7 +92,7 @@ public abstract class GPXParser extends FileParser { /** * Parser result. Maps geocode to cache. */ - private final Map<String, cgCache> result = new HashMap<String, cgCache>(100); + private final Set<String> result = new HashSet<String>(100); private ProgressInputStream progressStream; private final class UserDataListener implements EndTextElementListener { @@ -298,11 +301,17 @@ public abstract class GPXParser extends FileParser { createNoteFromGSAKUserdata(); - final String key = cache.getGeocode(); - if (result.containsKey(key)) { - Log.w("Duplicate geocode during GPX import: " + key); + final String geocode = cache.getGeocode(); + if (result.contains(geocode)) { + Log.w("Duplicate geocode during GPX import: " + geocode); } - result.put(key, cache); + // modify cache depending on the use case/connector + afterParsing(cache); + + // finally store the cache in the database + result.add(geocode); + cgeoapplication.getInstance().saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); + cgeoapplication.getInstance().removeAllFromCache(); showProgressMessage(progressHandler, progressStream.getProgress()); } else if (StringUtils.isNotBlank(cache.getName()) && StringUtils.containsIgnoreCase(type, "waypoint")) { @@ -319,7 +328,7 @@ public abstract class GPXParser extends FileParser { final String cacheGeocodeForWaypoint = "GC" + cache.getName().substring(2).toUpperCase(); // lookup cache for waypoint in already parsed caches - final cgCache cacheForWaypoint = result.get(cacheGeocodeForWaypoint); + final cgCache cacheForWaypoint = cgeoapplication.getInstance().loadCache(cacheGeocodeForWaypoint, LoadFlags.LOAD_CACHE_OR_DB); if (cacheForWaypoint != null) { final cgWaypoint waypoint = new cgWaypoint(cache.getShortdesc(), convertWaypointSym2Type(sym), false); waypoint.setId(-1); @@ -330,12 +339,14 @@ public abstract class GPXParser extends FileParser { waypoint.setCoords(cache.getCoords()); waypoint.setNote(cache.getDescription()); - ArrayList<cgWaypoint> mergedWayPoints = new ArrayList<cgWaypoint>(); + final ArrayList<cgWaypoint> mergedWayPoints = new ArrayList<cgWaypoint>(); mergedWayPoints.addAll(cacheForWaypoint.getWaypoints()); - cgWaypoint.mergeWayPoints(mergedWayPoints, Collections.singletonList(waypoint), true); - cacheForWaypoint.setWaypoints(mergedWayPoints, false); - result.put(cacheGeocodeForWaypoint, cacheForWaypoint); + final ArrayList<cgWaypoint> newPoints = new ArrayList<cgWaypoint>(); + newPoints.add(waypoint); + cgWaypoint.mergeWayPoints(newPoints, mergedWayPoints, true); + cacheForWaypoint.setWaypoints(newPoints, false); + cgeoapplication.getInstance().saveCache(cacheForWaypoint, EnumSet.of(SaveFlag.SAVE_DB)); showProgressMessage(progressHandler, progressStream.getProgress()); } } @@ -760,7 +771,7 @@ public abstract class GPXParser extends FileParser { try { progressStream = new ProgressInputStream(stream); Xml.parse(progressStream, Xml.Encoding.UTF_8, root.getContentHandler()); - return result.values(); + return cgeoapplication.getInstance().loadCaches(result, EnumSet.of(LoadFlag.LOAD_DB_MINIMAL)); } catch (SAXException e) { Log.e("Cannot parse .gpx file as GPX " + version + ": could not parse XML - " + e.toString()); throw new ParserException("Cannot parse .gpx file as GPX " + version + ": could not parse XML", e); @@ -768,6 +779,14 @@ public abstract class GPXParser extends FileParser { } /** + * @param cache + * currently imported cache + */ + protected void afterParsing(cgCache cache) { + // can be overridden by sub classes + } + + /** * GPX 1.0 and 1.1 use different XML elements to put the cache into, therefore needs to be overwritten in the * version specific subclasses * diff --git a/tests/src/cgeo/geocaching/files/GPXImporterTest.java b/tests/src/cgeo/geocaching/files/GPXImporterTest.java index 6c28a07..da99c11 100644 --- a/tests/src/cgeo/geocaching/files/GPXImporterTest.java +++ b/tests/src/cgeo/geocaching/files/GPXImporterTest.java @@ -55,27 +55,27 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { } public void testImportGpx() throws IOException { + final String geocode = "GC31J2H"; + removeCacheCompletely(geocode); File gc31j2h = new File(tempDir, "gc31j2h.gpx"); copyResourceToFile(R.raw.gc31j2h, gc31j2h); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); runImportThread(importThread); - assertEquals(5, importStepHandler.messages.size()); + assertEquals(4, importStepHandler.messages.size()); Iterator<Message> iMsg = importStepHandler.messages.iterator(); assertEquals(GPXImporter.IMPORT_STEP_START, iMsg.next().what); assertEquals(GPXImporter.IMPORT_STEP_READ_FILE, iMsg.next().what); - assertEquals(GPXImporter.IMPORT_STEP_STORE_CACHES, iMsg.next().what); assertEquals(GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, iMsg.next().what); assertEquals(GPXImporter.IMPORT_STEP_FINISHED, iMsg.next().what); - SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; - assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); + SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; + assertEquals(Collections.singletonList(geocode), new ArrayList<String>(search.getGeocodes())); - cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); + cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); - // can't assert, for whatever reason the waypoints are remembered in DB - // assertNull(cache.waypoints); + assertTrue(cache.getWaypoints().isEmpty()); } private void runImportThread(GPXImporter.ImportThread importThread) { @@ -96,8 +96,8 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); - SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); @@ -112,7 +112,7 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { final GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(tc2012, listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); final cgCache cache = cgeoapplication.getInstance().loadCache("AID1", LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); @@ -120,10 +120,10 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { } private void assertImportStepMessages(int... importSteps) { - for (int i = 0; i < Math.min(importSteps.length, importStepHandler.messages.size()); i++) { + assertEquals(importSteps.length, importStepHandler.messages.size()); + for (int i = 0; i < importSteps.length; i++) { assertEquals(importSteps[i], importStepHandler.messages.get(i).what); } - assertEquals(importSteps.length, importStepHandler.messages.size()); } public void testImportLoc() throws IOException { @@ -133,8 +133,8 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { GPXImporter.ImportLocFileThread importThread = new GPXImporter.ImportLocFileThread(oc5952, listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); - SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; assertEquals(Collections.singletonList("OC5952"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().loadCache("OC5952", LoadFlags.LOAD_CACHE_OR_DB); @@ -169,34 +169,37 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { } public void testImportGpxAttachment() { + final String geocode = "GC31J2H"; + removeCacheCompletely(geocode); Uri uri = Uri.parse("android.resource://cgeo.geocaching.test/raw/gc31j2h"); GPXImporter.ImportGpxAttachmentThread importThread = new GPXImporter.ImportGpxAttachmentThread(uri, getInstrumentation().getContext().getContentResolver(), listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); - SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; - assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + SearchResult search = (SearchResult) importStepHandler.messages.get(3).obj; + assertEquals(Collections.singletonList(geocode), new ArrayList<String>(search.getGeocodes())); - cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); + cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); - // can't assert, for whatever reason the waypoints are remembered in DB - // assertNull(cache.waypoints); + assertTrue(cache.getWaypoints().isEmpty()); } public void testImportGpxZip() throws IOException { + final String geocode = "GC31J2H"; + removeCacheCompletely(geocode); File pq7545915 = new File(tempDir, "7545915.zip"); copyResourceToFile(R.raw.pq7545915, pq7545915); GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread(pq7545915, listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); - SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; - assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; + assertEquals(Collections.singletonList(geocode), new ArrayList<String>(search.getGeocodes())); - cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); + cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); assertEquals(1, cache.getWaypoints().size()); // this is the original pocket query result without test waypoint } @@ -212,16 +215,18 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { } public void testImportGpxZipAttachment() { + final String geocode = "GC31J2H"; + removeCacheCompletely(geocode); Uri uri = Uri.parse("android.resource://cgeo.geocaching.test/raw/pq7545915"); GPXImporter.ImportGpxZipAttachmentThread importThread = new GPXImporter.ImportGpxZipAttachmentThread(uri, getInstrumentation().getContext().getContentResolver(), listId, importStepHandler, progressHandler); runImportThread(importThread); - assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); - SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; - assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); + assertImportStepMessages(GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); + SearchResult search = (SearchResult) importStepHandler.messages.get(4).obj; + assertEquals(Collections.singletonList(geocode), new ArrayList<String>(search.getGeocodes())); - cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); + cgCache cache = cgeoapplication.getInstance().loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); assertEquals(1, cache.getWaypoints().size()); // this is the original pocket query result without test waypoint } diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index 5ba4081..4b4f286 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -3,8 +3,10 @@ package cgeo.geocaching.files; import cgeo.geocaching.LogEntry; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgWaypoint; +import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; @@ -16,7 +18,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; 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 @@ -73,6 +77,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { } public void testGc31j2h() throws IOException, ParserException { + removeCacheCompletely("GC31J2H"); final List<cgCache> caches = readGPX10(R.raw.gc31j2h); assertEquals(1, caches.size()); final cgCache cache = caches.get(0); @@ -86,6 +91,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { } public void testGc31j2hWpts() throws IOException, ParserException { + removeCacheCompletely("GC31J2H"); List<cgCache> caches = readGPX10(R.raw.gc31j2h, R.raw.gc31j2h_wpts); assertEquals(1, caches.size()); cgCache cache = caches.get(0); @@ -155,7 +161,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { private static void assertGc31j2hWaypoints(final cgCache cache) { assertNotNull(cache.getWaypoints()); assertEquals(2, cache.getWaypoints().size()); - cgWaypoint wp = cache.getWaypoints().get(0); + cgWaypoint wp = cache.getWaypoints().get(1); assertEquals("GC31J2H", wp.getGeocode()); assertEquals("00", wp.getPrefix()); assertEquals("---", wp.getLookup()); @@ -165,7 +171,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertEquals(49.317517, wp.getCoords().getLatitude(), 0.000001); assertEquals(8.545083, wp.getCoords().getLongitude(), 0.000001); - wp = cache.getWaypoints().get(1); + wp = cache.getWaypoints().get(0); assertEquals("GC31J2H", wp.getGeocode()); assertEquals("S1", wp.getPrefix()); assertEquals("---", wp.getLookup()); @@ -187,18 +193,21 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { } private List<cgCache> readVersionedGPX(final GPXParser parser, int... resourceIds) throws IOException, ParserException { - Collection<cgCache> caches = null; + final Set<String> result = new HashSet<String>(); for (int resourceId : resourceIds) { final InputStream instream = getResourceStream(resourceId); try { - caches = parser.parse(instream, null); + Collection<cgCache> caches = parser.parse(instream, null); assertNotNull(caches); + for (cgCache cache : caches) { + result.add(cache.getGeocode()); + } } finally { instream.close(); } } - - return new ArrayList<cgCache>(caches); + // reload caches, because the parser only returns the minimum version of each cache + return new ArrayList<cgCache>(cgeoapplication.getInstance().loadCaches(result, LoadFlags.LOAD_ALL_DB_ONLY)); } public static void testParseDateWithFractionalSeconds() { @@ -237,10 +246,11 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { public void testGeoToad() throws Exception { final List<cgCache> caches = readGPX10(R.raw.geotoad); assertEquals(2, caches.size()); - cgCache cache; - cache = caches.get(0); - assertEquals("GC2KN6K", cache.getGeocode()); - cache = caches.get(1); - assertEquals("GC1T3MK", cache.getGeocode()); + List<String> codes = new ArrayList<String>(); + for (cgCache cache : caches) { + codes.add(cache.getGeocode()); + } + assertTrue(codes.contains("GC2KN6K")); + assertTrue(codes.contains("GC1T3MK")); } } diff --git a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java index ab5c290..0d3d53b 100644 --- a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java +++ b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java @@ -1,5 +1,9 @@ package cgeo.geocaching.test; +import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; + import android.content.res.Resources; import android.test.InstrumentationTestCase; @@ -7,9 +11,16 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.EnumSet; import java.util.Scanner; public abstract class AbstractResourceInstrumentationTestCase extends InstrumentationTestCase { + protected static void removeCacheCompletely(final String geocode) { + final EnumSet<RemoveFlag> flags = EnumSet.copyOf(LoadFlags.REMOVE_ALL); + flags.add(RemoveFlag.REMOVE_OWN_WAYPOINTS_ONLY_FOR_TESTING); + cgeoapplication.getInstance().removeCache(geocode, flags); + } + protected InputStream getResourceStream(int resourceId) { final Resources res = getInstrumentation().getContext().getResources(); return res.openRawResource(resourceId); |