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-modelconverter.git
commit 767340c4ccef0cbc0a09d0484b8b98065ef3e7dc Author: David Bosschaert <david.bosscha...@gmail.com> AuthorDate: Thu Mar 22 16:32:35 2018 +0000 Handle the fact that a single provisioning model can result in multiple features Also, allow multiple indentical bundles when they are associated with different run modes. --- .../modelconverter/impl/ProvisioningToFeature.java | 11 +++-- .../modelconverter/impl/ModelConverterTest.java | 57 ++++++++++++++++------ 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java b/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java index d34d471..9f88aae 100644 --- a/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java +++ b/src/main/java/org/apache/sling/feature/modelconverter/impl/ProvisioningToFeature.java @@ -71,10 +71,13 @@ public class ProvisioningToFeature { public static void convert(File file, String output) { Model model = createModel(Collections.singletonList(file), null, true, false); final List<org.apache.sling.feature.Feature> features = buildFeatures(model); - if (features.size() != 1) - throw new IllegalStateException("TODO"); - - writeFeature(features.get(0), output, 0); + if (features.size() != 1) { + for (int i=0; i<features.size(); i++) { + writeFeature(features.get(i), output, i+1); + } + } else { + writeFeature(features.get(0), output, 0); + } } public static void convert(List<File> files, String outputFile, String runModes, boolean createApp, diff --git a/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java b/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java index fb8b571..98f2dbc 100644 --- a/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java +++ b/src/test/java/org/apache/sling/feature/modelconverter/impl/ModelConverterTest.java @@ -29,6 +29,7 @@ import org.apache.sling.provisioning.model.ArtifactGroup; import org.apache.sling.provisioning.model.Configuration; import org.apache.sling.provisioning.model.Feature; import org.apache.sling.provisioning.model.KeyValueMap; +import org.apache.sling.provisioning.model.MergeUtility; import org.apache.sling.provisioning.model.Model; import org.apache.sling.provisioning.model.ModelConstants; import org.apache.sling.provisioning.model.ModelUtility; @@ -49,6 +50,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Dictionary; import java.util.Enumeration; @@ -129,15 +131,25 @@ public class ModelConverterTest { } public void testConvertFromProvModelRoundTrip(File orgProvModel) throws Exception { - File outJSONFile = new File(tempDir.toFile(), orgProvModel.getName() + ".json.generated"); - File outProvFile = new File(tempDir.toFile(), orgProvModel.getName() + ".txt.generated"); + String genJSONPrefix = orgProvModel.getName() + ".json"; + String genTxtPrefix = orgProvModel.getName() + ".txt"; + String genSuffix = ".generated"; + File outJSONFile = new File(tempDir.toFile(), genJSONPrefix + genSuffix); + List<File> allGenerateProvisioningModelFiles = new ArrayList<>(); ProvisioningToFeature.convert(orgProvModel, outJSONFile.getAbsolutePath()); - FeatureToProvisioning.convert(outJSONFile, outProvFile.getAbsolutePath(), - artifactManager); + + for (File f : tempDir.toFile().listFiles((p, n) -> n.startsWith(genJSONPrefix))) { + String infix = f.getName().substring(genJSONPrefix.length(), + f.getName().length() - genSuffix.length()); + + File genFile = new File(tempDir.toFile(), genTxtPrefix + infix + genSuffix); + allGenerateProvisioningModelFiles.add(genFile); + FeatureToProvisioning.convert(f, genFile.getAbsolutePath(), artifactManager); + } Model expected = readProvisioningModel(orgProvModel); - Model actual = readProvisioningModel(outProvFile); + Model actual = readProvisioningModel(allGenerateProvisioningModelFiles); assertModelsEqual(expected, actual); } @@ -168,19 +180,32 @@ public class ModelConverterTest { } private static Model readProvisioningModel(File modelFile) throws IOException { - try (final FileReader is = new FileReader(modelFile)) { - Model model = ModelReader.read(is, modelFile.getAbsolutePath()); - - // Fix the configurations up from the internal format to the Dictionary-based format - return ModelUtility.getEffectiveModel(model, - new ResolverOptions().variableResolver(new VariableResolver() { - @Override - public String resolve(final Feature feature, final String name) { - // Keep variables as-is in the model - return "${" + name + "}"; + return readProvisioningModel(Collections.singletonList(modelFile)); + } + + private static Model readProvisioningModel(List<File> modelFiles) throws IOException { + Model model = null; + for (File modelFile : modelFiles) { + try (FileReader fr = new FileReader(modelFile)) { + Model nextModel = ModelReader.read(fr, modelFile.getAbsolutePath()); + + if (model == null) { + model = nextModel; + } else { + MergeUtility.merge(model, nextModel); } - })); + } } + + // Fix the configurations up from the internal format to the Dictionary-based format + return ModelUtility.getEffectiveModel(model, + new ResolverOptions().variableResolver(new VariableResolver() { + @Override + public String resolve(final Feature feature, final String name) { + // Keep variables as-is in the model + return "${" + name + "}"; + } + })); } private void assertFeaturesEqual(org.apache.sling.feature.Feature expected, org.apache.sling.feature.Feature actual) { -- To stop receiving notification emails like this one, please contact dav...@apache.org.