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

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


The following commit(s) were added to refs/heads/master by this push:
     new 68c17db  SLING-9525 : Create repoinit analyser
68c17db is described below

commit 68c17db2cd1e9ac8f1aa45febbd62dc7c25897b2
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu Jun 18 10:02:02 2020 +0200

    SLING-9525 : Create repoinit analyser
---
 pom.xml                                            |  13 ++
 .../feature/analyser/task/impl/CheckRepoinit.java  |  95 ++++++++++++++
 ...apache.sling.feature.analyser.task.AnalyserTask |   3 +-
 .../analyser/task/impl/CheckRepoinitTest.java      | 146 +++++++++++++++++++++
 4 files changed, 256 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index ba0af66..ceef91d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -69,6 +69,7 @@
                         <configuration>
                             <createSourcesJar>true</createSourcesJar>
                             <shadeSourcesContent>true</shadeSourcesContent>
+                            
<createDependencyReducedPom>false</createDependencyReducedPom>
                             <relocations>
                                 <relocation>
                                     <pattern>org.apache.felix</pattern>
@@ -132,6 +133,12 @@
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.converter</artifactId>
+            <version>1.0.14</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.utils</artifactId>
             <version>1.11.0</version>
             <scope>provided</scope>
@@ -151,6 +158,12 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.repoinit.parser</artifactId>
+            <version>1.6.2</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
             <scope>provided</scope>
diff --git 
a/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinit.java 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinit.java
new file mode 100644
index 0000000..2df81a3
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinit.java
@@ -0,0 +1,95 @@
+/*
+ * 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.analyser.task.impl;
+
+import java.io.StringReader;
+
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.repoinit.parser.RepoInitParser;
+import org.apache.sling.repoinit.parser.RepoInitParsingException;
+import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
+import org.osgi.util.converter.Converters;
+
+public class CheckRepoinit implements AnalyserTask {
+
+    public static final String PID = 
"org.apache.sling.jcr.repoinit.impl.RepositoryInitializer";
+
+    public static final String FACTORY_PID = 
"org.apache.sling.jcr.repoinit.RepositoryInitializer";
+    @Override
+    public String getName() {
+        return "Repoinit Check";
+    }
+
+    @Override
+    public String getId() {
+        return "repoinit";
+    }
+
+    @Override
+    public void execute(final AnalyserTaskContext ctx) {
+        // check extension
+        final Extension ext = 
ctx.getFeature().getExtensions().getByName(Extension.EXTENSION_NAME_REPOINIT);
+        if ( ext != null ) {
+            if ( ext.getType() != ExtensionType.TEXT ) {
+                ctx.reportError("Repoinit extension must be of type TEXT");
+            } else  {
+                check(ctx, "extension", ext.getText());
+            }
+        }
+
+        // configuration
+        final Configuration cfg = 
ctx.getFeature().getConfigurations().getConfiguration(PID);
+        if ( cfg != null ) {
+            check(ctx, cfg, false);
+        }
+        for(final Configuration c : ctx.getFeature().getConfigurations()) {
+            if ( FACTORY_PID.equals(c.getFactoryPid()) ) {
+                check(ctx, c, true);
+            }
+        }
+
+    }
+
+    private void check(final AnalyserTaskContext ctx, final Configuration cfg, 
final boolean supportsScripts) {
+        // for now we only check scripts
+        if ( supportsScripts ) {
+            final Object val = cfg.getProperties().get("scripts");
+            if ( val != null ) {
+                final String[] scripts = 
Converters.standardConverter().convert(val).to(String[].class);
+                for(final String contents : scripts) {
+                    check(ctx, "configuration ".concat(cfg.getPid()), 
contents);
+                }
+            }
+        }
+    }
+
+    private void check(final AnalyserTaskContext ctx, final String id, final 
String contents) {
+        final RepoInitParser parser = new RepoInitParserService();
+
+        try {
+            parser.parse(new StringReader(contents));
+        } catch ( RepoInitParsingException e) {
+            ctx.reportError("Parsing error in repoinit from 
".concat(id).concat(" : ").concat(e.getMessage()));
+        }
+    }
+}
diff --git 
a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
 
b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
index 180fcc8..bfbe662 100644
--- 
a/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
+++ 
b/src/main/resources/META-INF/services/org.apache.sling.feature.analyser.task.AnalyserTask
@@ -5,4 +5,5 @@ 
org.apache.sling.feature.analyser.task.impl.CheckBundlesForResources
 org.apache.sling.feature.analyser.task.impl.CheckRequirementsCapabilities
 org.apache.sling.feature.analyser.task.impl.CheckContentPackageForInstallables
 org.apache.sling.feature.analyser.task.impl.CheckContentPackagesDependencies
-org.apache.sling.feature.analyser.task.impl.CheckApisJarsProperties
\ No newline at end of file
+org.apache.sling.feature.analyser.task.impl.CheckApisJarsProperties
+org.apache.sling.feature.analyser.task.impl.CheckRepoinit
\ No newline at end of file
diff --git 
a/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinitTest.java
 
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinitTest.java
new file mode 100644
index 0000000..517e1fa
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/analyser/task/impl/CheckRepoinitTest.java
@@ -0,0 +1,146 @@
+/*
+ * 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.analyser.task.impl;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Configuration;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.analyser.task.AnalyserTask;
+import org.apache.sling.feature.analyser.task.AnalyserTaskContext;
+import org.apache.sling.feature.scanner.BundleDescriptor;
+import org.apache.sling.feature.scanner.FeatureDescriptor;
+import org.junit.Test;
+
+public class CheckRepoinitTest {
+
+    @Test public void testValidExtension() throws Exception {
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        final AnalyserTask task = new CheckRepoinit();
+
+        // no extension
+        task.execute(ctx);
+        assertTrue(ctx.getErrors().isEmpty());
+
+        // create valid extension
+        final Extension ext = new Extension(ExtensionType.TEXT, 
Extension.EXTENSION_NAME_REPOINIT, ExtensionState.OPTIONAL);
+        ctx.getFeature().getExtensions().add(ext);
+        ext.setText("create path /acltest/A/B");
+
+        task.execute(ctx);
+        assertTrue(ctx.getErrors().isEmpty());
+    }
+
+    @Test public void testInvalidExtension() throws Exception {
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        final AnalyserTask task = new CheckRepoinit();
+
+        // wrong type
+        final Extension ext = new Extension(ExtensionType.JSON, 
Extension.EXTENSION_NAME_REPOINIT, ExtensionState.OPTIONAL);
+        ctx.getFeature().getExtensions().add(ext);
+
+        task.execute(ctx);
+        assertEquals(1, ctx.getErrors().size());
+    }
+
+    @Test public void testInvalidExtensionRepoinit() throws Exception {
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        final AnalyserTask task = new CheckRepoinit();
+
+        // invalid repoinit
+        final Extension ext = new Extension(ExtensionType.TEXT, 
Extension.EXTENSION_NAME_REPOINIT, ExtensionState.OPTIONAL);
+        ext.setText("create spaceship");
+        ctx.getFeature().getExtensions().add(ext);
+
+        task.execute(ctx);
+        assertEquals(1, ctx.getErrors().size());
+    }
+
+    @Test public void testValidFactoryConfig() throws Exception {
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        final AnalyserTask task = new CheckRepoinit();
+
+        final Configuration cfg = new 
Configuration(CheckRepoinit.FACTORY_PID.concat("~name"));
+        cfg.getProperties().put("scripts", "create path /acltest/A/B");
+        ctx.getFeature().getConfigurations().add(cfg);
+
+        task.execute(ctx);
+        assertTrue(ctx.getErrors().isEmpty());
+    }
+
+    @Test public void testInvalidFactoryConfig() throws Exception {
+        final AnalyserTaskContextImpl ctx = new AnalyserTaskContextImpl();
+        final AnalyserTask task = new CheckRepoinit();
+
+        final Configuration cfg = new 
Configuration(CheckRepoinit.FACTORY_PID.concat("~name"));
+        cfg.getProperties().put("scripts", "create spaceship");
+        ctx.getFeature().getConfigurations().add(cfg);
+
+        task.execute(ctx);
+        assertEquals(1, ctx.getErrors().size());
+    }
+
+    public static class AnalyserTaskContextImpl implements AnalyserTaskContext 
{
+
+        private final Feature f = new Feature(ArtifactId.parse("g:a:1"));
+
+        private final List<String> errors = new ArrayList<>();
+
+        @Override
+        public Feature getFeature() {
+            return f;
+        }
+
+        @Override
+        public FeatureDescriptor getFeatureDescriptor() {
+            return null;
+        }
+
+        @Override
+        public BundleDescriptor getFrameworkDescriptor() {
+            return null;
+        }
+
+        @Override
+        public Map<String, String> getConfiguration() {
+            return null;
+        }
+
+        @Override
+        public void reportWarning(String message) {
+        }
+
+        @Override
+        public void reportError(String message) {
+            errors.add(message);
+        }
+
+        public List<String> getErrors() {
+            return this.errors;
+        }
+    }
+}

Reply via email to