You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@submarine.apache.org by li...@apache.org on 2019/11/29 01:22:49 UTC

[submarine] branch master updated: SUBMARINE-69. Add tests to ZipUtilities class

This is an automated email from the ASF dual-hosted git repository.

liuxun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new f82ca81  SUBMARINE-69. Add tests to ZipUtilities class
f82ca81 is described below

commit f82ca811c73882533fa5c9851cac74387b08395e
Author: Adam Antal <ad...@cloudera.com>
AuthorDate: Wed Nov 27 16:23:30 2019 +0100

    SUBMARINE-69. Add tests to ZipUtilities class
    
    ### What is this PR for?
    Added unit tests to `ZipUtilities`
    
    ### What type of PR is it?
    Test
    
    ### Todos
    * [ ] - new UTs should pass
    
    ### What is the Jira issue?
    [SUBMARINE-69](https://issues.apache.org/jira/browse/SUBMARINE-69)
    
    ### How should this be tested?
    * New unit tests should pass.
    * No further testing is needed.
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Does the licenses files need update? No
    * Is there breaking changes for older versions? No
    * Does this needs documentation? No
    
    Author: Adam Antal <ad...@cloudera.com>
    
    Closes #112 from adamantal/SUBMARINE-69 and squashes the following commits:
    
    9f66792 [Adam Antal] SUBMARINE-69. Add tests to ZipUtilities class
---
 .../submitter/yarnservice/utils/ZipUtilities.java  |  30 +++--
 .../yarnservice/utils/ZipUtilitiesTest.java        | 148 +++++++++++++++++++++
 2 files changed, 165 insertions(+), 13 deletions(-)

diff --git a/submarine-server/server-submitter/submitter-yarnservice/src/main/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilities.java b/submarine-server/server-submitter/submitter-yarnservice/src/main/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilities.java
index 51ce6a6..88ea181 100644
--- a/submarine-server/server-submitter/submitter-yarnservice/src/main/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilities.java
+++ b/submarine-server/server-submitter/submitter-yarnservice/src/main/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilities.java
@@ -49,7 +49,6 @@ public final class ZipUtilities {
     File srcFile = new File(srcDir);
     LOG.info("Compressing directory {}", srcDir);
     addDirToZip(zos, srcFile, srcFile);
-    // close the ZipOutputStream
     zos.close();
     LOG.info("Compressed directory {} to file: {}", srcDir, dstFile);
     return dstFile;
@@ -67,19 +66,24 @@ public final class ZipUtilities {
         addDirToZip(zos, file, base);
         continue;
       }
-      byte[] buffer = new byte[1024];
-      try (FileInputStream fis = new FileInputStream(file)) {
-        String name = base.toURI().relativize(file.toURI()).getPath();
-        LOG.info("Adding file {} to zip", name);
-        zos.putNextEntry(new ZipEntry(name));
-        int length;
-        while ((length = fis.read(buffer)) > 0) {
-          zos.write(buffer, 0, length);
-        }
-        zos.flush();
-      } finally {
-        zos.closeEntry();
+      addFileToZip(zos, base, file);
+    }
+  }
+
+  private static void addFileToZip(ZipOutputStream zos, File base, File file)
+      throws IOException {
+    byte[] buffer = new byte[1024];
+    try (FileInputStream fis = new FileInputStream(file)) {
+      String name = base.toURI().relativize(file.toURI()).getPath();
+      LOG.info("Adding file {} to zip", name);
+      zos.putNextEntry(new ZipEntry(name));
+      int length;
+      while ((length = fis.read(buffer)) > 0) {
+        zos.write(buffer, 0, length);
       }
+      zos.flush();
+    } finally {
+      zos.closeEntry();
     }
   }
 }
diff --git a/submarine-server/server-submitter/submitter-yarnservice/src/test/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilitiesTest.java b/submarine-server/server-submitter/submitter-yarnservice/src/test/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilitiesTest.java
new file mode 100644
index 0000000..bb70254
--- /dev/null
+++ b/submarine-server/server-submitter/submitter-yarnservice/src/test/java/org/apache/submarine/server/submitter/yarnservice/utils/ZipUtilitiesTest.java
@@ -0,0 +1,148 @@
+/*
+ * 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.
+ */
+
+package org.apache.submarine.server.submitter.yarnservice.utils;
+
+import com.google.common.collect.Sets;
+import org.apache.submarine.FileUtilitiesForTests;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Set;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Test class for {@link ZipUtilities}.
+ */
+public class ZipUtilitiesTest {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(ZipUtilitiesTest.class);
+
+  private FileUtilitiesForTests fileUtils = new FileUtilitiesForTests();
+  private File tempDir;
+
+  @Before
+  public void setUp() {
+    fileUtils.setup();
+    tempDir = fileUtils.createDirInTempDir("testDir");
+  }
+
+  @After
+  public void teardown() throws IOException {
+    fileUtils.teardown();
+  }
+
+  private File getDestinationFile() {
+    File dstFile = fileUtils.getTempFileWithName(String.format(
+        "testFile_%d.zip", System.nanoTime()));
+    fileUtils.addTrackedFile(dstFile);
+
+    return dstFile;
+  }
+
+  @Test
+  public void testZipEmptyDir() throws IOException {
+    File dstFile = getDestinationFile();
+
+    ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath());
+    assertCountOfZipEntries(dstFile, 0);
+  }
+
+  @Test
+  public void testZipDirWithOneFile() throws IOException {
+    fileUtils.createFileInDir(tempDir, "test1");
+    File dstFile = getDestinationFile();
+
+    ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath());
+    assertCountOfZipEntries(dstFile, 1);
+  }
+
+  @Test
+  public void testZipDirWithMultipleFiles() throws IOException {
+    fileUtils.createFileInDir(tempDir, "test1");
+    fileUtils.createFileInDir(tempDir, "test2");
+    fileUtils.createFileInDir(tempDir, "test3");
+    File dstFile = getDestinationFile();
+
+    ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath());
+    assertCountOfZipEntries(dstFile, 3);
+  }
+
+  @Test
+  public void testZipDirComplex() throws IOException {
+    fileUtils.createFileInDir(tempDir, "test1");
+    fileUtils.createFileInDir(tempDir, "test2");
+    fileUtils.createFileInDir(tempDir, "test3");
+    File subdir1 = fileUtils.createDirectory(tempDir, "subdir1");
+    File subdir2 = fileUtils.createDirectory(tempDir, "subdir2");
+    fileUtils.createFileInDir(subdir1, "file1_1");
+    fileUtils.createFileInDir(subdir1, "file1_2");
+    fileUtils.createFileInDir(subdir2, "file2_1");
+    fileUtils.createFileInDir(subdir2, "file2_2");
+
+    File dstFile = getDestinationFile();
+
+    ZipUtilities.zipDir(tempDir.getAbsolutePath(), dstFile.getAbsolutePath());
+    assertZipEntriesByName(dstFile, Sets.newHashSet(
+        "test1", "test2", "test3",
+        "subdir1/file1_1", "subdir1/file1_2",
+        "subdir2/file2_1", "subdir2/file2_2"
+    ));
+  }
+
+  private void assertCountOfZipEntries(File file, int expected)
+      throws IOException {
+    ZipFile zipFile = new ZipFile(file);
+    Enumeration<? extends ZipEntry> entries = zipFile.entries();
+
+    int count = 0;
+    while (entries.hasMoreElements()) {
+      count++;
+      ZipEntry zipEntry = entries.nextElement();
+      LOG.info("Found zipEntry: " + zipEntry);
+    }
+    assertEquals(expected, count);
+  }
+
+  private void assertZipEntriesByName(File file, Set<String> expectedNames)
+      throws IOException {
+    ZipFile zipFile = new ZipFile(file);
+    Enumeration<? extends ZipEntry> entries = zipFile.entries();
+
+    Set<String> actualEntries = Sets.newHashSet();
+    while (entries.hasMoreElements()) {
+      ZipEntry zipEntry = entries.nextElement();
+      LOG.info("Found zipEntry: " + zipEntry);
+      actualEntries.add(zipEntry.getName());
+    }
+
+    LOG.info("Expected names of ZipEntries: " + expectedNames);
+    LOG.info("Actual names of ZipEntries: " + actualEntries);
+    assertEquals(expectedNames, actualEntries);
+  }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@submarine.apache.org
For additional commands, e-mail: dev-help@submarine.apache.org