Repository: brooklyn-server
Updated Branches:
  refs/heads/master fe312927d -> c02b3e355


add routine to create new temp bundle


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/97b50e4c
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/97b50e4c
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/97b50e4c

Branch: refs/heads/master
Commit: 97b50e4c304f0886f96a7cd503f1d634d1fbd9c4
Parents: 701a1d7
Author: Alex Heneveld <[email protected]>
Authored: Mon Jun 26 12:49:38 2017 +0100
Committer: Alex Heneveld <[email protected]>
Committed: Tue Jun 27 11:13:00 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/util/core/osgi/BundleMaker.java    | 27 ++++++-
 .../util/core/osgi/BundleMakerTest.java         | 78 ++++++++++++--------
 2 files changed, 73 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/97b50e4c/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java 
b/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
index b590cee..ebcc220 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/osgi/BundleMaker.java
@@ -144,11 +144,15 @@ public class BundleMaker {
     
     /** as {@link #copyAddingManifest(File, Manifest)} but taking manifest 
entries as a map for convenience */
     public File copyAddingManifest(File f, Map<String,String> attrs) {
+        return copyAddingManifest(f, manifestOf(attrs));
+    }
+
+    protected Manifest manifestOf(Map<String, String> attrs) {
         Manifest mf = new Manifest();
         for (Map.Entry<String,String> attr: attrs.entrySet()) {
             mf.getMainAttributes().putValue(attr.getKey(), attr.getValue());
         }
-        return copyAddingManifest(f, mf);
+        return mf;
     }
     
     /** create a copy of the given ZIP as a JAR with the given manifest, 
returning the new temp file */
@@ -350,5 +354,26 @@ public class BundleMaker {
         Streams.closeQuietly(itemFound);
         zout.closeEntry();
     }
+
+    /** Creates a temporary file with the given metadata */ 
+    public File createTempBundle(String nameHint, Manifest mf, Map<ZipEntry, 
InputStream> files) {
+        File f2 = Os.newTempFile(nameHint, "zip");
+        ZipOutputStream zout = null;
+        ZipFile zf = null;
+        try {
+            zout = new JarOutputStream(new FileOutputStream(f2), mf);
+            writeZipEntries(zout, files);
+        } catch (IOException e) {
+            throw Exceptions.propagate("Unable to read/write for "+nameHint, 
e);
+        } finally {
+            Streams.closeQuietly(zf);
+            Streams.closeQuietly(zout);
+        }
+        return f2;
+    }
+
+    public File createTempBundle(String nameHint, Map<String, String> mf, 
Map<ZipEntry, InputStream> files) {
+        return createTempBundle(nameHint, manifestOf(mf), files);
+    }
     
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/97b50e4c/core/src/test/java/org/apache/brooklyn/util/core/osgi/BundleMakerTest.java
----------------------------------------------------------------------
diff --git 
a/core/src/test/java/org/apache/brooklyn/util/core/osgi/BundleMakerTest.java 
b/core/src/test/java/org/apache/brooklyn/util/core/osgi/BundleMakerTest.java
index 515ab2c..f9c1737 100644
--- a/core/src/test/java/org/apache/brooklyn/util/core/osgi/BundleMakerTest.java
+++ b/core/src/test/java/org/apache/brooklyn/util/core/osgi/BundleMakerTest.java
@@ -26,8 +26,7 @@ import static org.apache.brooklyn.test.Asserts.assertTrue;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.util.Enumeration;
-import java.util.List;
+import java.util.Collections;
 import java.util.Map;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
@@ -38,6 +37,8 @@ import java.util.zip.ZipOutputStream;
 
 import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
+import org.apache.brooklyn.test.Asserts;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.os.Os;
 import org.apache.brooklyn.util.stream.Streams;
 import org.osgi.framework.Bundle;
@@ -50,7 +51,6 @@ import org.testng.annotations.Test;
 import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Lists;
 
 public class BundleMakerTest extends BrooklynMgmtUnitTestSupport {
 
@@ -81,21 +81,21 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
     @Test
     public void testCopyAdding() throws Exception {
         generatedJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of(new 
ZipEntry("myfile.txt"), new ByteArrayInputStream("mytext".getBytes())));
-        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"), false);
     }
     
     @Test
     public void testCopyAddingToNonEmpty() throws Exception {
         tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of(new 
ZipEntry("preExisting.txt"), new 
ByteArrayInputStream("myPreExisting".getBytes())));
         generatedJar = bundleMaker.copyAdding(tempJar, ImmutableMap.of(new 
ZipEntry("myfile.txt"), new ByteArrayInputStream("mytext".getBytes())));
-        assertJarContents(generatedJar, ImmutableMap.of("preExisting.txt", 
"myPreExisting", "myfile.txt", "mytext"));
+        assertJarContents(generatedJar, ImmutableMap.of("preExisting.txt", 
"myPreExisting", "myfile.txt", "mytext"), false);
     }
     
     @Test
     public void testCopyAddingOverwritesEntry() throws Exception {
         tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of(new 
ZipEntry("myfile.txt"), new ByteArrayInputStream("myPreExisting".getBytes())));
         generatedJar = bundleMaker.copyAdding(tempJar, ImmutableMap.of(new 
ZipEntry("myfile.txt"), new ByteArrayInputStream("mytext".getBytes())));
-        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"), false);
     }
     
     @Test
@@ -109,7 +109,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 "Manifest-Version: 1.2.3\r\n" + 
                 "mykey: myval\r\n" +
                 "\r\n";
-        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest));
+        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest), false);
     }
     
     @Test
@@ -121,7 +121,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 "Manifest-Version: 1.2.3\r\n" + 
                 "mykey: myval\r\n" +
                 "\r\n";
-        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest));
+        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest), false);
     }
     
     @Test
@@ -138,7 +138,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 "Manifest-Version: 1.2.3\r\n" + 
                 "mykey: myval\r\n" +
                 "\r\n";
-        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest));
+        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest), false);
     }
     
     @Test
@@ -147,7 +147,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 new ZipEntry("myfile.txt"), new 
ByteArrayInputStream("mytext".getBytes()),
                 new ZipEntry("myfile2.txt"), new 
ByteArrayInputStream("mytext2".getBytes())));
         generatedJar = bundleMaker.copyRemoving(tempJar, 
Predicates.equalTo("myfile.txt"));
-        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"), false);
     }
     
     @Test
@@ -156,7 +156,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 new ZipEntry("myfile.txt"), new 
ByteArrayInputStream("mytext".getBytes()),
                 new ZipEntry("myfile2.txt"), new 
ByteArrayInputStream("mytext2".getBytes())));
         generatedJar = bundleMaker.copyRemoving(tempJar, 
ImmutableSet.of("myfile.txt"));
-        assertJarContents(generatedJar, ImmutableMap.of("myfile2.txt", 
"mytext2"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile2.txt", 
"mytext2"), false);
     }
     
     // TODO Not supported - can't remove an entire directory like this
@@ -166,14 +166,14 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 new ZipEntry("mydir/myfile.txt"), new 
ByteArrayInputStream("mytext".getBytes()),
                 new ZipEntry("mydir2/myfile2.txt"), new 
ByteArrayInputStream("mytext2".getBytes())));
         generatedJar = bundleMaker.copyRemoving(tempJar, 
ImmutableSet.of("mydir"));
-        assertJarContents(generatedJar, ImmutableMap.of("mydir2/myfile2.txt", 
"mytext2"));
+        assertJarContents(generatedJar, ImmutableMap.of("mydir2/myfile2.txt", 
"mytext2"), false);
     }
     
     @Test
     public void testCopyRemovingItemsUnmatched() throws Exception {
         tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of(new 
ZipEntry("myfile.txt"), new ByteArrayInputStream("mytext".getBytes())));
         generatedJar = bundleMaker.copyRemoving(tempJar, 
ImmutableSet.of("wrong.txt"));
-        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"), false);
     }
     
     @Test
@@ -198,7 +198,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
     @Test
     public void testCreateJarFromClasspathDirNoManifest() throws Exception {
         generatedJar = 
bundleMaker.createJarFromClasspathDir("/org/apache/brooklyn/util/core/osgi/test/bundlemaker/nomanifest");
-        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext", "subdir/myfile2.txt", "mytext2"));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext", "subdir/myfile2.txt", "mytext2"), false);
     }
     
     @Test
@@ -209,7 +209,7 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 "Manifest-Version: 1.2.3\r\n" + 
                 "mykey: myval\r\n" +
                 "\r\n";
-        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest, "myfile.txt", "mytext", "subdir/myfile2.txt", "mytext2"));
+        assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, 
expectedManifest, "myfile.txt", "mytext", "subdir/myfile2.txt", "mytext2"), 
false);
     }
     
     @SuppressWarnings("deprecation")
@@ -233,6 +233,23 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
                 .get();
         assertEquals(bundle2, bundle);
     }
+    
+    @Test
+    public void testCreate() throws Exception {
+        Map<String, String> manifest = ImmutableMap.of(
+            Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", 
+            Constants.BUNDLE_VERSION, "4.5.6",
+            Constants.BUNDLE_SYMBOLICNAME, "myname");
+        
+        generatedJar = bundleMaker.createTempBundle("test", manifest,
+            ImmutableMap.of(new ZipEntry("myfile.txt"), new 
ByteArrayInputStream("mytext".getBytes())));
+        assertJarContents(generatedJar, ImmutableMap.of("myfile.txt", 
"mytext"), true);
+        
+        @SuppressWarnings("deprecation")
+        Bundle bundle = bundleMaker.installBundle(generatedJar, false);
+        assertEquals(bundle.getSymbolicName(), "myname");
+        assertEquals(bundle.getVersion(), new Version("4.5.6"));
+    }
 
     private File createEmptyJarFile() throws Exception {
         File result = Os.newTempFile("base", "jar");
@@ -241,29 +258,28 @@ public class BundleMakerTest extends 
BrooklynMgmtUnitTestSupport {
         return result;
     }
     
-    private void assertJarContents(File f, Map<String, String> 
expectedContents) throws Exception {
+    private void assertJarContents(File f, Map<String, String> 
expectedContents, boolean othersAllowed) throws Exception {
         ZipFile zipFile = new ZipFile(f);
-        String zipEntriesMsg = "entries="+enumerationToList(zipFile.entries());
+        expectedContents = MutableMap.copyOf(expectedContents);
         try {
-            for (Map.Entry<String, String> entry : 
expectedContents.entrySet()) {
-                ZipEntry zipEntry = zipFile.getEntry(entry.getKey());
-                assertNotNull(zipEntry, "No entry for "+entry.getKey()+"; 
"+zipEntriesMsg);
-                String entryContents = 
Streams.readFullyString(zipFile.getInputStream(zipEntry));
-                assertEquals(entryContents, entry.getValue());
+            for (ZipEntry zipEntry: Collections.list(zipFile.entries())) {
+                String expectedContent = 
expectedContents.remove(zipEntry.getName());
+                if (expectedContent==null) {
+                    if (!othersAllowed) {
+                        assertNotNull(expectedContent, "Unexpected item in 
ZIP: "+zipEntry.getName());
+                    }
+                } else {
+                    String entryContents = 
Streams.readFullyString(zipFile.getInputStream(zipEntry));
+                    assertEquals(entryContents, expectedContent, "Contents not 
eas expectd for "+zipEntry.getName());
+                }
+            }
+            if (!expectedContents.isEmpty()) {
+                Asserts.fail("ZIP did not contain expected contents: 
"+expectedContents.keySet());
             }
-            assertEquals(zipFile.size(), expectedContents.size(), 
zipEntriesMsg);
             
         } finally {
             zipFile.close();
         }
     }
     
-    private <T> List<T> enumerationToList(Enumeration<T> e) {
-        List<T> result = Lists.newArrayList();
-        while (e.hasMoreElements()) {
-            result.add(e.nextElement());
-        }
-        return result;
-        
-    }
 }

Reply via email to