/** create a copy of the given ZIP as a JAR with the given entries added at the end, returning the new temp file */ public File copyAddingAtEnd(File f, Map<ZipEntry, ? extends InputStream> entries) { return copyAdding(f, entries, Predicates.<String>alwaysTrue(), true); }
/** create a copy of the given ZIP as a JAR with the given entries added at the end (removing any duplicates), returning the new temp file */ public File copyAdding(File f, Map<ZipEntry, ? extends InputStream> entries) { return copyAdding(f, entries, Predicates.<String>alwaysTrue(), false); }
/** create a copy of the given ZIP as a JAR with the given entries removed, returning the new temp file */ public File copyRemoving(File f, Predicate<? super String> filter) { return copyAdding(f, MutableMap.<ZipEntry,InputStream>of(), filter, true); }
@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"), 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"), false); }
@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"), 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"), false); }
@Test public void testCopyRemovingPredicate() throws Exception { tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of( 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"), false); }
@Test(enabled=false) public void testCopyRemovingItemDir() throws Exception { tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of( 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"), false); }
@Test public void testCopyRemovingItems() throws Exception { tempJar = bundleMaker.copyAdding(emptyJar, ImmutableMap.of( 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"), false); }
static void installJavaScanningMoreEntitiesV2(ManagementContext mgmt, Object context) throws FileNotFoundException { // scanning bundle functionality added in 0.12.0, relatively new compared to non-osgi scanning TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); CampYamlLiteTest.installWithoutCatalogBom(mgmt, OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); BundleMaker bm = new BundleMaker(mgmt); File f = Os.newTempFile(context.getClass(), "jar"); Streams.copy(ResourceUtils.create(context).getResourceFromUrl(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH), new FileOutputStream(f)); f = bm.copyRemoving(f, MutableSet.of("catalog.bom")); f = bm.copyAdding(f, MutableMap.of(new ZipEntry("catalog.bom"), new ByteArrayInputStream( Strings.lines( "brooklyn.catalog:", " scanJavaAnnotations: true").getBytes() ) )); ((ManagementContextInternal)mgmt).getOsgiManager().get().install(new FileInputStream(f)).checkNoError(); }
@Test public void testCatalogBomLoadsFileInBundle() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); // add a file in the bundle String customText = "Sample data "+Identifiers.makeRandomId(4); jf = bm.copyAdding(jf, MutableMap.of( new ZipEntry("sample.txt"), (InputStream) new ByteArrayInputStream(customText.getBytes()))); installBundle(jf); String yaml = Strings.lines("name: simple-app-yaml", "services:", "- type: " + "basic1", " brooklyn.initializers:", " - type: "+GetFileContentsEffector.class.getName()); Entity app = createAndStartApplication(yaml); Entity basic1 = Iterables.getOnlyElement( app.getChildren() ); // check the file put in the bundle gets loaded without needing to do anything special String contents = basic1.invoke(GetFileContentsEffector.GET_FILE_CONTENTS, MutableMap.of(GetFileContentsEffector.FILENAME.getName(), "classpath://sample.txt")).get(); Asserts.assertEquals(contents, customText); }