diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2013-06-22 12:17:08 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2013-06-22 12:17:08 +0200 |
| commit | 01bfdcaeeae619fd638f48b8fe94463e8131edd2 (patch) | |
| tree | 192f5421c231d5e93fa112c21d023a703d9473c5 /tests/src | |
| parent | 22fa318357e3143ba196871bf5233aed41016766 (diff) | |
| download | cgeo-01bfdcaeeae619fd638f48b8fe94463e8131edd2.zip cgeo-01bfdcaeeae619fd638f48b8fe94463e8131edd2.tar.gz cgeo-01bfdcaeeae619fd638f48b8fe94463e8131edd2.tar.bz2 | |
refactoring: extract GPX serialization to make it testable
Diffstat (limited to 'tests/src')
| -rw-r--r-- | tests/src/cgeo/geocaching/export/GpxSerializerTest.java | 37 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java | 17 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java new file mode 100644 index 0000000..0080b76 --- /dev/null +++ b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java @@ -0,0 +1,37 @@ +package cgeo.geocaching.export; + +import cgeo.geocaching.Geocache; +import cgeo.geocaching.files.ParserException; +import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; +import cgeo.geocaching.test.R; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Collections; +import java.util.concurrent.atomic.AtomicReference; + +public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { + + public static void testWriteEmptyGPX() throws Exception { + final StringWriter writer = new StringWriter(); + new GpxSerializer().writeGPX(Collections.<String> emptyList(), writer, null); + assertEquals("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><gpx version=\"1.0\" creator=\"c:geo - http://www.cgeo.org/\" 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/1/cache.xsd\" xmlns=\"http://www.topografix.com/GPX/1/0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:groundspeak=\"http://www.groundspeak.com/cache/1/0\" />", writer.getBuffer().toString()); + } + + public void testProgressReporting() throws IOException, ParserException { + final AtomicReference<Integer> importedCount = new AtomicReference<Integer>(0); + final StringWriter writer = new StringWriter(); + + Geocache cache = loadCacheFromResource(R.raw.gc1bkp3_gpx101); + assertNotNull(cache); + + new GpxSerializer().writeGPX(Collections.singletonList("GC1BKP3"), writer, new GpxSerializer.ProgressListener() { + + @Override + public void publishProgress(int countExported) { + importedCount.set(countExported); + } + }); + assertEquals("Progress listener not called", 1, importedCount.get().intValue()); + } +} diff --git a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java index 304f98d..cb8238f 100644 --- a/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java +++ b/tests/src/cgeo/geocaching/test/AbstractResourceInstrumentationTestCase.java @@ -1,11 +1,14 @@ package cgeo.geocaching.test; +import cgeo.geocaching.Geocache; import cgeo.geocaching.SearchResult; import cgeo.geocaching.StoredList; import cgeo.geocaching.cgData; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; +import cgeo.geocaching.files.GPX10Parser; +import cgeo.geocaching.files.ParserException; import android.content.res.Resources; import android.test.InstrumentationTestCase; @@ -14,6 +17,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.Collection; import java.util.EnumSet; import java.util.Scanner; @@ -78,4 +82,17 @@ public abstract class AbstractResourceInstrumentationTestCase extends Instrument protected final int getTemporaryListId() { return temporaryListId; } + + final protected Geocache loadCacheFromResource(int resourceId) throws IOException, ParserException { + final InputStream instream = getResourceStream(resourceId); + try { + GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST_ID); + Collection<Geocache> caches = parser.parse(instream, null); + assertNotNull(caches); + assertFalse(caches.isEmpty()); + return caches.iterator().next(); + } finally { + instream.close(); + } + } } |
