From 47fefdd273f7784e9c166eedb2419011395a7574 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 28 Apr 2016 14:45:56 +0200 Subject: [PATCH 1/9] Switch from separate sort_mode to more general randomValueOtherThan ... for sort tests only ... --- .../search/sort/FieldSortBuilderTests.java | 5 +++-- .../search/sort/RandomSortDataGenerator.java | 14 -------------- .../search/sort/ScriptSortBuilderTests.java | 7 ++++--- .../java/org/elasticsearch/test/ESTestCase.java | 11 +++++++++++ 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index 5ca9aef658c..f209ebefdef 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.test.ESTestCase; import java.io.IOException; @@ -51,7 +52,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); } if (randomBoolean()) { @@ -77,7 +78,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); break; case 3: mutated.unmappedType(RandomSortDataGenerator.randomAscii(mutated.unmappedType())); diff --git a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java index a31de756bc9..3a40e9b56c6 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java @@ -63,20 +63,6 @@ public class RandomSortDataGenerator { return nestedPath; } - public static SortMode mode(SortMode original) { - Set set = new HashSet<>(); - set.add(original); - return mode(set); - } - - public static SortMode mode(Set except) { - SortMode mode = ESTestCase.randomFrom(SortMode.values()); - while (except.contains(mode)) { - mode = ESTestCase.randomFrom(SortMode.values()); - } - return mode; - } - public static Object missing(Object original) { Object missing = null; Object otherMissing = original; diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index a1a24632f09..85ee079ffe4 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -29,6 +29,7 @@ import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService.ScriptType; import org.elasticsearch.search.sort.ScriptSortBuilder.ScriptSortType; +import org.elasticsearch.test.ESTestCase; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -52,13 +53,13 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); } else { Set exceptThis = new HashSet<>(); exceptThis.add(SortMode.SUM); exceptThis.add(SortMode.AVG); exceptThis.add(SortMode.MEDIAN); - builder.sortMode(RandomSortDataGenerator.mode(exceptThis)); + builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis, () -> randomFrom(SortMode.values()))); } } if (randomBoolean()) { @@ -101,7 +102,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); } else { // script sort type String only allows MIN and MAX, so we only switch if (original.sortMode() == SortMode.MIN) { 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 8bbc95fe2ad..d8a3221c88f 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -417,6 +417,17 @@ public abstract class ESTestCase extends LuceneTestCase { return randomValue; } + /** + * helper to get a random value in a certain range that's different from the input + */ + public static T randomValueOtherThanMany(Collection input, Supplier randomSupplier) { + T randomValue = null; + do { + randomValue = randomSupplier.get(); + } while (input.contains(randomValue)); + return randomValue; + } + /** * Runs the code block for 10 seconds waiting for no assertion to trip. */ From 8dc0610eaa86b0b2bf5db0e6387fe98ded94bc55 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 28 Apr 2016 15:07:55 +0200 Subject: [PATCH 2/9] Get rid of duplicated random ascii generation --- .../search/sort/FieldSortBuilderTests.java | 16 ++++++++++++---- .../search/sort/GeoDistanceSortBuilderTests.java | 10 ++++++++-- .../search/sort/RandomSortDataGenerator.java | 8 -------- .../search/sort/ScriptSortBuilderTests.java | 4 +++- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index f209ebefdef..c557c714d0f 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -48,7 +48,9 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); } if (randomBoolean()) { @@ -60,7 +62,9 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); } return builder; @@ -72,7 +76,9 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 1: mutated.setNestedFilter(RandomSortDataGenerator.nestedFilter(mutated.getNestedFilter())); @@ -81,7 +87,9 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); break; case 3: - mutated.unmappedType(RandomSortDataGenerator.randomAscii(mutated.unmappedType())); + mutated.unmappedType(ESTestCase.randomValueOtherThan( + mutated.unmappedType(), + () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 4: mutated.missing(RandomSortDataGenerator.missing(mutated.missing())); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 87fd183f1ce..a43f9c54bb3 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -35,6 +35,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.geo.RandomGeoGenerator; import java.io.IOException; @@ -91,7 +92,10 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); } if (randomBoolean()) { result.coerce(! result.coerce()); @@ -176,7 +180,9 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 8: result.coerce(! original.coerce()); diff --git a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java index 3a40e9b56c6..86a12f7ad84 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java @@ -55,14 +55,6 @@ public class RandomSortDataGenerator { return nested; } - public static String randomAscii(String original) { - String nestedPath = ESTestCase.randomAsciiOfLengthBetween(1, 10); - while (nestedPath.equals(original)) { - nestedPath = ESTestCase.randomAsciiOfLengthBetween(1, 10); - } - return nestedPath; - } - public static Object missing(Object original) { Object missing = null; Object otherMissing = original; diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index 85ee079ffe4..b14b82aa092 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -66,7 +66,9 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); } return builder; } From 0c7e58de93c33d4d64073abd583978cd82a8e451 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 28 Apr 2016 15:14:15 +0200 Subject: [PATCH 3/9] Get rid of duplicate random sort order generation --- .../search/sort/FieldSortBuilderTests.java | 4 ++-- .../search/sort/GeoDistanceSortBuilderTests.java | 4 ++-- .../search/sort/RandomSortDataGenerator.java | 15 --------------- .../search/sort/ScoreSortBuilderTests.java | 3 ++- .../search/sort/ScriptSortBuilderTests.java | 2 +- 5 files changed, 7 insertions(+), 21 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index c557c714d0f..ebfa3bbb76f 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -40,7 +40,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(SortOrder.values()))); break; default: throw new IllegalStateException("Unsupported mutation."); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index a43f9c54bb3..723e569bdb5 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -83,7 +83,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase randomFrom(SortOrder.values()))); break; case 5: result.sortMode(mode(original.sortMode())); diff --git a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java index 86a12f7ad84..bad5ff83f76 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java @@ -84,19 +84,4 @@ public class RandomSortDataGenerator { } return missing; } - - /** - * return a random {@link SortOrder} settings, except the one provided by parameter if set - */ - public static SortOrder order(@Nullable SortOrder original) { - if (original == null) { - return ESTestCase.randomBoolean() ? SortOrder.ASC : SortOrder.DESC; - } - if (original.equals(SortOrder.ASC)) { - return SortOrder.DESC; - } else { - return SortOrder.ASC; - } - } - } diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java index 142a88c4036..222240a9ac4 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; +import org.elasticsearch.test.ESTestCase; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -45,7 +46,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase randomFrom(SortOrder.values()))); return result; } diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index b14b82aa092..9637df30772 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -49,7 +49,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase Date: Thu, 28 Apr 2016 15:27:36 +0200 Subject: [PATCH 4/9] Get rid of no longer needed missing generation method --- .../search/sort/FieldSortBuilderTests.java | 15 +++++++-- .../search/sort/RandomSortDataGenerator.java | 33 ------------------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index ebfa3bbb76f..27f87681777 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -28,6 +28,8 @@ import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.test.ESTestCase; import java.io.IOException; +import java.util.Arrays; +import java.util.List; public class FieldSortBuilderTests extends AbstractSortTestCase { @@ -36,7 +38,14 @@ public class FieldSortBuilderTests extends AbstractSortTestCase missingContent = Arrays.asList( + "_last", + "_first", + ESTestCase.randomAsciiOfLength(10), ESTestCase.randomUnicodeOfCodepointLengthBetween(5, 15), + ESTestCase.randomInt()); + + + public FieldSortBuilder randomFieldSortBuilder() { String fieldName = rarely() ? FieldSortBuilder.DOC_FIELD_NAME : randomAsciiOfLengthBetween(1, 10); FieldSortBuilder builder = new FieldSortBuilder(fieldName); if (randomBoolean()) { @@ -44,7 +53,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(missingContent))); } if (randomBoolean()) { @@ -92,7 +101,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 4: - mutated.missing(RandomSortDataGenerator.missing(mutated.missing())); + mutated.missing(ESTestCase.randomValueOtherThan(mutated.missing(), () -> randomFrom(missingContent))); break; case 5: mutated.order(ESTestCase.randomValueOtherThan(mutated.order(), () -> randomFrom(SortOrder.values()))); diff --git a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java index bad5ff83f76..43ff2428d81 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/sort/RandomSortDataGenerator.java @@ -19,16 +19,12 @@ package org.elasticsearch.search.sort; -import org.elasticsearch.common.Nullable; import org.elasticsearch.index.query.IdsQueryBuilder; import org.elasticsearch.index.query.MatchAllQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.test.ESTestCase; -import java.util.HashSet; -import java.util.Set; - public class RandomSortDataGenerator { private RandomSortDataGenerator() { // this is a helper class only, doesn't need a constructor @@ -55,33 +51,4 @@ public class RandomSortDataGenerator { return nested; } - public static Object missing(Object original) { - Object missing = null; - Object otherMissing = original; - - while (missing == null || missing.equals(otherMissing)) { - int missingId = ESTestCase.randomIntBetween(0, 4); - switch (missingId) { - case 0: - missing = ("_last"); - break; - case 1: - missing = ("_first"); - break; - case 2: - missing = ESTestCase.randomAsciiOfLength(10); - break; - case 3: - missing = ESTestCase.randomUnicodeOfCodepointLengthBetween(5, 15); - break; - case 4: - missing = ESTestCase.randomInt(); - break; - default: - throw new IllegalStateException("Unknown missing type."); - - } - } - return missing; - } } From a03b131319dd815c826c211100db5efacf8a7cb4 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 28 Apr 2016 15:31:56 +0200 Subject: [PATCH 5/9] Shorten nested test query builder generation. --- .../search/sort/FieldSortBuilderTests.java | 4 +-- .../sort/GeoDistanceSortBuilderTests.java | 4 +-- ....java => NestedQueryBuilderGenerator.java} | 33 ++++++++----------- .../search/sort/ScriptSortBuilderTests.java | 4 +-- 4 files changed, 19 insertions(+), 26 deletions(-) rename core/src/test/java/org/elasticsearch/search/sort/{RandomSortDataGenerator.java => NestedQueryBuilderGenerator.java} (61%) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index 27f87681777..abcab363dcc 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -67,7 +67,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 1: - mutated.setNestedFilter(RandomSortDataGenerator.nestedFilter(mutated.getNestedFilter())); + mutated.setNestedFilter(NestedQueryBuilderGenerator.nestedFilter(mutated.getNestedFilter())); break; case 2: mutated.sortMode(ESTestCase.randomValueOtherThan(mutated.sortMode(), () -> randomFrom(SortMode.values()))); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 723e569bdb5..9f1b1a325ef 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -89,7 +89,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase> builders = Arrays.asList( + new MatchAllQueryBuilder(), + new IdsQueryBuilder(), + new TermQueryBuilder(ESTestCase.randomAsciiOfLengthBetween(1, 10), ESTestCase.randomDouble())); + + public static QueryBuilder nestedFilter(QueryBuilder original) { @SuppressWarnings("rawtypes") - QueryBuilder nested = null; - while (nested == null || nested.equals(original)) { - switch (ESTestCase.randomInt(2)) { - case 0: - nested = new MatchAllQueryBuilder(); - break; - case 1: - nested = new IdsQueryBuilder(); - break; - default: - case 2: - nested = new TermQueryBuilder(ESTestCase.randomAsciiOfLengthBetween(1, 10), ESTestCase.randomDouble()); - break; - } - nested.boost((float) ESTestCase.randomDoubleBetween(0, 10, false)); - } + QueryBuilder nested = ESTestCase.randomValueOtherThan(original, () -> ESTestCase.randomFrom(builders)); + nested.boost((float) ESTestCase.randomDoubleBetween(0, 10, false)); return nested; } - } diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index 9637df30772..ba9d608f610 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -63,7 +63,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase Date: Mon, 2 May 2016 13:50:00 +0200 Subject: [PATCH 6/9] Use plain random values for generating *SortBuilders --- .../search/sort/FieldSortBuilderTests.java | 28 +++++++++---------- .../sort/GeoDistanceSortBuilderTests.java | 6 ++-- .../sort/NestedQueryBuilderGenerator.java | 7 ++--- .../search/sort/ScriptSortBuilderTests.java | 11 ++++---- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index abcab363dcc..cab40588777 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -53,27 +53,23 @@ public class FieldSortBuilderTests extends AbstractSortTestCase randomFrom(missingContent))); + builder.missing(randomFrom(missingContent)); } if (randomBoolean()) { - builder.unmappedType(ESTestCase.randomValueOtherThan( - builder.unmappedType(), - () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); + builder.unmappedType(ESTestCase.randomAsciiOfLengthBetween(1, 10)); } if (randomBoolean()) { - builder.sortMode(ESTestCase.randomValueOtherThan(builder.sortMode(), () -> randomFrom(SortMode.values()))); + builder.sortMode(randomFrom(SortMode.values())); } if (randomBoolean()) { - builder.setNestedFilter(NestedQueryBuilderGenerator.nestedFilter(builder.getNestedFilter())); + builder.setNestedFilter(NestedQueryBuilderGenerator.randomNestedFilter()); } if (randomBoolean()) { - builder.setNestedPath(ESTestCase.randomValueOtherThan( - builder.getNestedPath(), - () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); + builder.setNestedPath(ESTestCase.randomAsciiOfLengthBetween(1, 10)); } return builder; @@ -86,25 +82,27 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 1: - mutated.setNestedFilter(NestedQueryBuilderGenerator.nestedFilter(mutated.getNestedFilter())); + mutated.setNestedFilter(ESTestCase.randomValueOtherThan( + original.getNestedFilter(), + () -> NestedQueryBuilderGenerator.randomNestedFilter())); break; case 2: - mutated.sortMode(ESTestCase.randomValueOtherThan(mutated.sortMode(), () -> randomFrom(SortMode.values()))); + mutated.sortMode(ESTestCase.randomValueOtherThan(original.sortMode(), () -> randomFrom(SortMode.values()))); break; case 3: mutated.unmappedType(ESTestCase.randomValueOtherThan( - mutated.unmappedType(), + original.unmappedType(), () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); break; case 4: - mutated.missing(ESTestCase.randomValueOtherThan(mutated.missing(), () -> randomFrom(missingContent))); + mutated.missing(ESTestCase.randomValueOtherThan(original.missing(), () -> randomFrom(missingContent))); break; case 5: - mutated.order(ESTestCase.randomValueOtherThan(mutated.order(), () -> randomFrom(SortOrder.values()))); + mutated.order(ESTestCase.randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values()))); break; default: throw new IllegalStateException("Unsupported mutation."); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 9f1b1a325ef..3da59f79de6 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -89,7 +89,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase NestedQueryBuilderGenerator.randomNestedFilter())); break; case 7: result.setNestedPath(ESTestCase.randomValueOtherThan( diff --git a/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java index 7e6f88cffd5..de3318036e8 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java +++ b/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java @@ -38,10 +38,7 @@ public class NestedQueryBuilderGenerator { new IdsQueryBuilder(), new TermQueryBuilder(ESTestCase.randomAsciiOfLengthBetween(1, 10), ESTestCase.randomDouble())); - public static QueryBuilder nestedFilter(QueryBuilder original) { - @SuppressWarnings("rawtypes") - QueryBuilder nested = ESTestCase.randomValueOtherThan(original, () -> ESTestCase.randomFrom(builders)); - nested.boost((float) ESTestCase.randomDoubleBetween(0, 10, false)); - return nested; + public static QueryBuilder randomNestedFilter() { + return ESTestCase.randomFrom(builders).boost(ESTestCase.randomFloat()); } } diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index ba9d608f610..1d7247ba1e4 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService.ScriptType; @@ -63,12 +64,10 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); + builder.setNestedPath(ESTestCase.randomAsciiOfLengthBetween(1, 10)); } return builder; } @@ -115,7 +114,9 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase NestedQueryBuilderGenerator.randomNestedFilter())); break; case 3: result.setNestedPath(original.getNestedPath() + "_some_suffix"); From 372eceb8542d6928db8193649716ca9a6a3ac501 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Mon, 2 May 2016 15:41:05 +0200 Subject: [PATCH 7/9] Switch to using predicate for testing existing value --- .../search/sort/FieldSortBuilderTests.java | 1 + .../search/sort/ScriptSortBuilderTests.java | 2 +- .../java/org/elasticsearch/test/ESTestCase.java | 15 ++++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index cab40588777..ebd5403489b 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -30,6 +30,7 @@ import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; import java.util.List; +import java.util.function.Supplier; public class FieldSortBuilderTests extends AbstractSortTestCase { diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index 1d7247ba1e4..b809adcb27b 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -60,7 +60,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); + builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis::contains, () -> randomFrom(SortMode.values()))); } } if (randomBoolean()) { 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 d8a3221c88f..01f0d5b8151 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -87,6 +87,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.function.Supplier; import static org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList; @@ -410,21 +411,21 @@ public abstract class ESTestCase extends LuceneTestCase { * helper to get a random value in a certain range that's different from the input */ public static T randomValueOtherThan(T input, Supplier randomSupplier) { - T randomValue = null; - do { - randomValue = randomSupplier.get(); - } while (randomValue.equals(input)); - return randomValue; + if (input != null) { + return randomValueOtherThanMany(input::equals, randomSupplier); + } + + return(randomSupplier.get()); } /** * helper to get a random value in a certain range that's different from the input */ - public static T randomValueOtherThanMany(Collection input, Supplier randomSupplier) { + public static T randomValueOtherThanMany(Predicate input, Supplier randomSupplier) { T randomValue = null; do { randomValue = randomSupplier.get(); - } while (input.contains(randomValue)); + } while (input.test(randomValue)); return randomValue; } From ad8bf53bbdfd1d90e52cfd3c4c2e04b7525d0f23 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Wed, 4 May 2016 11:25:23 +0200 Subject: [PATCH 8/9] Fold helper class into abstract sort test class. Folds the helper class for random object generation into the abstract sort test class. Removes a few references to ESTestCase that were not needed due to inheriting from it along the way. --- .../search/sort/AbstractSortTestCase.java | 16 +++++++ .../search/sort/FieldSortBuilderTests.java | 30 ++++++------- .../sort/GeoDistanceSortBuilderTests.java | 4 +- .../sort/NestedQueryBuilderGenerator.java | 44 ------------------- .../search/sort/ScriptSortBuilderTests.java | 5 +-- 5 files changed, 34 insertions(+), 65 deletions(-) delete mode 100644 core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index a8562493c7a..76d5eba12f9 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -45,8 +45,12 @@ import org.elasticsearch.index.mapper.Mapper.BuilderContext; import org.elasticsearch.index.mapper.core.LegacyDoubleFieldMapper.DoubleFieldType; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.mapper.object.ObjectMapper.Nested; +import org.elasticsearch.index.query.IdsQueryBuilder; +import org.elasticsearch.index.query.MatchAllQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache; import org.elasticsearch.indices.query.IndicesQueriesRegistry; @@ -252,6 +256,18 @@ public abstract class AbstractSortTestCase> extends EST return doubleFieldType; } + protected static QueryBuilder randomNestedFilter() { + int id = randomIntBetween(0, 2); + switch(id) { + case 0: return (new MatchAllQueryBuilder()).boost(randomFloat()); + case 1: return (new IdsQueryBuilder()).boost(randomFloat()); + case 2: return (new TermQueryBuilder( + randomAsciiOfLengthBetween(1, 10), + randomDouble()).boost(randomFloat())); + default: throw new IllegalStateException("Only three query builders supported for testing sort"); + } + } + @SuppressWarnings("unchecked") private T copyItem(T original) throws IOException { try (BytesStreamOutput output = new BytesStreamOutput()) { diff --git a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java index ebd5403489b..4b6eb82304a 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/FieldSortBuilderTests.java @@ -25,12 +25,10 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.test.ESTestCase; import java.io.IOException; import java.util.Arrays; import java.util.List; -import java.util.function.Supplier; public class FieldSortBuilderTests extends AbstractSortTestCase { @@ -42,8 +40,8 @@ public class FieldSortBuilderTests extends AbstractSortTestCase missingContent = Arrays.asList( "_last", "_first", - ESTestCase.randomAsciiOfLength(10), ESTestCase.randomUnicodeOfCodepointLengthBetween(5, 15), - ESTestCase.randomInt()); + randomAsciiOfLength(10), randomUnicodeOfCodepointLengthBetween(5, 15), + randomInt()); public FieldSortBuilder randomFieldSortBuilder() { @@ -58,7 +56,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); + () -> randomAsciiOfLengthBetween(1, 10))); break; case 1: - mutated.setNestedFilter(ESTestCase.randomValueOtherThan( + mutated.setNestedFilter(randomValueOtherThan( original.getNestedFilter(), - () -> NestedQueryBuilderGenerator.randomNestedFilter())); + () -> randomNestedFilter())); break; case 2: - mutated.sortMode(ESTestCase.randomValueOtherThan(original.sortMode(), () -> randomFrom(SortMode.values()))); + mutated.sortMode(randomValueOtherThan(original.sortMode(), () -> randomFrom(SortMode.values()))); break; case 3: - mutated.unmappedType(ESTestCase.randomValueOtherThan( + mutated.unmappedType(randomValueOtherThan( original.unmappedType(), - () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); + () -> randomAsciiOfLengthBetween(1, 10))); break; case 4: - mutated.missing(ESTestCase.randomValueOtherThan(original.missing(), () -> randomFrom(missingContent))); + mutated.missing(randomValueOtherThan(original.missing(), () -> randomFrom(missingContent))); break; case 5: - mutated.order(ESTestCase.randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values()))); + mutated.order(randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values()))); break; default: throw new IllegalStateException("Unsupported mutation."); diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 3da59f79de6..817427f9ecc 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -89,7 +89,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase NestedQueryBuilderGenerator.randomNestedFilter())); + () -> randomNestedFilter())); break; case 7: result.setNestedPath(ESTestCase.randomValueOtherThan( diff --git a/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java b/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java deleted file mode 100644 index de3318036e8..00000000000 --- a/core/src/test/java/org/elasticsearch/search/sort/NestedQueryBuilderGenerator.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.search.sort; - -import org.elasticsearch.index.query.IdsQueryBuilder; -import org.elasticsearch.index.query.MatchAllQueryBuilder; -import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.index.query.TermQueryBuilder; -import org.elasticsearch.test.ESTestCase; - -import java.util.Arrays; -import java.util.List; - -public class NestedQueryBuilderGenerator { - private NestedQueryBuilderGenerator() { - // this is a helper class only, doesn't need a constructor - } - - private static List> builders = Arrays.asList( - new MatchAllQueryBuilder(), - new IdsQueryBuilder(), - new TermQueryBuilder(ESTestCase.randomAsciiOfLengthBetween(1, 10), ESTestCase.randomDouble())); - - public static QueryBuilder randomNestedFilter() { - return ESTestCase.randomFrom(builders).boost(ESTestCase.randomFloat()); - } -} diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index b809adcb27b..38a20a129aa 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -25,7 +25,6 @@ import org.elasticsearch.common.ParseFieldMatcher; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService.ScriptType; @@ -64,7 +63,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase NestedQueryBuilderGenerator.randomNestedFilter())); + () -> randomNestedFilter())); break; case 3: result.setNestedPath(original.getNestedPath() + "_some_suffix"); From 6f15f35819f26763a5b3f427af6b6a68fff3cebe Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Wed, 4 May 2016 12:14:39 +0200 Subject: [PATCH 9/9] Remove left over references to ESTestCase --- .../search/sort/GeoDistanceSortBuilderTests.java | 13 ++++++------- .../search/sort/ScoreSortBuilderTests.java | 3 +-- .../search/sort/ScriptSortBuilderTests.java | 11 +++++------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java index 817427f9ecc..a41138e2093 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/GeoDistanceSortBuilderTests.java @@ -35,7 +35,6 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.indices.query.IndicesQueriesRegistry; -import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.geo.RandomGeoGenerator; import java.io.IOException; @@ -93,9 +92,9 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase ESTestCase.randomAsciiOfLengthBetween(1, 10))); + () -> randomAsciiOfLengthBetween(1, 10))); } if (randomBoolean()) { result.coerce(! result.coerce()); @@ -171,20 +170,20 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase randomFrom(SortOrder.values()))); + result.order(randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values()))); break; case 5: result.sortMode(mode(original.sortMode())); break; case 6: - result.setNestedFilter(ESTestCase.randomValueOtherThan( + result.setNestedFilter(randomValueOtherThan( original.getNestedFilter(), () -> randomNestedFilter())); break; case 7: - result.setNestedPath(ESTestCase.randomValueOtherThan( + result.setNestedPath(randomValueOtherThan( result.getNestedPath(), - () -> ESTestCase.randomAsciiOfLengthBetween(1, 10))); + () -> randomAsciiOfLengthBetween(1, 10))); break; case 8: result.coerce(! original.coerce()); diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java index 222240a9ac4..605573e9b71 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScoreSortBuilderTests.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryParseContext; -import org.elasticsearch.test.ESTestCase; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -46,7 +45,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase randomFrom(SortOrder.values()))); + result.order(randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values()))); return result; } diff --git a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index 38a20a129aa..6491c36bf6d 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -29,7 +29,6 @@ import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptService.ScriptType; import org.elasticsearch.search.sort.ScriptSortBuilder.ScriptSortType; -import org.elasticsearch.test.ESTestCase; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -53,20 +52,20 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); + builder.sortMode(randomValueOtherThan(builder.sortMode(), () -> randomFrom(SortMode.values()))); } else { Set exceptThis = new HashSet<>(); exceptThis.add(SortMode.SUM); exceptThis.add(SortMode.AVG); exceptThis.add(SortMode.MEDIAN); - builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis::contains, () -> randomFrom(SortMode.values()))); + builder.sortMode(randomValueOtherThanMany(exceptThis::contains, () -> randomFrom(SortMode.values()))); } } if (randomBoolean()) { builder.setNestedFilter(randomNestedFilter()); } if (randomBoolean()) { - builder.setNestedPath(ESTestCase.randomAsciiOfLengthBetween(1, 10)); + builder.setNestedPath(randomAsciiOfLengthBetween(1, 10)); } return builder; } @@ -102,7 +101,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomFrom(SortMode.values()))); + result.sortMode(randomValueOtherThan(result.sortMode(), () -> randomFrom(SortMode.values()))); } else { // script sort type String only allows MIN and MAX, so we only switch if (original.sortMode() == SortMode.MIN) { @@ -113,7 +112,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase randomNestedFilter())); break;