diff options
Diffstat (limited to 'tests/src')
| -rw-r--r-- | tests/src/cgeo/CGeoTestCase.java | 60 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/GeocacheTest.java | 52 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/export/GpxSerializerTest.java | 45 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/files/GPXParserTest.java | 3 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/CryptUtilsTest.java | 12 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/UncertainPropertyTest.java | 19 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/utils/XmlUtilsTest.java | 48 |
7 files changed, 217 insertions, 22 deletions
diff --git a/tests/src/cgeo/CGeoTestCase.java b/tests/src/cgeo/CGeoTestCase.java index 6a63cbc..b741e95 100644 --- a/tests/src/cgeo/CGeoTestCase.java +++ b/tests/src/cgeo/CGeoTestCase.java @@ -3,11 +3,20 @@ package cgeo; import cgeo.geocaching.cgData; import cgeo.geocaching.cgeoapplication; import cgeo.geocaching.enumerations.LoadFlags; +import cgeo.geocaching.enumerations.LoadFlags.RemoveFlag; +import cgeo.geocaching.settings.Settings; +import cgeo.geocaching.settings.TestSettings; import android.test.ApplicationTestCase; +import java.util.EnumSet; + public abstract class CGeoTestCase extends ApplicationTestCase<cgeoapplication> { + private boolean oldStoreMapsFlag; + private boolean oldStoreWpMapsFlag; + private boolean oldMapStoreFlagsRecorded = false; + public CGeoTestCase() { super(cgeoapplication.class); } @@ -23,4 +32,55 @@ public abstract class CGeoTestCase extends ApplicationTestCase<cgeoapplication> cgData.removeCache(geocode, LoadFlags.REMOVE_ALL); } + /** + * remove cache from database and file system + * + * @param geocode + */ + 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); + cgData.removeCache(geocode, flags); + } + + /** + * must be called once before setting the flags + * can be called again after restoring the flags + */ + protected void recordMapStoreFlags() { + if (oldMapStoreFlagsRecorded) { + throw new IllegalStateException("MapStoreFlags already recorded!"); + } + oldStoreMapsFlag = Settings.isStoreOfflineMaps(); + oldStoreWpMapsFlag = Settings.isStoreOfflineWpMaps(); + oldMapStoreFlagsRecorded = true; + } + + /** + * can be called after recordMapStoreFlags, + * to set the flags for map storing as necessary + * @param storeCacheMap + * @param storeWpMaps + */ + protected void setMapStoreFlags(boolean storeCacheMap, boolean storeWpMaps) { + if (!oldMapStoreFlagsRecorded) { + throw new IllegalStateException("Previous MapStoreFlags havn't been recorded! Setting not allowed"); + } + + TestSettings.setStoreOfflineMaps(storeCacheMap); + TestSettings.setStoreOfflineWpMaps(storeWpMaps); + } + + /** + * has to be called after completion of the test (preferably in the finally part of a try statement) + */ + protected void restoreMapStoreFlags() { + if (!oldMapStoreFlagsRecorded) { + throw new IllegalStateException("Previous MapStoreFlags havn't been recorded. Restore not possible"); + } + + TestSettings.setStoreOfflineMaps(oldStoreMapsFlag); + TestSettings.setStoreOfflineWpMaps(oldStoreWpMapsFlag); + oldMapStoreFlagsRecorded = false; + } } diff --git a/tests/src/cgeo/geocaching/GeocacheTest.java b/tests/src/cgeo/geocaching/GeocacheTest.java index 4c09594..5850c4f 100644 --- a/tests/src/cgeo/geocaching/GeocacheTest.java +++ b/tests/src/cgeo/geocaching/GeocacheTest.java @@ -1,16 +1,14 @@ package cgeo.geocaching; +import cgeo.CGeoTestCase; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.Geopoint; -import android.os.Handler; -import android.test.AndroidTestCase; - import java.util.ArrayList; import java.util.Date; import java.util.List; -public class GeocacheTest extends AndroidTestCase { +public class GeocacheTest extends CGeoTestCase { final static private class MockedEventCache extends Geocache { public MockedEventCache(final Date date) { @@ -52,31 +50,41 @@ public class GeocacheTest extends AndroidTestCase { assertEquals("GC1234", cache.getGeocode()); } - public static void testUpdateWaypointFromNote() { + public void testUpdateWaypointFromNote() { assertWaypointsParsed("Test N51 13.888 E007 03.444", 1); } - public static void testUpdateWaypointsFromNote() { + public void testUpdateWaypointsFromNote() { assertWaypointsParsed("Test N51 13.888 E007 03.444 Test N51 13.233 E007 03.444 Test N51 09.123 E007 03.444", 3); } - private static void assertWaypointsParsed(String note, int expectedWaypoints) { - Geocache cache = new Geocache(); - cache.setGeocode("Test" + System.nanoTime()); - cache.setWaypoints(new ArrayList<Waypoint>(), false); - for (int i = 0; i < 2; i++) { - cache.setPersonalNote(note); - cache.parseWaypointsFromNote(); - final List<Waypoint> waypoints = cache.getWaypoints(); - assertNotNull(waypoints); - assertEquals(expectedWaypoints, waypoints.size()); - final Waypoint waypoint = waypoints.get(0); - assertEquals(new Geopoint("N51 13.888 E007 03.444"), waypoint.getCoords()); - // assertEquals("Test", waypoint.getNote()); - assertEquals(cgeoapplication.getInstance().getString(R.string.cache_personal_note) + " 1", waypoint.getName()); - cache.store(StoredList.TEMPORARY_LIST_ID, null); + private void assertWaypointsParsed(String note, int expectedWaypoints) { + + recordMapStoreFlags(); + + try { + setMapStoreFlags(false, false); + + Geocache cache = new Geocache(); + final String geocode = "Test" + System.nanoTime(); + cache.setGeocode(geocode); + cache.setWaypoints(new ArrayList<Waypoint>(), false); + for (int i = 0; i < 2; i++) { + cache.setPersonalNote(note); + cache.parseWaypointsFromNote(); + final List<Waypoint> waypoints = cache.getWaypoints(); + assertNotNull(waypoints); + assertEquals(expectedWaypoints, waypoints.size()); + final Waypoint waypoint = waypoints.get(0); + assertEquals(new Geopoint("N51 13.888 E007 03.444"), waypoint.getCoords()); + // assertEquals("Test", waypoint.getNote()); + assertEquals(cgeoapplication.getInstance().getString(R.string.cache_personal_note) + " 1", waypoint.getName()); + cache.store(StoredList.TEMPORARY_LIST_ID, null); + } + removeCacheCompletely(geocode); + } finally { + restoreMapStoreFlags(); } - cache.drop(new Handler()); } public static void testMergeDownloadedStored() { diff --git a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java index 0080b76..ad1cea0 100644 --- a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java +++ b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java @@ -1,12 +1,19 @@ package cgeo.geocaching.export; import cgeo.geocaching.Geocache; +import cgeo.geocaching.StoredList; +import cgeo.geocaching.files.GPX10Parser; import cgeo.geocaching.files.ParserException; 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; @@ -34,4 +41,42 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { }); 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); + assertNotNull(cache); + + final String gpxFirst = getGPXFromCache(geocode); + + assertTrue(gpxFirst.length() > 0); + + final GPX10Parser parser = new GPX10Parser(StoredList.TEMPORARY_LIST_ID); + + final InputStream stream = new ByteArrayInputStream(gpxFirst.getBytes(CharEncoding.UTF_8)); + Collection<Geocache> caches = parser.parse(stream, null); + assertNotNull(caches); + assertEquals(1, caches.size()); + + final String gpxSecond = getGPXFromCache(geocode); + assertEquals(replaceLogIds(gpxFirst), replaceLogIds(gpxSecond)); + } + + 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(); + } + } diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index 892b2a0..06661eb 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -150,6 +150,9 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertEquals(parseTime("2011-09-11T07:00:00Z"), log.date); assertEquals(-1, log.found); assertEquals("Sehr schöne Runde und wir haben wieder etwas Neues über Hockenheim gelernt. Super Tarnung.\nTFTC, Geoteufel", log.log); + assertFalse(log.isOwn()); + assertEquals(log.log, log.getDisplayText()); + assertTrue(log.daysSinceLog() > 700); // following info is not contained in pocket query gpx file assertEquals(0, cache.getAttributes().size()); diff --git a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java index e727747..9264d29 100644 --- a/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/CryptUtilsTest.java @@ -23,4 +23,16 @@ public class CryptUtilsTest extends TestCase { public static void testIssue1902() { assertEquals("ƖƖlƖƖƖƖ", CryptUtils.rot13("ƖƖyƖƖƖƖ")); } + + public static void testSha1() { + assertEquals("da39a3ee5e6b4b0d3255bfef95601890afd80709", CryptUtils.sha1("")); + // expected value taken from debugger. should assure every developer uses UTF-8 + assertEquals("cf2f343f59cea81afc0a5a566cb138ba349c548f", CryptUtils.sha1("äöü")); + } + + public static void testMd5() { + assertEquals("d41d8cd98f00b204e9800998ecf8427e", CryptUtils.md5("")); + // expected value taken from debugger. should assure every developer uses UTF-8 + assertEquals("a7f4e3ec08f09be2ef7ecb4eea5f8981", CryptUtils.md5("äöü")); + } } diff --git a/tests/src/cgeo/geocaching/utils/UncertainPropertyTest.java b/tests/src/cgeo/geocaching/utils/UncertainPropertyTest.java new file mode 100644 index 0000000..74aa680 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/UncertainPropertyTest.java @@ -0,0 +1,19 @@ +package cgeo.geocaching.utils; + +import junit.framework.TestCase; + +public class UncertainPropertyTest extends TestCase { + + public static void testHigherCertaintyWins() throws Exception { + final UncertainProperty<String> prop1 = new UncertainProperty<String>("prop1", 10); + final UncertainProperty<String> prop2 = new UncertainProperty<String>("prop2", 20); + assertEquals(prop2, UncertainProperty.getMergedProperty(prop1, prop2)); + } + + public static void testAvoidNull() throws Exception { + final UncertainProperty<String> prop1 = new UncertainProperty<String>("prop1", 10); + final UncertainProperty<String> prop2 = new UncertainProperty<String>(null, 20); + assertEquals(prop1, UncertainProperty.getMergedProperty(prop1, prop2)); + assertEquals(prop1, UncertainProperty.getMergedProperty(prop2, prop1)); + } +} diff --git a/tests/src/cgeo/geocaching/utils/XmlUtilsTest.java b/tests/src/cgeo/geocaching/utils/XmlUtilsTest.java new file mode 100644 index 0000000..a089ee0 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/XmlUtilsTest.java @@ -0,0 +1,48 @@ +package cgeo.geocaching.utils; + +import cgeo.org.kxml2.io.KXmlSerializer; + +import org.apache.commons.lang3.CharEncoding; +import org.xmlpull.v1.XmlSerializer; + +import java.io.IOException; +import java.io.StringWriter; + +import junit.framework.TestCase; + +public class XmlUtilsTest extends TestCase { + + private XmlSerializer xml; + private StringWriter stringWriter; + + @Override + protected void setUp() throws Exception { + super.setUp(); + stringWriter = new StringWriter(); + xml = new KXmlSerializer(); + xml.setOutput(stringWriter); + xml.startDocument(CharEncoding.UTF_8, null); + } + + public void testSimpleText() throws Exception { + XmlUtils.simpleText(xml, "", "tag", "text"); + assertXmlEquals("<tag>text</tag>"); + } + + public void testSimpleTextWithPrefix() throws Exception { + XmlUtils.simpleText(xml, "prefix", "tag", "text"); + assertXmlEquals("<n0:tag xmlns:n0=\"prefix\">text</n0:tag>"); + } + + private void assertXmlEquals(final String expected) throws IOException { + xml.endDocument(); + xml.flush(); + assertEquals("<?xml version='1.0' encoding='UTF-8' ?>" + expected, stringWriter.toString()); + } + + public void testMultipleTexts() throws Exception { + XmlUtils.multipleTexts(xml, "", "tag1", "text1", "tag2", "text2"); + assertXmlEquals("<tag1>text1</tag1><tag2>text2</tag2>"); + } + +} |
