/** 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)); }
@Test public void testCopyAddingManifest() throws Exception { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.2.3"); // needs version, or nothing added to manifest! manifest.getMainAttributes().putValue("mykey", "myval"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); String expectedManifest = "Manifest-Version: 1.2.3\r\n" + "mykey: myval\r\n" + "\r\n"; assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, expectedManifest), false); }
@Test public void testCopyAddingManifestOverwritesExisting() throws Exception { Map<String, String> origManifest = ImmutableMap.of(Attributes.Name.MANIFEST_VERSION.toString(), "4.5.6", "preExistingKey", "preExistingVal"); tempJar = bundleMaker.copyAddingManifest(emptyJar, origManifest); Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.2.3"); manifest.getMainAttributes().putValue("mykey", "myval"); generatedJar = bundleMaker.copyAddingManifest(tempJar, manifest); String expectedManifest = "Manifest-Version: 1.2.3\r\n" + "mykey: myval\r\n" + "\r\n"; assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, expectedManifest), false); }
@Test public void testCopyAddingManifestByMap() throws Exception { Map<String, String> manifest = ImmutableMap.of(Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", "mykey", "myval"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); String expectedManifest = "Manifest-Version: 1.2.3\r\n" + "mykey: myval\r\n" + "\r\n"; assertJarContents(generatedJar, ImmutableMap.of(JarFile.MANIFEST_NAME, expectedManifest), false); }
File zf2 = new BundleMaker(mgmt()).copyAddingManifest(zipFile, discoveredManifest); zipFile.delete(); zipFile = zf2;
@Test public void testHasOsgiManifestWhenValidManifest() throws Exception { Map<String, String> manifest = ImmutableMap.of(Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", Constants.BUNDLE_SYMBOLICNAME, "myname"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); assertTrue(bundleMaker.hasOsgiManifest(generatedJar)); }
@Test public void testHasOsgiManifestWhenInvalidManifest() throws Exception { Map<String, String> manifest = ImmutableMap.of(Attributes.Name.MANIFEST_VERSION.toString(), "1.2.3", "mykey", "myval"); generatedJar = bundleMaker.copyAddingManifest(emptyJar, manifest); assertFalse(bundleMaker.hasOsgiManifest(generatedJar)); }
public static void addCatalogItemsAsOsgiInUsualWay(ManagementContext mgmt, String catalogYaml, VersionedName bundleName, boolean force) { try { BundleMaker bundleMaker = new BundleMaker(mgmt); File bf = bundleMaker.createTempZip("test", MutableMap.of( new ZipEntry(BasicBrooklynCatalog.CATALOG_BOM), new ByteArrayInputStream(catalogYaml.getBytes()))); if (bundleName!=null) { bf = bundleMaker.copyAddingManifest(bf, MutableMap.of( "Manifest-Version", "2.0", Constants.BUNDLE_SYMBOLICNAME, bundleName.getSymbolicName(), Constants.BUNDLE_VERSION, bundleName.getOsgiVersion().toString())); } ReferenceWithError<OsgiBundleInstallationResult> b = ((ManagementContextInternal)mgmt).getOsgiManager().get().install( new FileInputStream(bf) ); b.checkNoError(); } catch (Exception e) { throw Exceptions.propagate(e); } }
@SuppressWarnings("deprecation") @Test public void testInstallBundle() 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.copyAddingManifest(emptyJar, manifest); Bundle bundle = bundleMaker.installBundle(generatedJar, false); assertEquals(bundle.getSymbolicName(), "myname"); assertEquals(bundle.getVersion(), new Version("4.5.6")); // Confirm it really is installed in the management context's OSGi framework Bundle bundle2 = Osgis.bundleFinder(mgmt.getOsgiManager().get().getFramework()) .symbolicName("myname") .version("4.5.6") .findUnique() .get(); assertEquals(bundle2, bundle); }
@Test public void testCatalogBomFromBundleWithManualManifest() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); jf = bm.copyRemoving(jf, MutableSet.of(JarFile.MANIFEST_NAME)); String customName = "catalog-bundle-1-manual-"+Identifiers.makeRandomId(4); jf = bm.copyAddingManifest(jf, MutableMap.of( "Manifest-Version", "2.0", "Bundle-SymbolicName", customName, "Bundle-Version", "0.0.0.SNAPSHOT")); Assert.assertTrue(bm.hasOsgiManifest(jf)); installBundle(jf); assertHasBasic1(); Entity basic1 = assertBasic1DeploysAndHasSensor(); RegisteredType item = mgmt().getTypeRegistry().get( basic1.getCatalogItemId() ); Collection<OsgiBundleWithUrl> libs = item.getLibraries(); Asserts.assertSize(libs, 1); Assert.assertEquals(Iterables.getOnlyElement(libs).getSymbolicName(), customName); }