diff --git a/core/pom.xml b/core/pom.xml index e367d333afd..7c70ebea052 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -293,6 +293,8 @@ org/elasticsearch/common/util/MockBigArrays$*.class org/elasticsearch/node/NodeMocksPlugin.class org/elasticsearch/node/MockNode.class + org/elasticsearch/node/MockNode.class + org/elasticsearch/common/io/PathUtilsForTesting.class diff --git a/core/src/main/java/org/elasticsearch/common/io/PathUtils.java b/core/src/main/java/org/elasticsearch/common/io/PathUtils.java index ada11bfd0fa..0002c1e5b5f 100644 --- a/core/src/main/java/org/elasticsearch/common/io/PathUtils.java +++ b/core/src/main/java/org/elasticsearch/common/io/PathUtils.java @@ -43,8 +43,8 @@ public final class PathUtils { /** the actual JDK default */ static final FileSystem ACTUAL_DEFAULT = FileSystems.getDefault(); - /** can be changed by tests (via reflection) */ - private static volatile FileSystem DEFAULT = ACTUAL_DEFAULT; + /** can be changed by tests */ + static volatile FileSystem DEFAULT = ACTUAL_DEFAULT; /** * Returns a {@code Path} from name components. diff --git a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java index 9059865bad7..b0949159b27 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java @@ -146,6 +146,13 @@ public class MultiMatchQueryBuilder extends QueryBuilder implements BoostableQue return type; } } + + /** + * Returns the type (for testing) + */ + public MultiMatchQueryBuilder.Type getType() { + return type; + } /** * Constructs a new text query. diff --git a/core/src/test/java/org/elasticsearch/common/io/PathUtilsForTesting.java b/core/src/test/java/org/elasticsearch/common/io/PathUtilsForTesting.java new file mode 100644 index 00000000000..25c092ca6ee --- /dev/null +++ b/core/src/test/java/org/elasticsearch/common/io/PathUtilsForTesting.java @@ -0,0 +1,41 @@ +/* + * 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.common.io; + +import org.apache.lucene.util.LuceneTestCase; + +import java.nio.file.FileSystem; + +/** + * Exposes some package private stuff in PathUtils for framework purposes only! + */ +public class PathUtilsForTesting { + + /** Sets a new default filesystem for testing */ + public static void setup() { + FileSystem mock = LuceneTestCase.getBaseTempDirForTestClass().getFileSystem(); + PathUtils.DEFAULT = mock; + } + + /** Resets filesystem back to the real system default */ + public static void teardown() { + PathUtils.DEFAULT = PathUtils.ACTUAL_DEFAULT; + } +} diff --git a/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java index 418083c1dd3..208922b885b 100644 --- a/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/analysis/PreBuiltAnalyzerIntegrationIT.java @@ -20,6 +20,7 @@ package org.elasticsearch.indices.analysis; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.TokenStream; import org.elasticsearch.Version; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -28,7 +29,7 @@ import org.elasticsearch.test.ESBackcompatTestCase; import org.elasticsearch.test.ESIntegTestCase; import org.junit.Test; -import java.lang.reflect.Field; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -161,44 +162,18 @@ public class PreBuiltAnalyzerIntegrationIT extends ESIntegTestCase { } } - // the close() method of a lucene analyzer sets the storedValue field to null - // we simply check this via reflection - ugly but works - private void assertLuceneAnalyzersAreNotClosed(Map> loadedAnalyzers) throws IllegalAccessException, NoSuchFieldException { + // ensure analyzers are still open by checking there is no ACE + private void assertLuceneAnalyzersAreNotClosed(Map> loadedAnalyzers) throws IOException { for (Map.Entry> preBuiltAnalyzerEntry : loadedAnalyzers.entrySet()) { - PreBuiltAnalyzers preBuiltAnalyzer = preBuiltAnalyzerEntry.getKey(); for (Version version : preBuiltAnalyzerEntry.getValue()) { Analyzer analyzer = preBuiltAnalyzerEntry.getKey().getCache().get(version); - - Field field = getFieldFromClass("storedValue", analyzer); - boolean currentAccessible = field.isAccessible(); - field.setAccessible(true); - Object storedValue = field.get(analyzer); - field.setAccessible(currentAccessible); - - assertThat(String.format(Locale.ROOT, "Analyzer %s in version %s seems to be closed", preBuiltAnalyzer.name(), version), storedValue, is(notNullValue())); + try (TokenStream stream = analyzer.tokenStream("foo", "bar")) { + stream.reset(); + while (stream.incrementToken()) { + } + stream.end(); + } } } } - - /** - * Searches for a field until it finds, loops through all superclasses - */ - private Field getFieldFromClass(String fieldName, Object obj) { - Field field = null; - boolean storedValueFieldFound = false; - Class clazz = obj.getClass(); - while (!storedValueFieldFound) { - try { - field = clazz.getDeclaredField(fieldName); - storedValueFieldFound = true; - } catch (NoSuchFieldException e) { - clazz = clazz.getSuperclass(); - } - - if (Object.class.equals(clazz)) throw new RuntimeException("Could not find storedValue field in class" + clazz); - } - - return field; - } - } diff --git a/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java b/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java index ca138d9de29..24eb8cb7071 100644 --- a/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java @@ -629,8 +629,6 @@ public class MultiMatchQueryIT extends ESIntegTestCase { } private MultiMatchQueryBuilder.Type getType(MultiMatchQueryBuilder builder) throws NoSuchFieldException, IllegalAccessException { - Field field = MultiMatchQueryBuilder.class.getDeclaredField("type"); - field.setAccessible(true); - return (MultiMatchQueryBuilder.Type) field.get(builder); + return builder.getType(); } } diff --git a/core/src/test/java/org/elasticsearch/test/ESTestCase.java b/core/src/test/java/org/elasticsearch/test/ESTestCase.java index 2916cbb0d48..47aef7f394d 100644 --- a/core/src/test/java/org/elasticsearch/test/ESTestCase.java +++ b/core/src/test/java/org/elasticsearch/test/ESTestCase.java @@ -29,6 +29,7 @@ import com.carrotsearch.randomizedtesting.generators.RandomInts; import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomStrings; import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter; + import org.apache.lucene.uninverting.UninvertingReader; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; @@ -41,6 +42,7 @@ import org.elasticsearch.client.Requests; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.routing.DjbHashFunction; import org.elasticsearch.common.io.PathUtils; +import org.elasticsearch.common.io.PathUtilsForTesting; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; @@ -62,9 +64,7 @@ import org.junit.Rule; import org.junit.rules.RuleChain; import java.io.IOException; -import java.lang.reflect.Field; import java.nio.file.DirectoryStream; -import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -137,20 +137,12 @@ public abstract class ESTestCase extends LuceneTestCase { @BeforeClass public static void setFileSystem() throws Exception { - Field field = PathUtils.class.getDeclaredField("DEFAULT"); - field.setAccessible(true); - FileSystem mock = LuceneTestCase.getBaseTempDirForTestClass().getFileSystem(); - field.set(null, mock); - assertEquals(mock, PathUtils.getDefaultFileSystem()); + PathUtilsForTesting.setup(); } @AfterClass public static void restoreFileSystem() throws Exception { - Field field1 = PathUtils.class.getDeclaredField("ACTUAL_DEFAULT"); - field1.setAccessible(true); - Field field2 = PathUtils.class.getDeclaredField("DEFAULT"); - field2.setAccessible(true); - field2.set(null, field1.get(null)); + PathUtilsForTesting.teardown(); } // setup a default exception handler which knows when and how to print a stacktrace diff --git a/core/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java b/core/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java index 6a4c883519f..5772543dc6b 100644 --- a/core/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java +++ b/core/src/test/java/org/elasticsearch/test/hamcrest/ElasticsearchAssertions.java @@ -688,7 +688,7 @@ public class ElasticsearchAssertions { ElasticsearchAssertions.assertVersionSerializable(version, new ThrowableWrapper(t)); } - private static final class ThrowableWrapper implements Streamable { + public static final class ThrowableWrapper implements Streamable { Throwable throwable; public ThrowableWrapper(Throwable t) { throwable = t; @@ -716,7 +716,6 @@ public class ElasticsearchAssertions { Class clazz = streamable.getClass(); Constructor constructor = clazz.getDeclaredConstructor(); assertThat(constructor, Matchers.notNullValue()); - constructor.setAccessible(true); Streamable newInstance = constructor.newInstance(); return newInstance; } catch (Throwable e) { diff --git a/core/src/test/java/org/elasticsearch/test/store/MockFSDirectoryService.java b/core/src/test/java/org/elasticsearch/test/store/MockFSDirectoryService.java index 9af67d473ac..7d90d84304e 100644 --- a/core/src/test/java/org/elasticsearch/test/store/MockFSDirectoryService.java +++ b/core/src/test/java/org/elasticsearch/test/store/MockFSDirectoryService.java @@ -217,52 +217,10 @@ public class MockFSDirectoryService extends FsDirectoryService { public static final class ElasticsearchMockDirectoryWrapper extends MockDirectoryWrapper { private final boolean crash; - private final Set superUnSyncedFiles; - private final Random superRandomState; public ElasticsearchMockDirectoryWrapper(Random random, Directory delegate, boolean crash) { super(random, delegate); this.crash = crash; - - // TODO: remove all this and cutover to MockFS (DisableFsyncFS) instead - try { - Field field = MockDirectoryWrapper.class.getDeclaredField("unSyncedFiles"); - field.setAccessible(true); - superUnSyncedFiles = (Set) field.get(this); - - field = MockDirectoryWrapper.class.getDeclaredField("randomState"); - field.setAccessible(true); - superRandomState = (Random) field.get(this); - } catch (ReflectiveOperationException roe) { - throw new RuntimeException(roe); - } - } - - /** - * Returns true if {@link #in} must sync its files. - * Currently, only {@link org.apache.lucene.store.NRTCachingDirectory} requires sync'ing its files - * because otherwise they are cached in an internal {@link org.apache.lucene.store.RAMDirectory}. If - * other directories require that too, they should be added to this method. - */ - private boolean mustSync() { - Directory delegate = in; - while (delegate instanceof FilterDirectory) { - if (delegate instanceof NRTCachingDirectory) { - return true; - } - delegate = ((FilterDirectory) delegate).getDelegate(); - } - return delegate instanceof NRTCachingDirectory; - } - - @Override - public synchronized void sync(Collection names) throws IOException { - // don't wear out our hardware so much in tests. - if (superRandomState.nextInt(100) == 0 || mustSync()) { - super.sync(names); - } else { - superUnSyncedFiles.removeAll(names); - } } @Override