You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by us...@apache.org on 2014/09/14 01:25:45 UTC

svn commit: r1624790 - in /lucene/dev/trunk/lucene: backward-codecs/src/test/org/apache/lucene/index/ benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/ test-framework/src/java/org/apache/lucene/util/

Author: uschindler
Date: Sat Sep 13 23:25:45 2014
New Revision: 1624790

URL: http://svn.apache.org/r1624790
Log:
LUCENE-5945: Nuke one more ZipFile usage

Modified:
    lucene/dev/trunk/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
    lucene/dev/trunk/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
    lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java

Modified: lucene/dev/trunk/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java?rev=1624790&r1=1624789&r2=1624790&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java (original)
+++ lucene/dev/trunk/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java Sat Sep 13 23:25:45 2014
@@ -295,8 +295,7 @@ public class TestBackwardsCompatibility 
     oldIndexDirs = new HashMap<>();
     for (String name : names) {
       Path dir = createTempDir(name);
-      Path dataFile = Paths.get(TestBackwardsCompatibility.class.getResource("index." + name + ".zip").toURI());
-      TestUtil.unzip(dataFile, dir);
+      TestUtil.unzip(TestBackwardsCompatibility.class.getResourceAsStream("index." + name + ".zip"), dir);
       oldIndexDirs.put(name, newFSDirectory(dir));
     }
   }
@@ -435,7 +434,7 @@ public class TestBackwardsCompatibility 
         System.out.println("TEST: index " + unsupportedNames[i]);
       }
       Path oldIndexDir = createTempDir(unsupportedNames[i]);
-      TestUtil.unzip(getDataPath("unsupported." + unsupportedNames[i] + ".zip"), oldIndexDir);
+      TestUtil.unzip(getDataInputStream("unsupported." + unsupportedNames[i] + ".zip"), oldIndexDir);
       BaseDirectoryWrapper dir = newFSDirectory(oldIndexDir);
       // don't checkindex, these are intentionally not supported
       dir.setCheckIndexOnClose(false);
@@ -1086,8 +1085,7 @@ public class TestBackwardsCompatibility 
     try {
       for (String name : oldIndexDirs.keySet()) {
         Path dir = createTempDir(name);
-        Path dataFile = Paths.get(TestBackwardsCompatibility.class.getResource("index." + name + ".zip").toURI());
-        TestUtil.unzip(dataFile, dir);
+        TestUtil.unzip(getDataInputStream("index." + name + ".zip"), dir);
         
         String path = dir.toAbsolutePath().toString();
         
@@ -1197,7 +1195,7 @@ public class TestBackwardsCompatibility 
 
   public void testMoreTerms() throws Exception {
     Path oldIndexDir = createTempDir("moreterms");
-    TestUtil.unzip(getDataPath(moreTermsIndex), oldIndexDir);
+    TestUtil.unzip(getDataInputStream(moreTermsIndex), oldIndexDir);
     Directory dir = newFSDirectory(oldIndexDir);
     // TODO: more tests
     TestUtil.checkIndex(dir);
@@ -1236,7 +1234,7 @@ public class TestBackwardsCompatibility 
   
   public void testDocValuesUpdates() throws Exception {
     Path oldIndexDir = createTempDir("dvupdates");
-    TestUtil.unzip(getDataPath(dvUpdatesIndex), oldIndexDir);
+    TestUtil.unzip(getDataInputStream(dvUpdatesIndex), oldIndexDir);
     Directory dir = newFSDirectory(oldIndexDir);
     
     verifyDocValues(dir);

Modified: lucene/dev/trunk/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java?rev=1624790&r1=1624789&r2=1624790&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java (original)
+++ lucene/dev/trunk/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/feeds/TrecContentSourceTest.java Sat Sep 13 23:25:45 2014
@@ -344,7 +344,7 @@ public class TrecContentSourceTest exten
    */
   public void testTrecFeedDirAllTypes() throws Exception {
     Path dataDir =  createTempDir("trecFeedAllTypes");
-    TestUtil.unzip(getDataPath("trecdocs.zip"), dataDir);
+    TestUtil.unzip(getDataInputStream("trecdocs.zip"), dataDir);
     TrecContentSource tcs = new TrecContentSource();
     Properties props = new Properties();
     props.setProperty("print.props", "false");

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java?rev=1624790&r1=1624789&r2=1624790&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java Sat Sep 13 23:25:45 2014
@@ -20,6 +20,7 @@ package org.apache.lucene.util;
 import java.io.Closeable;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintStream;
 import java.lang.annotation.Documented;
 import java.lang.annotation.ElementType;
@@ -138,6 +139,7 @@ import org.junit.Test;
 import org.junit.rules.RuleChain;
 import org.junit.rules.TestRule;
 import org.junit.runner.RunWith;
+
 import com.carrotsearch.randomizedtesting.JUnit4MethodProvider;
 import com.carrotsearch.randomizedtesting.LifecycleScope;
 import com.carrotsearch.randomizedtesting.MixWithSuiteName;
@@ -1673,6 +1675,14 @@ public abstract class LuceneTestCase ext
     }
   }
 
+  protected InputStream getDataInputStream(String name) throws IOException {
+    try {
+      return this.getClass().getResourceAsStream(name);
+    } catch (Exception e) {
+      throw new IOException("Cannot find resource: " + name);
+    }
+  }
+
   public void assertReaderEquals(String info, IndexReader leftReader, IndexReader rightReader) throws IOException {
     assertReaderStatisticsEquals(info, leftReader, rightReader);
     assertFieldsEquals(info, leftReader, MultiFields.getFields(leftReader), MultiFields.getFields(rightReader), true);

Modified: lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java?rev=1624790&r1=1624789&r2=1624790&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java (original)
+++ lucene/dev/trunk/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java Sat Sep 13 23:25:45 2014
@@ -38,7 +38,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 import java.util.regex.PatternSyntaxException;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
 
 import org.apache.lucene.codecs.Codec;
 import org.apache.lucene.codecs.DocValuesFormat;
@@ -99,39 +99,33 @@ public final class TestUtil {
 
   /** 
    * Convenience method unzipping zipName into destDir, cleaning up 
-   * destDir first. 
+   * destDir first.
+   * Closes the given InputStream after extracting! 
    */
-  public static void unzip(Path zipName, Path destDir) throws IOException {
+  public static void unzip(InputStream in, Path destDir) throws IOException {
     IOUtils.rm(destDir);
     Files.createDirectory(destDir);
 
-    ZipFile zipFile = new ZipFile(zipName.toFile());
-    Enumeration<? extends ZipEntry> entries = zipFile.entries();
-
-    while (entries.hasMoreElements()) {
-      ZipEntry entry = entries.nextElement();
-      
-      InputStream in = zipFile.getInputStream(entry);
-      Path targetFile = destDir.resolve(entry.getName());
-      
-      // be on the safe side: do not rely on that directories are always extracted
-      // before their children (although this makes sense, but is it guaranteed?)
-      Files.createDirectories(targetFile.getParent());
-      if (!entry.isDirectory()) {
-        OutputStream out = Files.newOutputStream(targetFile);
+    try (ZipInputStream zipInput = new ZipInputStream(in)) {
+      ZipEntry entry;
+      while ((entry = zipInput.getNextEntry()) != null) {
+        Path targetFile = destDir.resolve(entry.getName());
         
-        byte[] buffer = new byte[8192];
-        int len;
-        while((len = in.read(buffer)) >= 0) {
-          out.write(buffer, 0, len);
+        // be on the safe side: do not rely on that directories are always extracted
+        // before their children (although this makes sense, but is it guaranteed?)
+        Files.createDirectories(targetFile.getParent());
+        if (!entry.isDirectory()) {
+          OutputStream out = Files.newOutputStream(targetFile);
+          byte[] buffer = new byte[8192];
+          int len;
+          while((len = zipInput.read(buffer)) >= 0) {
+            out.write(buffer, 0, len);
+          }
+          out.close();
         }
-        
-        in.close();
-        out.close();
+        zipInput.closeEntry();
       }
     }
-    
-    zipFile.close();
   }
   
   public static void syncConcurrentMerges(IndexWriter writer) {