Răsfoiți Sursa

Switch to using predicate for testing existing value

Isabel Drost-Fromm 9 ani în urmă
părinte
comite
372eceb854

+ 1 - 0
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<FieldSortBuilder> {
 

+ 1 - 1
core/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java

@@ -60,7 +60,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
                 exceptThis.add(SortMode.SUM);
                 exceptThis.add(SortMode.AVG);
                 exceptThis.add(SortMode.MEDIAN);
-                builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis, () -> randomFrom(SortMode.values())));
+                builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis::contains, () -> randomFrom(SortMode.values())));
             }
         }
         if (randomBoolean()) {

+ 8 - 7
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> T randomValueOtherThan(T input, Supplier<T> 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> T randomValueOtherThanMany(Collection<T> input, Supplier<T> randomSupplier) {
+    public static <T> T randomValueOtherThanMany(Predicate<T> input, Supplier<T> randomSupplier) {
         T randomValue = null;
         do {
             randomValue = randomSupplier.get();
-        } while (input.contains(randomValue));
+        } while (input.test(randomValue));
         return randomValue;
     }