LUCENE-6795: fix some test issues (mostly static leaks)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1702144 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2015-09-10 01:48:34 +00:00
parent 37ec9f4344
commit e18e9ba178
6 changed files with 25 additions and 90 deletions

View File

@ -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);
}

View File

@ -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());

View File

@ -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):

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -57,12 +57,6 @@ grant {
permission java.lang.RuntimePermission "getStackTrace";
// needed for mock filesystems in tests
permission 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)