Skip to content

Commit

Permalink
Fix creation of generic-capabilities with multiple versions
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed May 20, 2022
1 parent abf1772 commit 89915de
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,30 @@ protected void addCapability(List<IProvidedCapability> caps, GenericDescription
capAttrs.compute(capNs,
(k, v) -> (v instanceof String) ? v : String.format("%s_%s-%s", iu.getId(), iu.getVersion(), capNo)); //$NON-NLS-1$

for (Version version : getVersions(capAttrs)) {
capAttrs.put(IProvidedCapability.PROPERTY_VERSION, version); // created capability contains a copy
caps.add(MetadataFactory.createProvidedCapability(capNs, capAttrs));
}
}

@SuppressWarnings("unchecked")
private Collection<Version> getVersions(Map<String, Object> capAttrs) {
// Resolve the mandatory p2 version
// By convention versioned OSGi capabilities have a "version" attribute
// containing the OSGi Version object
// If this is not the case use an empty version (e.g. "osgi.ee" has a list of
// versions).
// TODO If present but not a Version log a warning somehow that it is ignored?
// Or fail the publication?
capAttrs.compute(IProvidedCapability.PROPERTY_VERSION,
(k, v) -> (v instanceof Version) ? v : Version.emptyVersion);

caps.add(MetadataFactory.createProvidedCapability(capNs, capAttrs));
Object versionValue = capAttrs.get(IProvidedCapability.PROPERTY_VERSION);
if (versionValue instanceof Version) {
return List.of((Version) versionValue);
}else if (versionValue instanceof Collection
&& ((Collection<?>) versionValue).stream().allMatch(Version.class::isInstance)) {
return (Collection<Version>) versionValue;
} else {
// TODO If present but not a Version log a warning somehow that it is ignored?
// Or fail the publication?
return List.of(Version.emptyVersion);
}
}

private Object convertAttribute(Object attr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipInputStream;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.internal.p2.director.QueryableArray;
import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
import org.eclipse.equinox.internal.p2.metadata.OSGiVersion;
import org.eclipse.equinox.internal.p2.metadata.RequiredCapability;
import org.eclipse.equinox.internal.p2.metadata.TranslationSupport;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
Expand Down Expand Up @@ -511,4 +514,29 @@ public void testPublishBundlesWhereOneBundleIsInvalid() throws Exception {
IPublisherResult.ROOT);
assertThat(ius.size(), is(1));
}

public void testMultiVersionCapability() throws Exception {
File testData = getTestData("dymamicImport", "testData/multiVersionCapability/bundle1");
IInstallableUnit iu = BundlesAction.createBundleIU(BundlesAction.createBundleDescription(testData), null,
new PublisherInfo());
Map<String, List<IProvidedCapability>> namespace2capability = iu.getProvidedCapabilities().stream()
.sorted(Comparator.comparing(IProvidedCapability::getVersion))
.collect(Collectors.groupingBy(IProvidedCapability::getNamespace));

List<IProvidedCapability> list0 = namespace2capability.get("cap0");
assertEquals(1, list0.size());
assertEquals("name0", list0.get(0).getName());
assertEquals(new OSGiVersion(1, 0, 0, ""), list0.get(0).getVersion());

List<IProvidedCapability> list1 = namespace2capability.get("cap1");
assertEquals(1, list1.size());
assertEquals("name1", list1.get(0).getName());
assertEquals(new OSGiVersion(1, 1, 0, ""), list1.get(0).getVersion());

List<IProvidedCapability> list2 = namespace2capability.get("cap2");
assertEquals(2, list2.size());
list2.forEach(c -> assertEquals("name2", c.getName()));
assertEquals(new OSGiVersion(1, 0, 0, ""), list2.get(0).getVersion());
assertEquals(new OSGiVersion(2, 1, 0, ""), list2.get(1).getVersion());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: testMultiVersionCapability
Bundle-Version: 1.0.0.qualifier
Provide-Capability:
cap0;cap0=name0;version:Version="1.0";uses:="some.other",
cap1;cap1=name1;version:List<Version>="1.1";uses:="some.other",
cap2;cap2=name2;version:List<Version>="1,2.1";uses:="some.other"

0 comments on commit 89915de

Please sign in to comment.