diff --git a/core/src/test/java/org/elasticsearch/common/geo/GeoHashTests.java b/core/src/test/java/org/elasticsearch/common/geo/GeoHashTests.java index 413062e4a2d..d2ae8401c55 100644 --- a/core/src/test/java/org/elasticsearch/common/geo/GeoHashTests.java +++ b/core/src/test/java/org/elasticsearch/common/geo/GeoHashTests.java @@ -18,6 +18,7 @@ */ package org.elasticsearch.common.geo; +import org.apache.lucene.geo.Rectangle; import org.elasticsearch.test.ESTestCase; /** @@ -57,4 +58,16 @@ public class GeoHashTests extends ESTestCase { } } } + + public void testBboxFromHash() { + String hash = randomGeohash(1, 12); + int level = hash.length(); + Rectangle bbox = GeoHashUtils.bbox(hash); + // check that the length is as expected + double expectedLonDiff = 360.0 / (Math.pow(8.0, (level + 1) / 2) * Math.pow(4.0, level / 2)); + double expectedLatDiff = 180.0 / (Math.pow(4.0, (level + 1) / 2) * Math.pow(8.0, level / 2)); + assertEquals(expectedLonDiff, bbox.maxLon - bbox.minLon, 0.00001); + assertEquals(expectedLatDiff, bbox.maxLat - bbox.minLat, 0.00001); + assertEquals(hash, GeoHashUtils.stringEncode(bbox.minLon, bbox.minLat, level)); + } } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index 0d9ed196838..03ee7d1f540 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -19,7 +19,6 @@ package org.elasticsearch.test; -import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.io.JsonStringEncoder; @@ -128,7 +127,6 @@ import static org.hamcrest.Matchers.not; public abstract class AbstractQueryTestCase> extends ESTestCase { - private static final GeohashGenerator geohashGenerator = new GeohashGenerator(); public static final String STRING_FIELD_NAME = "mapped_string"; protected static final String STRING_FIELD_NAME_2 = "mapped_string_2"; protected static final String INT_FIELD_NAME = "mapped_int"; @@ -685,18 +683,6 @@ public abstract class AbstractQueryTestCase> return (serviceHolder.currentTypes.length == 0) ? MetaData.ALL : randomFrom(serviceHolder.currentTypes); } - public static String randomGeohash(int minPrecision, int maxPrecision) { - return geohashGenerator.ofStringLength(random(), minPrecision, maxPrecision); - } - - public static class GeohashGenerator extends CodepointSetGenerator { - private static final char[] ASCII_SET = "0123456789bcdefghjkmnpqrstuvwxyz".toCharArray(); - - public GeohashGenerator() { - super(ASCII_SET); - } - } - protected static Fuzziness randomFuzziness(String fieldName) { switch (fieldName) { case INT_FIELD_NAME: diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 6d63b6a5428..fc34106a8da 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -24,6 +24,7 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope; import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite; +import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator; import com.carrotsearch.randomizedtesting.generators.RandomInts; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; @@ -663,6 +664,20 @@ public abstract class ESTestCase extends LuceneTestCase { return things; } + public static String randomGeohash(int minPrecision, int maxPrecision) { + return geohashGenerator.ofStringLength(random(), minPrecision, maxPrecision); + } + + private static final GeohashGenerator geohashGenerator = new GeohashGenerator(); + + public static class GeohashGenerator extends CodepointSetGenerator { + private final static char[] ASCII_SET = "0123456789bcdefghjkmnpqrstuvwxyz".toCharArray(); + + public GeohashGenerator() { + super(ASCII_SET); + } + } + /** * Randomly shuffles the fields inside objects in the {@link XContentBuilder} passed in. * Recursively goes through inner objects and also shuffles them. Exceptions for this