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.0.0
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-provisioning-model.git

commit bccda6c935c8a0d1b28931a6ec0a322c6d40daa2
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Tue Oct 21 12:29:26 2014 +0000

    Add some tests to verify the parsed and effective models
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/tooling/support/provisioning-model@1633358
 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/provisioning/model/Artifact.java  |  21 +++-
 .../apache/sling/provisioning/model/Feature.java   |   2 +-
 .../sling/provisioning/model/ModelConstants.java   |   7 +-
 .../provisioning/model/CustomResolverTest.java     |  41 ++++++++
 .../org/apache/sling/provisioning/model/U.java     | 113 +++++++++++++++++++++
 .../apache/sling/provisioning/model/io/IOTest.java |  44 +++-----
 src/test/resources/boot.txt                        |   3 +
 src/test/resources/example.txt                     |   7 +-
 src/test/resources/main.txt                        |   2 +-
 9 files changed, 206 insertions(+), 34 deletions(-)

diff --git a/src/main/java/org/apache/sling/provisioning/model/Artifact.java 
b/src/main/java/org/apache/sling/provisioning/model/Artifact.java
index 371ce2b..800578d 100644
--- a/src/main/java/org/apache/sling/provisioning/model/Artifact.java
+++ b/src/main/java/org/apache/sling/provisioning/model/Artifact.java
@@ -25,7 +25,7 @@ import java.util.Map;
  * In addition, the classifier and type can be specified as well. If no type 
is specified, "jar" is assumed.
  * An artifact can have any metadata.
  */
-public class Artifact extends Commentable {
+public class Artifact extends Commentable implements Comparable {
 
     /** The required group id. */
     private final String groupId;
@@ -131,6 +131,25 @@ public class Artifact extends Commentable {
         return new Artifact(gId, aId, version, classifier, type);
     }
 
+    @Override
+    public int hashCode() {
+        return toMvnUrl().hashCode();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if(o == null) return false;
+        if(!(o instanceof Artifact)) return false;
+        return toMvnUrl().equals(((Artifact)o).toMvnUrl());
+    }
+
+    @Override
+    public int compareTo(Object o) {
+        if(o == null) return 1;
+        if(!(o instanceof Artifact)) return 1;
+        return toMvnUrl().compareTo(((Artifact)o).toMvnUrl());
+    }
+
     /**
      * Return a mvn url
      * @return A mvn url
diff --git a/src/main/java/org/apache/sling/provisioning/model/Feature.java 
b/src/main/java/org/apache/sling/provisioning/model/Feature.java
index d04b1d0..3e851d7 100644
--- a/src/main/java/org/apache/sling/provisioning/model/Feature.java
+++ b/src/main/java/org/apache/sling/provisioning/model/Feature.java
@@ -86,7 +86,7 @@ public class Feature
      * @param runModes The run modes or {@code null}
      * @return The feature or {@code null}.
      */
-    public RunMode getRunMode(final String[] runModes) {
+    public RunMode getRunMode(final String ... runModes) {
         final String[] sortedRunModes = 
RunMode.getSortedRunModesArray(runModes);
         RunMode result = null;
         for(final RunMode current : this.runModes) {
diff --git 
a/src/main/java/org/apache/sling/provisioning/model/ModelConstants.java 
b/src/main/java/org/apache/sling/provisioning/model/ModelConstants.java
index e5f8301..4411602 100644
--- a/src/main/java/org/apache/sling/provisioning/model/ModelConstants.java
+++ b/src/main/java/org/apache/sling/provisioning/model/ModelConstants.java
@@ -56,5 +56,10 @@ public abstract class ModelConstants {
 
     /** Name of the standalone run mode. */
     public static final String RUN_MODE_STANDALONE = ":standalone";
-
+    
+    /** Default start level value */
+    public static final String DEFAULT_RUN_MODE = null;
+    
+    /** Default run mode value */
+    public static final int DEFAULT_START_LEVEL = 0;
 }
diff --git 
a/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java 
b/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java
new file mode 100644
index 0000000..4f7598d
--- /dev/null
+++ b/src/test/java/org/apache/sling/provisioning/model/CustomResolverTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.VariableResolver;
+import org.junit.Test;
+
+/** Read and merge our test models, write and read them again
+ *  and verify the result at various stages.
+ */
+public class CustomResolverTest {
+
+    public static class CustomResolver implements VariableResolver {
+        @Override
+        public String resolve(Feature feature, String name) {
+            return "#" + feature.getName() + "#" + name + "#";
+        }
+    }
+    
+    @Test public void testCustomResolverNoFilter() throws Exception {
+        final Model m = U.readCompleteTestModel();
+        final VariableResolver r = new CustomResolver();
+        final Model effective = ModelUtility.getEffectiveModel(m, r);
+        final ArtifactGroup g = U.getGroup(effective, "example", "jackrabbit", 
15);
+        U.assertArtifact(g, 
"mvn:org.apache.sling/org.apache.sling.jcr.jackrabbit.server/#example#jackrabbit.version#/jar");
+    }
+}
diff --git a/src/test/java/org/apache/sling/provisioning/model/U.java 
b/src/test/java/org/apache/sling/provisioning/model/U.java
new file mode 100644
index 0000000..a49f2f2
--- /dev/null
+++ b/src/test/java/org/apache/sling/provisioning/model/U.java
@@ -0,0 +1,113 @@
+/*
+ * 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 static 
org.apache.sling.provisioning.model.ModelConstants.DEFAULT_RUN_MODE;
+import static 
org.apache.sling.provisioning.model.ModelConstants.DEFAULT_START_LEVEL;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.Map;
+
+import org.apache.sling.provisioning.model.io.ModelReader;
+
+/** Test utilities */
+public class U {
+    
+    public static final String[] TEST_MODEL_FILENAMES = 
+            new String[] {"boot.txt", "example.txt", "main.txt", "oak.txt"};
+    
+    public static void assertArtifact(ArtifactGroup g, String mvnUrl) {
+        final Artifact a = Artifact.fromMvnUrl(mvnUrl);
+        if(!g.items.contains(a)) {
+            fail("Expecting ArtifactGroup to contain '" + mvnUrl + "': " + g);
+        }
+    }
+    
+    /** Read our test model by merging our TEST_MODEL_FILENAMES */ 
+    public static Model readCompleteTestModel() throws Exception {
+        final Model result = new Model();
+
+        for(final String name : TEST_MODEL_FILENAMES) {
+            final Reader reader = new 
InputStreamReader(U.class.getResourceAsStream("/" + name), "UTF-8");
+            try {
+                final Model current = ModelReader.read(reader, name);
+                final Map<Traceable, String> errors = 
ModelUtility.validate(current);
+                if (errors != null ) {
+                    throw new Exception("Invalid model at " + name + " : " + 
errors);
+                }
+                ModelUtility.merge(result, current);
+            } finally {
+                reader.close();
+            }
+        }
+
+        final Map<Traceable, String> errors = ModelUtility.validate(result);
+        if (errors != null ) {
+            throw new Exception("Invalid merged model : " + errors);
+        }
+        return result;
+    }
+    
+    public static ArtifactGroup getGroup(Model m, String feature, String 
runMode, int startLevel) {
+        final Feature f = m.getFeature(feature);
+        assertNotNull(f);
+        final RunMode rm = f.getRunMode(runMode);
+        assertNotNull(rm);
+        final ArtifactGroup g = rm.getArtifactGroup(startLevel);
+        assertNotNull(g);
+        return g;
+    }
+    
+    /** Verify that m matches what we expect after
+     *  reading and merging our test files.
+     */
+    public static void verifyTestModel(Model m, boolean 
variablesAlreadyResolved) {
+        final String [] f = { ":launchpad","example","main","oak" };
+        for(String name : f) {
+            assertNotNull("Expecting feature to be present:" + name, 
m.getFeature(name));
+        }
+        
+        {
+            final ArtifactGroup g = getGroup(m, "example", DEFAULT_RUN_MODE, 
DEFAULT_START_LEVEL);
+            U.assertArtifact(g, 
"mvn:commons-collections/commons-collections/3.2.1/jar");
+            U.assertArtifact(g, "mvn:org.example/jar-is-default/1.2/jar");
+        }
+        
+        {
+            final ArtifactGroup g = getGroup(m, "example", "jackrabbit", 15);
+            if(variablesAlreadyResolved) {
+                U.assertArtifact(g, 
"mvn:org.apache.sling/org.apache.sling.jcr.jackrabbit.server/2.1.3-SNAPSHOT/jar");
+            } else {
+                U.assertArtifact(g, 
"mvn:org.apache.sling/org.apache.sling.jcr.jackrabbit.server/${jackrabbit.version}/jar");
+            }
+        }
+        
+        {
+            final ArtifactGroup g = getGroup(m, ":boot", DEFAULT_RUN_MODE, 
DEFAULT_START_LEVEL);
+            if(variablesAlreadyResolved) {
+                U.assertArtifact(g, 
"mvn:org.apache.sling/org.apache.sling.fragment.ws/1.42-from-boot/jar");
+            } else {
+                U.assertArtifact(g, 
"mvn:org.apache.sling/org.apache.sling.fragment.ws/${ws.version}/jar");
+            }
+        }
+        
+
+    }
+}
diff --git a/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java 
b/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
index 313f9d4..47b80cc 100644
--- a/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
+++ b/src/test/java/org/apache/sling/provisioning/model/io/IOTest.java
@@ -16,46 +16,27 @@
  */
 package org.apache.sling.provisioning.model.io;
 
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.Map;
 
 import org.apache.sling.provisioning.model.Model;
 import org.apache.sling.provisioning.model.ModelUtility;
+import org.apache.sling.provisioning.model.U;
 import org.apache.sling.provisioning.model.Traceable;
 import org.junit.Test;
 
+/** Read and merge our test models, write and read them again
+ *  and verify the result at various stages.
+ */
 public class IOTest {
 
-    /**
-     * Not really a unit test...but better than nothing
-     */
     @Test public void testReadWrite() throws Exception {
-        final Model result = new Model();
-        final String[] candidates = new String[] {"boot.txt", "example.txt", 
"main.txt", "oak.txt"};
-
-        for(final String name : candidates) {
-            final Reader reader = new 
InputStreamReader(this.getClass().getResourceAsStream("/" + name), "UTF-8");
-            try {
-                final Model current = ModelReader.read(reader, name);
-                final Map<Traceable, String> errors = 
ModelUtility.validate(current);
-                if (errors != null ) {
-                    throw new Exception("Invalid model at " + name + " : " + 
errors);
-                }
-                ModelUtility.merge(result, current);
-            } finally {
-                reader.close();
-            }
-        }
-
-        final Map<Traceable, String> errors = ModelUtility.validate(result);
-        if (errors != null ) {
-            throw new Exception("Invalid assembled model : " + errors);
-        }
+        final Model result = U.readCompleteTestModel();
+        
+        U.verifyTestModel(result, false);
 
-        // write the complete model
+        // Write the merged model
         StringWriter writer = new StringWriter();
         try {
             ModelWriter.write(writer, result);
@@ -63,7 +44,7 @@ public class IOTest {
             writer.close();
         }
 
-        // and read it again
+        // read it again
         StringReader reader = new StringReader(writer.toString());
         final Model readModel = ModelReader.read(reader, "memory");
         reader.close();
@@ -71,5 +52,12 @@ public class IOTest {
         if (readErrors != null ) {
             throw new Exception("Invalid read model : " + readErrors);
         }
+        
+        // and verify the result
+        U.verifyTestModel(readModel, false);
+        
+        // Resolve variables and verify the result
+        final Model effective = ModelUtility.getEffectiveModel(readModel, 
null);
+        U.verifyTestModel(effective, true);
     }
 }
diff --git a/src/test/resources/boot.txt b/src/test/resources/boot.txt
index 7f08d39..ef2b1a8 100644
--- a/src/test/resources/boot.txt
+++ b/src/test/resources/boot.txt
@@ -9,6 +9,9 @@
 #
 [feature name=:boot]
 
+[variables]
+  ws.version=1.42-from-boot
+
 # additional entries for sling.properties
 # ---------------------------------------
 # jackrabbit and oak run modes are mutually exclusive,
diff --git a/src/test/resources/example.txt b/src/test/resources/example.txt
index 0153cd3..4b6b312 100644
--- a/src/test/resources/example.txt
+++ b/src/test/resources/example.txt
@@ -8,7 +8,8 @@
 #
 # Variables
 [variables]
-  ws.version=1.0.2
+  ws.version=1.12-from-example
+  jackrabbit.version=2.1.3-SNAPSHOT
 
 # Settings, artifacts and configurations belong to a run mode. If none is 
specified
 # the default run mode is used. The same goes with the start level for 
artifacts
@@ -23,6 +24,8 @@
     commons-codec/commons-codec/1.9/jar
     commons-lang/commons-lang/2.6/jar
     org.apache.commons/commons-math/2.2/jar
+    org.example/jar-is-default/1.2
+    
 # Artifacts can have additional information like a SHA1 etc.
 #
     org.apache.commons/commons-math/2.2/jar [sha1=2353750701ABE]
@@ -59,6 +62,6 @@ org.apache.sling.another.config [format=properties]
 #
 [artifacts startLevel=15 runModes=jackrabbit]
     org.apache.derby/derby/10.5.3.0_1/jar
-    org.apache.sling/org.apache.sling.jcr.jackrabbit.server/2.1.3-SNAPSHOT/jar
+    
org.apache.sling/org.apache.sling.jcr.jackrabbit.server/${jackrabbit.version}/jar
 
 
diff --git a/src/test/resources/main.txt b/src/test/resources/main.txt
index 1afbc3f..b514682 100644
--- a/src/test/resources/main.txt
+++ b/src/test/resources/main.txt
@@ -1,7 +1,7 @@
 [feature name=main]
 
 [variables]
-  ws.version=1.0.2
+  ws.version=1.0.2-from-main
 
 [artifacts]
     commons-io/commons-io/1.4/jar

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to