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.3.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-provisioning-model.git
commit ce9607fd06847d76bf30fdd3e2ddef22aa0adc1a Author: Stefan Seifert <[email protected]> AuthorDate: Tue Jul 14 11:39:49 2015 +0000 SLING-4880 apply resolved dependencies to raw model instead of attaching effective model add new optional parameter "allowUnresolvedPomDependencies" (default: false) git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/tooling/support/provisioning-model@1690902 13f79535-47bb-0310-9956-ffa450edef68 --- .../provisioning/model/ModelResolveUtility.java | 2 +- .../sling/provisioning/model/ModelUtility.java | 42 +++++++++++- .../provisioning/model/ModelProcessorTest.java | 36 +++------- .../ModelUtilityApplyArtifactVersionsTest.java | 76 ++++++++++++++++++++++ .../model/ModelUtilityApplyVariablesTest.java | 5 +- 5 files changed, 129 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java b/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java index 43e9b0f..ccae06d 100644 --- a/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java +++ b/src/main/java/org/apache/sling/provisioning/model/ModelResolveUtility.java @@ -90,7 +90,7 @@ class ModelResolveUtility { */ static String resolveArtifactVersion(final String groupId, final String artifactId, final String version, final String classifier, final String type, ArtifactVersionResolver artifactVersionResolver) { - if (artifactVersionResolver != null && "LATEST".equals(version)) { + if (artifactVersionResolver != null && (version == null || "LATEST".equals(version))) { String newVersion = artifactVersionResolver.resolve(new Artifact(groupId, artifactId, version, classifier, type)); if (newVersion != null) { return newVersion; 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 f960d9f..8add571 100644 --- a/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java +++ b/src/main/java/org/apache/sling/provisioning/model/ModelUtility.java @@ -17,6 +17,7 @@ package org.apache.sling.provisioning.model; import static org.apache.sling.provisioning.model.ModelResolveUtility.getProcessedConfiguration; +import static org.apache.sling.provisioning.model.ModelResolveUtility.resolveArtifactVersion; import java.util.Arrays; import java.util.Enumeration; @@ -422,19 +423,19 @@ public abstract class ModelUtility { * The variable resolver may look up variables on it's own, or fallback to the variables already defined for the feature. * All resolved variable values are collected and put to the "variables" section of the resulting model. * @param model Original model - * @param variableResolver Variable resolver + * @param resolver Variable resolver * @return Model with updated "variables" section. * @throws IllegalArgumentException If a variable can't be replaced or configuration properties can't be parsed * @since 1.3 */ - public static Model applyVariables(final Model model, final VariableResolver variableResolver) { + public static Model applyVariables(final Model model, final VariableResolver resolver) { // define delegating resolver that collects all variable names and value per feature final Map<String,Map<String,String>> collectedVars = new HashMap<String, Map<String,String>>(); VariableResolver variableCollector = new VariableResolver() { @Override public String resolve(Feature feature, String name) { - String value = variableResolver.resolve(feature, name); + String value = resolver.resolve(feature, name); if (value != null) { Map<String,String> featureVars = collectedVars.get(feature.getName()); if (featureVars == null) { @@ -469,4 +470,39 @@ public abstract class ModelUtility { return variablesUpdater.process(model); } + /** + * Resolves artifact versions that are no set explicitly in the provisioning file via the given resolver (version = "LATEST"). + * If the resolver does not resolve to a version "LATEST" is left in the model. + * The resolver may decide to raise an IllegalArgumentException in this case if unresolved dependencies are no allowed. + * @param model Original model + * @param resolver Artifact version resolver + * @return Model with updated artifact versions + * @throws IllegalArgumentException If the provider does not allow unresolved version and a version could not be resolved + * @since 1.3 + */ + public static Model applyArtifactVersions(final Model model, final ArtifactVersionResolver resolver) { + + // define a processor that updates the versions of artifacts + ModelProcessor versionUpdater = new ModelProcessor() { + @Override + protected Artifact processArtifact(Artifact artifact, Feature newFeature, RunMode newRunMode) { + String newVersion = resolveArtifactVersion( + artifact.getGroupId(), + artifact.getArtifactId(), + artifact.getVersion(), + artifact.getClassifier(), + artifact.getType(), + resolver); + return new Artifact(artifact.getGroupId(), + artifact.getArtifactId(), + newVersion, + artifact.getClassifier(), + artifact.getType()); + } + }; + + // return model with updated version artifacts + return versionUpdater.process(model); + } + } diff --git a/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java b/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java index 629568b..b8e4145 100644 --- a/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java +++ b/src/test/java/org/apache/sling/provisioning/model/ModelProcessorTest.java @@ -19,11 +19,9 @@ package org.apache.sling.provisioning.model; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import java.util.Enumeration; -import java.util.Iterator; import java.util.Map.Entry; import org.junit.Before; @@ -114,20 +112,9 @@ public class ModelProcessorTest { assertEquals("LocRMG11", group12.getLocation()); assertEquals("ComRMG11", group12.getComment()); - Iterator<Artifact> artifacts = group12.iterator(); - Artifact artifact1 = artifacts.next(); - assertEquals("#g1", artifact1.getGroupId()); - assertEquals("#a1", artifact1.getArtifactId()); - assertEquals("#v1", artifact1.getVersion()); - assertEquals("#c1", artifact1.getClassifier()); - assertEquals("#t1", artifact1.getType()); - - Artifact artifact2 = artifacts.next(); - assertEquals("#g2", artifact2.getGroupId()); - assertEquals("#a2", artifact2.getArtifactId()); - assertEquals("#v2", artifact2.getVersion()); - - assertFalse(artifacts.hasNext()); + U.assertArtifactsInGroup(group12, 2); + U.assertArtifact(group12, "mvn:#g1/#a1/#v1/#t1/#c1"); + U.assertArtifact(group12, "mvn:#g2/#a2/#v2/#jar"); assertEquals("LocConf12", runMode12.getConfigurations().getLocation()); assertEquals("ComConf12", runMode12.getConfigurations().getComment()); @@ -157,11 +144,8 @@ public class ModelProcessorTest { ArtifactGroup group21 = runMode21.getArtifactGroup(20); assertNotNull(group21); - artifacts = group21.iterator(); - Artifact artifact3 = artifacts.next(); - assertEquals("#g3", artifact3.getGroupId()); - assertEquals("#a3", artifact3.getArtifactId()); - assertFalse(artifacts.hasNext()); + U.assertArtifactsInGroup(group21, 1); + U.assertArtifact(group21, "mvn:#g3/#a3/#LATEST/#jar"); } @@ -179,11 +163,11 @@ public class ModelProcessorTest { @Override protected Artifact processArtifact(Artifact artifact, Feature feature, RunMode runMode) { Artifact newArtifact = new Artifact( - "#" + artifact.getGroupId(), - "#" + artifact.getArtifactId(), - "#" + artifact.getVersion(), - "#" + artifact.getClassifier(), - "#" + artifact.getType()); + artifact.getGroupId()!=null ? "#" + artifact.getGroupId() : null, + artifact.getArtifactId()!=null ? "#" + artifact.getArtifactId() : null, + artifact.getVersion()!=null ? "#" + artifact.getVersion() : "#LATEST", + artifact.getClassifier()!=null ? "#" + artifact.getClassifier() : null, + artifact.getType()!=null ? "#" + artifact.getType() : null); return newArtifact; } diff --git a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java new file mode 100644 index 0000000..41de065 --- /dev/null +++ b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyArtifactVersionsTest.java @@ -0,0 +1,76 @@ +/* + * 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.provisioning.model; + +import org.apache.sling.provisioning.model.ModelUtility.ArtifactVersionResolver; +import org.junit.Before; +import org.junit.Test; + +public class ModelUtilityApplyArtifactVersionsTest { + + private Model testModel; + private ArtifactVersionResolver testArtifactVersionResolver; + + @Before + public void setUp() { + testModel = new Model(); + + Feature feature = testModel.getOrCreateFeature("feature1"); + RunMode runMode = feature.getOrCreateRunMode(new String[] { "rm1"}); + ArtifactGroup group = runMode.getOrCreateArtifactGroup(10); + + group.add(new Artifact("g1", "a1", "v1", "c1", "t1")); + group.add(new Artifact("g2", "a2", "LATEST", null, null)); + group.add(new Artifact("g3", "a3", "LATEST", null, null)); + + // dummy variable resolver that simulates external resolving of some variables + testArtifactVersionResolver = new ArtifactVersionResolver() { + @Override + public String resolve(Artifact artifact) { + if ("g2".equals(artifact.getGroupId()) && "a2".equals(artifact.getArtifactId())) { + return "v2"; + } + return null; + } + }; + } + + @Test + public void testApplyArtifactVersions() { + Model model = ModelUtility.applyArtifactVersions(testModel, testArtifactVersionResolver); + + Feature feature = model.getFeature("feature1"); + RunMode runMode = feature.getRunMode("rm1"); + ArtifactGroup group = runMode.getArtifactGroup(10); + + U.assertArtifactsInGroup(group, 3); + U.assertArtifact(group, "mvn:g1/a1/v1/t1/c1"); + U.assertArtifact(group, "mvn:g2/a2/v2/jar"); + U.assertArtifact(group, "mvn:g3/a3//jar"); + } + + @Test(expected=IllegalArgumentException.class) + public void testApplyVariablesInvalidVariable() { + ModelUtility.applyArtifactVersions(testModel, new ArtifactVersionResolver() { + @Override + public String resolve(Artifact artifact) { + throw new IllegalArgumentException("Unable to resolve."); + } + }); + } + +} diff --git a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java index a25c206..6efdffe 100644 --- a/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java +++ b/src/test/java/org/apache/sling/provisioning/model/ModelUtilityApplyVariablesTest.java @@ -79,8 +79,9 @@ public class ModelUtilityApplyVariablesTest { RunMode runMode = effectiveFeature.getRunMode("rm1"); ArtifactGroup group = runMode.getArtifactGroup(10); - group.add(new Artifact("g1", "a1", "${param1}", "c1", "t1")); - group.add(new Artifact("g2", "a2", "${extparam2}", null, null)); + U.assertArtifactsInGroup(group, 2); + U.assertArtifact(group, "mvn:g1/a1/v1/t1/c1"); + U.assertArtifact(group, "mvn:g2/a2/extvalue2/jar"); Configuration conf = runMode.getConfiguration("pid1", null); assertEquals("extvalue1", conf.getProperties().get("conf1")); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
