You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2022/01/18 05:56:50 UTC

[sling-org-apache-sling-feature-extension-apiregions] branch master updated: SLING-11068 split up analyser task for unversioned packages

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-apiregions.git


The following commit(s) were added to refs/heads/master by this push:
     new 7068513  SLING-11068 split up analyser task for unversioned packages
     new 5ab0b39  Merge pull request #17 from royteeuwen/feature/split-up-apiregions-analyser-tasks
7068513 is described below

commit 706851373cc572a148bd97a6f290f37aef1ed254
Author: Roy Teeuwen <ro...@amplexor.com>
AuthorDate: Mon Jan 17 21:28:52 2022 +0100

    SLING-11068 split up analyser task for unversioned packages
---
 .../CheckApiRegionsBundleExportsImports.java       |  52 --------
 .../CheckApiRegionsBundleUnversionedPackages.java  | 134 +++++++++++++++++++++
 ...apache.sling.feature.analyser.task.AnalyserTask |   1 +
 ...eckApiRegionsBundleUnversionedPackagesTest.java |  88 ++++++++++++++
 src/test/resources/test-bundle5.jar                | Bin 0 -> 458 bytes
 5 files changed, 223 insertions(+), 52 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
index 5ce1635..0a5797c 100644
--- a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleExportsImports.java
@@ -53,9 +53,6 @@ public class CheckApiRegionsBundleExportsImports implements AnalyserTask {
     private static final String NO_REGION = " __NO_REGION__ ";
     private static final String OWN_FEATURE = " __OWN_FEATURE__ ";
 
-    /** Ignore JDK packages */
-    private static final List<String> IGNORED_IMPORT_PREFIXES = Arrays.asList("java.", "javax.", "org.w3c.", "org.xml.");
-
     @Override
     public String getName() {
         return "Bundle Import/Export Check";
@@ -68,12 +65,8 @@ public class CheckApiRegionsBundleExportsImports implements AnalyserTask {
 
     public static final class Report {
 
-        public List<PackageInfo> exportWithoutVersion = new ArrayList<>();
-
         public List<PackageInfo> exportMatchingSeveral = new ArrayList<>();
 
-        public List<PackageInfo> importWithoutVersion = new ArrayList<>();
-
         public List<PackageInfo> missingExports = new ArrayList<>();
 
         public List<PackageInfo> missingExportsWithVersion = new ArrayList<>();
@@ -92,48 +85,12 @@ public class CheckApiRegionsBundleExportsImports implements AnalyserTask {
         return report;
     }
 
-    private void checkForVersionOnExportedPackages(final AnalyserTaskContext ctx, final Map<BundleDescriptor, Report> reports) {
-        for(final BundleDescriptor info : ctx.getFeatureDescriptor().getBundleDescriptors()) {
-            if ( info.getExportedPackages() != null ) {
-                for(final PackageInfo i : info.getExportedPackages()) {
-                    if ( i.getPackageVersion().compareTo(Version.emptyVersion) == 0 ) {
-                        getReport(reports, info).exportWithoutVersion.add(i);
-                    }
-                }
-            }
-        }
-    }
-
-    private boolean ignoreImportPackage(final String name) {
-        for(final String prefix : IGNORED_IMPORT_PREFIXES) {
-            if ( name.startsWith(prefix) ) {
-                return true;
-            }
-        }
-        return false;
-    }
-    
-    private void checkForVersionOnImportingPackages(final AnalyserTaskContext ctx, final Map<BundleDescriptor, Report> reports) {
-        for(final BundleDescriptor info : ctx.getFeatureDescriptor().getBundleDescriptors()) {
-            if ( info.getImportedPackages() != null ) {
-                for(final PackageInfo i : info.getImportedPackages()) {
-                    if ( i.getVersion() == null && !ignoreImportPackage(i.getName()) ) {
-                        getReport(reports, info).importWithoutVersion.add(i);
-                    }
-                }
-            }
-        }
-    }
-
     @Override
     public void execute(final AnalyserTaskContext ctx) throws Exception {
         boolean ignoreAPIRegions = ctx.getConfiguration().getOrDefault(
                 IGNORE_API_REGIONS_CONFIG_KEY, "false").equalsIgnoreCase("true");
 
-        // basic checks
         final Map<BundleDescriptor, Report> reports = new HashMap<>();
-        checkForVersionOnExportedPackages(ctx, reports);
-        checkForVersionOnImportingPackages(ctx, reports);
 
         final SortedMap<Integer, List<BundleDescriptor>> bundlesMap = new TreeMap<>();
         for(final BundleDescriptor bi : ctx.getFeatureDescriptor().getBundleDescriptors()) {
@@ -258,15 +215,6 @@ public class CheckApiRegionsBundleExportsImports implements AnalyserTask {
         for(final Map.Entry<BundleDescriptor, Report> entry : reports.entrySet()) {
             final String key = "Bundle " + entry.getKey().getArtifact().getId().getArtifactId() + ":" + entry.getKey().getArtifact().getId().getVersion();
 
-            if ( !entry.getValue().importWithoutVersion.isEmpty() ) {
-                ctx.reportArtifactWarning(entry.getKey().getArtifact().getId(),
-                        key + " is importing package(s) " + getPackageInfo(entry.getValue().importWithoutVersion, false) + " without specifying a version range.");
-            }
-            if ( !entry.getValue().exportWithoutVersion.isEmpty() ) {
-                ctx.reportArtifactWarning(entry.getKey().getArtifact().getId(),
-                        key + " is exporting package(s) " + getPackageInfo(entry.getValue().importWithoutVersion, false) + " without a version.");
-            }
-
             if ( !entry.getValue().missingExports.isEmpty() ) {
                 ctx.reportArtifactError(entry.getKey().getArtifact().getId(),
                         key + " is importing package(s) " + getPackageInfo(entry.getValue().missingExports, false) + " in start level " +
diff --git a/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackages.java b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackages.java
new file mode 100644
index 0000000..affe8cd
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackages.java
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.extension.apiregions.analyser;
+
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.extension.apiregions.api.ApiRegions;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+import org.apache.sling.feature.scanner.PackageInfo;
+import org.osgi.framework.Version;
+
+import java.util.*;
+
+public class CheckApiRegionsBundleUnversionedPackages implements AnalyserTask {
+
+    /** Ignore JDK packages */
+    private static final List<String> IGNORED_IMPORT_PREFIXES = Arrays.asList("java.", "javax.", "org.w3c.", "org.xml.");
+
+    @Override
+    public String getName() {
+        return "Bundle Unversioned Packages Check";
+    }
+
+    @Override
+    public String getId() {
+        return ApiRegions.EXTENSION_NAME + "-unversioned-packages";
+    }
+
+    public static final class Report {
+
+        public List<PackageInfo> exportWithoutVersion = new ArrayList<>();
+
+        public List<PackageInfo> importWithoutVersion = new ArrayList<>();
+
+    }
+
+    private Report getReport(final Map<BundleDescriptor, Report> reports, final BundleDescriptor info) {
+        Report report = reports.get(info);
+        if ( report == null ) {
+            report = new Report();
+            reports.put(info, report);
+        }
+        return report;
+    }
+
+    private void checkForVersionOnExportedPackages(final AnalyserTaskContext ctx, final Map<BundleDescriptor, Report> reports) {
+        for(final BundleDescriptor info : ctx.getFeatureDescriptor().getBundleDescriptors()) {
+            if ( info.getExportedPackages() != null ) {
+                for(final PackageInfo i : info.getExportedPackages()) {
+                    if ( i.getPackageVersion().compareTo(Version.emptyVersion) == 0 ) {
+                        getReport(reports, info).exportWithoutVersion.add(i);
+                    }
+                }
+            }
+        }
+    }
+
+    private boolean ignoreImportPackage(final String name) {
+        for(final String prefix : IGNORED_IMPORT_PREFIXES) {
+            if ( name.startsWith(prefix) ) {
+                return true;
+            }
+        }
+        return false;
+    }
+    
+    private void checkForVersionOnImportingPackages(final AnalyserTaskContext ctx, final Map<BundleDescriptor, Report> reports) {
+        for(final BundleDescriptor info : ctx.getFeatureDescriptor().getBundleDescriptors()) {
+            if ( info.getImportedPackages() != null ) {
+                for(final PackageInfo i : info.getImportedPackages()) {
+                    if ( i.getVersion() == null && !ignoreImportPackage(i.getName()) ) {
+                        getReport(reports, info).importWithoutVersion.add(i);
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public void execute(final AnalyserTaskContext ctx) throws Exception {
+        final Map<BundleDescriptor, Report> reports = new HashMap<>();
+        checkForVersionOnExportedPackages(ctx, reports);
+        checkForVersionOnImportingPackages(ctx, reports);
+
+        for(final Map.Entry<BundleDescriptor, Report> entry : reports.entrySet()) {
+            final String key = "Bundle " + entry.getKey().getArtifact().getId().getArtifactId() + ":" + entry.getKey().getArtifact().getId().getVersion();
+
+            if ( !entry.getValue().importWithoutVersion.isEmpty() ) {
+                ctx.reportArtifactWarning(entry.getKey().getArtifact().getId(),
+                        key + " is importing package(s) " + getPackageInfo(entry.getValue().importWithoutVersion) + " without specifying a version range.");
+            }
+            if ( !entry.getValue().exportWithoutVersion.isEmpty() ) {
+                ctx.reportArtifactWarning(entry.getKey().getArtifact().getId(),
+                        key + " is exporting package(s) " + getPackageInfo(entry.getValue().exportWithoutVersion) + " without a version.");
+            }
+        }
+    }
+
+    private String getPackageInfo(final List<PackageInfo> pcks) {
+        if ( pcks.size() == 1 ) {
+                return pcks.get(0).getName();
+        }
+        final StringBuilder sb = new StringBuilder();
+        boolean first = true;
+        sb.append('[');
+        for(final PackageInfo info : pcks) {
+            if ( first ) {
+                first = false;
+            } else {
+                sb.append(", ");
+            }
+            sb.append(info.getName());
+        }
+        sb.append(']');
+        return sb.toString();
+    }
+
+}
diff --git a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
index 1ed0442..4f80111 100644
--- a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
+++ b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
@@ -3,6 +3,7 @@ org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsDependenci
 org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsDuplicates
 org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsOrder
 org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsBundleExportsImports
+org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsBundleUnversionedPackages
 org.apache.sling.feature.extension.apiregions.analyser.CheckApiRegionsCrossFeatureDups
 org.apache.sling.feature.extension.apiregions.analyser.CheckArtifactRules
 org.apache.sling.feature.extension.apiregions.analyser.CheckConfigurationApi
diff --git a/src/test/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackagesTest.java b/src/test/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackagesTest.java
new file mode 100644
index 0000000..37747c1
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/extension/apiregions/analyser/CheckApiRegionsBundleUnversionedPackagesTest.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.feature.extension.apiregions.analyser;
+
+import org.apache.sling.feature.*;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.extension.apiregions.api.ApiRegions;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+import org.apache.sling.feature.scanner.FeatureDescriptor;
+import org.apache.sling.feature.scanner.impl.BundleDescriptorImpl;
+import org.apache.sling.feature.scanner.impl.FeatureDescriptorImpl;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class CheckApiRegionsBundleUnversionedPackagesTest {
+    private static File resourceRoot;
+
+    @BeforeClass
+    public static void setupClass() {
+        resourceRoot =
+                new File(CheckApiRegionsBundleUnversionedPackagesTest.class.
+                        getResource("/test-framework.jar").getFile()).getParentFile();
+    }
+
+    @Test
+    public void testId() {
+        assertEquals(ApiRegions.EXTENSION_NAME + "-unversioned-packages", new CheckApiRegionsBundleUnversionedPackages().getId());
+    }
+
+    @Test
+    public void testBundleUnversionedPackage() throws Exception {
+        CheckApiRegionsBundleUnversionedPackages t = new CheckApiRegionsBundleUnversionedPackages();
+
+        Feature f = new Feature(ArtifactId.fromMvnId("f:f:1"));
+        f.setComplete(true);
+        FeatureDescriptor fd = new FeatureDescriptorImpl(f);
+
+        fdAddBundle(fd, "g:b1:1", "test-bundle5.jar");
+
+        AnalyserTaskContext ctx = Mockito.mock(AnalyserTaskContext.class);
+        Mockito.when(ctx.getFeature()).thenReturn(f);
+        Mockito.when(ctx.getFeatureDescriptor()).thenReturn(fd);
+        t.execute(ctx);
+
+        Mockito.verify(ctx, Mockito.times(2)).reportArtifactWarning(Mockito.any(), Mockito.anyString());
+
+        Mockito.verify(ctx).reportArtifactWarning(
+                Mockito.eq(ArtifactId.fromMvnId("g:b1:1")),
+                Mockito.contains("org.foo.i"));
+        Mockito.verify(ctx).reportArtifactWarning(
+                Mockito.eq(ArtifactId.fromMvnId("g:b1:1")),
+                Mockito.contains("org.foo.e"));
+    }
+
+
+    private void fdAddBundle(FeatureDescriptor fd, String id, String file, ArtifactId... origins) throws IOException {
+        Artifact artifact = new Artifact(ArtifactId.fromMvnId(id));
+        artifact.setFeatureOrigins(origins);
+        BundleDescriptor bd1 = new BundleDescriptorImpl(
+                artifact, new File(resourceRoot, file).toURI().toURL(), 0);
+        fd.getBundleDescriptors().add(bd1);
+    }
+}
diff --git a/src/test/resources/test-bundle5.jar b/src/test/resources/test-bundle5.jar
new file mode 100644
index 0000000..cb5d2de
Binary files /dev/null and b/src/test/resources/test-bundle5.jar differ