You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/27 09:53:38 UTC

[sling-org-apache-sling-feature-analyser] 22/28: [Sling Feature Model] Refactor FeatureUtil out of the support module

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

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

commit b52c8b4a0b5caafe31a363e002b3cc889dda379d
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Wed Apr 25 13:40:18 2018 +0100

    [Sling Feature Model] Refactor FeatureUtil out of the support module
    
    Also move the Resolver API to the resolver module.
---
 pom.xml                                            |   8 +
 .../apache/sling/feature/analyser/main/Main.java   |  12 +-
 .../sling/feature/analyser/AnalyserTest.java       | 117 -----------
 .../feature/analyser/TestBundleResourceImpl.java   | 232 ---------------------
 src/test/resources/feature_complete.json           |  82 --------
 src/test/resources/feature_incomplete.json         |  82 --------
 6 files changed, 14 insertions(+), 519 deletions(-)

diff --git a/pom.xml b/pom.xml
index a2bca4b..c8e69fa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -167,5 +167,13 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <!-- 
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.resolver</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+         -->        
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/feature/analyser/main/Main.java b/src/main/java/org/apache/sling/feature/analyser/main/Main.java
index 7874afa..79cc35b 100644
--- a/src/main/java/org/apache/sling/feature/analyser/main/Main.java
+++ b/src/main/java/org/apache/sling/feature/analyser/main/Main.java
@@ -16,19 +16,19 @@
  */
 package org.apache.sling.feature.analyser.main;
 
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-
 import org.apache.sling.feature.Application;
 import org.apache.sling.feature.analyser.Analyser;
 import org.apache.sling.feature.io.ArtifactManagerConfig;
+import org.apache.sling.feature.io.IOUtils;
 import org.apache.sling.feature.io.json.ApplicationJSONReader;
 import org.apache.sling.feature.scanner.Scanner;
-import org.apache.sling.feature.support.FeatureUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
 public class Main {
 
     public static void main(final String[] args) {
@@ -59,7 +59,7 @@ public class Main {
             System.exit(1);
         }
         if ( app.getFramework() == null ) {
-            app.setFramework(FeatureUtil.getFelixFrameworkId(null));
+            app.setFramework(IOUtils.getFelixFrameworkId(null));
         }
 
         try {
diff --git a/src/test/java/org/apache/sling/feature/analyser/AnalyserTest.java b/src/test/java/org/apache/sling/feature/analyser/AnalyserTest.java
deleted file mode 100644
index 3d14b85..0000000
--- a/src/test/java/org/apache/sling/feature/analyser/AnalyserTest.java
+++ /dev/null
@@ -1,117 +0,0 @@
-/*
- * 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.analyser;
-
-import static junit.framework.TestCase.fail;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.sling.feature.Application;
-import org.apache.sling.feature.Artifact;
-import org.apache.sling.feature.Feature;
-import org.apache.sling.feature.analyser.Analyser;
-import org.apache.sling.feature.io.ArtifactManager;
-import org.apache.sling.feature.io.ArtifactManagerConfig;
-import org.apache.sling.feature.io.json.FeatureJSONReader;
-import org.apache.sling.feature.io.json.FeatureJSONReader.SubstituteVariables;
-import org.apache.sling.feature.scanner.BundleDescriptor;
-import org.apache.sling.feature.scanner.Scanner;
-import org.apache.sling.feature.scanner.impl.BundleDescriptorImpl;
-import org.apache.sling.feature.support.FeatureUtil;
-import org.apache.sling.feature.support.resolver.FeatureResolver;
-import org.apache.sling.feature.support.resolver.FeatureResource;
-import org.junit.Test;
-
-public class AnalyserTest {
-    @Test
-    public void testAnalyserWithCompleteFeature() throws Exception {
-        final Scanner scanner = new Scanner(new ArtifactManagerConfig());
-        final Analyser analyser = new Analyser(scanner);
-        try ( final Reader reader = new InputStreamReader(AnalyserTest.class.getResourceAsStream("/feature_complete.json"),
-                "UTF-8") ) {
-            Feature feature = FeatureJSONReader.read(reader, "feature", SubstituteVariables.RESOLVE);
-
-            Application app = FeatureUtil.assembleApplication(null, ArtifactManager.getArtifactManager(new ArtifactManagerConfig()),
-                    getTestResolver(), feature);
-
-            analyser.analyse(app);
-        }
-    }
-
-    @Test
-    public void testAnalyserWithInCompleteFeature() throws Exception {
-        final Scanner scanner = new Scanner(new ArtifactManagerConfig());
-        final Analyser analyser = new Analyser(scanner);
-        try ( final Reader reader = new InputStreamReader(AnalyserTest.class.getResourceAsStream("/feature_incomplete.json"),
-                "UTF-8") ) {
-            Feature feature = FeatureJSONReader.read(reader, "feature", SubstituteVariables.RESOLVE);
-
-            Application app = FeatureUtil.assembleApplication(null, ArtifactManager.getArtifactManager(new ArtifactManagerConfig()),
-                    getTestResolver(), feature);
-
-            try {
-                analyser.analyse(app);
-
-                fail("Expected an exception");
-            }
-            catch (Exception ex) {
-                // Pass
-            }
-        }
-    }
-
-    private FeatureResolver getTestResolver() {
-        return new FeatureResolver() {
-            @Override
-            public void close() throws Exception {
-            }
-
-            @Override
-            public List<FeatureResource> orderResources(List<Feature> features) {
-                try {
-                    // Just return the resources in the same order as they are listed in the features
-                    List<FeatureResource> l = new ArrayList<>();
-
-                    for (Feature f : features) {
-                        for (Artifact a : f.getBundles()) {
-                            BundleDescriptor bd = getBundleDescriptor(ArtifactManager.getArtifactManager(new ArtifactManagerConfig()), a);
-                            l.add(new TestBundleResourceImpl(bd, f));
-                        }
-                    }
-
-                    return l;
-                } catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
-            }
-
-            private BundleDescriptor getBundleDescriptor(ArtifactManager artifactManager, Artifact b) throws IOException {
-                final File file = artifactManager.getArtifactHandler(b.getId().toMvnUrl()).getFile();
-                if ( file == null ) {
-                    throw new IOException("Unable to find file for " + b.getId());
-                }
-
-                return new BundleDescriptorImpl(b, file, -1);
-            }
-        };
-    }
-}
diff --git a/src/test/java/org/apache/sling/feature/analyser/TestBundleResourceImpl.java b/src/test/java/org/apache/sling/feature/analyser/TestBundleResourceImpl.java
deleted file mode 100644
index a9e4808..0000000
--- a/src/test/java/org/apache/sling/feature/analyser/TestBundleResourceImpl.java
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
- * 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.analyser;
-
-import org.apache.felix.utils.resource.CapabilityImpl;
-import org.apache.felix.utils.resource.RequirementImpl;
-import org.apache.sling.feature.Artifact;
-import org.apache.sling.feature.Feature;
-import org.apache.sling.feature.scanner.BundleDescriptor;
-import org.apache.sling.feature.support.resolver.FeatureResource;
-import org.apache.sling.feature.support.util.PackageInfo;
-import org.osgi.framework.Version;
-import org.osgi.framework.VersionRange;
-import org.osgi.framework.namespace.BundleNamespace;
-import org.osgi.framework.namespace.ExecutionEnvironmentNamespace;
-import org.osgi.framework.namespace.PackageNamespace;
-import org.osgi.resource.Capability;
-import org.osgi.resource.Requirement;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-/**
- * Implementation of the OSGi Resource interface, used by the test
- */
-public class TestBundleResourceImpl implements FeatureResource {
-    final Artifact artifact;
-    final String bsn;
-    final Version version;
-    final Map<String, List<Capability>> capabilities;
-    final Map<String, List<Requirement>> requirements;
-    final Feature feature;
-
-    /**
-     * Create a resource based on a BundleDescriptor.
-     * @param bd The BundleDescriptor to represent.
-     */
-    public TestBundleResourceImpl(BundleDescriptor bd, Feature feat) {
-        artifact = bd.getArtifact();
-        bsn = bd.getBundleSymbolicName();
-        version = bd.getArtifact().getId().getOSGiVersion();
-        feature = feat;
-
-        Map<String, List<Capability>> caps = new HashMap<>();
-        for (Capability c : bd.getCapabilities()) {
-            List<Capability> l = caps.get(c.getNamespace());
-            if (l == null) {
-                l = new ArrayList<>();
-                caps.put(c.getNamespace(), l);
-            }
-            l.add(new CapabilityImpl(this, c));
-        }
-
-        // Add the package capabilities (export package)
-        List<Capability> pkgCaps = new ArrayList<>();
-        for(PackageInfo exported : bd.getExportedPackages()) {
-            Map<String, Object> attrs = new HashMap<>();
-            attrs.put(PackageNamespace.PACKAGE_NAMESPACE, exported.getName());
-            attrs.put(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE, exported.getPackageVersion());
-            attrs.put(PackageNamespace.CAPABILITY_BUNDLE_SYMBOLICNAME_ATTRIBUTE, bd.getBundleSymbolicName());
-            attrs.put(PackageNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, new Version(bd.getBundleVersion()));
-            pkgCaps.add(new CapabilityImpl(this, PackageNamespace.PACKAGE_NAMESPACE, null, attrs));
-        }
-        caps.put(PackageNamespace.PACKAGE_NAMESPACE, Collections.unmodifiableList(pkgCaps));
-
-        // Add the bundle capability
-        Map<String, Object> battrs = new HashMap<>();
-        battrs.put(BundleNamespace.BUNDLE_NAMESPACE, bd.getBundleSymbolicName());
-        battrs.put(BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE, new Version(bd.getBundleVersion()));
-        Capability bundleCap = new CapabilityImpl(this, BundleNamespace.BUNDLE_NAMESPACE, null, battrs);
-        caps.put(BundleNamespace.BUNDLE_NAMESPACE, Collections.singletonList(bundleCap));
-        capabilities = Collections.unmodifiableMap(caps);
-
-        Map<String, List<Requirement>> reqs = new HashMap<>();
-        for (Requirement r : bd.getRequirements()) {
-            List<Requirement> l = reqs.get(r.getNamespace());
-            if (l == null) {
-                l = new ArrayList<>();
-                reqs.put(r.getNamespace(), l);
-            }
-            // Add the requirement and associate with this resource
-            l.add(new RequirementImpl(this, r));
-        }
-
-        // TODO What do we do with the execution environment?
-        reqs.remove(ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE);
-
-        // Add the package requirements (import package)
-        List<Requirement> pkgReqs = new ArrayList<>();
-        for(PackageInfo imported : bd.getImportedPackages()) {
-            Map<String, String> dirs = new HashMap<>();
-            VersionRange range = imported.getPackageVersionRange();
-            String rangeFilter;
-            if (range != null) {
-                rangeFilter = range.toFilterString(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
-            } else {
-                rangeFilter = "";
-            }
-            dirs.put(PackageNamespace.REQUIREMENT_FILTER_DIRECTIVE,
-                "(&(" + PackageNamespace.PACKAGE_NAMESPACE + "=" + imported.getName() + ")" + rangeFilter + ")");
-            if (imported.isOptional())
-                dirs.put(PackageNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE,
-                    PackageNamespace.RESOLUTION_OPTIONAL);
-            pkgReqs.add(new RequirementImpl(this, PackageNamespace.PACKAGE_NAMESPACE, dirs, null));
-        }
-        reqs.put(PackageNamespace.PACKAGE_NAMESPACE, Collections.unmodifiableList(pkgReqs));
-        requirements = Collections.unmodifiableMap(reqs);
-    }
-
-    @Override
-    public Artifact getArtifact() {
-        return artifact;
-    }
-
-    @Override
-    public String getId() {
-        return bsn;
-    }
-
-    @Override
-    public Version getVersion() {
-        return version;
-    }
-
-    @Override
-    public List<Capability> getCapabilities(String namespace) {
-        if (namespace == null) {
-            return capabilities.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
-        }
-
-        List<Capability> caps = capabilities.get(namespace);
-        if (caps == null)
-            return Collections.emptyList();
-        return caps;
-    }
-
-    @Override
-    public List<Requirement> getRequirements(String namespace) {
-        if (namespace == null) {
-            return requirements.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
-        }
-
-        List<Requirement> reqs = requirements.get(namespace);
-        if (reqs == null)
-            return Collections.emptyList();
-        return reqs;
-    }
-
-    @Override
-    public Feature getFeature() {
-        return feature;
-    }
-
-    @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result + ((artifact == null) ? 0 : artifact.hashCode());
-        result = prime * result + ((bsn == null) ? 0 : bsn.hashCode());
-        result = prime * result + ((capabilities == null) ? 0 : capabilities.hashCode());
-        result = prime * result + ((feature == null) ? 0 : feature.hashCode());
-        result = prime * result + ((requirements == null) ? 0 : requirements.hashCode());
-        result = prime * result + ((version == null) ? 0 : version.hashCode());
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj)
-            return true;
-        if (obj == null)
-            return false;
-        if (getClass() != obj.getClass())
-            return false;
-        TestBundleResourceImpl other = (TestBundleResourceImpl) obj;
-        if (artifact == null) {
-            if (other.artifact != null)
-                return false;
-        } else if (!artifact.equals(other.artifact))
-            return false;
-        if (bsn == null) {
-            if (other.bsn != null)
-                return false;
-        } else if (!bsn.equals(other.bsn))
-            return false;
-        if (capabilities == null) {
-            if (other.capabilities != null)
-                return false;
-        } else if (!capabilities.equals(other.capabilities))
-            return false;
-        if (feature == null) {
-            if (other.feature != null)
-                return false;
-        } else if (!feature.equals(other.feature))
-            return false;
-        if (requirements == null) {
-            if (other.requirements != null)
-                return false;
-        } else if (!requirements.equals(other.requirements))
-            return false;
-        if (version == null) {
-            if (other.version != null)
-                return false;
-        } else if (!version.equals(other.version))
-            return false;
-        return true;
-    }
-
-    @Override
-    public String toString() {
-        return "BundleResourceImpl [" + bsn + " " + version + "]";
-    }
-}
diff --git a/src/test/resources/feature_complete.json b/src/test/resources/feature_complete.json
deleted file mode 100644
index 6271a9c..0000000
--- a/src/test/resources/feature_complete.json
+++ /dev/null
@@ -1,82 +0,0 @@
-{
-    "id" : "test/test.complete/0.1",
-
-    "bundles" : [
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.log/5.0.0",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.logservice/1.0.6",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/jcl-over-slf4j/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/log4j-over-slf4j/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/slf4j-api/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.configadmin/1.8.14",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.eventadmin/1.4.8",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.metatype/1.1.2",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.scr/2.0.12",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.http.jetty/3.4.2",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.http.servlet-api/1.1.2",
-          "start-order" : 5
-        },
-        {
-          "id" : "commons-io/commons-io/2.5",
-          "start-order" : 5
-        },
-        {
-          "id" : "commons-fileupload/commons-fileupload/1.3.2",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.inventory/1.0.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.ds/2.0.6",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.event/1.1.6",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.packageadmin/1.0.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole/4.3.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.log.webconsole/1.0.0",
-          "start-order" : 5
-        }
-    ]
-}
diff --git a/src/test/resources/feature_incomplete.json b/src/test/resources/feature_incomplete.json
deleted file mode 100644
index 514e878..0000000
--- a/src/test/resources/feature_incomplete.json
+++ /dev/null
@@ -1,82 +0,0 @@
-{
-    "id" : "test/test.incomplete/0.1",
-
-    "bundles" : [
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.log/5.0.0",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.logservice/1.0.6",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/jcl-over-slf4j/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/log4j-over-slf4j/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.slf4j/slf4j-api/1.7.21",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.configadmin/1.8.14",
-          "start-order" : 1
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.eventadmin/1.4.8",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.metatype/1.1.2",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.scr/2.0.12",
-          "start-order" : 4
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.http.servlet-api/1.1.2",
-          "start-order" : 5
-        },
-        {
-          "id" : "commons-io/commons-io/2.5",
-          "start-order" : 5
-        },
-        {
-          "id" : "commons-fileupload/commons-fileupload/1.3.2",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.inventory/1.0.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.ds/2.0.6",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.event/1.1.6",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole.plugins.packageadmin/1.0.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.felix/org.apache.felix.webconsole/4.3.4",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.sling/org.apache.sling.commons.log.webconsole/1.0.0",
-          "start-order" : 5
-        },
-        {
-          "id" : "org.apache.sling/org.apache.sling.i18n/2.5.8",
-          "start-order" : 6
-        }
-    ]
-}

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.