aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMathieu Alorent <github@kumy.net>2015-07-14 14:01:30 +0200
committerMathieu Alorent <malorent.ext@orange.com>2015-07-20 10:31:19 +0200
commitaf46d3b08c3c91dab638ba83cf32a638657c5e7a (patch)
tree5938e0404cea1d62fd4d3213b714fa4448c0a2fd
parent27e92d7a8764df874064efcc9f5c1824be1ef277 (diff)
downloadcgeo-af46d3b08c3c91dab638ba83cf32a638657c5e7a.zip
cgeo-af46d3b08c3c91dab638ba83cf32a638657c5e7a.tar.gz
cgeo-af46d3b08c3c91dab638ba83cf32a638657c5e7a.tar.bz2
Fix #4976: Request to gc.com tile server sent for caches from unknown sources (e.g. lab caches)
Introduce IConnector.handledGeocodes() which filter Set of Geocodes and return only Geocodes usable by the specified connector. Then filter Geocodes in GCMap.searchByGeocodes(). Conflicts: tests/src/cgeo/geocaching/connector/tc/TerraCachingConnectorTest.java
-rw-r--r--main/src/cgeo/geocaching/connector/AbstractConnector.java13
-rw-r--r--main/src/cgeo/geocaching/connector/IConnector.java10
-rw-r--r--main/src/cgeo/geocaching/connector/gc/GCMap.java6
-rw-r--r--tests/src/cgeo/geocaching/connector/ConnectorFactoryTest.java27
-rw-r--r--tests/src/cgeo/geocaching/connector/GeocachingAustraliaConnectorTest.java30
-rw-r--r--tests/src/cgeo/geocaching/connector/GeopeitusConnectorTest.java28
-rw-r--r--tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java19
-rw-r--r--tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java6
-rw-r--r--tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java8
-rw-r--r--tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java8
-rw-r--r--tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java21
11 files changed, 169 insertions, 7 deletions
diff --git a/main/src/cgeo/geocaching/connector/AbstractConnector.java b/main/src/cgeo/geocaching/connector/AbstractConnector.java
index 1fc14a0..edd4aa8 100644
--- a/main/src/cgeo/geocaching/connector/AbstractConnector.java
+++ b/main/src/cgeo/geocaching/connector/AbstractConnector.java
@@ -25,7 +25,9 @@ import rx.functions.Action1;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
public abstract class AbstractConnector implements IConnector {
@@ -35,6 +37,17 @@ public abstract class AbstractConnector implements IConnector {
}
@Override
+ public Set<String> handledGeocodes(@NonNull final Set<String> geocodes) {
+ final Set<String> strippedList = new HashSet<>();
+ for (final String geocode: geocodes) {
+ if (canHandle(geocode)) {
+ strippedList.add(geocode);
+ }
+ }
+ return strippedList;
+ }
+
+ @Override
public boolean supportsWatchList() {
return false;
}
diff --git a/main/src/cgeo/geocaching/connector/IConnector.java b/main/src/cgeo/geocaching/connector/IConnector.java
index 74b1028..479d378 100644
--- a/main/src/cgeo/geocaching/connector/IConnector.java
+++ b/main/src/cgeo/geocaching/connector/IConnector.java
@@ -10,6 +10,7 @@ import org.eclipse.jdt.annotation.Nullable;
import java.util.Collection;
import java.util.List;
+import java.util.Set;
public interface IConnector {
/**
@@ -29,6 +30,15 @@ public interface IConnector {
public boolean canHandle(final @NonNull String geocode);
/**
+ * Return a new geocodes list, with only geocodes for which this connector is responsible.
+ *
+ * @param geocodes
+ * list of geocodes of a cache
+ * @return return a new stripped list
+ */
+ public Set<String> handledGeocodes(final @NonNull Set<String> geocodes);
+
+ /**
* Get the browser URL for the given cache.
*
*/
diff --git a/main/src/cgeo/geocaching/connector/gc/GCMap.java b/main/src/cgeo/geocaching/connector/gc/GCMap.java
index 6f38fd4..295d22c 100644
--- a/main/src/cgeo/geocaching/connector/gc/GCMap.java
+++ b/main/src/cgeo/geocaching/connector/gc/GCMap.java
@@ -51,7 +51,11 @@ public class GCMap {
public static SearchResult searchByGeocodes(final Set<String> geocodes) {
final SearchResult result = new SearchResult();
- final String geocodeList = StringUtils.join(geocodes.toArray(), "|");
+ final Set<String> filteredGeocodes = GCConnector.getInstance().handledGeocodes(geocodes);
+ if (filteredGeocodes.isEmpty()) {
+ return result;
+ }
+ final String geocodeList = StringUtils.join(filteredGeocodes.toArray(), "|");
try {
final Parameters params = new Parameters("i", geocodeList, "_", String.valueOf(System.currentTimeMillis()));
diff --git a/tests/src/cgeo/geocaching/connector/ConnectorFactoryTest.java b/tests/src/cgeo/geocaching/connector/ConnectorFactoryTest.java
index 05ca6e4..e793350 100644
--- a/tests/src/cgeo/geocaching/connector/ConnectorFactoryTest.java
+++ b/tests/src/cgeo/geocaching/connector/ConnectorFactoryTest.java
@@ -8,6 +8,8 @@ import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase;
import cgeo.geocaching.test.mock.GC1ZXX2;
import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
public class ConnectorFactoryTest extends AbstractResourceInstrumentationTestCase {
@@ -96,4 +98,29 @@ public class ConnectorFactoryTest extends AbstractResourceInstrumentationTestCas
assertThat(ConnectorFactory.getTrackableFromURL("http://coord.info/GC1234")).isNull();
assertThat(ConnectorFactory.getTrackableFromURL("http://www.coord.info/GC1234")).isNull();
}
+
+ public static Set<String> getGeocodeSample() {
+ Set<String> geocodes = new HashSet<>(18);
+ geocodes.add("GC1234");
+ geocodes.add("OC1234");
+ geocodes.add("OX1234");
+ geocodes.add("EC1234");
+ geocodes.add("TCABC");
+ geocodes.add("WM1234");
+ geocodes.add("GE1234");
+ geocodes.add("GA1234");
+ geocodes.add("TP1234");
+
+ geocodes.add("GC5678");
+ geocodes.add("OC5678");
+ geocodes.add("OX5678");
+ geocodes.add("EC5678");
+ geocodes.add("TC2JP");
+ geocodes.add("WM5678");
+ geocodes.add("GE5678");
+ geocodes.add("GA5678");
+ geocodes.add("TP5678");
+
+ return geocodes;
+ }
}
diff --git a/tests/src/cgeo/geocaching/connector/GeocachingAustraliaConnectorTest.java b/tests/src/cgeo/geocaching/connector/GeocachingAustraliaConnectorTest.java
new file mode 100644
index 0000000..71e7d8c
--- /dev/null
+++ b/tests/src/cgeo/geocaching/connector/GeocachingAustraliaConnectorTest.java
@@ -0,0 +1,30 @@
+package cgeo.geocaching.connector;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import junit.framework.TestCase;
+
+import java.util.Set;
+
+public class GeocachingAustraliaConnectorTest extends TestCase {
+
+ private static IConnector getGeocachingAustraliaConnector() {
+ final IConnector gaConnector = ConnectorFactory.getConnector("GA1234");
+ assertThat(gaConnector).isNotNull();
+ return gaConnector;
+ }
+
+ public static void testCanHandle() {
+ final IConnector wmConnector = getGeocachingAustraliaConnector();
+
+ assertThat(wmConnector.canHandle("GA1234")).isTrue();
+ assertThat(wmConnector.canHandle("GAAB12")).isFalse();
+ assertThat(wmConnector.canHandle("TP1234")).isTrue();
+ assertThat(wmConnector.canHandle("TPAB12")).isFalse();
+ }
+
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(getGeocachingAustraliaConnector().handledGeocodes(geocodes)).containsOnly("GA1234", "TP1234", "GA5678", "TP5678");
+ }
+}
diff --git a/tests/src/cgeo/geocaching/connector/GeopeitusConnectorTest.java b/tests/src/cgeo/geocaching/connector/GeopeitusConnectorTest.java
new file mode 100644
index 0000000..fbb7fe6
--- /dev/null
+++ b/tests/src/cgeo/geocaching/connector/GeopeitusConnectorTest.java
@@ -0,0 +1,28 @@
+package cgeo.geocaching.connector;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import junit.framework.TestCase;
+
+import java.util.Set;
+
+public class GeopeitusConnectorTest extends TestCase {
+
+ private static IConnector getGeopeitusConnector() {
+ final IConnector geopeitusConnector = ConnectorFactory.getConnector("GE1234");
+ assertThat(geopeitusConnector).isNotNull();
+ return geopeitusConnector;
+ }
+
+ public static void testCanHandle() {
+ final IConnector wmConnector = getGeopeitusConnector();
+
+ assertThat(wmConnector.canHandle("GE1234")).isTrue();
+ assertThat(wmConnector.canHandle("GEAB12")).isFalse();
+ }
+
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(getGeopeitusConnector().handledGeocodes(geocodes)).containsOnly("GE1234", "GE5678");
+ }
+}
diff --git a/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java b/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java
index 513c5c2..61479f5 100644
--- a/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java
+++ b/tests/src/cgeo/geocaching/connector/WaymarkingConnectorTest.java
@@ -3,14 +3,21 @@ package cgeo.geocaching.connector;
import static org.assertj.core.api.Assertions.assertThat;
import junit.framework.TestCase;
+import java.util.Set;
+
public class WaymarkingConnectorTest extends TestCase {
+ private static IConnector getWaymarkingConnector() {
+ final IConnector wmConnector = ConnectorFactory.getConnector("WM1234");
+ assertThat(wmConnector).isNotNull();
+ return wmConnector;
+ }
+
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();
+ final IConnector wmConnector = getWaymarkingConnector();
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");
@@ -20,10 +27,14 @@ public class WaymarkingConnectorTest extends TestCase {
}
public static void testCanHandle() {
- final IConnector wmConnector = ConnectorFactory.getConnector("WM1234");
- assertThat(wmConnector).isNotNull();
+ final IConnector wmConnector = getWaymarkingConnector();
assertThat(wmConnector.canHandle("WM1234")).isTrue();
assertThat(wmConnector.canHandle("GC1234")).isFalse();
}
+
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(getWaymarkingConnector().handledGeocodes(geocodes)).containsOnly("WM1234", "WM5678");
+ }
}
diff --git a/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java
index f4073a9..f7090a2 100644
--- a/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java
+++ b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java
@@ -3,10 +3,12 @@ package cgeo.geocaching.connector.ec;
import static org.assertj.core.api.Assertions.assertThat;
import cgeo.geocaching.Geocache;
+import cgeo.geocaching.connector.ConnectorFactoryTest;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LogType;
import java.util.List;
+import java.util.Set;
import junit.framework.TestCase;
@@ -36,4 +38,8 @@ public class ECConnectorTest extends TestCase {
assertThat(ECConnector.getInstance().getGeocodeFromUrl("http://extremcaching.com/index.php/output-2/738")).isEqualTo("EC738");
}
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(ECConnector.getInstance().handledGeocodes(geocodes)).containsOnly("EC1234", "EC5678");
+ }
}
diff --git a/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java b/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java
index b9f959c..e8aaaf3 100644
--- a/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java
+++ b/tests/src/cgeo/geocaching/connector/gc/GCConnectorTest.java
@@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import cgeo.geocaching.SearchResult;
import cgeo.geocaching.connector.ConnectorFactory;
+import cgeo.geocaching.connector.ConnectorFactoryTest;
import cgeo.geocaching.connector.trackable.TravelBugConnector;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.location.Geopoint;
@@ -12,6 +13,8 @@ import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.settings.TestSettings;
import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase;
+import java.util.Set;
+
public class GCConnectorTest extends AbstractResourceInstrumentationTestCase {
public static void testGetViewport() {
@@ -102,4 +105,9 @@ public class GCConnectorTest extends AbstractResourceInstrumentationTestCase {
// uppercase is managed in ConnectorFactory
assertThat(GCConnector.getInstance().getGeocodeFromUrl("http://coord.info/gc77")).isEqualTo("gc77");
}
+
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(GCConnector.getInstance().handledGeocodes(geocodes)).containsOnly("GC1234", "GC5678");
+ }
}
diff --git a/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java b/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java
index 0a2b248..57f43c3 100644
--- a/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java
+++ b/tests/src/cgeo/geocaching/connector/oc/OCConnectorTest.java
@@ -3,10 +3,13 @@ package cgeo.geocaching.connector.oc;
import static org.assertj.core.api.Assertions.assertThat;
import cgeo.geocaching.connector.ConnectorFactory;
+import cgeo.geocaching.connector.ConnectorFactoryTest;
import cgeo.geocaching.connector.IConnector;
import junit.framework.TestCase;
+import java.util.Set;
+
public class OCConnectorTest extends TestCase {
/**
@@ -20,6 +23,7 @@ public class OCConnectorTest extends TestCase {
private static IConnector getOcDeConnector() {
final IConnector ocConnector = ConnectorFactory.getConnector("OCXXX");
+ assertThat(ocConnector).isNotNull();
assertThat(ocConnector.getHost()).contains(".de");
return ocConnector;
}
@@ -35,4 +39,8 @@ public class OCConnectorTest extends TestCase {
assertThat(connector.getGeocodeFromUrl("http://www.opencaching.us/viewcache.php?wp=OU07A0")).isEqualTo("OU07A0");
}
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(getOcDeConnector().handledGeocodes(geocodes)).containsOnly("OC1234", "OC5678");
+ }
}
diff --git a/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java b/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java
index 11f2d66..c957912 100644
--- a/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java
+++ b/tests/src/cgeo/geocaching/connector/ox/OXConnectorTest.java
@@ -1,13 +1,26 @@
package cgeo.geocaching.connector.ox;
import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.geocaching.connector.ConnectorFactory;
+import cgeo.geocaching.connector.ConnectorFactoryTest;
+import cgeo.geocaching.connector.IConnector;
+
import junit.framework.TestCase;
+import java.util.Set;
+
public class OXConnectorTest extends TestCase {
+ private static IConnector getOXConnector() {
+ final IConnector oxConnector = ConnectorFactory.getConnector("OXZZZZZ");
+ assertThat(oxConnector).isNotNull();
+ return oxConnector;
+ }
+
public static void testCanHandle() {
// http://www.opencaching.com/api_doc/concepts/oxcodes.html
- final OXConnector oxConnector = new OXConnector();
+ final IConnector oxConnector = getOXConnector();
assertThat(oxConnector.canHandle("OXZZZZZ")).isTrue();
assertThat(oxConnector.canHandle("OX1")).isTrue();
assertThat(oxConnector.canHandle("GCABCDE")).isFalse();
@@ -15,9 +28,13 @@ public class OXConnectorTest extends TestCase {
}
public static void testGetGeocodeFromUrl() {
- final OXConnector connector = new OXConnector();
+ final IConnector connector = getOXConnector();
assertThat(connector.getGeocodeFromUrl("http://www.opencaching.com/de/#!geocache/OX1R421")).isEqualTo("OX1R421");
assertThat(connector.getGeocodeFromUrl("http://www.opencaching.com/#!geocache/OX1R421")).isEqualTo("OX1R421");
}
+ public static void testHandledGeocodes() {
+ Set<String> geocodes = ConnectorFactoryTest.getGeocodeSample();
+ assertThat(getOXConnector().handledGeocodes(geocodes)).containsOnly("OX1234", "OX5678");
+ }
}