package cgeo.geocaching.export; import static org.assertj.core.api.Assertions.assertThat; import cgeo.geocaching.Geocache; import cgeo.geocaching.files.GPX10Parser; import cgeo.geocaching.files.ParserException; import cgeo.geocaching.list.StoredList; import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; import cgeo.geocaching.test.R; import org.apache.commons.lang3.CharEncoding; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.Collection; 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. emptyList(), writer, null); assertThat(writer.getBuffer().toString()).isEqualTo("" + ""); } public void testProgressReporting() throws IOException, ParserException { final AtomicReference importedCount = new AtomicReference(0); final StringWriter writer = new StringWriter(); Geocache cache = loadCacheFromResource(R.raw.gc1bkp3_gpx101); assertThat(cache).isNotNull(); 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()); } /** * This test verifies that a loop of import, export, import leads to the same cache information. * * @throws IOException * @throws ParserException */ public void testStableExportImportExport() throws IOException, ParserException { final String geocode = "GC1BKP3"; final int cacheResource = R.raw.gc1bkp3_gpx101; final Geocache cache = loadCacheFromResource(cacheResource); assertThat(cache).isNotNull(); final String gpxFirst = getGPXFromCache(geocode); assertThat(gpxFirst.length() > 0).isTrue(); final GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST_ID); final InputStream stream = new ByteArrayInputStream(gpxFirst.getBytes(CharEncoding.UTF_8)); Collection caches = parser.parse(stream, null); assertThat(caches).isNotNull(); assertThat(caches).hasSize(1); final String gpxSecond = getGPXFromCache(geocode); assertThat(replaceLogIds(gpxSecond)).isEqualTo(replaceLogIds(gpxFirst)); } private static String replaceLogIds(String gpx) { return gpx.replaceAll("log id=\"\\d*\"", ""); } private static String getGPXFromCache(String geocode) throws IOException { final StringWriter writer = new StringWriter(); new GpxSerializer().writeGPX(Collections.singletonList(geocode), writer, null); return writer.toString(); } public static void testStateFromStateCountry() throws Exception { Geocache cache = withLocation("state, country"); assertThat(GpxSerializer.getState(cache)).isEqualTo("state"); } public static void testCountryFromStateCountry() throws Exception { Geocache cache = withLocation("state, country"); assertThat(GpxSerializer.getCountry(cache)).isEqualTo("country"); } public static void testCountryFromCountryOnly() throws Exception { Geocache cache = withLocation("somewhere"); assertThat(GpxSerializer.getCountry(cache)).isEqualTo("somewhere"); } public static void testStateFromCountryOnly() throws Exception { Geocache cache = withLocation("somewhere"); assertThat(GpxSerializer.getState(cache)).isEmpty(); } public static void testCountryFromExternalCommaString() throws Exception { Geocache cache = withLocation("first,second"); // this was not created by c:geo, therefore don't split it assertThat(GpxSerializer.getState(cache)).isEmpty(); } private static Geocache withLocation(final String location) { Geocache cache = new Geocache(); cache.setLocation(location); return cache; } }