From 47fefdd273f7784e9c166eedb2419011395a7574 Mon Sep 17 00:00:00 2001 From: Isabel Drost-Fromm Date: Thu, 28 Apr 2016 14:45:56 +0200 Subject: [PATCH] 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. */