Apache Lucene open-source search software
LUCENE-6795: fix some test issues (mostly static leaks)
Robert Muire18e9ba17821
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.javaindex 0613dd37d90..c639c3c23ec 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/StemmerTestBase.java@@ -27,11 +27,17 @@ import java.util.List;import org.apache.lucene.util.CharsRef;import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;+import org.junit.AfterClass;/** base class for hunspell stemmer tests */-abstract class StemmerTestBase extends LuceneTestCase {+public abstract class StemmerTestBase extends LuceneTestCase {private static Stemmer stemmer;+ @AfterClass+ public static void afterClass() {+ stemmer = null;+ }+static void init(String affix, String dictionary) throws IOException, ParseException {init(false, affix, dictionary);}diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.javaindex d5a3114c141..e86cf6d2757 100644--- a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java+++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/tasks/AddIndexesTaskTest.java@@ -31,6 +31,7 @@ import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.Version;+import org.junit.AfterClass;import org.junit.BeforeClass;/** Tests the functionality of {@link AddIndexesTask}. */@@ -56,6 +57,11 @@ public class AddIndexesTaskTest extends BenchmarkTestCase {}}+ @AfterClass+ public static void afterClassAddIndexesTaskTest() {+ testDir = inputDir = null;+ }+private PerfRunData createPerfRunData() throws Exception {Properties props = new Properties();props.setProperty("writer.version", Version.LATEST.toString());diff --git a/lucene/core/src/test/org/apache/lucene/index/TestReadOnlyIndex.java b/lucene/core/src/test/org/apache/lucene/index/TestReadOnlyIndex.javaindex 40b53b9172b..88d08732ab9 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestReadOnlyIndex.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestReadOnlyIndex.java@@ -34,6 +34,7 @@ import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.LuceneTestCase;+import org.junit.AfterClass;import org.junit.BeforeClass;public class TestReadOnlyIndex extends LuceneTestCase {@@ -59,6 +60,11 @@ public class TestReadOnlyIndex extends LuceneTestCase {analyzer.close();}+ @AfterClass+ public static void afterClass() throws Exception {+ indexPath = null;+ }+public void testReadOnlyIndex() throws Exception {runWithRestrictedPermissions(this::doTestReadOnlyIndex,// add some basic permissions (because we are limited already - so we grant all important ones):diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.javaindex 5fd74e26b7a..138c30ac175 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestNumericQueryParser.java@@ -71,7 +71,7 @@ public class TestNumericQueryParser extends LuceneTestCase {private static Locale LOCALE;private static TimeZone TIMEZONE;private static Map<String,Number> RANDOM_NUMBER_MAP;- final private static EscapeQuerySyntax ESCAPER = new EscapeQuerySyntaxImpl();+ private static EscapeQuerySyntax ESCAPER = new EscapeQuerySyntaxImpl();final private static String DATE_FIELD_NAME = "date";private static int DATE_STYLE;private static int TIME_STYLE;@@ -526,6 +526,11 @@ public class TestNumericQueryParser extends LuceneTestCase {directory.close();directory = null;qp = null;+ LOCALE = null;+ TIMEZONE = null;+ NUMBER_FORMAT = null;+ DATE_FORMAT = null;+ ESCAPER = null;}}diff --git a/lucene/test-framework/src/test/org/apache/lucene/util/TestLeaveFilesIfTestFails.java b/lucene/test-framework/src/test/org/apache/lucene/util/TestLeaveFilesIfTestFails.javaindex 7410a5b6e56..e69de29bb2d 100644--- a/lucene/test-framework/src/test/org/apache/lucene/util/TestLeaveFilesIfTestFails.java+++ b/lucene/test-framework/src/test/org/apache/lucene/util/TestLeaveFilesIfTestFails.java@@ -1,82 +0,0 @@-package org.apache.lucene.util;--/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF 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.- */--import java.io.IOException;-import java.nio.channels.SeekableByteChannel;-import java.nio.file.Files;-import java.nio.file.Path;-import java.nio.file.StandardOpenOption;--import org.apache.lucene.util.Constants;-import org.apache.lucene.util.IOUtils;-import org.apache.lucene.util.LuceneTestCase;-import org.junit.Assert;-import org.junit.Test;-import org.junit.runner.JUnitCore;-import org.junit.runner.Result;--import com.carrotsearch.randomizedtesting.RandomizedTest;--public class TestLeaveFilesIfTestFails extends WithNestedTests {- public TestLeaveFilesIfTestFails() {- super(true);- }-- public static class Nested1 extends WithNestedTests.AbstractNestedTest {- static Path file;- public void testDummy() {- file = createTempDir("leftover");- fail();- }- }-- @Test- public void testLeaveFilesIfTestFails() throws IOException {- Result r = JUnitCore.runClasses(Nested1.class);- Assert.assertEquals(1, r.getFailureCount());- Assert.assertTrue(Nested1.file != null && Files.exists(Nested1.file));- IOUtils.rm(Nested1.file);- }-- public static class Nested2 extends WithNestedTests.AbstractNestedTest {- static Path file;- static Path parent;- static SeekableByteChannel openFile;-- @SuppressWarnings("deprecation")- public void testDummy() throws Exception {- file = createTempDir("leftover").resolve("child.locked");- openFile = Files.newByteChannel(file, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);-- parent = LuceneTestCase.getBaseTempDirForTestClass();- }- }-- @Test- public void testWindowsUnremovableFile() throws IOException {- RandomizedTest.assumeTrue("Requires Windows.", Constants.WINDOWS);- RandomizedTest.assumeFalse(LuceneTestCase.LEAVE_TEMPORARY);-- Result r = JUnitCore.runClasses(Nested2.class);- Assert.assertEquals(1, r.getFailureCount());-- Nested2.openFile.close();- IOUtils.rm(Nested2.parent);- }-}diff --git a/lucene/tools/junit4/tests.policy b/lucene/tools/junit4/tests.policyindex 439bdbe9a98..f5729d3e797 100644--- a/lucene/tools/junit4/tests.policy+++ b/lucene/tools/junit4/tests.policy@@ -57,12 +57,6 @@ grant {permission java.lang.RuntimePermission "getStackTrace";// needed for mock filesystems in testspermission java.lang.RuntimePermission "fileSystemProvider";- // needed for mock filesystems in tests (to capture implCloseChannel)- permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.ch";- // needed by junit nested compat tests (due to static fields reflection), TODO clean these up:- permission java.lang.RuntimePermission "accessClassInPackage.sun.util.calendar";- permission java.lang.RuntimePermission "accessClassInPackage.sun.util.locale";- permission java.lang.RuntimePermission "accessClassInPackage.sun.nio.fs";// needed by queryparser/ NLS., TODO clean this up:permission java.lang.RuntimePermission "accessClassInPackage.sun.util";// needed for test of IOUtils.spins (maybe it can be avoided)