Apache Solr open-source search software
LUCENE-5945: more test cleanups
Robert Muir535454eb2546
diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries.javaindex 288e3249c58..c37bb82b62c 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries.java@@ -18,17 +18,16 @@ package org.apache.lucene.analysis.hunspell;*/import java.io.InputStream;-import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;-import java.util.zip.ZipEntry;-import java.util.zip.ZipFile;import org.apache.lucene.analysis.hunspell.Dictionary;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks;import org.apache.lucene.util.RamUsageTester;+import org.apache.lucene.util.TestUtil;import org.junit.Ignore;/**@@ -157,18 +156,22 @@ public class TestAllDictionaries extends LuceneTestCase {};public void test() throws Exception {+ Path tmp = LuceneTestCase.createTempDir();+for (int i = 0; i < tests.length; i += 3) {Path f = DICTIONARY_HOME.resolve(tests[i]);assert Files.exists(f);- try (ZipFile zip = new ZipFile(f.toFile(), StandardCharsets.UTF_8)) {- ZipEntry dicEntry = zip.getEntry(tests[i+1]);- assert dicEntry != null;- ZipEntry affEntry = zip.getEntry(tests[i+2]);- assert affEntry != null;+ IOUtils.rm(tmp);+ Files.createDirectory(tmp);- try (InputStream dictionary = zip.getInputStream(dicEntry);- InputStream affix = zip.getInputStream(affEntry)) {+ try (InputStream in = Files.newInputStream(f)) {+ TestUtil.unzip(in, tmp);+ Path dicEntry = tmp.resolve(tests[i+1]);+ Path affEntry = tmp.resolve(tests[i+2]);++ try (InputStream dictionary = Files.newInputStream(dicEntry);+ InputStream affix = Files.newInputStream(affEntry)) {Dictionary dic = new Dictionary(affix, dictionary);System.out.println(tests[i] + "\t" + RamUsageTester.humanSizeOf(dic) + "\t(" +"words=" + RamUsageTester.humanSizeOf(dic.words) + ", " +@@ -184,22 +187,26 @@ public class TestAllDictionaries extends LuceneTestCase {}public void testOneDictionary() throws Exception {+ Path tmp = LuceneTestCase.createTempDir();+String toTest = "zu_ZA.zip";for (int i = 0; i < tests.length; i++) {if (tests[i].equals(toTest)) {Path f = DICTIONARY_HOME.resolve(tests[i]);assert Files.exists(f);- try (ZipFile zip = new ZipFile(f.toFile(), StandardCharsets.UTF_8)) {- ZipEntry dicEntry = zip.getEntry(tests[i+1]);- assert dicEntry != null;- ZipEntry affEntry = zip.getEntry(tests[i+2]);- assert affEntry != null;+ IOUtils.rm(tmp);+ Files.createDirectory(tmp);++ try (InputStream in = Files.newInputStream(f)) {+ TestUtil.unzip(in, tmp);+ Path dicEntry = tmp.resolve(tests[i+1]);+ Path affEntry = tmp.resolve(tests[i+2]);- try (InputStream dictionary = zip.getInputStream(dicEntry);- InputStream affix = zip.getInputStream(affEntry)) {- new Dictionary(affix, dictionary);- }+ try (InputStream dictionary = Files.newInputStream(dicEntry);+ InputStream affix = Files.newInputStream(affEntry)) {+ new Dictionary(affix, dictionary);+ }}}}diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.javaindex 6fb59e8f667..fd48702506e 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/hunspell/TestAllDictionaries2.java@@ -18,16 +18,15 @@ package org.apache.lucene.analysis.hunspell;*/import java.io.InputStream;-import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;-import java.util.zip.ZipEntry;-import java.util.zip.ZipFile;import org.apache.lucene.analysis.hunspell.Dictionary;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.RamUsageTester;+import org.apache.lucene.util.TestUtil;import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks;import org.junit.Ignore;@@ -172,18 +171,22 @@ public class TestAllDictionaries2 extends LuceneTestCase {};public void test() throws Exception {+ Path tmp = LuceneTestCase.createTempDir();+for (int i = 0; i < tests.length; i += 3) {Path f = DICTIONARY_HOME.resolve(tests[i]);assert Files.exists(f);- try (ZipFile zip = new ZipFile(f.toFile(), StandardCharsets.UTF_8)) {- ZipEntry dicEntry = zip.getEntry(tests[i+1]);- assert dicEntry != null;- ZipEntry affEntry = zip.getEntry(tests[i+2]);- assert affEntry != null;+ IOUtils.rm(tmp);+ Files.createDirectory(tmp);- try (InputStream dictionary = zip.getInputStream(dicEntry);- InputStream affix = zip.getInputStream(affEntry)) {+ try (InputStream in = Files.newInputStream(f)) {+ TestUtil.unzip(in, tmp);+ Path dicEntry = tmp.resolve(tests[i+1]);+ Path affEntry = tmp.resolve(tests[i+2]);++ try (InputStream dictionary = Files.newInputStream(dicEntry);+ InputStream affix = Files.newInputStream(affEntry)) {Dictionary dic = new Dictionary(affix, dictionary);System.out.println(tests[i] + "\t" + RamUsageTester.humanSizeOf(dic) + "\t(" +"words=" + RamUsageTester.humanSizeOf(dic.words) + ", " +@@ -199,22 +202,26 @@ public class TestAllDictionaries2 extends LuceneTestCase {}public void testOneDictionary() throws Exception {+ Path tmp = LuceneTestCase.createTempDir();+String toTest = "hungarian_dictionary-1.6.1.1-fx+tb+sm+fn.xpi";for (int i = 0; i < tests.length; i++) {if (tests[i].equals(toTest)) {Path f = DICTIONARY_HOME.resolve(tests[i]);assert Files.exists(f);- try (ZipFile zip = new ZipFile(f.toFile(), StandardCharsets.UTF_8)) {- ZipEntry dicEntry = zip.getEntry(tests[i+1]);- assert dicEntry != null;- ZipEntry affEntry = zip.getEntry(tests[i+2]);- assert affEntry != null;+ IOUtils.rm(tmp);+ Files.createDirectory(tmp);++ try (InputStream in = Files.newInputStream(f)) {+ TestUtil.unzip(in, tmp);+ Path dicEntry = tmp.resolve(tests[i+1]);+ Path affEntry = tmp.resolve(tests[i+2]);- try (InputStream dictionary = zip.getInputStream(dicEntry);- InputStream affix = zip.getInputStream(affEntry)) {+ try (InputStream dictionary = Files.newInputStream(dicEntry);+ InputStream affix = Files.newInputStream(affEntry)) {new Dictionary(affix, dictionary);- }+ }}}}diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/QueryDriver.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/QueryDriver.javaindex 200574d762a..4be533463df 100644--- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/QueryDriver.java+++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/quality/trec/QueryDriver.java@@ -24,7 +24,6 @@ import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.store.FSDirectory;-import org.apache.lucene.util.IOUtils;import java.io.OutputStreamWriter;import java.io.PrintWriter;@@ -55,7 +54,8 @@ public class QueryDriver {Path topicsFile = Paths.get(args[0]);Path qrelsFile = Paths.get(args[1]);- SubmissionReport submitLog = new SubmissionReport(new PrintWriter(args[2], IOUtils.UTF_8 /* huh, no nio.Charset ctor? */), "lucene");+ Path submissionFile = Paths.get(args[2]);+ SubmissionReport submitLog = new SubmissionReport(new PrintWriter(Files.newBufferedWriter(submissionFile, StandardCharsets.UTF_8)), "lucene");FSDirectory dir = FSDirectory.open(Paths.get(args[3]));String fieldSpec = args.length == 5 ? args[4] : "T"; // default to Title-only if not specified.IndexReader reader = DirectoryReader.open(dir);diff --git a/lucene/core/build.xml b/lucene/core/build.xmlindex 3ae8f80dad3..67e2f96c636 100644--- a/lucene/core/build.xml+++ b/lucene/core/build.xml@@ -30,13 +30,15 @@org/apache/lucene/util/PrintStreamInfoStream.class"/>- <property name="forbidden-rue-excludes" value="- org/apache/lucene/util/RamUsageEstimator.class+ <!-- Needs to start a process -->+ <property name="forbidden-tests-excludes" value="+ org/apache/lucene/index/TestIndexWriterOnJRECrash.class"/><!-- TODO: maybe let people get closedchannel if they cancel(true) --><property name="forbidden-base-excludes" value="org/apache/lucene/store/SimpleFSDirectory.class+ org/apache/lucene/store/SimpleFSDirectory$SimpleFSIndexInput.class"/><import file="../common-build.xml"/>diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/VocabularyAssert.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/VocabularyAssert.javaindex dc3765a4a74..de77e96326d 100644--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/VocabularyAssert.java+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/VocabularyAssert.java@@ -22,11 +22,13 @@ import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.charset.StandardCharsets;+import java.nio.file.Files;import java.nio.file.Path;-import java.util.zip.ZipFile;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.BaseTokenStreamTestCase;+import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.TestUtil;import org.junit.Assert;/** Utility class for doing vocabulary-based stemming tests */@@ -62,21 +64,24 @@ public class VocabularyAssert {/** Run a vocabulary test against two data files inside a zip file */public static void assertVocabulary(Analyzer a, Path zipFile, String voc, String out) throws IOException {- ZipFile zip = new ZipFile(zipFile.toFile());- InputStream v = zip.getInputStream(zip.getEntry(voc));- InputStream o = zip.getInputStream(zip.getEntry(out));- assertVocabulary(a, v, o);- v.close();- o.close();- zip.close();+ Path tmp = LuceneTestCase.createTempDir();+ try (InputStream in = Files.newInputStream(zipFile)) {+ TestUtil.unzip(in, tmp);+ }+ try (InputStream v = Files.newInputStream(tmp.resolve(voc));+ InputStream o = Files.newInputStream(tmp.resolve(out))) {+ assertVocabulary(a, v, o);+ }}/** Run a vocabulary test against a tab-separated data file inside a zip file */public static void assertVocabulary(Analyzer a, Path zipFile, String vocOut) throws IOException {- ZipFile zip = new ZipFile(zipFile.toFile());- InputStream vo = zip.getInputStream(zip.getEntry(vocOut));- assertVocabulary(a, vo);- vo.close();- zip.close();+ Path tmp = LuceneTestCase.createTempDir();+ try (InputStream in = Files.newInputStream(zipFile)) {+ TestUtil.unzip(in, tmp);+ }+ try (InputStream in = Files.newInputStream(tmp.resolve(vocOut))) {+ assertVocabulary(a, in);+ }}}diff --git a/lucene/tools/forbiddenApis/lucene.txt b/lucene/tools/forbiddenApis/lucene.txtindex bc8430b2f39..5b89af7e93f 100644--- a/lucene/tools/forbiddenApis/lucene.txt+++ b/lucene/tools/forbiddenApis/lucene.txt@@ -17,4 +17,10 @@java.io.Filejava.io.FileInputStreamjava.io.FileOutputStream-# TODO: all kinds of other stuff taking "String" but making a file itself...+java.io.PrintStream#<init>(java.lang.String,java.lang.String)+java.io.PrintWriter#<init>(java.lang.String,java.lang.String)+java.util.Formatter#<init>(java.lang.String,java.lang.String,java.util.Locale)+java.io.RandomAccessFile+java.nio.file.Path#toFile()+java.util.jar.JarFile+java.util.zip.ZipFile