ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject git commit: AMBARI-8125 - Views: Sometimes view only partially extracts (tbeerbower)
Date Mon, 03 Nov 2014 22:03:19 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 4beab82be -> 1df912537


AMBARI-8125 - Views: Sometimes view only partially extracts (tbeerbower)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/1df91253
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/1df91253
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/1df91253

Branch: refs/heads/trunk
Commit: 1df912537095d4b9de79f904b71681ebef073ef5
Parents: 4beab82
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Mon Nov 3 17:02:45 2014 -0500
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Mon Nov 3 17:03:04 2014 -0500

----------------------------------------------------------------------
 .../ambari/server/view/ViewArchiveUtility.java  | 10 +--
 .../ambari/server/view/ViewExtractor.java       | 95 ++++++++++----------
 .../ambari/server/view/ViewExtractorTest.java   | 60 ++++++-------
 .../ambari/server/view/ViewRegistryTest.java    | 85 +++++++++---------
 4 files changed, 125 insertions(+), 125 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/1df91253/ambari-server/src/main/java/org/apache/ambari/server/view/ViewArchiveUtility.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewArchiveUtility.java
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewArchiveUtility.java
index 950f552..8720d7c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewArchiveUtility.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewArchiveUtility.java
@@ -37,7 +37,7 @@ import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
 
 /**
  * Helper class for basic view archive utility.
@@ -124,14 +124,14 @@ public class ViewArchiveUtility {
   }
 
   /**
-   * Get a new jar file instance from the given file.
+   * Get a new jar file stream from the given file.
    *
    * @param file  the file
    *
-   * @return a new jar file instance
+   * @return a new jar file stream
    */
-  public JarFile getJarFile(File file) throws IOException {
-    return new JarFile(file);
+  public JarInputStream getJarFileStream(File file) throws IOException {
+    return new JarInputStream(new FileInputStream(file));
   }
 
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/1df91253/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExtractor.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExtractor.java
b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExtractor.java
index cd17404..fdce03c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExtractor.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ViewExtractor.java
@@ -25,15 +25,13 @@ import org.slf4j.LoggerFactory;
 import javax.inject.Inject;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.Enumeration;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
 
 /**
  * Extractor for view archives.
@@ -45,6 +43,7 @@ public class ViewExtractor {
    */
   private static final String ARCHIVE_CLASSES_DIR = "WEB-INF/classes";
   private static final String ARCHIVE_LIB_DIR     = "WEB-INF/lib";
+  private static final int    BUFFER_SIZE         = 1024;
 
   @Inject
   ViewArchiveUtility archiveUtility;
@@ -83,60 +82,66 @@ public class ViewExtractor {
         LOG.info(msg);
 
         if (archiveDir.mkdir()) {
-          JarFile viewJarFile = archiveUtility.getJarFile(viewArchive);
-          Enumeration enumeration = viewJarFile.entries();
-
-          msg = "Extracting files from " + viewArchive.getName() + ".";
-
-          view.setStatusDetail(msg);
-          LOG.info(msg);
-
-          while (enumeration.hasMoreElements()) {
-            JarEntry jarEntry  = (JarEntry) enumeration.nextElement();
-            String   entryPath = archivePath + File.separator + jarEntry.getName();
-
-            LOG.debug("Extracting " + entryPath);
-
-            File entryFile = archiveUtility.getFile(entryPath);
-
-            if (jarEntry.isDirectory()) {
+          JarInputStream jarInputStream = archiveUtility.getJarFileStream(viewArchive);
+          try {
+            msg = "Extracting files from " + viewArchive.getName() + ".";
+            view.setStatusDetail(msg);
+            LOG.info(msg);
+
+            // create the META-INF directory
+            File metaInfDir = archiveUtility.getFile(archivePath + File.separator + "META-INF");
+            if (!metaInfDir.mkdir()) {
+              msg = "Could not create archive META-INF directory.";
+
+              view.setStatusDetail(msg);
+              LOG.error(msg);
+              throw new ExtractionException(msg);
+            }
 
-              LOG.debug("Making directory " + entryPath);
+            JarEntry jarEntry;
+            while ((jarEntry = jarInputStream.getNextJarEntry())!= null){
+              try {
+                String   entryPath = archivePath + File.separator + jarEntry.getName();
 
-              if (!entryFile.mkdir()) {
-                msg = "Could not create archive entry directory " + entryPath + ".";
+                LOG.debug("Extracting " + entryPath);
 
-                view.setStatusDetail(msg);
-                LOG.error(msg);
-                throw new ExtractionException(msg);
-              }
-            } else {
+                File entryFile = archiveUtility.getFile(entryPath);
 
-              LOG.debug("Getting input stream for " + jarEntry.getName());
+                if (jarEntry.isDirectory()) {
 
-              InputStream is = viewJarFile.getInputStream(jarEntry);
-              try {
-                LOG.debug("Getting output stream for " + entryPath);
+                  LOG.debug("Making directory " + entryPath);
 
-                FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile);
-                try {
-                  LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath);
+                  if (!entryFile.mkdir()) {
+                    msg = "Could not create archive entry directory " + entryPath + ".";
 
-                  while (is.available() > 0) {
-                    fos.write(is.read());
+                    view.setStatusDetail(msg);
+                    LOG.error(msg);
+                    throw new ExtractionException(msg);
+                  }
+                } else {
+
+                  FileOutputStream fos = archiveUtility.getFileOutputStream(entryFile);
+                  try {
+                    LOG.debug("Begin copying from " + jarEntry.getName() + " to "+ entryPath);
+
+                    byte[] buffer = new byte[BUFFER_SIZE];
+                    int n;
+                    while((n = jarInputStream.read(buffer)) > -1) {
+                      fos.write(buffer, 0, n);
+                    }
+                    LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath);
+
+                  } finally {
+                    fos.flush();
+                    fos.close();
                   }
-
-                  LOG.debug("Finish copying from " + jarEntry.getName() + " to "+ entryPath);
-
-                } finally {
-                  LOG.debug("Closing output stream for " + entryPath);
-                  fos.close();
                 }
               } finally {
-                LOG.debug("Closing input stream for " + jarEntry.getName());
-                is.close();
+                jarInputStream.closeEntry();
               }
             }
+          } finally {
+            jarInputStream.close();
           }
         } else {
           msg = "Could not create archive directory " + archivePath + ".";

http://git-wip-us.apache.org/repos/asf/ambari/blob/1df91253/ambari-server/src/test/java/org/apache/ambari/server/view/ViewExtractorTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewExtractorTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewExtractorTest.java
index b66714e..047c8a3 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewExtractorTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewExtractorTest.java
@@ -33,18 +33,18 @@ import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
 
+import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
+import static org.easymock.EasyMock.eq;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.reset;
@@ -61,20 +61,19 @@ public class ViewExtractorTest {
   private static final File entryFile  = createNiceMock(File.class);
   private static final File classesDir = createNiceMock(File.class);
   private static final File libDir = createNiceMock(File.class);
-  private static final JarFile viewJarFile = createNiceMock(JarFile.class);
+  private static final File metaInfDir = createNiceMock(File.class);
+  private static final JarInputStream viewJarFile = createNiceMock(JarInputStream.class);
   private static final JarEntry jarEntry = createNiceMock(JarEntry.class);
-  private static final InputStream is = createMock(InputStream.class);
   private static final FileOutputStream fos = createMock(FileOutputStream.class);
   private static final Configuration configuration = createNiceMock(Configuration.class);
   private static final File viewDir = createNiceMock(File.class);
-  private static final Enumeration<JarEntry> enumeration = createMock(Enumeration.class);
   private static final File fileEntry = createNiceMock(File.class);
   private static final ViewDAO viewDAO = createMock(ViewDAO.class);
 
   @Before
   public void resetGlobalMocks() {
-    reset(extractedArchiveDir, viewArchive,archiveDir,entryFile, classesDir, libDir, viewJarFile,
-        jarEntry, is, fos, configuration, viewDir, enumeration, fileEntry, viewDAO);
+    reset(extractedArchiveDir, viewArchive,archiveDir,entryFile, classesDir, libDir, metaInfDir,
viewJarFile,
+        jarEntry, fos, configuration, viewDir, fileEntry, viewDAO);
   }
 
   @Test
@@ -101,23 +100,21 @@ public class ViewExtractorTest {
     expect(archiveDir.mkdir()).andReturn(true);
     expect(archiveDir.toURI()).andReturn(new URI("file:./"));
 
-    expect(viewJarFile.entries()).andReturn(enumeration);
-    expect(viewJarFile.getInputStream(jarEntry)).andReturn(is);
+    expect(metaInfDir.mkdir()).andReturn(true);
 
-    expect(enumeration.hasMoreElements()).andReturn(true);
-    expect(enumeration.hasMoreElements()).andReturn(false);
-    expect(enumeration.nextElement()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(null);
 
     expect(jarEntry.getName()).andReturn("view.xml");
 
-    expect(is.available()).andReturn(1);
-    expect(is.available()).andReturn(0);
-
-    expect(is.read()).andReturn(10);
-    fos.write(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(-1);
+    fos.write(anyObject(byte[].class), eq(0), eq(10));
 
+    fos.flush();
     fos.close();
-    is.close();
+    viewJarFile.closeEntry();
+    viewJarFile.close();
 
     expect(classesDir.exists()).andReturn(true);
     expect(classesDir.toURI()).andReturn(new URI("file:./"));
@@ -127,14 +124,14 @@ public class ViewExtractorTest {
     expect(libDir.listFiles()).andReturn(new File[]{fileEntry});
     expect(fileEntry.toURI()).andReturn(new URI("file:./"));
 
-    replay(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, viewJarFile,
-        jarEntry, is, fos, configuration, viewDir, enumeration, fileEntry, viewDAO);
+    replay(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir,
viewJarFile,
+        jarEntry, fos, configuration, viewDir, fileEntry, viewDAO);
 
     ViewExtractor viewExtractor = getViewExtractor(viewDefinition);
     viewExtractor.extractViewArchive(viewDefinition, viewArchive, archiveDir);
 
-    verify(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, viewJarFile,
-        jarEntry, is, fos, configuration, viewDir, enumeration, fileEntry, viewDAO);
+    verify(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir,
viewJarFile,
+        jarEntry, fos, configuration, viewDir, fileEntry, viewDAO);
   }
 
   @Test
@@ -149,15 +146,15 @@ public class ViewExtractorTest {
 
     expect(extractedArchiveDir.exists()).andReturn(true);
 
-    replay(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, viewJarFile,
-        jarEntry, is, fos, configuration, viewDir, enumeration, fileEntry, viewDAO);
+    replay(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir,
viewJarFile,
+        jarEntry, fos, configuration, viewDir, fileEntry, viewDAO);
 
     ViewExtractor viewExtractor = getViewExtractor(viewDefinition);
 
     Assert.assertTrue(viewExtractor.ensureExtractedArchiveDirectory("/var/lib/ambari-server/resources/views/work"));
 
-    verify(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, viewJarFile,
-        jarEntry, is, fos, configuration, viewDir, enumeration, fileEntry, viewDAO);
+    verify(extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir, libDir, metaInfDir,
viewJarFile,
+        jarEntry, fos, configuration, viewDir, fileEntry, viewDAO);
 
     reset(extractedArchiveDir);
 
@@ -198,11 +195,12 @@ public class ViewExtractorTest {
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes",
classesDir);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir);
+    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir);
 
     Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>();
     outputStreams.put(entryFile, fos);
 
-    Map<File, JarFile> jarFiles = new HashMap<File, JarFile>();
+    Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>();
     jarFiles.put(viewArchive, viewJarFile);
 
     TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files,
outputStreams, jarFiles);
@@ -219,10 +217,10 @@ public class ViewExtractorTest {
     private final Map<File, ViewConfig> viewConfigs;
     private final Map<String, File> files;
     private final Map<File, FileOutputStream> outputStreams;
-    private final Map<File, JarFile> jarFiles;
+    private final Map<File, JarInputStream> jarFiles;
 
     public TestViewArchiveUtility(Map<File, ViewConfig> viewConfigs, Map<String,
File> files, Map<File,
-        FileOutputStream> outputStreams, Map<File, JarFile> jarFiles) {
+        FileOutputStream> outputStreams, Map<File, JarInputStream> jarFiles) {
       this.viewConfigs = viewConfigs;
       this.files = files;
       this.outputStreams = outputStreams;
@@ -256,7 +254,7 @@ public class ViewExtractorTest {
     }
 
     @Override
-    public JarFile getJarFile(File file) throws IOException {
+    public JarInputStream getJarFileStream(File file) throws IOException {
       return jarFiles.get(file);
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/1df91253/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
index 1d98b6c..66e2392 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/view/ViewRegistryTest.java
@@ -18,6 +18,7 @@
 
 package org.apache.ambari.server.view;
 
+import static org.easymock.EasyMock.anyObject;
 import static org.easymock.EasyMock.capture;
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.createNiceMock;
@@ -38,7 +39,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -46,7 +46,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
 
 import javax.xml.bind.JAXBException;
 
@@ -188,12 +188,11 @@ public class ViewRegistryTest {
     File entryFile  = createNiceMock(File.class);
     File classesDir = createNiceMock(File.class);
     File libDir = createNiceMock(File.class);
+    File metaInfDir = createNiceMock(File.class);
     File fileEntry = createNiceMock(File.class);
 
-    JarFile viewJarFile = createNiceMock(JarFile.class);
-    Enumeration<JarEntry> enumeration = createMock(Enumeration.class);
+    JarInputStream viewJarFile = createNiceMock(JarInputStream.class);
     JarEntry jarEntry = createNiceMock(JarEntry.class);
-    InputStream is = createMock(InputStream.class);
     FileOutputStream fos = createMock(FileOutputStream.class);
 
     ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity();
@@ -228,11 +227,12 @@ public class ViewRegistryTest {
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes",
classesDir);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir);
+    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir);
 
     Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>();
     outputStreams.put(entryFile, fos);
 
-    Map<File, JarFile> jarFiles = new HashMap<File, JarFile>();
+    Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>();
     jarFiles.put(viewArchive, viewJarFile);
 
     // set expectations
@@ -254,24 +254,22 @@ public class ViewRegistryTest {
     expect(archiveDir.mkdir()).andReturn(true);
     expect(archiveDir.toURI()).andReturn(new URI("file:./"));
 
-    expect(viewJarFile.entries()).andReturn(enumeration);
-    expect(viewJarFile.getInputStream(jarEntry)).andReturn(is);
+    expect(metaInfDir.mkdir()).andReturn(true);
 
-    expect(enumeration.hasMoreElements()).andReturn(true);
-    expect(enumeration.hasMoreElements()).andReturn(false);
-    expect(enumeration.nextElement()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(null);
 
     expect(jarEntry.getName()).andReturn("view.xml");
     expect(jarEntry.isDirectory()).andReturn(false);
 
-    expect(is.available()).andReturn(1);
-    expect(is.available()).andReturn(0);
-
-    expect(is.read()).andReturn(10);
-    fos.write(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(-1);
+    fos.write(anyObject(byte[].class), eq(0), eq(10));
 
+    fos.flush();
     fos.close();
-    is.close();
+    viewJarFile.closeEntry();
+    viewJarFile.close();
 
     expect(extractedArchiveDir.exists()).andReturn(false);
     expect(extractedArchiveDir.mkdir()).andReturn(true);
@@ -290,7 +288,7 @@ public class ViewRegistryTest {
 
     // replay mocks
     replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, resourceDAO, viewDAO,
viewInstanceDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, fos, resourceDAO, viewDAO,
viewInstanceDAO);
 
     TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files,
outputStreams, jarFiles);
 
@@ -320,7 +318,7 @@ public class ViewRegistryTest {
 
     // verify mocks
     verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, resourceDAO, viewDAO,
viewInstanceDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, fos, resourceDAO, viewDAO,
viewInstanceDAO);
   }
 
   @Test
@@ -333,12 +331,11 @@ public class ViewRegistryTest {
     File entryFile  = createNiceMock(File.class);
     File classesDir = createNiceMock(File.class);
     File libDir = createNiceMock(File.class);
+    File metaInfDir = createNiceMock(File.class);
     File fileEntry = createNiceMock(File.class);
 
-    JarFile viewJarFile = createNiceMock(JarFile.class);
-    Enumeration<JarEntry> enumeration = createMock(Enumeration.class);
+    JarInputStream viewJarFile = createNiceMock(JarInputStream.class);
     JarEntry jarEntry = createNiceMock(JarEntry.class);
-    InputStream is = createMock(InputStream.class);
     FileOutputStream fos = createMock(FileOutputStream.class);
 
     ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity();
@@ -369,11 +366,12 @@ public class ViewRegistryTest {
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes",
classesDir);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir);
+    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir);
 
     Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>();
     outputStreams.put(entryFile, fos);
 
-    Map<File, JarFile> jarFiles = new HashMap<File, JarFile>();
+    Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>();
     jarFiles.put(viewArchive, viewJarFile);
 
     // set expectations
@@ -395,24 +393,22 @@ public class ViewRegistryTest {
     expect(archiveDir.mkdir()).andReturn(true);
     expect(archiveDir.toURI()).andReturn(new URI("file:./"));
 
-    expect(viewJarFile.entries()).andReturn(enumeration);
-    expect(viewJarFile.getInputStream(jarEntry)).andReturn(is);
+    expect(metaInfDir.mkdir()).andReturn(true);
 
-    expect(enumeration.hasMoreElements()).andReturn(true);
-    expect(enumeration.hasMoreElements()).andReturn(false);
-    expect(enumeration.nextElement()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(jarEntry);
+    expect(viewJarFile.getNextJarEntry()).andReturn(null);
 
     expect(jarEntry.getName()).andReturn("view.xml");
     expect(jarEntry.isDirectory()).andReturn(false);
 
-    expect(is.available()).andReturn(1);
-    expect(is.available()).andReturn(0);
-
-    expect(is.read()).andReturn(10);
-    fos.write(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(10);
+    expect(viewJarFile.read(anyObject(byte[].class))).andReturn(-1);
+    fos.write(anyObject(byte[].class), eq(0), eq(10));
 
+    fos.flush();
     fos.close();
-    is.close();
+    viewJarFile.closeEntry();
+    viewJarFile.close();
 
     expect(extractedArchiveDir.exists()).andReturn(false);
     expect(extractedArchiveDir.mkdir()).andReturn(true);
@@ -430,7 +426,7 @@ public class ViewRegistryTest {
 
     // replay mocks
     replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, fos, viewDAO);
 
     TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files,
outputStreams, jarFiles);
 
@@ -453,7 +449,7 @@ public class ViewRegistryTest {
 
     // verify mocks
     verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, fos, viewDAO);
   }
 
   @Test
@@ -1015,10 +1011,10 @@ public class ViewRegistryTest {
     File entryFile  = createNiceMock(File.class);
     File classesDir = createNiceMock(File.class);
     File libDir = createNiceMock(File.class);
+    File metaInfDir = createNiceMock(File.class);
     File fileEntry = createNiceMock(File.class);
 
-    JarFile viewJarFile = createNiceMock(JarFile.class);
-    Enumeration<JarEntry> enumeration = createMock(Enumeration.class);
+    JarInputStream viewJarFile = createNiceMock(JarInputStream.class);
     JarEntry jarEntry = createNiceMock(JarEntry.class);
     InputStream is = createMock(InputStream.class);
     FileOutputStream fos = createMock(FileOutputStream.class);
@@ -1053,11 +1049,12 @@ public class ViewRegistryTest {
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes",
classesDir);
     files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir);
+    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir);
 
     Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>();
     outputStreams.put(entryFile, fos);
 
-    Map<File, JarFile> jarFiles = new HashMap<File, JarFile>();
+    Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>();
     jarFiles.put(viewArchive, viewJarFile);
 
     // set expectations
@@ -1080,7 +1077,7 @@ public class ViewRegistryTest {
 
     // replay mocks
     replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewExtractor, resourceDAO,
viewDAO, viewInstanceDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO,
viewDAO, viewInstanceDAO);
 
     TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files,
outputStreams, jarFiles);
 
@@ -1089,17 +1086,17 @@ public class ViewRegistryTest {
 
     // verify mocks
     verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile,
classesDir,
-        libDir, fileEntry, viewJarFile, enumeration, jarEntry, is, fos, viewExtractor, resourceDAO,
viewDAO, viewInstanceDAO);
+        libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO,
viewDAO, viewInstanceDAO);
   }
 
   public static class TestViewArchiveUtility extends ViewArchiveUtility {
     private final Map<File, ViewConfig> viewConfigs;
     private final Map<String, File> files;
     private final Map<File, FileOutputStream> outputStreams;
-    private final Map<File, JarFile> jarFiles;
+    private final Map<File, JarInputStream> jarFiles;
 
     public TestViewArchiveUtility(Map<File, ViewConfig> viewConfigs, Map<String,
File> files, Map<File,
-        FileOutputStream> outputStreams, Map<File, JarFile> jarFiles) {
+        FileOutputStream> outputStreams, Map<File, JarInputStream> jarFiles) {
       this.viewConfigs = viewConfigs;
       this.files = files;
       this.outputStreams = outputStreams;
@@ -1133,7 +1130,7 @@ public class ViewRegistryTest {
     }
 
     @Override
-    public JarFile getJarFile(File file) throws IOException {
+    public JarInputStream getJarFileStream(File file) throws IOException {
       return jarFiles.get(file);
     }
   }


Mime
View raw message