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:57:22 UTC

[sling-org-apache-sling-feature-applicationbuilder] 06/21: SLING-7521 Order bundles in the generated app based on feature order and start order

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-applicationbuilder.git

commit efcb9d351f28430fb9cd8b0c9acd9373c6a11417
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Mon Mar 5 15:05:23 2018 +0000

    SLING-7521 Order bundles in the generated app based on feature order and start order
    
    Order resource (bundles and features) in the resulting application based on the order
    of resolved features and then also in the order of the start order within the feature.
---
 pom.xml                                            |  31 ++++-
 .../impl/ApplicationBuilderTest.java               | 144 +++++++++++++++++++++
 src/test/resources/featureA.json                   |   5 +
 src/test/resources/featureB.json                   |  14 ++
 src/test/resources/featureC.json                   |  18 +++
 src/test/resources/featureD.json                   |   5 +
 6 files changed, 214 insertions(+), 3 deletions(-)

diff --git a/pom.xml b/pom.xml
index 9f0d02f..917e103 100644
--- a/pom.xml
+++ b/pom.xml
@@ -132,11 +132,36 @@
             <version>0.0.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.resolver</artifactId>
+            <version>1.0.1</version>
+            <scope>provided</scope>
+        </dependency>
                 
-      <!-- Testing -->
+        <!-- Testing -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
-        	<groupId>junit</groupId>
-        	<artifactId>junit</artifactId>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>5.6.10</version>
+            <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.feature.analyser</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>        
     </dependencies>
 </project>
diff --git a/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java b/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
new file mode 100644
index 0000000..414d426
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/applicationbuilder/impl/ApplicationBuilderTest.java
@@ -0,0 +1,144 @@
+/*
+ * 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.applicationbuilder.impl;
+
+import org.apache.sling.feature.Application;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.process.ApplicationBuilder;
+import org.apache.sling.feature.process.BuilderContext;
+import org.apache.sling.feature.process.FeatureProvider;
+import org.apache.sling.feature.process.FeatureResolver;
+import org.apache.sling.feature.resolver.FrameworkResolver;
+import org.apache.sling.feature.support.ArtifactHandler;
+import org.apache.sling.feature.support.ArtifactManager;
+import org.apache.sling.feature.support.ArtifactManagerConfig;
+import org.apache.sling.feature.support.json.ApplicationJSONWriter;
+import org.apache.sling.feature.support.json.FeatureJSONReader;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Constants;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class ApplicationBuilderTest {
+    private Path tempDir;
+
+    @Before
+    public void setup() throws Exception {
+        tempDir = Files.createTempDirectory(getClass().getSimpleName());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        // Delete the temp dir again
+        Files.walk(tempDir)
+            .sorted(Comparator.reverseOrder())
+            .map(Path::toFile)
+            .forEach(File::delete);
+    }
+
+    private Map<String, String> getFrameworkProps() {
+        return Collections.singletonMap(Constants.FRAMEWORK_STORAGE, tempDir.toFile().getAbsolutePath());
+    }
+
+    @Test
+    public void testBundleOrdering() throws Exception {
+        FeatureProvider fp = new TestFeatureProvider();
+        BuilderContext bc = new BuilderContext(fp);
+        ArtifactManager am = ArtifactManager.getArtifactManager(new ArtifactManagerConfig());
+
+        Feature fa = readFeature("/featureA.json", am);
+        Feature fb = readFeature("/featureB.json", am);
+        Feature[] features = {fa, fb};
+
+        try (FeatureResolver fr = new FrameworkResolver(am, getFrameworkProps())) {
+            Application app = ApplicationBuilder.assemble(null, bc, fr, features);
+            String genApp = writeApplication(app);
+
+            String expected = "{\"features\":["
+                    + "\"org.apache.sling.test.features:featureB:1.0.0\","
+                    + "\"org.apache.sling.test.features:featureA:1.0.0\"],"
+                + "\"bundles\":["
+                    + "{\"id\":\"commons-io:commons-io:2.6\",\"start-order\":\"10\"},"
+                    + "{\"id\":\"org.apache.felix:org.apache.felix.http.servlet-api:1.1.2\",\"start-order\":\"15\"},"
+                    + "{\"id\":\"commons-fileupload:commons-fileupload:1.3.3\",\"start-order\":\"16\"}]}";
+            assertEquals(expected, genApp);
+        }
+    }
+
+    @Test
+    public void testFeatureDependency() throws Exception {
+        FeatureProvider fp = new TestFeatureProvider();
+        BuilderContext bc = new BuilderContext(fp);
+        ArtifactManager am = ArtifactManager.getArtifactManager(new ArtifactManagerConfig());
+
+        // Feature D has a bundle (slf4j-api) with a dependency on feature C,
+        // which provides a package for slf4j
+        Feature fc = readFeature("/featureC.json", am);
+        Feature fd = readFeature("/featureD.json", am);
+        Feature[] features = {fd, fc};
+
+        try (FeatureResolver fr = new FrameworkResolver(am, getFrameworkProps())) {
+            Application app = ApplicationBuilder.assemble(null, bc, fr, features);
+            String genApp = writeApplication(app);
+
+            String expected = "{\"features\":["
+                    + "\"org.apache.sling.test.features:featureC:1.0.0\","
+                    + "\"org.apache.sling.test.features:featureD:1.0.0\"],"
+                    + "\"bundles\":[{\"id\":\"org.slf4j:slf4j-api:1.7.25\",\"start-order\":\"6\"}]}";
+            assertEquals(expected, genApp);
+        }
+    }
+
+    private static String writeApplication(Application app) throws Exception {
+        Writer writer = new StringWriter();
+        ApplicationJSONWriter.write(writer, app);
+        return writer.toString();
+    }
+
+    private Feature readFeature(final String res,
+            final ArtifactManager artifactManager) throws Exception {
+        URL url = getClass().getResource(res);
+        String file = new File(url.toURI()).getAbsolutePath();
+        final ArtifactHandler featureArtifact = artifactManager.getArtifactHandler(file);
+
+        try (final FileReader r = new FileReader(featureArtifact.getFile())) {
+            final Feature f = FeatureJSONReader.read(r, featureArtifact.getUrl());
+            return f;
+        }
+    }
+
+    private static class TestFeatureProvider implements FeatureProvider {
+        @Override
+        public Feature provide(ArtifactId id) {
+            return null;
+        }
+    }
+}
diff --git a/src/test/resources/featureA.json b/src/test/resources/featureA.json
new file mode 100644
index 0000000..1daed33
--- /dev/null
+++ b/src/test/resources/featureA.json
@@ -0,0 +1,5 @@
+{
+    "id": "org.apache.sling.test.features/featureA/1.0.0",
+    "bundles": 
+        ["commons-fileupload/commons-fileupload/1.3.3"]
+}
\ No newline at end of file
diff --git a/src/test/resources/featureB.json b/src/test/resources/featureB.json
new file mode 100644
index 0000000..952c810
--- /dev/null
+++ b/src/test/resources/featureB.json
@@ -0,0 +1,14 @@
+{
+    "id": "org.apache.sling.test.features/featureB/1.0.0",
+    "bundles": 
+        [
+            {
+                "id": "org.apache.felix/org.apache.felix.http.servlet-api/1.1.2",
+                "start-order" : 10
+            },
+            {
+                "id": "commons-io/commons-io/2.6",
+                "start-order" : 5
+            }
+        ]
+}
\ No newline at end of file
diff --git a/src/test/resources/featureC.json b/src/test/resources/featureC.json
new file mode 100644
index 0000000..0aed875
--- /dev/null
+++ b/src/test/resources/featureC.json
@@ -0,0 +1,18 @@
+{
+    "id": "org.apache.sling.test.features/featureC/1.0.0",
+    "capabilities": [
+        {
+            "namespace": "org.foo.bar",
+            "attributes": {
+                "org.foo.bar": "toast",
+                "version:Version": "1.1"
+            }
+        }, {
+            "namespace": "osgi.wiring.package",
+            "attributes": {
+                "osgi.wiring.package": "org.slf4j.impl",
+                "version:Version": "1.7.1.test"
+            }
+        }
+    ]
+}
\ No newline at end of file
diff --git a/src/test/resources/featureD.json b/src/test/resources/featureD.json
new file mode 100644
index 0000000..e9aa806
--- /dev/null
+++ b/src/test/resources/featureD.json
@@ -0,0 +1,5 @@
+{
+    "id": "org.apache.sling.test.features/featureD/1.0.0",
+    "bundles": 
+        ["org.slf4j/slf4j-api/1.7.25"]
+}
\ No newline at end of file

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