diff options
Diffstat (limited to 'tests')
37 files changed, 383 insertions, 128 deletions
diff --git a/tests/.settings/org.eclipse.jdt.core.prefs b/tests/.settings/org.eclipse.jdt.core.prefs index 0c3bd0c..af15cda 100644 --- a/tests/.settings/org.eclipse.jdt.core.prefs +++ b/tests/.settings/org.eclipse.jdt.core.prefs @@ -51,7 +51,7 @@ org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=warning org.eclipse.jdt.core.compiler.problem.missingJavadocComments=ignore org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=public -org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=no_tag +org.eclipse.jdt.core.compiler.problem.missingJavadocTagDescription=all_standard_tags org.eclipse.jdt.core.compiler.problem.missingJavadocTags=ignore org.eclipse.jdt.core.compiler.problem.missingJavadocTagsMethodTypeParameters=disabled org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled diff --git a/tests/.settings/org.eclipse.jdt.ui.prefs b/tests/.settings/org.eclipse.jdt.ui.prefs index 1fda530..06e562c 100644 --- a/tests/.settings/org.eclipse.jdt.ui.prefs +++ b/tests/.settings/org.eclipse.jdt.ui.prefs @@ -8,7 +8,7 @@ org.eclipse.jdt.ui.ondemandthreshold=99 org.eclipse.jdt.ui.staticondemandthreshold=99 sp_cleanup.add_default_serial_version_id=true sp_cleanup.add_generated_serial_version_id=false -sp_cleanup.add_missing_annotations=false +sp_cleanup.add_missing_annotations=true sp_cleanup.add_missing_deprecated_annotations=true sp_cleanup.add_missing_methods=false sp_cleanup.add_missing_nls_tags=false @@ -20,16 +20,16 @@ sp_cleanup.always_use_parentheses_in_expressions=false sp_cleanup.always_use_this_for_non_static_field_access=false sp_cleanup.always_use_this_for_non_static_method_access=false sp_cleanup.convert_functional_interfaces=false -sp_cleanup.convert_to_enhanced_for_loop=false +sp_cleanup.convert_to_enhanced_for_loop=true sp_cleanup.correct_indentation=false sp_cleanup.format_source_code=true sp_cleanup.format_source_code_changes_only=true sp_cleanup.insert_inferred_type_arguments=false -sp_cleanup.make_local_variable_final=false -sp_cleanup.make_parameters_final=false -sp_cleanup.make_private_fields_final=false +sp_cleanup.make_local_variable_final=true +sp_cleanup.make_parameters_final=true +sp_cleanup.make_private_fields_final=true sp_cleanup.make_type_abstract_if_missing_method=false -sp_cleanup.make_variable_declarations_final=false +sp_cleanup.make_variable_declarations_final=true sp_cleanup.never_use_blocks=false sp_cleanup.never_use_parentheses_in_expressions=true sp_cleanup.on_save_use_additional_actions=true @@ -37,16 +37,16 @@ sp_cleanup.organize_imports=true sp_cleanup.qualify_static_field_accesses_with_declaring_class=false sp_cleanup.qualify_static_member_accesses_through_instances_with_declaring_class=true sp_cleanup.qualify_static_member_accesses_through_subtypes_with_declaring_class=true -sp_cleanup.qualify_static_member_accesses_with_declaring_class=false +sp_cleanup.qualify_static_member_accesses_with_declaring_class=true sp_cleanup.qualify_static_method_accesses_with_declaring_class=false sp_cleanup.remove_private_constructors=true sp_cleanup.remove_redundant_type_arguments=false sp_cleanup.remove_trailing_whitespaces=true sp_cleanup.remove_trailing_whitespaces_all=true sp_cleanup.remove_trailing_whitespaces_ignore_empty=false -sp_cleanup.remove_unnecessary_casts=false -sp_cleanup.remove_unnecessary_nls_tags=false -sp_cleanup.remove_unused_imports=false +sp_cleanup.remove_unnecessary_casts=true +sp_cleanup.remove_unnecessary_nls_tags=true +sp_cleanup.remove_unused_imports=true sp_cleanup.remove_unused_local_variables=false sp_cleanup.remove_unused_private_fields=true sp_cleanup.remove_unused_private_members=false @@ -55,7 +55,7 @@ sp_cleanup.remove_unused_private_types=true sp_cleanup.sort_members=false sp_cleanup.sort_members_all=false sp_cleanup.use_anonymous_class_creation=false -sp_cleanup.use_blocks=false +sp_cleanup.use_blocks=true sp_cleanup.use_blocks_only_for_return_and_throw=false sp_cleanup.use_lambda=false sp_cleanup.use_parentheses_in_expressions=false diff --git a/tests/res/layout/cgeo_tests_activity.xml b/tests/res/layout/cgeo_tests_activity.xml index 43225ac..d035210 100644 --- a/tests/res/layout/cgeo_tests_activity.xml +++ b/tests/res/layout/cgeo_tests_activity.xml @@ -13,6 +13,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_gravity="center_vertical" android:gravity="center_vertical" @@ -27,6 +28,7 @@ android:layout_height="fill_parent" android:layout_above="@+id/buttonRun" android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/headline" > <TextView @@ -43,6 +45,7 @@ android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="false" + android:layout_alignParentStart="false" android:onClick="runTests" android:text="@string/run_tests" /> diff --git a/tests/src/cgeo/CGeoTestCase.java b/tests/src/cgeo/CGeoTestCase.java index 7fc1e8d..2dbac3d 100644 --- a/tests/src/cgeo/CGeoTestCase.java +++ b/tests/src/cgeo/CGeoTestCase.java @@ -29,14 +29,12 @@ public abstract class CGeoTestCase extends ApplicationTestCase<CgeoApplication> } /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */ - protected static void deleteCacheFromDB(String geocode) { + protected static void deleteCacheFromDB(final String geocode) { DataStore.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); @@ -71,10 +69,8 @@ public abstract class CGeoTestCase extends ApplicationTestCase<CgeoApplication> /** * 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) { + protected void setMapStoreFlags(final boolean storeCacheMap, final boolean storeWpMaps) { if (!oldMapStoreFlagsRecorded) { throw new IllegalStateException("Previous MapStoreFlags havn't been recorded! Setting not allowed"); } diff --git a/tests/src/cgeo/geocaching/CgeoApplicationTest.java b/tests/src/cgeo/geocaching/CgeoApplicationTest.java index ebe317d..6763d7b 100644 --- a/tests/src/cgeo/geocaching/CgeoApplicationTest.java +++ b/tests/src/cgeo/geocaching/CgeoApplicationTest.java @@ -16,7 +16,7 @@ import cgeo.geocaching.list.StoredList; import cgeo.geocaching.loaders.RecaptchaReceiver; import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.location.Viewport; -import cgeo.geocaching.maps.LiveMapStrategy.Strategy; +import cgeo.geocaching.maps.LivemapStrategy; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.TestSettings; import cgeo.geocaching.test.RegExPerformanceTest; @@ -95,7 +95,7 @@ public class CgeoApplicationTest extends CGeoTestCase { // no assumption possible: assertThat(tb.getSpottedName()).isEqualTo("Nice place for a break cache"); // we can't check specifics in the log entries since they change, but we can verify data was parsed - for (LogEntry log : tb.getLogs()) { + for (final LogEntry log : tb.getLogs()) { assertThat(log.date).isGreaterThan(0); assertThat(log.author).isNotEmpty(); if (log.type == LogType.PLACED_IT || log.type == LogType.RETRIEVED_IT) { @@ -136,7 +136,6 @@ public class CgeoApplicationTest extends CGeoTestCase { /** * Set the login data to the cgeo login, run the given Runnable, and restore the login. * - * @param runnable */ private static void withMockedLoginDo(final Runnable runnable) { final ImmutablePair<String, String> login = Settings.getGcCredentials(); @@ -212,9 +211,8 @@ public class CgeoApplicationTest extends CGeoTestCase { /** * mock the "exclude disabled caches" and "exclude my caches" options for the execution of the runnable * - * @param runnable */ - private static void withMockedFilters(Runnable runnable) { + private static void withMockedFilters(final Runnable runnable) { // backup user settings final boolean excludeMine = Settings.isExcludeMyCaches(); final boolean excludeDisabled = Settings.isExcludeDisabledCaches(); @@ -293,7 +291,7 @@ public class CgeoApplicationTest extends CGeoTestCase { @Override public void run() { // backup user settings - final Strategy strategy = Settings.getLiveMapStrategy(); + final LivemapStrategy strategy = Settings.getLiveMapStrategy(); final CacheType cacheType = Settings.getCacheType(); try { @@ -308,7 +306,7 @@ public class CgeoApplicationTest extends CGeoTestCase { final Viewport viewport = new Viewport(mockedCache, 0.003, 0.003); // check coords for DETAILED - Settings.setLiveMapStrategy(Strategy.DETAILED); + Settings.setLiveMapStrategy(LivemapStrategy.DETAILED); SearchResult search = ConnectorFactory.searchByViewport(viewport, tokens); assertThat(search).isNotNull(); assertThat(search.getGeocodes().contains(mockedCache.getGeocode())).isTrue(); @@ -320,7 +318,7 @@ public class CgeoApplicationTest extends CGeoTestCase { assertThat(parsedCache.isReliableLatLon()).isEqualTo(Settings.isGCPremiumMember()); // check update after switch strategy to FAST - Settings.setLiveMapStrategy(Strategy.FAST); + Settings.setLiveMapStrategy(LivemapStrategy.FAST); Tile.cache.removeFromTileCache(mockedCache); search = ConnectorFactory.searchByViewport(viewport, tokens); @@ -351,8 +349,8 @@ public class CgeoApplicationTest extends CGeoTestCase { @Override public void run() { - final Strategy strategy = Settings.getLiveMapStrategy(); - final Strategy testStrategy = Strategy.FAST; // FASTEST, FAST or DETAILED for tests + final LivemapStrategy strategy = Settings.getLiveMapStrategy(); + final LivemapStrategy testStrategy = LivemapStrategy.FAST; // FASTEST, FAST or DETAILED for tests Settings.setLiveMapStrategy(testStrategy); final CacheType cacheType = Settings.getCacheType(); @@ -377,7 +375,7 @@ public class CgeoApplicationTest extends CGeoTestCase { Log.d("cgeoApplicationTest.testSearchByViewportNotLoggedIn: Coords actual = " + cacheFromViewport.getCoords()); assertThat(cache.getCoords().distanceTo(cacheFromViewport.getCoords()) <= 1e-3).isFalse(); // depending on the chosen strategy the coords can be reliable or not - assertThat(cacheFromViewport.isReliableLatLon()).isEqualTo(testStrategy == Strategy.DETAILED); + assertThat(cacheFromViewport.isReliableLatLon()).isEqualTo(testStrategy == LivemapStrategy.DETAILED); // premium cache cache = new GC2JVEH(); @@ -402,7 +400,7 @@ public class CgeoApplicationTest extends CGeoTestCase { * Test cache parsing. Esp. useful after a GC.com update */ public static void testSearchByGeocodeBasis() { - for (MockedCache mockedCache : RegExPerformanceTest.MOCKED_CACHES) { + for (final MockedCache mockedCache : RegExPerformanceTest.MOCKED_CACHES) { final String oldUser = mockedCache.getMockedDataUser(); try { mockedCache.setMockedDataUser(Settings.getUsername()); @@ -426,7 +424,7 @@ public class CgeoApplicationTest extends CGeoTestCase { } /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */ - private static void deleteCacheFromDBAndLogout(String geocode) { + private static void deleteCacheFromDBAndLogout(final String geocode) { deleteCacheFromDB(geocode); GCLogin.getInstance().logout(); diff --git a/tests/src/cgeo/geocaching/GeocacheTest.java b/tests/src/cgeo/geocaching/GeocacheTest.java index 37b6883..26c677d 100644 --- a/tests/src/cgeo/geocaching/GeocacheTest.java +++ b/tests/src/cgeo/geocaching/GeocacheTest.java @@ -295,6 +295,7 @@ public class GeocacheTest extends CGeoTestCase { assertTime("<u><em>Uhrzeit:</em></u> 17-20 " + timeHours + "</span></strong>", 17, 00); assertTime("von 11 bis 13 " + timeHours, 11, 00); assertTime("from 11 to 13 " + timeHours, 11, 00); + assertTime("von 19.15 " + timeHours + " bis ca.20.30 " + timeHours + " statt", 19, 15); } public static void testGuessEventTimeShortDescription() { @@ -302,7 +303,7 @@ public class GeocacheTest extends CGeoTestCase { cache.setType(CacheType.EVENT); cache.setDescription(StringUtils.EMPTY); cache.setShortDescription("text 14:20 text"); - assertThat(cache.guessEventTimeMinutes()).isEqualTo(String.valueOf(14 * 60 + 20)); + assertThat(cache.guessEventTimeMinutes()).isEqualTo(14 * 60 + 20); } private static void assertTime(final String description, final int hours, final int minutes) { @@ -310,14 +311,14 @@ public class GeocacheTest extends CGeoTestCase { cache.setDescription(description); cache.setType(CacheType.EVENT); final int minutesAfterMidnight = hours * 60 + minutes; - assertThat(cache.guessEventTimeMinutes()).isEqualTo(String.valueOf(minutesAfterMidnight)); + assertThat(cache.guessEventTimeMinutes()).isEqualTo(minutesAfterMidnight); } private static void assertNoTime(final String description) { final Geocache cache = new Geocache(); cache.setDescription(description); cache.setType(CacheType.EVENT); - assertThat(cache.guessEventTimeMinutes()).isNull(); + assertThat(cache.guessEventTimeMinutes()).isEqualTo(-1); } public static void testGetPossibleLogTypes() throws Exception { diff --git a/tests/src/cgeo/geocaching/StaticMapsProviderTest.java b/tests/src/cgeo/geocaching/StaticMapsProviderTest.java index b107912..02b1d02 100644 --- a/tests/src/cgeo/geocaching/StaticMapsProviderTest.java +++ b/tests/src/cgeo/geocaching/StaticMapsProviderTest.java @@ -7,6 +7,7 @@ import cgeo.geocaching.files.LocalStorage; import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.settings.TestSettings; +import cgeo.geocaching.utils.FileUtils; import cgeo.geocaching.utils.RxUtils; import junit.framework.TestCase; @@ -78,7 +79,7 @@ public class StaticMapsProviderTest extends TestCase { private static void deleteCacheDirectory(String geocode) { File cacheDir = LocalStorage.getStorageDir(geocode); - LocalStorage.deleteDirectory(cacheDir); + FileUtils.deleteDirectory(cacheDir); } } diff --git a/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java b/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java new file mode 100644 index 0000000..c4eacc5 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java @@ -0,0 +1,21 @@ +package cgeo.geocaching.connector; + +import static org.assertj.core.api.Assertions.assertThat; +import junit.framework.TestCase; + +public class WaymarkingConnectorTest extends TestCase { + + public static void testGetGeocodeFromUrl() { + assertThat(ConnectorFactory.getGeocodeFromURL("http://coord.info/WM1234")).isEqualTo("WM1234"); + assertThat(ConnectorFactory.getGeocodeFromURL("http://www.waymarking.com/waymarks/WMNCDT_American_Legion_Flagpole_1983_University_of_Oregon")).isEqualTo("WMNCDT"); + + final IConnector wmConnector = ConnectorFactory.getConnector("WM1234"); + assertThat(wmConnector).isNotNull(); + + assertThat(wmConnector.getGeocodeFromUrl("http://coord.info/WM1234")).isEqualTo("WM1234"); + assertThat(wmConnector.getGeocodeFromUrl("http://www.waymarking.com/waymarks/WMNCDT_American_Legion_Flagpole_1983_University_of_Oregon")).isEqualTo("WMNCDT"); + + assertThat(wmConnector.getGeocodeFromUrl("http://coord.info/GC12ABC")).isNull(); + assertThat(wmConnector.getGeocodeFromUrl("http://coord.info/TB1234")).isNull(); + } +} diff --git a/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java index aa6a185..f4073a9 100644 --- a/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java +++ b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java @@ -32,4 +32,8 @@ public class ECConnectorTest extends TestCase { return geocache; } + public static void testGetGeocodeFromUrl() throws Exception { + assertThat(ECConnector.getInstance().getGeocodeFromUrl("http://extremcaching.com/index.php/output-2/738")).isEqualTo("EC738"); + } + } diff --git a/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java b/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java index 24f65fd..30a9af9 100644 --- a/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java +++ b/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java @@ -82,7 +82,7 @@ public class GCConnectorTest extends AbstractResourceInstrumentationTestCase { assertTileAt(7536, 4915, new Tile(new Geopoint("S 33° 50.326 E 151° 12.426"), 13)); } - private static void assertTileAt(int x, int y, final Tile tile) { + private static void assertTileAt(final int x, final int y, final Tile tile) { assertThat(tile.getX()).isEqualTo(x); assertThat(tile.getY()).isEqualTo(y); } @@ -96,5 +96,6 @@ public class GCConnectorTest extends AbstractResourceInstrumentationTestCase { assertThat(GCConnector.getInstance().getGeocodeFromUrl("http://coord.info/TB1234")).isNull(); assertThat(GCConnector.getInstance().getGeocodeFromUrl("http://www.coord.info/TB1234")).isNull(); + assertThat(GCConnector.getInstance().getGeocodeFromUrl("http://www.coord.info/WM1234")).isNull(); } } diff --git a/tests/src/cgeo/geocaching/connector/oc/OCCZConnectorTest.java b/tests/src/cgeo/geocaching/connector/oc/OCCZConnectorTest.java new file mode 100644 index 0000000..52fc5c8 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/oc/OCCZConnectorTest.java @@ -0,0 +1,14 @@ +package cgeo.geocaching.connector.oc; + +import static org.assertj.core.api.Assertions.assertThat; +import junit.framework.TestCase; + +public class OCCZConnectorTest extends TestCase { + + public static void testGetGeocodeFromUrl() throws Exception { + final OCCZConnector connector = new OCCZConnector(); + assertThat(connector.getGeocodeFromUrl("http://opencaching.cz/viewcache.php?cacheid=610")).isEqualTo("OZ0262"); + assertThat(connector.getGeocodeFromUrl("http://www.opencaching.de/viewcache.php?cacheid=151223")).isNull(); + } + +} diff --git a/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java b/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java index 0aad3aa..697b8d8 100644 --- a/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java +++ b/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java @@ -24,4 +24,15 @@ public class OCConnectorTest extends TestCase { return ocConnector; } + public static void testGetGeocodeFromUrlDe() throws Exception { + final IConnector connector = ConnectorFactory.getConnector("OC0028"); + assertThat(connector.getGeocodeFromUrl("http://opencaching.de/OC0028")).isEqualTo("OC0028"); + assertThat(connector.getGeocodeFromUrl("http://www.opencaching.de/OC0028")).isEqualTo("OC0028"); + } + + public static void testGetGeocodeFromUrlUs() throws Exception { + final IConnector connector = ConnectorFactory.getConnector("OU07A0"); + assertThat(connector.getGeocodeFromUrl("http://www.opencaching.us/viewcache.php?wp=OU07A0")).isEqualTo("OU07A0"); + } + } diff --git a/tests/src/cgeo/geocaching/connector/oc/OkapiClientTest.java b/tests/src/cgeo/geocaching/connector/oc/OkapiClientTest.java index aa66ac9..22b2dbe 100644 --- a/tests/src/cgeo/geocaching/connector/oc/OkapiClientTest.java +++ b/tests/src/cgeo/geocaching/connector/oc/OkapiClientTest.java @@ -30,7 +30,7 @@ public class OkapiClientTest extends CGeoTestCase { public static void testOCSearchMustWorkWithoutOAuthAccessTokens() { final String geoCode = "OC1234"; - Geocache cache = OkapiClient.getCache(geoCode); + final Geocache cache = OkapiClient.getCache(geoCode); assertThat(cache).overridingErrorMessage("You must have a valid OKAPI key installed for running this test (but you do not need to set credentials in the app).").isNotNull(); assertThat(cache.getName()).isEqualTo("Wupper-Schein"); } @@ -55,9 +55,16 @@ public class OkapiClientTest extends CGeoTestCase { final String geoCode = "OC6465"; removeCacheCompletely(geoCode); - Geocache cache = OkapiClient.getCache(geoCode); + final Geocache cache = OkapiClient.getCache(geoCode); assertThat(cache).as("Cache from OKAPI").isNotNull(); assertThat(cache.getLogCounts().get(LogType.WILL_ATTEND)).isGreaterThan(0); } + public static void testGetAllLogs() { + final String geoCode = "OC10CB8"; + final Geocache cache = OkapiClient.getCache(geoCode); + final int defaultLogCount = 10; + assertThat(cache.getLogs().size()).isGreaterThan(defaultLogCount); + } + } diff --git a/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java b/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java index 5433387..11f2d66 100644 --- a/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java +++ b/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java @@ -14,4 +14,10 @@ public class OXConnectorTest extends TestCase { assertThat(oxConnector.canHandle("OX_")).isFalse(); } + public static void testGetGeocodeFromUrl() { + final OXConnector connector = new OXConnector(); + assertThat(connector.getGeocodeFromUrl("http://www.opencaching.com/de/#!geocache/OX1R421")).isEqualTo("OX1R421"); + assertThat(connector.getGeocodeFromUrl("http://www.opencaching.com/#!geocache/OX1R421")).isEqualTo("OX1R421"); + } + } diff --git a/tests/src/cgeo/geocaching/connector/trackable/SwaggieConnectorTest.java b/tests/src/cgeo/geocaching/connector/trackable/SwaggieConnectorTest.java new file mode 100644 index 0000000..54c3319 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/trackable/SwaggieConnectorTest.java @@ -0,0 +1,29 @@ +package cgeo.geocaching.connector.trackable; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.Trackable; + +import junit.framework.TestCase; + +public class SwaggieConnectorTest extends TestCase { + + public static void testCanHandleTrackable() { + assertThat(new SwaggieConnector().canHandleTrackable("SW0001")).isTrue(); + assertThat(new SwaggieConnector().canHandleTrackable("SWABCD")).isFalse(); + assertThat(new SwaggieConnector().canHandleTrackable("GK82A2")).isFalse(); + assertThat(new SwaggieConnector().canHandleTrackable("TB1234")).isFalse(); + assertThat(new SwaggieConnector().canHandleTrackable("UNKNOWN")).isFalse(); + } + + public static void testGetTrackableCodeFromUrl() throws Exception { + assertThat(new SwaggieConnector().getTrackableCodeFromUrl("http://geocaching.com.au/swaggie/sw0017")).isEqualTo("SW0017"); + } + + public static void testGetUrl() throws Exception { + final Trackable trackable = new Trackable(); + trackable.setGeocode("SW0017"); + assertThat(new SwaggieConnector().getUrl(trackable)).isEqualTo("http://geocaching.com.au/swaggie/SW0017"); + } + +} diff --git a/tests/src/cgeo/geocaching/connector/trackable/SwaggieParserTest.java b/tests/src/cgeo/geocaching/connector/trackable/SwaggieParserTest.java new file mode 100644 index 0000000..2661fb3 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/trackable/SwaggieParserTest.java @@ -0,0 +1,34 @@ +package cgeo.geocaching.connector.trackable; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.Trackable; +import cgeo.geocaching.connector.ConnectorFactory; +import cgeo.geocaching.network.Network; + +import junit.framework.TestCase; + +public class SwaggieParserTest extends TestCase { + + public static void testParse() { + final Trackable trackableIn = new Trackable(); + trackableIn.setGeocode("SW0017"); + + final String trackableUrl = ConnectorFactory.getTrackableConnector(trackableIn.getGeocode()).getUrl(trackableIn); + assertThat(trackableUrl).isNotNull(); + + final String page = Network.getResponseData(Network.getRequest(trackableUrl)); + assertThat(page).isNotNull(); + + assert page != null; + final Trackable trackable = SwaggieParser.parse(page); + assertThat(trackable).isNotNull(); + assert trackable != null; + + assertThat(trackable.getName()).isEqualTo("Law and Order 1"); + assertThat(trackable.getGeocode()).isEqualTo("SW0017"); + assertThat(trackable.getDetails()).isEqualTo("A mini CD that may or may not contain a DivX .AVI of the Law and Order: CSI episode that features geocaching as part of the storyline."); + assertThat(trackable.getType()).isEqualTo("Swaggie"); + assertThat(trackable.getOwner()).isEqualTo("Bear_Left"); + } +} diff --git a/tests/src/cgeo/geocaching/connector/trackable/TravelBugConnectorTest.java b/tests/src/cgeo/geocaching/connector/trackable/TravelBugConnectorTest.java index 8d80822..7123e87 100644 --- a/tests/src/cgeo/geocaching/connector/trackable/TravelBugConnectorTest.java +++ b/tests/src/cgeo/geocaching/connector/trackable/TravelBugConnectorTest.java @@ -26,14 +26,16 @@ public class TravelBugConnectorTest extends TestCase { } public static void testOnlineSearchBySecretCode() { - Trackable trackable = getConnector().searchTrackable("GST9HV", null, null); + final Trackable trackable = getConnector().searchTrackable("GST9HV", null, null); assertThat(trackable).isNotNull(); + assert trackable != null; assertThat(trackable.getName()).isEqualTo("Deutschland"); } public static void testOnlineSearchByPublicCode() { - Trackable trackable = getConnector().searchTrackable("TB4JD36", null, null); + final Trackable trackable = getConnector().searchTrackable("TB4JD36", null, null); assertThat(trackable).isNotNull(); + assert trackable != null; assertThat(trackable.getName()).isEqualTo("Mein Kilometerzähler"); } diff --git a/tests/src/cgeo/geocaching/connector/trackable/UnknownTrackableConnectorTest.java b/tests/src/cgeo/geocaching/connector/trackable/UnknownTrackableConnectorTest.java new file mode 100644 index 0000000..ded0a1a --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/trackable/UnknownTrackableConnectorTest.java @@ -0,0 +1,44 @@ +package cgeo.geocaching.connector.trackable; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.Trackable; + +import junit.framework.TestCase; + +public class UnknownTrackableConnectorTest extends TestCase { + + private static UnknownTrackableConnector getConnector() { + return new UnknownTrackableConnector(); + } + + public static void testCanHandleTrackable() throws Exception { + assertThat(getConnector().canHandleTrackable("TB1234")).isFalse(); + } + + public static void testGetUrl() throws Exception { + try { + getConnector().getUrl(new Trackable()); + fail("IllegalStateException expected"); + } catch (final IllegalStateException e) { + // empty + } + } + + public static void testSearchTrackable() throws Exception { + assertThat(getConnector().searchTrackable("TB1234", null, null)).isNull(); + } + + public static void testIsLoggable() throws Exception { + assertThat(getConnector().isLoggable()).isFalse(); + } + + public static void testGetTrackableCodeFromUrl() throws Exception { + assertThat(getConnector().getTrackableCodeFromUrl("http://www.sometrackable.com/1234")).isNull(); + } + + public static void testGetUserActions() throws Exception { + assertThat(getConnector().getUserActions()).isEmpty(); + } + +} diff --git a/tests/src/cgeo/geocaching/export/ExportTest.java b/tests/src/cgeo/geocaching/export/ExportTest.java index bec676f..21c45a0 100644 --- a/tests/src/cgeo/geocaching/export/ExportTest.java +++ b/tests/src/cgeo/geocaching/export/ExportTest.java @@ -25,7 +25,7 @@ public class ExportTest extends CGeoTestCase { final Geocache cache = new Geocache(); cache.setGeocode("GCX1234"); final LogEntry log = new LogEntry(1353244820000L, LogType.FOUND_IT, "Hidden in a tree"); - FieldNotes fieldNotes = new FieldNotes(); + final FieldNotes fieldNotes = new FieldNotes(); fieldNotes.add(cache, log); assertEquals("Non matching export " + fieldNotes.getContent(), "GCX1234,2012-11-18T13:20:20Z,Found it,\"Hidden in a tree\"\n", fieldNotes.getContent()); } @@ -55,8 +55,8 @@ public class ExportTest extends CGeoTestCase { cache.setDetailed(true); DataStore.saveCache(cache, LoadFlags.SAVE_ALL); - List<Geocache> exportList = Collections.singletonList(cache); - GpxExportTester gpxExport = new GpxExportTester(); + final List<Geocache> exportList = Collections.singletonList(cache); + final GpxExportTester gpxExport = new GpxExportTester(); File result = null; try { result = gpxExport.testExportSync(exportList); @@ -67,17 +67,22 @@ public class ExportTest extends CGeoTestCase { assertThat(result).isNotNull(); // make sure we actually exported waypoints - String gpx = org.apache.commons.io.FileUtils.readFileToString(result); + final String gpx = org.apache.commons.io.FileUtils.readFileToString(result); assertThat(gpx).contains("<wpt"); assertThat(gpx).contains(cache.getGeocode()); + if (cache.getUrl() != null) { + assertThat(gpx).contains("<url>"); + } else { + assertThat(gpx).doesNotContain("<url>"); + } FileUtils.deleteIgnoringFailure(result); } private static class GpxExportTester extends GpxExport { - public File testExportSync(List<Geocache> caches) throws InterruptedException, ExecutionException { - final ArrayList<String> geocodes = new ArrayList<String>(caches.size()); + public File testExportSync(final List<Geocache> caches) throws InterruptedException, ExecutionException { + final ArrayList<String> geocodes = new ArrayList<>(caches.size()); for (final Geocache cache : caches) { geocodes.add(cache.getGeocode()); } diff --git a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java index c43ad38..b20b066 100644 --- a/tests/src/cgeo/geocaching/export/GpxSerializerTest.java +++ b/tests/src/cgeo/geocaching/export/GpxSerializerTest.java @@ -38,13 +38,13 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { final AtomicReference<Integer> importedCount = new AtomicReference<Integer>(0); final StringWriter writer = new StringWriter(); - Geocache cache = loadCacheFromResource(R.raw.gc1bkp3_gpx101); + final 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) { + public void publishProgress(final int countExported) { importedCount.set(countExported); } }); @@ -54,8 +54,6 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { /** * 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"; @@ -70,7 +68,7 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { 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); + final Collection<Geocache> caches = parser.parse(stream, null); assertThat(caches).isNotNull(); assertThat(caches).hasSize(1); @@ -78,43 +76,43 @@ public class GpxSerializerTest extends AbstractResourceInstrumentationTestCase { assertThat(replaceLogIds(gpxSecond)).isEqualTo(replaceLogIds(gpxFirst)); } - private static String replaceLogIds(String gpx) { + private static String replaceLogIds(final String gpx) { return gpx.replaceAll("log id=\"\\d*\"", ""); } - private static String getGPXFromCache(String geocode) throws IOException { + private static String getGPXFromCache(final 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"); + final Geocache cache = withLocation("state, country"); assertThat(GpxSerializer.getState(cache)).isEqualTo("state"); } public static void testCountryFromStateCountry() throws Exception { - Geocache cache = withLocation("state, country"); + final Geocache cache = withLocation("state, country"); assertThat(GpxSerializer.getCountry(cache)).isEqualTo("country"); } public static void testCountryFromCountryOnly() throws Exception { - Geocache cache = withLocation("somewhere"); + final Geocache cache = withLocation("somewhere"); assertThat(GpxSerializer.getCountry(cache)).isEqualTo("somewhere"); } public static void testStateFromCountryOnly() throws Exception { - Geocache cache = withLocation("somewhere"); + final 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 + final 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(); + final Geocache cache = new Geocache(); cache.setLocation(location); return cache; } diff --git a/tests/src/cgeo/geocaching/files/GPXImporterTest.java b/tests/src/cgeo/geocaching/files/GPXImporterTest.java index 9426fe8..86fa90d 100644 --- a/tests/src/cgeo/geocaching/files/GPXImporterTest.java +++ b/tests/src/cgeo/geocaching/files/GPXImporterTest.java @@ -153,11 +153,11 @@ public class GPXImporterTest extends AbstractResourceInstrumentationTestCase { assertThat(cache.getName()).isEqualTo("First Aid Station #1"); } - private void assertImportStepMessages(int... importSteps) { - assertThat(importStepHandler.messages).hasSize(importSteps.length); - for (int i = 0; i < importSteps.length; i++) { + private void assertImportStepMessages(final int... importSteps) { + for (int i = 0; i < Math.min(importSteps.length, importStepHandler.messages.size()); i++) { assertThat(importStepHandler.messages.get(i).what).isEqualTo(importSteps[i]); } + assertThat(importStepHandler.messages).hasSize(importSteps.length); } public void testImportLoc() throws IOException { diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index 2cc1f34..7c40b2a 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -6,6 +6,8 @@ import cgeo.geocaching.DataStore; import cgeo.geocaching.Geocache; import cgeo.geocaching.LogEntry; import cgeo.geocaching.Waypoint; +import cgeo.geocaching.connector.ConnectorFactory; +import cgeo.geocaching.connector.IConnector; import cgeo.geocaching.enumerations.CacheSize; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LoadFlags; @@ -14,7 +16,7 @@ import cgeo.geocaching.enumerations.WaypointType; import cgeo.geocaching.location.Geopoint; import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; import cgeo.geocaching.test.R; -import cgeo.geocaching.utils.DateUtils; +import cgeo.geocaching.utils.CalendarUtils; import cgeo.geocaching.utils.SynchronizedDateFormat; import java.io.IOException; @@ -177,7 +179,7 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertThat(log.log).isEqualTo("Sehr schöne Runde und wir haben wieder etwas Neues über Hockenheim gelernt. Super Tarnung.\nTFTC, Geoteufel"); assertThat(log.isOwn()).isFalse(); assertThat(log.getDisplayText()).isEqualTo(log.log); - assertThat(DateUtils.daysSince(log.date) > 700).isTrue(); + assertThat(CalendarUtils.daysSince(log.date) > 700).isTrue(); // following info is not contained in pocket query gpx file assertThat(cache.getAttributes()).isEmpty(); @@ -411,6 +413,9 @@ public class GPXParserTest extends AbstractResourceInstrumentationTestCase { assertThat(lab.getName()).isEqualTo("01_Munich Olympic Walk Of Stars_Updated-Project MUNICH2014 - Mia san Giga! Olympiapark"); assertThat(lab.getShortDescription()).isEqualTo("01_Munich Olympic Walk Of Stars_Updated (Giga! Olympia Park)-Project MUNICH2014 - Mia san Giga! Olympiapark"); assertThat(lab.getDescription()).startsWith("DEU:"); + + final IConnector unknownConnector = ConnectorFactory.getConnector("ABC123"); + assertThat(ConnectorFactory.getConnector(lab)).isSameAs(unknownConnector); } } diff --git a/tests/src/cgeo/geocaching/filter/StateArchivedFilterTest.java b/tests/src/cgeo/geocaching/filter/StateArchivedFilterTest.java index d474857..3353fe9 100644 --- a/tests/src/cgeo/geocaching/filter/StateArchivedFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateArchivedFilterTest.java @@ -4,11 +4,10 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateArchivedFilter; public class StateArchivedFilterTest extends CGeoTestCase { - private StateFilter.StateArchivedFilter archivedFilter; + private StateFilterFactory.StateArchivedFilter archivedFilter; private Geocache archivedCache; @Override @@ -16,7 +15,7 @@ public class StateArchivedFilterTest extends CGeoTestCase { super.setUp(); // members can only be setup here, after application is initialized - archivedFilter = new StateArchivedFilter(); + archivedFilter = new StateFilterFactory.StateArchivedFilter(); archivedCache = new Geocache(); archivedCache.setArchived(true); } diff --git a/tests/src/cgeo/geocaching/filter/StateDisabledFilterTest.java b/tests/src/cgeo/geocaching/filter/StateDisabledFilterTest.java index 71a1134..eff3a67 100644 --- a/tests/src/cgeo/geocaching/filter/StateDisabledFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateDisabledFilterTest.java @@ -4,18 +4,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateDisabledFilter; public class StateDisabledFilterTest extends CGeoTestCase { - private StateFilter.StateDisabledFilter disabledFilter; + private StateFilterFactory.StateDisabledFilter disabledFilter; private Geocache disabledCache; @Override protected void setUp() throws Exception { super.setUp(); - disabledFilter = new StateDisabledFilter(); + disabledFilter = new StateFilterFactory.StateDisabledFilter(); disabledCache = new Geocache(); disabledCache.setDisabled(true); } diff --git a/tests/src/cgeo/geocaching/filter/StateFoundFilterTest.java b/tests/src/cgeo/geocaching/filter/StateFoundFilterTest.java index e763881..beedd51 100644 --- a/tests/src/cgeo/geocaching/filter/StateFoundFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateFoundFilterTest.java @@ -4,18 +4,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateFoundFilter; public class StateFoundFilterTest extends CGeoTestCase { - private StateFilter.StateFoundFilter foundFilter; + private StateFilterFactory.StateFoundFilter foundFilter; private Geocache foundCache; @Override protected void setUp() throws Exception { super.setUp(); - foundFilter = new StateFoundFilter(); + foundFilter = new StateFilterFactory.StateFoundFilter(); foundCache = new Geocache(); foundCache.setFound(true); } diff --git a/tests/src/cgeo/geocaching/filter/StateNonPremiumFilterTest.java b/tests/src/cgeo/geocaching/filter/StateNonPremiumFilterTest.java index c698e8b..87a8909 100644 --- a/tests/src/cgeo/geocaching/filter/StateNonPremiumFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateNonPremiumFilterTest.java @@ -4,18 +4,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateNonPremiumFilter; public class StateNonPremiumFilterTest extends CGeoTestCase { - private StateFilter.StateNonPremiumFilter nonPremiumFilter; + private StateFilterFactory.StateNonPremiumFilter nonPremiumFilter; private Geocache premiumCache; @Override protected void setUp() throws Exception { super.setUp(); - nonPremiumFilter = new StateNonPremiumFilter(); + nonPremiumFilter = new StateFilterFactory.StateNonPremiumFilter(); premiumCache = new Geocache(); premiumCache.setPremiumMembersOnly(true); } diff --git a/tests/src/cgeo/geocaching/filter/StateNotFoundFilterTest.java b/tests/src/cgeo/geocaching/filter/StateNotFoundFilterTest.java index 2bf9d94..bb5098c 100644 --- a/tests/src/cgeo/geocaching/filter/StateNotFoundFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateNotFoundFilterTest.java @@ -4,18 +4,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateNotFoundFilter; public class StateNotFoundFilterTest extends CGeoTestCase { - private StateFilter.StateNotFoundFilter notFoundFilter; + private StateFilterFactory.StateNotFoundFilter notFoundFilter; private Geocache foundCache; @Override protected void setUp() throws Exception { super.setUp(); - notFoundFilter = new StateNotFoundFilter(); + notFoundFilter = new StateFilterFactory.StateNotFoundFilter(); foundCache = new Geocache(); foundCache.setFound(true); } diff --git a/tests/src/cgeo/geocaching/filter/StatePremiumFilterTest.java b/tests/src/cgeo/geocaching/filter/StatePremiumFilterTest.java index ac8682b..c06a4fe 100644 --- a/tests/src/cgeo/geocaching/filter/StatePremiumFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StatePremiumFilterTest.java @@ -4,18 +4,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StatePremiumFilter; public class StatePremiumFilterTest extends CGeoTestCase { - private StateFilter.StatePremiumFilter premiumFilter; + private StateFilterFactory.StatePremiumFilter premiumFilter; private Geocache premiumCache; @Override protected void setUp() throws Exception { super.setUp(); - premiumFilter = new StatePremiumFilter(); + premiumFilter = new StateFilterFactory.StatePremiumFilter(); premiumCache = new Geocache(); premiumCache.setPremiumMembersOnly(true); } diff --git a/tests/src/cgeo/geocaching/filter/StateStoredFilterTest.java b/tests/src/cgeo/geocaching/filter/StateStoredFilterTest.java index d110921..5d1eecc 100644 --- a/tests/src/cgeo/geocaching/filter/StateStoredFilterTest.java +++ b/tests/src/cgeo/geocaching/filter/StateStoredFilterTest.java @@ -4,20 +4,18 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.CGeoTestCase; import cgeo.geocaching.Geocache; -import cgeo.geocaching.filter.StateFilter.StateNotStoredFilter; -import cgeo.geocaching.filter.StateFilter.StateStoredFilter; public class StateStoredFilterTest extends CGeoTestCase { - private StateFilter.StateStoredFilter storedFilter; - private StateFilter.StateNotStoredFilter notStoredFilter; + private StateFilterFactory.StateStoredFilter storedFilter; + private StateFilterFactory.StateNotStoredFilter notStoredFilter; private Geocache cache; @Override protected void setUp() throws Exception { super.setUp(); - storedFilter = new StateStoredFilter(); - notStoredFilter = new StateNotStoredFilter(); + storedFilter = new StateFilterFactory.StateStoredFilter(); + notStoredFilter = new StateFilterFactory.StateNotStoredFilter(); cache = new Geocache(); } diff --git a/tests/src/cgeo/geocaching/sorting/NameComparatorTest.java b/tests/src/cgeo/geocaching/sorting/NameComparatorTest.java index 533135d..6d4fa46 100644 --- a/tests/src/cgeo/geocaching/sorting/NameComparatorTest.java +++ b/tests/src/cgeo/geocaching/sorting/NameComparatorTest.java @@ -18,7 +18,7 @@ public class NameComparatorTest extends AndroidTestCase { } } - private NameComparator comp = new NameComparator(); + private final NameComparator comp = new NameComparator(); public void testLexical() { assertSorted(new NamedCache("A"), new NamedCache("Z")); @@ -38,11 +38,10 @@ public class NameComparatorTest extends AndroidTestCase { /** * Assert that a given collection of names is already sorted correctly. * - * @param names */ - private void assertSortedNames(String... names) { - ArrayList<Geocache> caches = new ArrayList<Geocache>(names.length); - for (String name : names) { + private void assertSortedNames(final String... names) { + final ArrayList<Geocache> caches = new ArrayList<Geocache>(names.length); + for (final String name : names) { caches.add(new NamedCache(name)); } Collections.sort(caches, comp); diff --git a/tests/src/cgeo/geocaching/test/WhitespaceTest.java b/tests/src/cgeo/geocaching/test/WhitespaceTest.java index 1ce7cc8..110cdbc 100644 --- a/tests/src/cgeo/geocaching/test/WhitespaceTest.java +++ b/tests/src/cgeo/geocaching/test/WhitespaceTest.java @@ -30,8 +30,6 @@ public class WhitespaceTest extends AbstractResourceInstrumentationTestCase { * The place for the implementation to prove that the new version of replaceWhitespace is faster than * BaseUtils.replaceWhitespace() * - * @param data - * @return */ public static String replaceWhitespaceManually(final String data) { final int length = data.length(); @@ -39,7 +37,7 @@ public class WhitespaceTest extends AbstractResourceInstrumentationTestCase { data.getChars(0, length, chars, 0); int resultSize = 0; boolean lastWasWhitespace = true; - for (char c : chars) { + for (final char c : chars) { if (c == ' ' || c == '\n' || c == '\r' || c == '\t') { if (!lastWasWhitespace) { chars[resultSize++] = ' '; diff --git a/tests/src/cgeo/geocaching/utils/DateUtilsTest.java b/tests/src/cgeo/geocaching/utils/CalendarUtilsTest.java index d0a2b4a..c074903 100644 --- a/tests/src/cgeo/geocaching/utils/DateUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/CalendarUtilsTest.java @@ -5,17 +5,17 @@ import static org.assertj.core.api.Assertions.assertThat; import cgeo.geocaching.Geocache; import cgeo.geocaching.enumerations.CacheType; -import java.util.Calendar; - import junit.framework.TestCase; -public class DateUtilsTest extends TestCase { +import java.util.Calendar; + +public class CalendarUtilsTest extends TestCase { public static void testDaysSince() { final Calendar start = Calendar.getInstance(); for (int hour = 0; hour < 24; hour++) { start.set(Calendar.HOUR_OF_DAY, hour); - assertThat(DateUtils.daysSince(start.getTimeInMillis())).isEqualTo(0); + assertThat(CalendarUtils.daysSince(start.getTimeInMillis())).isEqualTo(0); } } @@ -32,12 +32,22 @@ public class DateUtilsTest extends TestCase { assertPastEvent(start, true); } - private static void assertPastEvent(final Calendar start, boolean expectedPast) { + private static void assertPastEvent(final Calendar start, final boolean expectedPast) { final Geocache cache = new Geocache(); cache.setType(CacheType.EVENT); cache.setHidden(start.getTime()); - assertThat(DateUtils.isPastEvent(cache)).isEqualTo(expectedPast); + assertThat(CalendarUtils.isPastEvent(cache)).isEqualTo(expectedPast); } + public static void testIsFuture() { + final Calendar date = Calendar.getInstance(); + assertThat(CalendarUtils.isFuture(date)).isFalse(); + + date.add(Calendar.DAY_OF_MONTH, 1); + assertThat(CalendarUtils.isFuture(date)).isFalse(); + + date.add(Calendar.DAY_OF_MONTH, 1); + assertThat(CalendarUtils.isFuture(date)).isTrue(); + } } diff --git a/tests/src/cgeo/geocaching/utils/CheckerUtilsTest.java b/tests/src/cgeo/geocaching/utils/CheckerUtilsTest.java new file mode 100644 index 0000000..fa7c7e7 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/CheckerUtilsTest.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.Geocache; + +import junit.framework.TestCase; + +public class CheckerUtilsTest extends TestCase { + + public static void testGetCheckerUrl() throws Exception { + assertUrl("<p style=\"text-align:center;\"><a href=\"http://geocheck.org/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f\" target=\"_blank\"><img", "http://geocheck.org/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f"); + assertUrl("<p style=\"text-align:center;\"><a href=\"http://google.com/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f\" target=\"_blank\"><img", null); + assertUrl("http://www.certitudes.org/certitude?wp=GC5MVX7", "http://www.certitudes.org/certitude?wp=GC5MVX7"); + assertUrl("http://geochecker.com/index.php?code=e001928e3c2682ec2bae0f24b9d02cfb&action=check&wp=474350573454&name=47656f636865636b6572205465737420666f72204e33382030302e303030205737362030302e303030", "http://geochecker.com/index.php?code=e001928e3c2682ec2bae0f24b9d02cfb&action=check&wp=474350573454&name=47656f636865636b6572205465737420666f72204e33382030302e303030205737362030302e303030"); + assertUrl("<p>Haarige Aussichten gibt es <a href=\"http://www.geochecker.com/index.php?code=cd52752a8649c5e385a624b5341176f9&action=check&wp=4743314a43384b&name=4b61747a656e&language=German\">hier</a>.</p></span>", "http://www.geochecker.com/index.php?code=cd52752a8649c5e385a624b5341176f9&action=check&wp=4743314a43384b&name=4b61747a656e&language=German"); + } + + private static void assertUrl(final String description, final String expected) { + final Geocache geocache = new Geocache(); + geocache.setDescription(description); + assertThat(CheckerUtils.getCheckerUrl(geocache)).isEqualTo(expected); + } + +} diff --git a/tests/src/cgeo/geocaching/utils/FileUtilsTest.java b/tests/src/cgeo/geocaching/utils/FileUtilsTest.java new file mode 100644 index 0000000..65638e1 --- /dev/null +++ b/tests/src/cgeo/geocaching/utils/FileUtilsTest.java @@ -0,0 +1,49 @@ +package cgeo.geocaching.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import cgeo.geocaching.files.LocalStorage; + +import junit.framework.TestCase; + +import java.io.File; +import java.io.IOException; + +public class FileUtilsTest extends TestCase { + + final File testDir = LocalStorage.getStorageDir("automated-tests"); + final File baseFile = new File(testDir, "prefix.ext"); + final File alternative1 = new File(testDir, "prefix_1.ext"); + final File alternative2 = new File(testDir, "prefix_2.ext"); + + public void testGetUniqueNamedFile() throws IOException { + FileUtils.deleteDirectory(testDir); + testDir.mkdirs(); + try { + assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(baseFile); + baseFile.createNewFile(); + assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative1); + alternative1.createNewFile(); + assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative2); + assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative2); + } finally { + FileUtils.deleteDirectory(testDir); + } + } + + public static void testFileUrl() { + assertThat(FileUtils.isFileUrl("file:///tmp/foo/bar")).isTrue(); + assertThat(FileUtils.isFileUrl("http://www.google.com")).isFalse(); + assertThat(FileUtils.fileToUrl(new File("/tmp/foo/bar"))).isEqualTo("file:///tmp/foo/bar"); + assertThat(FileUtils.urlToFile("file:///tmp/foo/bar").getPath()).isEqualTo("/tmp/foo/bar"); + } + + public void testCreateRemoveDirectories() { + FileUtils.deleteDirectory(testDir); + assertThat(testDir.exists()).isFalse(); + FileUtils.mkdirs(testDir); + assertThat(testDir.exists()).isTrue(); + FileUtils.deleteDirectory(testDir); + assertThat(testDir.exists()).isFalse(); + } +} diff --git a/tests/src/cgeo/geocaching/utils/ProcessUtilsTest.java b/tests/src/cgeo/geocaching/utils/ProcessUtilsTest.java index 2c6ed18..b442c8a 100644 --- a/tests/src/cgeo/geocaching/utils/ProcessUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/ProcessUtilsTest.java @@ -6,7 +6,7 @@ import junit.framework.TestCase; public class ProcessUtilsTest extends TestCase { public static void testIsInstalled() { - assertThat(ProcessUtils.isInstalled("com.android.launcher")).isTrue(); + assertThat(ProcessUtils.isInstalled("com.android.settings")).isTrue(); } public static void testIsInstalledNotLaunchable() { @@ -16,7 +16,7 @@ public class ProcessUtilsTest extends TestCase { } public static void testIsLaunchable() { - assertThat(ProcessUtils.isInstalled("com.android.settings")).isTrue(); + assertThat(ProcessUtils.isLaunchable("com.android.settings")).isTrue(); } } diff --git a/tests/src/cgeo/geocaching/utils/RxUtilsTest.java b/tests/src/cgeo/geocaching/utils/RxUtilsTest.java index 2487184..cc8589b 100644 --- a/tests/src/cgeo/geocaching/utils/RxUtilsTest.java +++ b/tests/src/cgeo/geocaching/utils/RxUtilsTest.java @@ -4,7 +4,6 @@ import static org.assertj.core.api.Assertions.assertThat; import rx.Observable; import rx.Subscription; -import rx.functions.Func1; import rx.subjects.PublishSubject; import rx.subjects.ReplaySubject; @@ -21,16 +20,6 @@ public class RxUtilsTest extends AndroidTestCase { range.onCompleted(); } - public static void testTakeUntil() { - final Observable<Integer> observable = range.lift(RxUtils.operatorTakeUntil(new Func1<Integer, Boolean>() { - @Override - public Boolean call(final Integer value) { - return value > 6; - } - })); - assertThat(observable.toList().toBlocking().single().toArray()).isEqualTo(new int[]{1, 2, 3, 4, 5, 6, 7}); - } - public static void testRememberLast() { final PublishSubject<String> rawObservable = PublishSubject.create(); final Observable<String> observable = RxUtils.rememberLast(rawObservable, "initial"); diff --git a/tests/src/cgeo/junit/CgeoTestRunner.java b/tests/src/cgeo/junit/CgeoTestRunner.java index 16a5f12..94803cf 100644 --- a/tests/src/cgeo/junit/CgeoTestRunner.java +++ b/tests/src/cgeo/junit/CgeoTestRunner.java @@ -11,10 +11,12 @@ import android.util.Log; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; /** * Test runner which derives from the newer Google instrumentation test runner used by the Espresso test framework. It - * adds junit report functionality by cloning the behaviour of the {@link JUnitReportTestRunner}. + * adds junit report functionality by cloning the behavior of the {@link JUnitReportTestRunner}. * */ public class CgeoTestRunner extends GoogleInstrumentationTestRunner { @@ -86,21 +88,32 @@ public class CgeoTestRunner extends GoogleInstrumentationTestRunner { @Override public void start() { - mListener = new JUnitReportListener(getContext(), getTargetContext(), mReportFile, mReportDir, mFilterTraces, mMultiFile); - try { - Class<?> c = getClass(); - Field bridgeTestRunner = c.getSuperclass().getDeclaredField("bridgeTestRunner"); - bridgeTestRunner.setAccessible(true); - Object obj = bridgeTestRunner.get(this); - Method m = obj.getClass().getDeclaredMethod("getAndroidTestRunner", (Class[]) null); - AndroidTestRunner androidTestRunner = (AndroidTestRunner) m.invoke(obj); - androidTestRunner.addTestListener(mListener); - } catch (NoSuchFieldException | InvocationTargetException | IllegalAccessException | NoSuchMethodException | SecurityException x) { - Log.e(LOG_TAG, x.toString()); - } + makeAndroidTestRunnerAccessible(); super.start(); } + private void makeAndroidTestRunnerAccessible() { + AccessController.doPrivileged(new PrivilegedAction<Void>() { + @Override + public Void run() { + mListener = new JUnitReportListener(getContext(), getTargetContext(), mReportFile, mReportDir, mFilterTraces, mMultiFile); + try { + Class<?> c = getClass(); + Field bridgeTestRunner = c.getSuperclass().getDeclaredField("bridgeTestRunner"); + bridgeTestRunner.setAccessible(true); + Object obj = bridgeTestRunner.get(this); + Method m = obj.getClass().getDeclaredMethod("getAndroidTestRunner", (Class[]) null); + AndroidTestRunner androidTestRunner = (AndroidTestRunner) m.invoke(obj); + androidTestRunner.addTestListener(mListener); + } catch (NoSuchFieldException | InvocationTargetException | IllegalAccessException + | NoSuchMethodException | SecurityException x) { + Log.e(LOG_TAG, x.toString()); + } + return null; + } + }); + } + @Override public void finish(int resultCode, Bundle results) { if (mListener != null) { |