This is an automated email from the ASF dual-hosted git repository. pauls pushed a commit to branch issues/SLING-11075 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git
commit 156351108f58111909c21b96adf0224fe5bf67a4 Author: Karl Pauls <[email protected]> AuthorDate: Wed Jan 19 13:06:24 2022 +0100 SLING-11075: strip attributes from bsn --- .../cpconverter/handlers/BundleEntryHandler.java | 22 +++++++++++++++++---- .../handlers/AbstractBundleEntryHandlerTest.java | 1 + .../handlers/BundleEntryHandlerGAVTest.java | 13 ++++++++++++ .../feature/cpconverter/handlers/peaberry.jar | Bin 0 -> 193114 bytes 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java b/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java index 4412fea..812edf7 100644 --- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java +++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java @@ -236,7 +236,7 @@ public class BundleEntryHandler extends AbstractRegexEntryHandler { if (groupId == null) { // maybe the included jar is just an OSGi bundle but not a valid Maven artifact - groupId = getCheckedProperty(jarFile.getManifest(), Constants.BUNDLE_SYMBOLICNAME); + groupId = getName(getCheckedProperty(jarFile.getManifest(), Constants.BUNDLE_SYMBOLICNAME)); // Make sure there are not spaces in the name to adhere to the Maven Group Id specification groupId = groupId.replace(' ', '_').replace(':', '_').replace('/', '_').replace('\\', '_'); if (groupId.indexOf('.') != -1) { @@ -252,19 +252,33 @@ public class BundleEntryHandler extends AbstractRegexEntryHandler { // create artifact and store symbolic name and version in metadata final Artifact result = new Artifact(new ArtifactId(groupId, artifactId, version, classifier, JAR_TYPE)); - setMetadataFromManifest(jarFile.getManifest(), Constants.BUNDLE_VERSION, result); - setMetadataFromManifest(jarFile.getManifest(), Constants.BUNDLE_SYMBOLICNAME, result); + setMetadataFromManifest(jarFile.getManifest(), Constants.BUNDLE_VERSION, result, false); + setMetadataFromManifest(jarFile.getManifest(), Constants.BUNDLE_SYMBOLICNAME, result, true); return result; } - private static void setMetadataFromManifest(@NotNull Manifest manifest, @NotNull String name, @NotNull Artifact artifact) { + private static void setMetadataFromManifest(@NotNull Manifest manifest, @NotNull String name, @NotNull Artifact artifact, boolean strip) { String value = manifest.getMainAttributes().getValue(name); + if (strip) { + value = getName(value); + } if (value != null) { artifact.getMetadata().put(name, value); } } + private static @Nullable String getName(@Nullable String headerValue) { + if (headerValue == null) { + return null; + } + int idx = headerValue.indexOf(';'); + if (idx != -1) { + return headerValue.substring(0, idx); + } + return headerValue; + } + private static @NotNull String getCheckedProperty(@NotNull Manifest manifest, @NotNull String name) { String property = manifest.getMainAttributes().getValue(name); if (property != null) { diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/AbstractBundleEntryHandlerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/AbstractBundleEntryHandlerTest.java index a4b1162..edd539f 100644 --- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/AbstractBundleEntryHandlerTest.java +++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/AbstractBundleEntryHandlerTest.java @@ -60,6 +60,7 @@ public abstract class AbstractBundleEntryHandlerTest { @Before public void setUp() throws IOException { handler = new BundleEntryHandler(); + handler.setSlingInitialContentPolicy(ContentPackage2FeatureModelConverter.SlingInitialContentPolicy.KEEP); ArtifactsDeployer deployer = Mockito.spy(ArtifactsDeployer.class); when(converter.getArtifactsDeployer()).thenReturn(deployer); when(converter.getTempDirectory()).thenReturn(tmpFolder.getRoot()); diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerGAVTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerGAVTest.java index 820e737..c013f80 100644 --- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerGAVTest.java +++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerGAVTest.java @@ -74,6 +74,19 @@ public class BundleEntryHandlerGAVTest extends AbstractBundleEntryHandlerTest { } @Test + public void testNoGAVNoMeta() throws Exception { + setUpArchive("/jcr_root/apps/gav/install/peaberry.jar", "peaberry.jar"); + handler.handle("/jcr_root/apps/gav/install/peaberry.jar", archive, entry, converter); + ArgumentCaptor<Artifact> captor = ArgumentCaptor.forClass(Artifact.class); + Mockito.verify(featuresManager).addArtifact(Mockito.isNull(), captor.capture(), Mockito.isNull()); + final Artifact result = captor.getValue(); + assertNotNull(result); + assertEquals(ArtifactId.fromMvnId("org.ops4j:peaberry:jar:1.3.0"), result.getId()); + assertEquals("org.ops4j.peaberry", result.getMetadata().get(Constants.BUNDLE_SYMBOLICNAME)); + assertEquals("1.3.0", result.getMetadata().get(Constants.BUNDLE_VERSION)); + } + + @Test public void testBundleBelowConfigFolderWithEnforcement() throws Exception { handler.setEnforceBundlesBelowInstallFolder(true); when(entry.getName()).thenReturn("mybundle.jar"); diff --git a/src/test/resources/org/apache/sling/feature/cpconverter/handlers/peaberry.jar b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/peaberry.jar new file mode 100644 index 0000000..eb6b5c0 Binary files /dev/null and b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/peaberry.jar differ
