You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:51 UTC

[sling-org-apache-sling-provisioning-model] 04/07: SLING-6216 : Merge versioned features by using highest version

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

rombert pushed a commit to annotated tag org.apache.sling.provisioning.model-1.6.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-provisioning-model.git

commit f12e1de3fc910e00aef32cd03b922fc82c88b2c9
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sun Oct 30 11:00:33 2016 +0000

    SLING-6216 : Merge versioned features by using highest version
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/support/provisioning-model@1767141 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/provisioning/model/KeyValueMap.java      |  8 ++
 .../sling/provisioning/model/MergeUtility.java     | 30 ++++++++
 .../sling/provisioning/model/ModelUtility.java     |  3 +-
 .../sling/provisioning/model/io/package-info.java  |  3 +-
 .../sling/provisioning/model/package-info.java     |  3 +-
 .../sling/provisioning/model/MergeUtilityTest.java | 86 ++++++++++++++++++++++
 6 files changed, 128 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/sling/provisioning/model/KeyValueMap.java b/src/main/java/org/apache/sling/provisioning/model/KeyValueMap.java
index 4d08db1..6646ab6 100644
--- a/src/main/java/org/apache/sling/provisioning/model/KeyValueMap.java
+++ b/src/main/java/org/apache/sling/provisioning/model/KeyValueMap.java
@@ -93,4 +93,12 @@ public class KeyValueMap<T>
     public int size() {
         return this.properties.size();
     }
+
+    /**
+     * Clear the map
+     * @since 1.7
+     */
+    public void clear() {
+        this.properties.clear();
+    }
 }
diff --git a/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java b/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java
index 86f3203..265643f 100644
--- a/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java
+++ b/src/main/java/org/apache/sling/provisioning/model/MergeUtility.java
@@ -92,6 +92,9 @@ public abstract class MergeUtility {
      * <p>
      * For each feature, the following actions are performed:
      * <ul>
+     *   <li>If either the base feature or the additional feature has a version, then
+     *    the one with the higher version is used, the other one is skipped. A missing
+     *    version is considered the lowest possible version.</li>
      *   <li>The feature type of the base feature is set to the type of the additional feature.</li>
      *   <li>All additional sections of the additional feature are added to the base feature.</li>
      *   <li>All variables from the additional feature are set on the base feature, overriding
@@ -107,6 +110,33 @@ public abstract class MergeUtility {
         // features
         for(final Feature feature : additional.getFeatures()) {
             final Feature baseFeature = base.getOrCreateFeature(feature.getName());
+
+            // version check first
+            boolean overwrite = false;
+            if ( baseFeature.getVersion() != null ) {
+                if ( feature.getVersion() == null ) {
+                    continue;
+                }
+                final Version baseVersion = new Version(baseFeature.getVersion());
+                final Version addVersion = new Version(feature.getVersion());
+                if ( baseVersion.compareTo(addVersion) >= 0 ) {
+                    continue;
+                }
+                overwrite = true;
+            } else {
+                if ( feature.getVersion() != null ) {
+                    overwrite = true;
+                }
+            }
+            if ( overwrite ) {
+                // set version
+                baseFeature.setVersion(feature.getVersion());
+                // remove everything from base feature
+                baseFeature.getRunModes().clear();
+                baseFeature.getAdditionalSections().clear();
+                baseFeature.getVariables().clear();
+            }
+
             baseFeature.setType(feature.getType());
 
             // additional sections
diff --git a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
index e4b1479..71b8a15 100644
--- a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
+++ b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java
@@ -157,8 +157,9 @@ public abstract class ModelUtility {
 
     /**
      * Validates the model.
+     *
      * @param model The model to validate
-     * @return A map with errors or {@code null}.
+     * @return A map with errors or {@code null} if valid.
      */
     public static Map<Traceable, String> validate(final Model model) {
         final Map<Traceable, String> errors = new HashMap<Traceable, String>();
diff --git a/src/main/java/org/apache/sling/provisioning/model/io/package-info.java b/src/main/java/org/apache/sling/provisioning/model/io/package-info.java
index 175208e..a100458 100644
--- a/src/main/java/org/apache/sling/provisioning/model/io/package-info.java
+++ b/src/main/java/org/apache/sling/provisioning/model/io/package-info.java
@@ -17,8 +17,7 @@
  * under the License.
  */
 
-@Version("1.2")
+@org.osgi.annotation.versioning.Version("1.2")
 package org.apache.sling.provisioning.model.io;
 
-import aQute.bnd.annotation.Version;
 
diff --git a/src/main/java/org/apache/sling/provisioning/model/package-info.java b/src/main/java/org/apache/sling/provisioning/model/package-info.java
index 9821b30..1a04095 100644
--- a/src/main/java/org/apache/sling/provisioning/model/package-info.java
+++ b/src/main/java/org/apache/sling/provisioning/model/package-info.java
@@ -17,8 +17,7 @@
  * under the License.
  */
 
-@Version("1.6.0")
+@org.osgi.annotation.versioning.Version("1.7.0")
 package org.apache.sling.provisioning.model;
 
-import aQute.bnd.annotation.Version;
 
diff --git a/src/test/java/org/apache/sling/provisioning/model/MergeUtilityTest.java b/src/test/java/org/apache/sling/provisioning/model/MergeUtilityTest.java
index afff784..37f3bb7 100644
--- a/src/test/java/org/apache/sling/provisioning/model/MergeUtilityTest.java
+++ b/src/test/java/org/apache/sling/provisioning/model/MergeUtilityTest.java
@@ -291,4 +291,90 @@ public class MergeUtilityTest {
         final List<Artifact> list = U.assertArtifactsInGroup(model.getFeature("f").getRunMode().getArtifactGroup(0), 1);
         U.assertArtifact(list.get(0), "g", "c", "1.6.0", "jar", null);
     }
+
+    @Test public void mergeBaseNoVersionWithVersionedFeature() {
+        final Model base = new Model();
+        final Feature f1 = base.getOrCreateFeature("f1");
+        f1.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "a", "0.1.0", null, null));
+        f1.getVariables().put("a", "1");
+        f1.getAdditionalSections().add(new Section("foo"));
+
+        final Model add = new Model();
+        final Feature f1Add = add.getOrCreateFeature("f1");
+        f1Add.setVersion("1.0");
+        f1Add.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "b", "1.0.0", null, null));
+
+        MergeUtility.merge(base, add);
+        final List<Artifact> list = U.assertArtifactsInGroup(base.getFeature("f1").getRunMode().getArtifactGroup(0), 1);
+        U.assertArtifact(list.get(0), "g", "b", "1.0.0", "jar", null);
+        assertTrue(f1.getVariables().isEmpty());
+        assertTrue(f1.getAdditionalSections().isEmpty());
+    }
+
+    @Test public void mergeBaseVersionedFeatureWithHigherVersionedFeature() {
+        final Model base = new Model();
+        final Feature f1 = base.getOrCreateFeature("f1");
+        f1.setVersion("0.1.0");
+        f1.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "a", "0.1.0", null, null));
+        f1.getVariables().put("a", "1");
+        f1.getAdditionalSections().add(new Section("foo"));
+
+        final Model add = new Model();
+        final Feature f1Add = add.getOrCreateFeature("f1");
+        f1Add.setVersion("1.0");
+        f1Add.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "b", "1.0.0", null, null));
+
+        MergeUtility.merge(base, add);
+        final List<Artifact> list = U.assertArtifactsInGroup(base.getFeature("f1").getRunMode().getArtifactGroup(0), 1);
+        U.assertArtifact(list.get(0), "g", "b", "1.0.0", "jar", null);
+        assertTrue(f1.getVariables().isEmpty());
+        assertTrue(f1.getAdditionalSections().isEmpty());
+    }
+
+    @Test public void mergeBaseVersionedFeatureWithNoVersion() {
+        final Model base = new Model();
+        final Feature f1 = base.getOrCreateFeature("f1");
+        f1.setVersion("1.0");
+        f1.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "a", "0.1.0", null, null));
+        f1.getVariables().put("a", "1");
+        f1.getAdditionalSections().add(new Section("foo"));
+
+        final Model add = new Model();
+        final Feature f1Add = add.getOrCreateFeature("f1");
+        f1Add.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "b", "1.0.0", null, null));
+        f1Add.getVariables().put("b", "1");
+        f1Add.getAdditionalSections().add(new Section("foo2"));
+
+        MergeUtility.merge(base, add);
+        final List<Artifact> list = U.assertArtifactsInGroup(base.getFeature("f1").getRunMode().getArtifactGroup(0), 1);
+        U.assertArtifact(list.get(0), "g", "a", "0.1.0", "jar", null);
+        assertEquals(1, f1.getVariables().size());
+        assertNotNull(f1.getVariables().get("a"));
+        assertEquals(1, f1.getAdditionalSections().size());
+        assertEquals(1, f1.getAdditionalSections("foo").size());
+    }
+
+    @Test public void mergeBaseVersionedFeatureWithLowerVersionedFeature() {
+        final Model base = new Model();
+        final Feature f1 = base.getOrCreateFeature("f1");
+        f1.setVersion("1.0");
+        f1.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "a", "0.1.0", null, null));
+        f1.getVariables().put("a", "1");
+        f1.getAdditionalSections().add(new Section("foo"));
+
+        final Model add = new Model();
+        final Feature f1Add = add.getOrCreateFeature("f1");
+        f1Add.setVersion("0.1.0");
+        f1Add.getOrCreateRunMode(null).getOrCreateArtifactGroup(0).add(new Artifact("g", "b", "1.0.0", null, null));
+        f1Add.getVariables().put("b", "1");
+        f1Add.getAdditionalSections().add(new Section("foo2"));
+
+        MergeUtility.merge(base, add);
+        final List<Artifact> list = U.assertArtifactsInGroup(base.getFeature("f1").getRunMode().getArtifactGroup(0), 1);
+        U.assertArtifact(list.get(0), "g", "a", "0.1.0", "jar", null);
+        assertEquals(1, f1.getVariables().size());
+        assertNotNull(f1.getVariables().get("a"));
+        assertEquals(1, f1.getAdditionalSections().size());
+        assertEquals(1, f1.getAdditionalSections("foo").size());
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.