Apache Lucene open-source search software
Cleanups and refactorings after Mark's suggestions.
Dawid Weissee084bbe609a
diff --git a/lucene/common-build.xml b/lucene/common-build.xmlindex 8cbf655a27d..889bfc1de7b 100644--- a/lucene/common-build.xml+++ b/lucene/common-build.xml@@ -990,6 +990,9 @@<propertyref prefix="tests.jettyConnector" /><propertyref prefix="tests.disableHdfs" /><propertyref prefix="tests.filter" />+ <propertyref prefix="tests.leavetmpdir" />+ <propertyref prefix="tests.leaveTemporary" />+ <propertyref prefix="solr.test.leavetmpdir" /></syspropertyset><!-- Pass randomized settings to the forked JVM. -->diff --git a/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java b/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.javaindex b84695c3b7b..132fa56f519 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java@@ -179,6 +179,6 @@ public class TestAtomicUpdate extends LuceneTestCase {directory = newFSDirectory(dirPath);runTest(directory);directory.close();- TestUtil.rmDir(dirPath);+ TestUtil.rm(dirPath);}}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.javaindex a302506a990..84771b1f227 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java@@ -446,7 +446,7 @@ public void testFilesOpenClose() throws IOException {dir.close();// Try to erase the data - this ensures that the writer closed all files- TestUtil.rmDir(dirFile);+ TestUtil.rm(dirFile);dir = newFSDirectory(dirFile);// Now create the data set again, just as before@@ -463,7 +463,7 @@ public void testFilesOpenClose() throws IOException {// The following will fail if reader did not close// all files- TestUtil.rmDir(dirFile);+ TestUtil.rm(dirFile);}public void testOpenReaderAfterDelete() throws IOException {@@ -716,7 +716,7 @@ public void testFilesOpenClose() throws IOException {// good exceptionpublic void testNoDir() throws Throwable {File tempDir = TestUtil.createTempDir("doesnotexist");- TestUtil.rmDir(tempDir);+ TestUtil.rm(tempDir);Directory dir = newFSDirectory(tempDir);try {DirectoryReader.open(dir);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.javaindex 8ab81adb679..248d1cbff98 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java@@ -223,7 +223,7 @@ public class TestFieldsReader extends LuceneTestCase {reader.close();dir.close();} finally {- TestUtil.rmDir(indexDir);+ TestUtil.rm(indexDir);}}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.javaindex f04ca6c7b27..7c71924f1bf 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java@@ -108,6 +108,6 @@ public class TestNeverDelete extends LuceneTestCase {w.close();d.close();- TestUtil.rmDir(tmpDir);+ TestUtil.rm(tmpDir);}}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.javaindex 47731be22e6..8aee732bbb8 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java@@ -274,7 +274,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {writer.close();reader.close();} finally {- TestUtil.rmDir(indexDir);+ TestUtil.rm(indexDir);}}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.javaindex 9f13e3a2928..0bf2b0fd0bc 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java@@ -218,7 +218,7 @@ public class TestDirectory extends LuceneTestCase {assertFalse(dir.isOpen);}- TestUtil.rmDir(path);+ TestUtil.rm(path);}// LUCENE-1464@@ -230,7 +230,7 @@ public class TestDirectory extends LuceneTestCase {assertTrue(!path.exists());dir.close();} finally {- TestUtil.rmDir(path);+ TestUtil.rm(path);}}@@ -265,7 +265,7 @@ public class TestDirectory extends LuceneTestCase {Directory fsDir = new SimpleFSDirectory(path, null);assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length);} finally {- TestUtil.rmDir(path);+ TestUtil.rm(path);}}@@ -285,7 +285,7 @@ public class TestDirectory extends LuceneTestCase {}} finally {fsDir.close();- TestUtil.rmDir(path);+ TestUtil.rm(path);}}}diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.javaindex 67914d26ea5..e81eeb3b826 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java@@ -104,8 +104,8 @@ public class TestFileSwitchDirectory extends LuceneTestCase {public void testNoDir() throws Throwable {File primDir = TestUtil.createTempDir("foo");File secondDir = TestUtil.createTempDir("bar");- TestUtil.rmDir(primDir);- TestUtil.rmDir(secondDir);+ TestUtil.rm(primDir);+ TestUtil.rm(secondDir);Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet());try {DirectoryReader.open(dir);diff --git a/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java b/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.javaindex d633a425142..0061baf1024 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java@@ -171,7 +171,7 @@ public class TestLockFactory extends LuceneTestCase {dir.close();// Cleanup- TestUtil.rmDir(indexDir);+ TestUtil.rm(indexDir);}// Verify: NativeFSLockFactory works correctly@@ -250,8 +250,8 @@ public class TestLockFactory extends LuceneTestCase {dir1.close();dir2.close();- TestUtil.rmDir(fdir1);- TestUtil.rmDir(fdir2);+ TestUtil.rm(fdir1);+ TestUtil.rm(fdir2);}// Verify: default LockFactory has no prefix (ie@@ -273,7 +273,7 @@ public class TestLockFactory extends LuceneTestCase {assertNull("Default lock prefix should be null", dir.getLockFactory().getLockPrefix());dir.close();- TestUtil.rmDir(dirName);+ TestUtil.rm(dirName);}private class WriterThread extends Thread {diff --git a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.javaindex de796348f2f..4bacbb3263e 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestNRTCachingDirectory.java@@ -124,7 +124,7 @@ public class TestNRTCachingDirectory extends LuceneTestCase {// LUCENE-3382 -- make sure we get exception if the directory really does not exist.public void testNoDir() throws Throwable {File tempDir = TestUtil.createTempDir("doesnotexist");- TestUtil.rmDir(tempDir);+ TestUtil.rm(tempDir);Directory dir = new NRTCachingDirectory(newFSDirectory(tempDir), 2.0, 25.0);try {DirectoryReader.open(dir);diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.javaindex 13221d4c6b4..ce489d59798 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java@@ -645,7 +645,7 @@ public abstract class ThreadedIndexingAndSearchingTestCase extends LuceneTestCasTestUtil.checkIndex(dir);dir.close();- TestUtil.rmDir(tempDir);+ TestUtil.rm(tempDir);if (VERBOSE) {System.out.println("TEST: done [" + (System.currentTimeMillis()-t0) + " ms]");diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex 05d51c23224..ee5e3845821 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -228,6 +228,23 @@ public abstract class LuceneTestCase extends Assert {String[] value();}+ /**+ * Marks any suites which are known not to close all the temporary+ * files. This may prevent temp. files and folders from being cleaned+ * up after the suite is completed.+ *+ * @see TestUtil#createTempDir()+ * @see TestUtil#createTempFile(String, String)+ */+ @Documented+ @Inherited+ @Retention(RetentionPolicy.RUNTIME)+ @Target(ElementType.TYPE)+ public @interface SuppressTempFileChecks {+ /** Point to JIRA entry. */+ public String bugUrl() default "None";+ }+// -----------------------------------------------------------------// Truly immutable fields and constants, initialized once and valid// for all suites ever since.@@ -291,6 +308,20 @@ public abstract class LuceneTestCase extends Assert {/** Throttling, see {@link MockDirectoryWrapper#setThrottling(Throttling)}. */public static final Throttling TEST_THROTTLING = TEST_NIGHTLY ? Throttling.SOMETIMES : Throttling.NEVER;+ /** Leave temporary files on disk, even on successful runs. */+ public static final boolean LEAVE_TEMPORARY;+ static {+ boolean defaultValue = false;+ for (String property : Arrays.asList(+ "tests.leaveTemporary" /* ANT tasks's (junit4) flag. */,+ "tests.leavetemporary" /* lowercase */,+ "tests.leavetmpdir" /* default */,+ "solr.test.leavetmpdir" /* Solr's legacy */)) {+ defaultValue |= systemPropertyAsBoolean(property, false);+ }+ LEAVE_TEMPORARY = defaultValue;+ }+/*** These property keys will be ignored in verification of altered properties.* @see SystemPropertiesInvariantRule@@ -1147,7 +1178,7 @@ public abstract class LuceneTestCase extends Assert {final Class<? extends Directory> clazz = CommandLineUtil.loadDirectoryClass(clazzName);// If it is a FSDirectory type, try its ctor(File)if (FSDirectory.class.isAssignableFrom(clazz)) {- final File dir = TestUtil.createTempDir("index");+ final File dir = new File(TestUtil.createTempDir(), "index");dir.mkdirs(); // ensure it's created so we 'have' it.return newFSDirectoryImpl(clazz.asSubclass(FSDirectory.class), dir);}diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java b/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.javasimilarity index 74%rename from lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.javarename to lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.javaindex 5bcc38ded0f..d30c379ef30 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.java@@ -24,12 +24,12 @@ import java.io.IOException;/*** A {@link Closeable} that attempts to remove a given file/folder.*/-final class CloseableFile implements Closeable {+final class RemoveUponClose implements Closeable {private final File file;private final TestRuleMarkFailure failureMarker;private final String creationStack;- public CloseableFile(File file, TestRuleMarkFailure failureMarker) {+ public RemoveUponClose(File file, TestRuleMarkFailure failureMarker) {this.file = file;this.failureMarker = failureMarker;@@ -45,20 +45,12 @@ final class CloseableFile implements Closeable {// only if there were no other test failures.if (failureMarker.wasSuccessful()) {if (file.exists()) {- if (file.listFiles().length > 0) {- // throw new IOException("Temporary folder not clean: " + file.getAbsolutePath() + ". Created at stack trace:\n" + creationStack);- }-try {- TestUtil.rmDir(file);+ TestUtil.rm(file);} catch (IOException e) {- // Ignore the exception from rmDir.- }-- // Re-check.- if (file.exists()) {throw new IOException(- "Could not remove temporary folder: " + file.getAbsolutePath() + ". Created at stack trace:\n" + creationStack);+ "Could not remove temporary location '"+ + file.getAbsolutePath() + "', created at stack trace:\n" + creationStack, e);}}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.javaindex 6ef10cf99ac..9f08e7981b1 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java@@ -28,6 +28,7 @@ import java.io.PrintStream;import java.math.BigDecimal;import java.math.BigInteger;import java.nio.CharBuffer;+import java.util.ArrayList;import java.util.Arrays;import java.util.Enumeration;import java.util.HashMap;@@ -36,6 +37,7 @@ import java.util.Map;import java.util.Random;import java.util.concurrent.ExecutorService;import java.util.concurrent.TimeUnit;+import java.util.concurrent.atomic.AtomicInteger;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;import java.util.zip.ZipEntry;@@ -85,6 +87,7 @@ import org.apache.lucene.search.FilteredQuery.FilterStrategy;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.junit.Assert;import com.carrotsearch.randomizedtesting.RandomizedContext;@@ -101,26 +104,36 @@ public final class TestUtil {// the max number of retries we're going to do in getTempDirprivate static final int GET_TEMP_DIR_RETRY_THRESHOLD = 1000;-+/*** Deletes a file or a directory (and everything underneath it).*/public static void rm(File location) throws IOException {- if (!location.exists()) {- return;+ ArrayList<File> unremoved = rm(location, new ArrayList<File>());+ if (!unremoved.isEmpty()) {+ StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n");+ for (File f : unremoved) {+ b.append(" ")+ .append(f.getAbsolutePath())+ .append("\n");+ }+ throw new IOException(b.toString());}+ }- if (location.isDirectory()) {- for (File f : location.listFiles()) {- rm(f);+ private static ArrayList<File> rm(File location, ArrayList<File> unremoved) {+ if (location.exists()) {+ if (location.isDirectory()) {+ for (File f : location.listFiles()) {+ rm(f, unremoved);+ }}- } else {+if (!location.delete()) {- throw new IOException("Could not delete: " + location.getAbsolutePath());+ unremoved.add(location);}}-- assert !location.exists();+ return unremoved;}/**@@ -130,7 +143,7 @@ public final class TestUtil {public static void unzip(File zipName, File destDir) throws IOException {rm(destDir);destDir.mkdir();- LuceneTestCase.closeAfterSuite(new CloseableFile(destDir, LuceneTestCase.suiteFailureMarker));+ maybeRemoveAfterSuite(destDir);ZipFile zipFile = new ZipFile(zipName);Enumeration<? extends ZipEntry> entries = zipFile.entries();@@ -779,104 +792,128 @@ public final class TestUtil {}/**- * Returns a new, empty temporary folder, based on the given name. The folder will be- * deleted at the end of the suite. Failure to delete the temporary folder will cause- * an exception (typically on Windows).+ * Returns a new, empty temporary folder, based on the current test class's name.+ *+ * @see #createTempDir(String, File)*/- public static File createTempDir(String name) {- return createTempDir(name, new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir"))));+ public static File createTempDir() {+ Class<?> clazz = RandomizedContext.current().getTargetClass();+ String prefix = clazz.getName();+ prefix = prefix.replaceFirst("^org.apache.lucene.", "lucene-");+ prefix = prefix.replaceFirst("^org.apache.solr.", "solr-");+ return createTempDir(prefix);}-+/**- * Returns a new, empty temporary folder, based on the given name. The folder will be- * deleted at the end of the suite. Failure to delete the temporary folder will cause- * an exception (typically on Windows).+ * @see #createTempDir(String, File)*/- public static File createTempDir(String name, File tmpDir) {- return createTempDir(name, tmpDir, true);+ public static File createTempDir(String prefix) {+ return createTempDir(prefix, null);}-+/**- * Returns a new, empty temporary folder, based on the given name. The folder will be- * deleted at the end of the suite. Failure to delete the temporary folder will cause- * an exception (typically on Windows).+ * Returns a new and empty temporary folder, based on the given name. The folder will be+ * deleted at the end of the test suite. Failure to delete the temporary folder will cause+ * an exception (typically happens on Windows on unclosed file handles).*/- public static File createTempDir(String name, File tmpDir, boolean ensureCleanedUp) {- if (name.length() < 3) {- throw new IllegalArgumentException("description must be at least 3 characters");+ public static File createTempDir(String prefix, File parent) {+ if (prefix.length() < 3) {+ throw new IllegalArgumentException("The prefix must be at least 3 characters: " + prefix);}-- // always pull a long from master random. that way, the randomness of the test++ if (parent == null) {+ parent = tempDirectory();+ }++ if (!parent.exists()) {+ throw new RuntimeException("Parent location does not exist: " + parent.getAbsolutePath());+ }++ if (!parent.isDirectory()) {+ throw new RuntimeException("Parent location is not a folder: " + parent.getAbsolutePath());+ }++ if (!parent.canWrite()) {+ throw new RuntimeException("Parent folder not writeable: " + parent.getAbsolutePath());+ }++ // Always pull a long from master random. that way, the randomness of the test// is not affected by whether it initialized the counter (in genTempFile) or not.// note that the Random used by genTempFile is *not* the master Random, and therefore// does not affect the randomness of the test.- final Random random = new Random(RandomizedContext.current().getRandom().nextLong());int attempt = 0;File f;do {- f = genTempFile(random, name + "_", "", tmpDir);+ f = genTempFile(prefix + "_", "", parent);} while (!f.mkdir() && (attempt++) < GET_TEMP_DIR_RETRY_THRESHOLD);if (attempt > GET_TEMP_DIR_RETRY_THRESHOLD) {throw new RuntimeException(- "failed to get a temporary dir too many times. check your temp directory and consider manually cleaning it.");+ "Failed to get a temporary dir too many times, check your temp directory and consider manually cleaning it: "+ + parent.getAbsolutePath());+ }++ return maybeRemoveAfterSuite(f);+ }++ private static File maybeRemoveAfterSuite(File f) {+ if (LuceneTestCase.LEAVE_TEMPORARY) {+ System.err.println("INFO: Will leave temporary file: " + f.getAbsolutePath());+ return f;}- if (ensureCleanedUp) {- LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker));++ Class<?> suiteClass = RandomizedContext.current().getTargetClass();+ if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) {+ System.err.println("WARNING: Will leave temporary files (bugUrl: "+ + suiteClass.getAnnotation(SuppressTempFileChecks.class).bugUrl() + "): "+ + f.getAbsolutePath());+ return f;}++ LuceneTestCase.closeAfterSuite(new RemoveUponClose(f, LuceneTestCase.suiteFailureMarker));return f;}public static File createTempFile(String prefix, String suffix) throws IOException {- String tmpDir = System.getProperty("tempDir", System.getProperty("java.io.tmpdir"));- return createTempFile(prefix, suffix, new File(tmpDir));+ return createTempFile(prefix, suffix, tempDirectory());+ }++ /**+ * Do NOT expose this method public. Use {@link #createTempDir()} instead.+ */+ private static File tempDirectory() {+ File directory = new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir")));+ assert directory.exists() &&+ directory.isDirectory() &&+ directory.canWrite();+ return directory;}/*** Insecure, fast version of {@link File#createTempFile(String, String)}, uses* Random instead of SecureRandom.*/- public static File createTempFile(String prefix, String suffix, File directory)- throws IOException {+ public static File createTempFile(String prefix, String suffix, File directory) throws IOException {if (prefix.length() < 3) {throw new IllegalArgumentException("prefix must be at least 3 characters");}String newSuffix = suffix == null ? ".tmp" : suffix;- // always pull a long from master random. that way, the randomness of the test- // is not affected by whether it initialized the counter (in genTempFile) or not.- // note that the Random used by genTempFile is *not* the master Random, and therefore- // does not affect the randomness of the test.- final Random random = new Random(RandomizedContext.current().getRandom().nextLong());File result;do {- result = genTempFile(random, prefix, newSuffix, directory);+ result = genTempFile(prefix, newSuffix, directory);} while (!result.createNewFile());- LuceneTestCase.closeAfterSuite(new CloseableFile(result, LuceneTestCase.suiteFailureMarker));- return result;+ return maybeRemoveAfterSuite(result);}- /* identify for differnt VM processes */- private static String counterBase;-/* Temp file counter */- private static int counter;- private static final Object counterLock = new Object();-- private static File genTempFile(Random random, String prefix, String suffix, File directory) {- final int identify;- synchronized (counterLock) {- if (counterBase == null) { // init once- counter = random.nextInt() & 0xFFFF; // up to five digits number- counterBase = Integer.toString(counter);- }- identify = counter++;- }- StringBuilder newName = new StringBuilder();- newName.append(prefix);- newName.append(counterBase);- newName.append(identify);- newName.append(suffix);- return new File(directory, newName.toString());+ private static final AtomicInteger counter = new AtomicInteger();++ private static File genTempFile(String prefix, String suffix, File directory) {+ return new File(directory,+ prefix+ + RandomizedContext.current().getRunnerSeedAsString()+ + "-" + counter.incrementAndGet()+ + suffix);}public static void assertEquals(TopDocs expected, TopDocs actual) {diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestContentStreamDataSource.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestContentStreamDataSource.javaindex 51ac5ea980b..a15e2d18320 100644--- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestContentStreamDataSource.java+++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestContentStreamDataSource.java@@ -20,8 +20,6 @@ import java.io.File;import java.util.List;import org.apache.commons.io.FileUtils;-import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.util.TestUtil;import org.apache.solr.client.solrj.embedded.JettySolrRunner;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.client.solrj.request.DirectXmlRequest;@@ -151,10 +149,7 @@ public class TestContentStreamDataSource extends AbstractDataImportHandlerTestCapublic void setUp() throws Exception {- File home = new File(dataDir,- getClass().getName() + "-" + System.currentTimeMillis());-- homeDir = new File(home, "inst");+ homeDir = createTempDir("inst");dataDir = new File(homeDir + "/collection1", "data");confDir = new File(homeDir + "/collection1", "conf");diff --git a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.javaindex 38a03344f98..3036bbe9339 100644--- a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java+++ b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java@@ -76,7 +76,7 @@ public class ResourceLoaderTest extends SolrTestCaseJ4}loader.close();} finally {- TestUtil.rmDir(temp);+ TestUtil.rm(temp);}}diff --git a/solr/core/src/test/org/apache/solr/handler/component/DistributedSpellCheckComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/DistributedSpellCheckComponentTest.javaindex 5d11c7aa4e1..e4d83c0defc 100644--- a/solr/core/src/test/org/apache/solr/handler/component/DistributedSpellCheckComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/DistributedSpellCheckComponentTest.java@@ -23,8 +23,8 @@ import java.util.List;import junit.framework.Assert;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.lucene.util.LuceneTestCase.Slow;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.BaseDistributedSearchTestCase;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.response.QueryResponse;@@ -41,7 +41,7 @@ import org.junit.BeforeClass;* @see org.apache.solr.handler.component.SpellCheckComponent*/@Slow-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class DistributedSpellCheckComponentTest extends BaseDistributedSearchTestCase {public DistributedSpellCheckComponentTest()diff --git a/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.javaindex 0f675ea4f40..2f4cc3888d8 100644--- a/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java+++ b/solr/core/src/test/org/apache/solr/handler/component/SpellCheckComponentTest.java@@ -21,8 +21,8 @@ import java.io.File;import java.util.*;import org.apache.lucene.util.LuceneTestCase.Slow;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.SpellingParams;@@ -42,7 +42,7 @@ import org.junit.Test;* @since solr 1.3*/@Slow-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class SpellCheckComponentTest extends SolrTestCaseJ4 {static String rh = "spellCheckCompRH";diff --git a/solr/core/src/test/org/apache/solr/spelling/DirectSolrSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/DirectSolrSpellCheckerTest.javaindex b4694a13687..56a75ac9f3a 100644--- a/solr/core/src/test/org/apache/solr/spelling/DirectSolrSpellCheckerTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/DirectSolrSpellCheckerTest.java@@ -21,8 +21,8 @@ import java.util.Collection;import java.util.Map;import org.apache.lucene.analysis.Token;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.params.SpellingParams;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;@@ -35,7 +35,7 @@ import org.junit.Test;/*** Simple tests for {@link DirectSolrSpellChecker}*/-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class DirectSolrSpellCheckerTest extends SolrTestCaseJ4 {private static SpellingQueryConverter queryConverter;diff --git a/solr/core/src/test/org/apache/solr/spelling/FileBasedSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/FileBasedSpellCheckerTest.javaindex 8ff77367f2b..b76001c4b1d 100644--- a/solr/core/src/test/org/apache/solr/spelling/FileBasedSpellCheckerTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/FileBasedSpellCheckerTest.java@@ -24,8 +24,8 @@ import java.util.Map;import org.apache.lucene.analysis.Token;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;import org.apache.solr.search.SolrIndexSearcher;@@ -38,7 +38,7 @@ import org.junit.Test;** @since solr 1.3**/-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class FileBasedSpellCheckerTest extends SolrTestCaseJ4 {private static SpellingQueryConverter queryConverter;diff --git a/solr/core/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.javaindex 91e9730403b..f1c743aeca4 100644--- a/solr/core/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/IndexBasedSpellCheckerTest.java@@ -37,8 +37,8 @@ import org.apache.lucene.search.spell.SuggestMode;import org.apache.lucene.search.spell.SuggestWord;import org.apache.lucene.search.spell.SuggestWordFrequencyComparator;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.component.SpellCheckComponent;@@ -51,7 +51,7 @@ import org.junit.Test;/*** @since solr 1.3*/-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class IndexBasedSpellCheckerTest extends SolrTestCaseJ4 {protected static SpellingQueryConverter queryConverter;diff --git a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.javaindex 8ebebf0fa10..f1b3651909d 100644--- a/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/SpellCheckCollatorTest.java@@ -21,9 +21,9 @@ import java.util.List;import java.util.Set;import org.apache.lucene.util.LuceneTestCase.Slow;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.lucene.util.TestUtil;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.params.CommonParams;import org.apache.solr.common.params.GroupParams;@@ -42,7 +42,7 @@ import org.junit.BeforeClass;import org.junit.Test;@Slow-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class SpellCheckCollatorTest extends SolrTestCaseJ4 {@BeforeClasspublic static void beforeClass() throws Exception {diff --git a/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java b/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.javaindex f3578307e67..395361fe7e3 100644--- a/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java+++ b/solr/core/src/test/org/apache/solr/spelling/WordBreakSolrSpellCheckerTest.java@@ -23,8 +23,8 @@ import java.util.Map;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.analysis.Token;+import org.apache.lucene.util.LuceneTestCase.SuppressTempFileChecks;import org.apache.solr.SolrTestCaseJ4;-import org.apache.solr.SolrTestCaseJ4.SuppressTempDirCleanUp;import org.apache.solr.common.util.NamedList;import org.apache.solr.core.SolrCore;import org.apache.solr.handler.component.SpellCheckComponent;@@ -33,7 +33,7 @@ import org.apache.solr.util.RefCounted;import org.junit.BeforeClass;import org.junit.Test;-@SuppressTempDirCleanUp(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")+@SuppressTempFileChecks(bugUrl = "https://issues.apache.org/jira/browse/SOLR-1877 Spellcheck IndexReader leak bug?")public class WordBreakSolrSpellCheckerTest extends SolrTestCaseJ4 {@BeforeClassdiff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.javaindex fe13e345703..4e6cf8024d4 100644--- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java@@ -134,25 +134,9 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {public String bugUrl() default "None";}-- /**- * Annotation for test classes to prevent TEMP_DIR cleanup.- */- @Documented- @Inherited- @Retention(RetentionPolicy.RUNTIME)- @Target(ElementType.TYPE)- public @interface SuppressTempDirCleanUp {- /** Point to JIRA entry. */- public String bugUrl() default "None";- }-// these are meant to be accessed sequentially, but are volatile just to ensure any test// thread will read the latest valueprotected static volatile SSLTestConfig sslConfig;-- private static boolean LEAVE_TEST_TMP_DIR = Boolean.getBoolean("solr.test.leavetmpdir");- private static boolean LEAVE_TEST_TMP_DIR_ANNOTATION;@ClassRulepublic static TestRule solrClassRules =@@ -166,14 +150,10 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {@BeforeClass@SuppressWarnings("unused")private static void beforeClass() {- String cname = getSimpleClassName();-- LEAVE_TEST_TMP_DIR_ANNOTATION = RandomizedContext.current().getTargetClass()- .isAnnotationPresent(SuppressTempDirCleanUp.class);-- boolean ensureClosed = !(LEAVE_TEST_TMP_DIR || LEAVE_TEST_TMP_DIR_ANNOTATION);- rootTmpDir = TestUtil.createTempDir("solrtest-" + cname, null, ensureClosed);- initCoreDataDir = TestUtil.createTempDir("solrtest-" + cname, rootTmpDir, ensureClosed);+ // Create the root parent folder for all other temporary solr files.+ rootTmpDir = TestUtil.createTempDir();++ initCoreDataDir = TestUtil.createTempDir("init-core-data", rootTmpDir);System.err.println("Creating dataDir: " + initCoreDataDir.getAbsolutePath());@@ -207,42 +187,19 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {resetFactory();coreName = ConfigSolrXmlOld.DEFAULT_DEFAULT_CORE_NAME;} finally {- try {-- if (LEAVE_TEST_TMP_DIR) {- System.err- .println("NOTE: per solr.test.leavetmpdir, the test tmp directory will not be removed: "- + rootTmpDir.getAbsolutePath());- } else {- // TODO: tmp files should already get cleaned up by the test framework, but- // we still do it here as well, so that we clean up as much as we can, even- // when a test is the SuppressTempDirCleanUp annotation- if (rootTmpDir != null && rootTmpDir.exists() && !recurseDelete(rootTmpDir)) {- String msg = "!!!! WARNING: best effort to remove "- + rootTmpDir.getAbsolutePath() + " FAILED !!!!!";- if (LEAVE_TEST_TMP_DIR_ANNOTATION) {- System.err.println(msg);- } else {- // TODO: do we want to fail here with this message? Is the test framework- // fail that should happen better?- fail(msg);- }- }- }- } finally {- initCoreDataDir = null;- System.clearProperty("jetty.testMode");- System.clearProperty("tests.shardhandler.randomSeed");- System.clearProperty("enable.update.log");- System.clearProperty("useCompoundFile");- System.clearProperty("urlScheme");-- if (isSSLMode()) {- HttpClientUtil.setConfigurer(new HttpClientConfigurer());- }- // clean up static- sslConfig = null;+ initCoreDataDir = null;+ System.clearProperty("jetty.testMode");+ System.clearProperty("tests.shardhandler.randomSeed");+ System.clearProperty("enable.update.log");+ System.clearProperty("useCompoundFile");+ System.clearProperty("urlScheme");++ if (isSSLMode()) {+ HttpClientUtil.setConfigurer(new HttpClientConfigurer());}++ // clean up static+ sslConfig = null;}IpTables.unblockAllPorts();@@ -511,9 +468,9 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {public static File createTempDir() {return createTempDir(null);}-- public static File createTempDir(String tag) {- return TestUtil.createTempDir(getClassName() + (tag == null ? "" : "-" + tag), rootTmpDir, !(LEAVE_TEST_TMP_DIR || LEAVE_TEST_TMP_DIR_ANNOTATION));++ public static File createTempDir(String prefix) {+ return TestUtil.createTempDir(prefix == null ? "temp" : prefix, rootTmpDir);}public static void resetExceptionIgnores() {diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.javaindex d76a6a9eb67..537ad6b096e 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/AbstractZkTestCase.java@@ -154,13 +154,11 @@ public abstract class AbstractZkTestCase extends SolrTestCaseJ4 {System.clearProperty("jetty.port");System.clearProperty(ZOOKEEPER_FORCE_SYNC);- zkServer.shutdown();-- zkServer = null;+ if (zkServer != null) {+ zkServer.shutdown();+ zkServer = null;+ }zkDir = null;-- // wait just a bit for any zk client threads to outlast timeout- Thread.sleep(2000);}protected void printLayout(String zkHost) throws Exception {