You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2020/11/02 09:41:10 UTC

[sling-maven-plugin] branch feature/deprecate-validate created (now 22b42cf)

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

kwin pushed a change to branch feature/deprecate-validate
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git.


      at 22b42cf  SLING-9866 remove validate mojo

This branch includes the following new commits:

     new 22b42cf  SLING-9866 remove validate mojo

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-maven-plugin] 01/01: SLING-9866 remove validate mojo

Posted by kw...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch feature/deprecate-validate
in repository https://gitbox.apache.org/repos/asf/sling-maven-plugin.git

commit 22b42cfba3fcb69e52b8a02a3778a6efde7cdfab
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Mon Nov 2 10:40:58 2020 +0100

    SLING-9866 remove validate mojo
---
 sling-maven-plugin/pom.xml                         |   6 +-
 .../sling/maven/bundlesupport/JsonSupport.java     |  23 +--
 .../sling/maven/bundlesupport/ValidationMojo.java  | 178 +--------------------
 .../sling/maven/bundlesupport/JsonSupportTest.java |  19 ---
 .../maven/bundlesupport/ValidationMojoTest.java    |  47 ------
 5 files changed, 11 insertions(+), 262 deletions(-)

diff --git a/sling-maven-plugin/pom.xml b/sling-maven-plugin/pom.xml
index 6e21f08..d92b925 100644
--- a/sling-maven-plugin/pom.xml
+++ b/sling-maven-plugin/pom.xml
@@ -163,7 +163,11 @@
             <artifactId>jcr</artifactId>
             <scope>compile</scope>
         </dependency>
-
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.servlets.post</artifactId>
+            <version>2.3.36</version>
+        </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>osgi.core</artifactId>
diff --git a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/JsonSupport.java b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/JsonSupport.java
index 1586438..6efaeb2 100644
--- a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/JsonSupport.java
+++ b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/JsonSupport.java
@@ -18,8 +18,6 @@
  */
 package org.apache.sling.maven.bundlesupport;
 
-import static org.apache.sling.jcr.contentparser.impl.JsonTicksConverter.tickToDoubleQuote;
-
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -74,24 +72,7 @@ public final class JsonSupport {
             return jsonReader.readArray();
         }
     }
-    
-    /**
-     * Validate JSON structure
-     * @param jsonString JSON string
-     * @param tickToDoubleQuote Tick to double quote
-     * @throws javax.json.JsonException when JSON structure is invalid
-     */
-    public static void validateJsonStructure(String jsonString, boolean tickToDoubleQuote) {
-        String transformedJsonString = jsonString;
-        if (tickToDoubleQuote) {
-            transformedJsonString = tickToDoubleQuote(jsonString);
-        }
-        try (StringReader reader = new StringReader(transformedJsonString);
-                JsonReader jsonReader = JSON_READER_FACTORY.createReader(reader)) {
-            jsonReader.read();
-        }
-    }
-    
+
     @SuppressWarnings("unchecked")
     public static void accumulate(Map<String,Object> obj, String name, String value) {
         Object current = obj.get(name);
@@ -99,7 +80,7 @@ public final class JsonSupport {
             obj.put(name, value);
         }
         else if (current instanceof List) {
-            List<String> array = new ArrayList<>((List)current);
+            List<String> array = new ArrayList<>((List<String>)current);
             array.add(value);
             obj.put(name, array);
         }
diff --git a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
index faa3ba1..22dcf2c 100644
--- a/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
+++ b/sling-maven-plugin/src/main/java/org/apache/sling/maven/bundlesupport/ValidationMojo.java
@@ -18,195 +18,25 @@
  */
 package org.apache.sling.maven.bundlesupport;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import javax.json.JsonException;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang3.math.NumberUtils;
-import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.plugins.annotations.Component;
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
-import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.Scanner;
-import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
  * The <code>validate</code> goal checks the JSON code of a bundle.
+ * No longer functional.
+ * @deprecated Please use a dedicated plugin like <a href="https://github.com/sylvainlaurent/yaml-json-validator-maven-plugin">yaml-json-validator-maven-plugin</a> instead.
  */
+@Deprecated
 @Mojo(name = "validate", defaultPhase = LifecyclePhase.PROCESS_RESOURCES)
 public class ValidationMojo extends AbstractMojo {
 
-    private static final Pattern LINE_NUMBER_PATTERN = Pattern.compile("lineNumber=(\\d+),");
-    private static final Pattern COLUMN_NUMBER_PATTERN = Pattern.compile("columnNumber=(\\d+),");
-    private static final Pattern MESSAGE_CLEANUP_PATTERN = Pattern.compile("^(.*) on \\[lineNumber=\\d+, columnNumber=\\d+, streamOffset=\\d+\\](.*)$", Pattern.DOTALL);
-    
-    /**
-     * The Maven project.
-     */
-    @Parameter( defaultValue = "${project}", readonly = true )
-    private MavenProject project;
-
-    /**
-     * Whether to skip the validation. 
-     */
-    @Parameter(property = "sling.validation.skip", defaultValue = "false", required = true)
-    private boolean skip;
-
-    /**
-     * Whether to skip the json validation.
-     * At the time, there's no difference between <code>skip</code> and <code>skipJson</code> because only JSON files will be validated by now.
-     */
-    @Parameter(property = "sling.validation.skipJson", defaultValue = "false", required = true)
-    private boolean skipJson;
-
-    /**
-     * Whether to accept quote ticks in JSON files or not. 
-     */
-    @Parameter(property = "sling.validation.jsonQuoteTick", defaultValue = "false", required = false)
-    private boolean jsonQuoteTick;
-
-    @Component
-    private BuildContext buildContext;
-    
     /**
      * @see org.apache.maven.plugin.AbstractMojo#execute()
      */
     public void execute() throws MojoExecutionException, MojoFailureException {
-        if ( this.skip ) {
-            getLog().info("Validation is skipped.");
-            return;
-        }
-        
-        final Iterator<Resource> rsrcIterator = this.project.getResources().iterator();
-        while ( rsrcIterator.hasNext() ) {
-            final Resource rsrc = rsrcIterator.next();
-
-            final File directory = new File(rsrc.getDirectory());
-            if ( directory.exists() ) {
-
-                if (!buildContext.hasDelta(directory)) {
-                    getLog().debug("No files found to validate, skipping.");
-                    return;
-                }
-                
-                getLog().debug("Scanning " + rsrc.getDirectory());
-                final Scanner scanner = buildContext.newScanner(directory);
-
-                if ( rsrc.getExcludes() != null && rsrc.getExcludes().size() > 0 ) {
-                    scanner.setExcludes( (String[]) rsrc.getExcludes().toArray(new String[rsrc.getExcludes().size()] ) );
-                }
-                scanner.addDefaultExcludes();
-                if ( rsrc.getIncludes() != null && rsrc.getIncludes().size() > 0 ) {
-                    scanner.setIncludes( (String[]) rsrc.getIncludes().toArray(new String[rsrc.getIncludes().size()] ));
-                }
-
-                scanner.scan();
-
-                final String[] files = scanner.getIncludedFiles();
-                int countProcessed = 0;
-                List<Exception> failures = new ArrayList<>();
-                if ( files != null ) {
-                    for(int m=0; m<files.length; m++) {
-                        final File file = new File(directory, files[m]);
-                        buildContext.removeMessages(file);
-                        try {
-                            this.validate(file);
-                        }
-                        catch (Exception ex) {
-                            failures.add(ex);
-                            buildContext.addMessage(file,
-                                    parseLineNumber(ex.getMessage()),
-                                    parseColumnNumber(ex.getMessage()), 
-                                    cleanupMessage(ex.getMessage()),
-                                    BuildContext.SEVERITY_ERROR,
-                                    ex.getCause());
-                        }
-                        countProcessed++;
-                    }
-                }
-                
-                if (!failures.isEmpty()) {
-                    if (!buildContext.isIncremental()) {
-                        throw new MojoFailureException("Validated " + countProcessed + " file(s), found " + failures.size() + " failures.");
-                    }
-                }
-                else {
-                    getLog().info("Validated " + countProcessed + " file(s).");
-                }
-            }
-        }
-    }
-
-    private void validate(final File file) throws MojoExecutionException {
-        getLog().debug("Validating " + file.getPath());
-        if ( file.isFile() ) {
-            if ( file.getName().endsWith(".json") && !this.skipJson ) {
-                getLog().debug("Validation JSON file " + file.getPath());
-                FileInputStream fis = null;
-                String json = null;
-                try {
-                    fis = new FileInputStream(file);
-                    json = IOUtils.toString(fis, StandardCharsets.UTF_8);
-                } catch (IOException ex) {
-                    throw new MojoExecutionException(ex.getMessage(), ex);
-                } finally {
-                    IOUtils.closeQuietly(fis);
-                }
-                // validate JSON
-                try {
-                    JsonSupport.validateJsonStructure(json, jsonQuoteTick);
-                } catch (JsonException ex) {
-                    throw new MojoExecutionException("Invalid JSON: " + ex.getMessage());
-                }
-            }
-        }
+        getLog().warn("This mojo does no longer work. Please use a dedicated plugin like https://github.com/sylvainlaurent/yaml-json-validator-maven-plugin instead.");
     }
-    
-    static int parseLineNumber(String message) {
-        return parseNumber(message, LINE_NUMBER_PATTERN);
-    }
-    
-    static int parseColumnNumber(String message) {
-        return parseNumber(message, COLUMN_NUMBER_PATTERN);
-    }
-
-    static int parseNumber(String message, Pattern pattern) {
-        Matcher matcher = pattern.matcher(message);
-        if (matcher.find()) {
-            return NumberUtils.toInt(matcher.group(1));
-        }
-        else {
-            return 0;
-        }
-    }
-    
-    static String cleanupMessage(String message) {
-        String result;
-        Matcher matcher = MESSAGE_CLEANUP_PATTERN.matcher(message);
-        if (matcher.matches()) {
-            result = matcher.group(1) + matcher.group(2);
-        }
-        else {
-            result = message;
-        }
-        result = StringUtils.replace(result, "\n", "\\n");
-        result = StringUtils.replace(result, "\r", "");
-        return result;
-    }
-
 }
diff --git a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/JsonSupportTest.java b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/JsonSupportTest.java
index 5cead52..24f13b3 100644
--- a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/JsonSupportTest.java
+++ b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/JsonSupportTest.java
@@ -22,14 +22,12 @@ import static org.apache.sling.maven.bundlesupport.JsonSupport.accumulate;
 import static org.apache.sling.maven.bundlesupport.JsonSupport.parseArray;
 import static org.apache.sling.maven.bundlesupport.JsonSupport.parseObject;
 import static org.apache.sling.maven.bundlesupport.JsonSupport.toJson;
-import static org.apache.sling.maven.bundlesupport.JsonSupport.validateJsonStructure;
 import static org.junit.Assert.assertEquals;
 
 import java.util.HashMap;
 import java.util.Map;
 
 import javax.json.JsonArray;
-import javax.json.JsonException;
 import javax.json.JsonObject;
 
 import org.junit.Test;
@@ -53,23 +51,6 @@ public class JsonSupportTest {
     }
 
     @Test
-    public void testValidateJsonStructure() {
-        validateJsonStructure("{\"prop1\":123}", false);
-        validateJsonStructure("[{\"prop1\":123}]", false);
-        validateJsonStructure("{'prop1':123}", true);
-    }
-
-    @Test(expected=JsonException.class)
-    public void testValidateJsonStructure_Invalid() {
-        validateJsonStructure("wurstbrot", false);
-    }
-
-    @Test(expected=JsonException.class)
-    public void testValidateJsonStructure_InvalidQuoteTick() {
-        validateJsonStructure("{'prop1':123}", false);
-    }
-
-    @Test
     public void testAccumulate_NewValue() {
         Map<String,Object> map = new HashMap<>();
         accumulate(map, "prop1", "value1");
diff --git a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java b/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
deleted file mode 100644
index 584c62f..0000000
--- a/sling-maven-plugin/src/test/java/org/apache/sling/maven/bundlesupport/ValidationMojoTest.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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.maven.bundlesupport;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-public class ValidationMojoTest {
-    
-    private static String MESSAGE = "JSON validation failed: Unexpected character '\n' (Codepoint: 10) "
-            + "on [lineNumber=47, columnNumber=1434, streamOffset=2866]. "
-            + "Reason is [[End of file hit too early]]";
-
-    @Test
-    public void testParseLineNumber() {
-        assertEquals(47, ValidationMojo.parseLineNumber(MESSAGE));
-    }
-
-    @Test
-    public void testParseColumnNumber() throws Exception {
-        assertEquals(1434, ValidationMojo.parseColumnNumber(MESSAGE));
-    }
-
-    @Test
-    public void testCleanupMessage() throws Exception {
-        assertEquals("JSON validation failed: Unexpected character '\\n' (Codepoint: 10). Reason is [[End of file hit too early]]",
-                ValidationMojo.cleanupMessage(MESSAGE));
-    }
-
-}