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/12/03 16:40:50 UTC

[sling-org-apache-sling-feature] branch master updated: SLING-8156 Extension should provide a copy() method

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


The following commit(s) were added to refs/heads/master by this push:
     new 3a94835  SLING-8156 Extension should provide a copy() method
3a94835 is described below

commit 3a9483531b4895b6db6677b511383111221479e3
Author: David Bosschaert <bo...@adobe.com>
AuthorDate: Mon Dec 3 16:40:25 2018 +0000

    SLING-8156 Extension should provide a copy() method
---
 .../java/org/apache/sling/feature/Extension.java   | 24 +++++++++
 .../org/apache/sling/feature/ExtensionTest.java    | 59 ++++++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git a/src/main/java/org/apache/sling/feature/Extension.java b/src/main/java/org/apache/sling/feature/Extension.java
index b9b513c..91f0b6b 100644
--- a/src/main/java/org/apache/sling/feature/Extension.java
+++ b/src/main/java/org/apache/sling/feature/Extension.java
@@ -181,6 +181,30 @@ public class Extension {
         return artifacts;
     }
 
+    /**
+     * Create a copy of the Extension
+     * @return A copy of the Extension
+     */
+    public Extension copy() {
+        Extension c = new Extension(type, name, required);
+        switch(type) {
+        case TEXT:
+            c.setText(text);
+            break;
+        case JSON:
+            c.setJSON(text);
+            break;
+        case ARTIFACTS:
+            if (artifacts != null) {
+                for (Artifact a : artifacts) {
+                    c.getArtifacts().add(a.copy(a.getId()));
+                }
+            }
+            break;
+        }
+        return c;
+    }
+
     @Override
     public int hashCode() {
         return name.hashCode();
diff --git a/src/test/java/org/apache/sling/feature/ExtensionTest.java b/src/test/java/org/apache/sling/feature/ExtensionTest.java
new file mode 100644
index 0000000..f6bd849
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/ExtensionTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ExtensionTest {
+    @Test
+    public void testCopyTextExtension() {
+        Extension ex = new Extension(ExtensionType.TEXT, "t1", false);
+        ex.setText("foo");
+        Extension ex2 = ex.copy();
+
+        assertEquals(ex.getType(), ex2.getType());
+        assertEquals("foo", ex2.getText());
+    }
+
+    @Test
+    public void testCopyJSONExtension() {
+        Extension ex = new Extension(ExtensionType.JSON, "t1", false);
+        ex.setJSON("[123]");
+        Extension ex2 = ex.copy();
+
+        assertEquals(ex.getType(), ex2.getType());
+        assertEquals("[123]", ex2.getJSON());
+    }
+
+    @Test
+    public void testCopyArtifactsExtension() {
+        Extension ex = new Extension(ExtensionType.ARTIFACTS, "t1", false);
+        Artifact art = new Artifact(ArtifactId.fromMvnId("g:a:123"));
+        art.getMetadata().put("test", "blah");
+        ex.getArtifacts().add(art);
+        Extension ex2 = ex.copy();
+
+        assertEquals(ex.getType(), ex2.getType());
+        assertEquals(1, ex2.getArtifacts().size());
+        Artifact art2 = ex2.getArtifacts().iterator().next();
+        assertEquals("g:a:123", art2.getId().toMvnId());
+        assertEquals(1, art2.getMetadata().size());
+        assertEquals("blah", art2.getMetadata().get("test"));
+    }
+}