You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/03/11 12:31:32 UTC

[sling-whiteboard] branch master updated: [cp2fm] enhanced tests and improved runMode handling

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

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


The following commit(s) were added to refs/heads/master by this push:
     new ddeb4cf  [cp2fm] enhanced tests and improved runMode handling
ddeb4cf is described below

commit ddeb4cf335be78639ae1cdbc914a32f9f968eef7
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Mon Mar 11 13:31:18 2019 +0100

    [cp2fm] enhanced tests and improved runMode handling
---
 .../ContentPackage2FeatureModelConverter.java      | 48 +++++++++++++++++-----
 .../AbstractConfigurationEntryHandler.java         | 28 +++----------
 .../sling/cp2fm/handlers/BundleEntryHandler.java   | 11 ++++-
 .../cp2fm/handlers/BundleEntryHandlerTest.java     | 11 ++---
 .../handlers/ConfigurationEntryHandlerTest.java    | 15 +++++--
 .../cp2fm/handlers/DefaultEntryHandlerTest.java    |  1 +
 6 files changed, 73 insertions(+), 41 deletions(-)

diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
index 7461550..8681e4c 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
@@ -22,6 +22,8 @@ import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -49,6 +51,7 @@ import org.apache.sling.cp2fm.handlers.DefaultEntryHandler;
 import org.apache.sling.cp2fm.spi.EntryHandler;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
 import org.codehaus.plexus.archiver.Archiver;
@@ -121,15 +124,21 @@ public class ContentPackage2FeatureModelConverter {
     }
 
     public Feature getRunMode(String runMode) {
-        if (targetFeature == null) {
-            throw new IllegalStateException("Target Feature not initialized yet, please make sure convert() method was invoked.");
+        if (getTargetFeature() == null) {
+            throw new IllegalStateException("Target Feature not initialized yet, please make sure convert() method was invoked first.");
         }
 
-        return runModes.computeIfAbsent(runMode, k -> new Feature(new ArtifactId(targetFeature.getId().getGroupId(),
-                                                                                 targetFeature.getId().getArtifactId(),
-                                                                                 targetFeature.getId().getVersion(),
-                                                                                 targetFeature.getId().getClassifier() + '-' + runMode,
-                                                                                 targetFeature.getId().getType())));
+        if (runMode == null) {
+            return getTargetFeature();
+        }
+
+        ArtifactId id = getTargetFeature().getId();
+
+        return runModes.computeIfAbsent(runMode, k -> new Feature(new ArtifactId(id.getGroupId(),
+                                                                                 id.getArtifactId(),
+                                                                                 id.getVersion(),
+                                                                                 id.getClassifier() + '-' + runMode,
+                                                                                 id.getType())));
     }
 
     public void convert(File contentPackage) throws Exception {
@@ -215,7 +224,8 @@ public class ContentPackage2FeatureModelConverter {
                 archiver.createArchive();
 
                 try (InputStream input = new FileInputStream(destFile)) {
-                    deployLocallyAndAttach(input,
+                    deployLocallyAndAttach(null,
+                                           input,
                                            targetFeature.getId().getGroupId(),
                                            targetFeature.getId().getArtifactId(),
                                            targetFeature.getId().getVersion(),
@@ -252,6 +262,23 @@ public class ContentPackage2FeatureModelConverter {
         }
     }
 
+    public void addConfiguration(String runMode, String pid, Dictionary<String, Object> configurationProperties) {
+        Feature feature = getRunMode(runMode);
+        Configuration configuration = feature.getConfigurations().getConfiguration(pid);
+
+        if (configuration == null) {
+            configuration = new Configuration(pid);
+            feature.getConfigurations().add(configuration);
+        }
+
+        Enumeration<String> keys = configurationProperties.keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement();
+            Object value = configurationProperties.get(key);
+            configuration.getProperties().put(key, value);
+        }
+    }
+
     private void seralize(Feature feature) throws Exception {
         StringBuilder fileName = new StringBuilder().append(feature.getId().getArtifactId());
 
@@ -341,7 +368,8 @@ public class ContentPackage2FeatureModelConverter {
         return defaultEntryHandler;
     }
 
-    public void deployLocallyAndAttach(InputStream input,
+    public void deployLocallyAndAttach(String runMode,
+                                       InputStream input,
                                        String groupId,
                                        String artifactId,
                                        String version,
@@ -351,7 +379,7 @@ public class ContentPackage2FeatureModelConverter {
 
         Artifact artifact = new Artifact(new ArtifactId(groupId, artifactId, version, classifier, type));
         artifact.setStartOrder(bundlesStartOrder);
-        getTargetFeature().getBundles().add(artifact);
+        getRunMode(runMode).getBundles().add(artifact);
     }
 
     public void deployLocally(InputStream input,
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
index 337c2f9..fabefb4 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
@@ -35,13 +35,13 @@ abstract class AbstractConfigurationEntryHandler extends AbstractRegexEntryHandl
 
     @Override
     public final void handle(String path, Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception {
-        String name = entry.getName().substring(0, entry.getName().lastIndexOf('.'));
+        String pid = entry.getName().substring(0, entry.getName().lastIndexOf('.'));
 
-        logger.info("Processing configuration '{}'.", name);
+        logger.info("Processing configuration '{}'.", pid);
 
         Dictionary<String, Object> configurationProperties;
         try (InputStream input = archive.openInputStream(entry)) {
-            configurationProperties = parseConfiguration(name, input);
+            configurationProperties = parseConfiguration(pid, input);
         }
 
         if (configurationProperties.isEmpty()) {
@@ -49,31 +49,15 @@ abstract class AbstractConfigurationEntryHandler extends AbstractRegexEntryHandl
             return;
         }
 
-        Feature feature;
-
         Matcher matcher = getPattern().matcher(path);
         String runMode = null;
         // we are pretty sure it matches, here
-        if (matcher.matches() && (runMode = matcher.group(3)) != null) {
+        if (matcher.matches()) {
             // there is a specified RunMode
-            feature = converter.getRunMode(runMode);
-        } else {
-            feature = converter.getTargetFeature();
+            runMode = matcher.group(3);
         }
 
-        Configuration configuration = feature.getConfigurations().getConfiguration(name);
-
-        if (configuration == null) {
-            configuration = new Configuration(name);
-            feature.getConfigurations().add(configuration);
-        }
-
-        Enumeration<String> keys = configurationProperties.keys();
-        while (keys.hasMoreElements()) {
-            String key = keys.nextElement();
-            Object value = configurationProperties.get(key);
-            configuration.getProperties().put(key, value);
-        }
+        converter.addConfiguration(runMode, pid, configurationProperties);
     }
 
     protected abstract Dictionary<String, Object> parseConfiguration(String name, InputStream input) throws Exception;
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
index d4deff6..e1cab62 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
@@ -28,6 +28,7 @@ import java.io.InputStream;
 import java.util.Properties;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.apache.commons.io.IOUtils;
@@ -81,8 +82,16 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
         String artifactId = getTrimmedProperty(properties, NAME_ARTIFACT_ID);
         String version = getTrimmedProperty(properties, NAME_VERSION);
 
+        Matcher matcher = getPattern().matcher(path);
+        String runMode = null;
+        // we are pretty sure it matches, here
+        if (matcher.matches()) {
+            // there is a specified RunMode
+            runMode = matcher.group(3);
+        }
+
         try (InputStream input = archive.openInputStream(entry)) {
-            converter.deployLocallyAndAttach(input, groupId, artifactId, version, null, JAR_TYPE);
+            converter.deployLocallyAndAttach(runMode, input, groupId, artifactId, version, null, JAR_TYPE);
         }
 
         if (pomXml != null) {
diff --git a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
index c465ce7..57613a0 100644
--- a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
+++ b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
@@ -34,8 +34,9 @@ import java.util.Collection;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.cp2fm.handlers.BundleEntryHandler;
 import org.apache.sling.cp2fm.spi.EntryHandler;
-import org.apache.sling.feature.Bundles;
+import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -86,19 +87,19 @@ public final class BundleEntryHandlerTest {
         File testDirectory = new File(System.getProperty("testDirectory"), getClass().getName() + '_' + System.currentTimeMillis());
         when(converter.getOutputDirectory()).thenReturn(testDirectory);
 
-        doCallRealMethod().when(converter).deployLocallyAndAttach(any(InputStream.class), anyString(), anyString(), anyString(), anyString(), anyString());
+        doCallRealMethod().when(converter).deployLocallyAndAttach(anyString(), any(InputStream.class), anyString(), anyString(), anyString(), anyString(), anyString());
         doCallRealMethod().when(converter).deployLocally(any(InputStream.class), anyString(), anyString(), anyString(), anyString(), anyString());
 
-        Feature feature = mock(Feature.class);
-        when(feature.getBundles()).thenReturn(new Bundles());
+        Feature feature = new Feature(new ArtifactId("org.apache.sling", "org.apache.sling.cp2fm", "0.0.1", null, null));
         when(converter.getTargetFeature()).thenReturn(feature);
+        when(converter.getRunMode(anyString())).thenReturn(feature);
 
         bundleEntryHandler.handle(bundleLocation, archive, entry, converter);
 
         assertTrue(new File(testDirectory, "bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.pom").exists());
         assertTrue(new File(testDirectory, "bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.jar").exists());
 
-        assertFalse(feature.getBundles().isEmpty());
+        assertFalse(converter.getTargetFeature().getBundles().isEmpty());
         assertEquals(1, feature.getBundles().size());
         assertEquals("org.apache.felix:org.apache.felix.framework:6.0.1", feature.getBundles().get(0).getId().toMvnId());
     }
diff --git a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandlerTest.java b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandlerTest.java
index 5ad1fd6..449d13c 100644
--- a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandlerTest.java
+++ b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/ConfigurationEntryHandlerTest.java
@@ -19,8 +19,11 @@ package org.apache.sling.cp2fm.handlers;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
@@ -29,6 +32,12 @@ import java.util.Collection;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.cp2fm.handlers.AbstractConfigurationEntryHandler;
+import org.apache.sling.cp2fm.handlers.ConfigurationEntryHandler;
+import org.apache.sling.cp2fm.handlers.JsonConfigurationEntryHandler;
+import org.apache.sling.cp2fm.handlers.PropertiesConfigurationEntryHandler;
+import org.apache.sling.cp2fm.handlers.XmlConfigurationEntryHandler;
+import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Configuration;
 import org.apache.sling.feature.Configurations;
 import org.apache.sling.feature.Feature;
@@ -66,10 +75,10 @@ public class ConfigurationEntryHandlerTest {
         when(entry.getName()).thenReturn(resourceConfiguration.substring(resourceConfiguration.lastIndexOf('/') + 1));
         when(archive.openInputStream(entry)).thenReturn(getClass().getResourceAsStream(resourceConfiguration));
 
-        Feature feature = mock(Feature.class);
-        when(feature.getConfigurations()).thenReturn(new Configurations());
-        ContentPackage2FeatureModelConverter converter = mock(ContentPackage2FeatureModelConverter.class);
+        Feature feature = new Feature(new ArtifactId("org.apache.sling", "org.apache.sling.cp2fm", "0.0.1", null, null));
+        ContentPackage2FeatureModelConverter converter = spy(ContentPackage2FeatureModelConverter.class);
         when(converter.getTargetFeature()).thenReturn(feature);
+        doCallRealMethod().when(converter).addConfiguration(anyString(), anyString(), any());
         when(converter.getRunMode(anyString())).thenReturn(feature);
 
         configurationEntryHandler.handle(resourceConfiguration, archive, entry, converter);
diff --git a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandlerTest.java b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandlerTest.java
index 3d7e643..8afcca2 100644
--- a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandlerTest.java
+++ b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandlerTest.java
@@ -28,6 +28,7 @@ import java.util.Collection;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.cp2fm.handlers.DefaultEntryHandler;
 import org.apache.sling.cp2fm.spi.EntryHandler;
 import org.junit.Test;
 import org.junit.runner.RunWith;