You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/12/02 12:30:47 UTC

[sling-htl-maven-plugin] branch master updated: SLING-8860 - Issue a warning when data-sly-test is passed a constant value for evaluation

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

radu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-htl-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 923db43  SLING-8860 - Issue a warning when data-sly-test is passed a constant value for evaluation
923db43 is described below

commit 923db432dddfc6a1130e72e5627e83ee50211207
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Mon Dec 2 13:29:50 2019 +0100

    SLING-8860 - Issue a warning when data-sly-test is passed a constant value for evaluation
    
    * updated compiler version and added new test
    * minor code cleanup
---
 pom.xml                                            |  11 ++-
 .../apache/sling/maven/htl/ValidateMojoTest.java   | 103 ++++++++++++++-------
 .../data-sly-test-constant-values.pom.xml          |  53 +++++++++++
 .../projects/data-sly-test-constant-values.sly     |  23 +++++
 4 files changed, 152 insertions(+), 38 deletions(-)

diff --git a/pom.xml b/pom.xml
index ec9e01b..fcf2eb7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -50,12 +50,9 @@
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <site.jira.version.id>12336741,12338350,12338646,12338974,12341271,12342047,12342267,12342272,12343462,
-            12343846,12344079,12344332,12344876,12345571</site.jira.version.id>
+            12343846,12344079,12344332,12344876,12345571,12346551</site.jira.version.id>
         <maven.version>3.3.9</maven.version>
         <maven.site.path>${project.artifactId}-archives/${project.artifactId}-LATEST</maven.site.path>
-        <sightly.runtime.version>1.1.1-1.4.0-SNAPSHOT</sightly.runtime.version>
-        <sightly.compiler.version>1.2.0-1.4.0-SNAPSHOT</sightly.compiler.version>
-        <sightly.compiler.java.version>1.1.3-1.4.0-SNAPSHOT</sightly.compiler.java.version>
         <argLine />
     </properties>
 
@@ -176,6 +173,12 @@
             <artifactId>slf4j-simple</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>3.0.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java b/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java
index 34d0280..61d18c7 100644
--- a/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java
+++ b/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java
@@ -1,24 +1,25 @@
-/*******************************************************************************
- * 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.
- ******************************************************************************/
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.htl;
 
 import java.io.File;
-import java.io.IOException;
-import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.List;
 
 import org.apache.commons.io.FileUtils;
@@ -37,6 +38,7 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
+import org.sonatype.plexus.build.incremental.BuildContext;
 import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
 import static org.junit.Assert.assertEquals;
@@ -44,6 +46,14 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 public class ValidateMojoTest {
 
@@ -53,6 +63,7 @@ public class ValidateMojoTest {
     private static final String EXCLUDE_HTML = "src/main/resources/apps/projects/exclude.html";
     private static final String INVALID_OPTIONS_SLY = "src/main/resources/apps/projects/invalid-options.sly";
     private static final String NON_DEFAULT_OPTIONS_SLY = "src/main/resources/apps/projects/non-default-options.sly";
+    private static final String DATA_SLY_TEST_CONSTANT_VALUES_SLY = "src/main/resources/apps/projects/data-sly-test-constant-values.sly";
     private static final String TEST_PROJECT = "test-project";
     private static final String EXPLICIT_INCLUDES_POM = "explicit-includes.pom.xml";
     private static final String EXPLICIT_EXCLUDES_POM = "explicit-excludes.pom.xml";
@@ -64,6 +75,7 @@ public class ValidateMojoTest {
     private static final String GENERATE_JAVA_CLASSES_IGNORE_IMPORTS_POM = "generate-java-classes-ignore-imports.pom.xml";
     private static final String INVALID_OPTIONS_POM = "invalid-options.pom.xml";
     private static final String NON_DEFAULT_OPTIONS_POM = "non-default-options.pom.xml";
+    private static final String DATA_SLY_TEST_CONSTANT_VALUES_POM_XML = "data-sly-test-constant-values.pom.xml";
 
 
     @Rule
@@ -80,7 +92,7 @@ public class ValidateMojoTest {
     };
 
     @After
-    public void tearDown() throws IOException {
+    public void tearDown() {
         File baseDir = new File(System.getProperty("basedir"));
         FileUtils.deleteQuietly(new File(baseDir, "target"));
     }
@@ -93,11 +105,11 @@ public class ValidateMojoTest {
             validateMojo.execute();
         } catch (MojoFailureException e) {
             List<File> processedFiles = validateMojo.getProcessedFiles();
-            assertEquals("Expected 4 files to process.", 4, processedFiles.size());
+            assertEquals("Expected 5 files to process.", 5, processedFiles.size());
             assertTrue("Expected error.sly to be one of the processed files.", processedFiles.contains(new File(baseDir, ERROR_SLY)));
             assertTrue("Expected warning.sly to be one of the processed files.", processedFiles.contains(new File(baseDir, WARNING_SLY)));
-            assertEquals("Expected compilation errors.", true, validateMojo.hasErrors());
-            assertEquals("Expected compilation warnings.", true, validateMojo.hasWarnings());
+            assertTrue("Expected compilation errors.", validateMojo.hasErrors());
+            assertTrue("Expected compilation warnings.", validateMojo.hasWarnings());
         }
     }
 
@@ -109,8 +121,8 @@ public class ValidateMojoTest {
         List<File> processedFiles = validateMojo.getProcessedFiles();
         assertEquals("Expected 1 file to process.", 1, processedFiles.size());
         assertTrue("Expected script.html to be the only processed file.", processedFiles.contains(new File(baseDir, SCRIPT_HTML)));
-        assertEquals("Did not expect compilation errors.", false, validateMojo.hasErrors());
-        assertEquals("Did not expect compilation warnings.", false, validateMojo.hasWarnings());
+        assertFalse("Did not expect compilation errors.", validateMojo.hasErrors());
+        assertFalse("Did not expect compilation warnings.", validateMojo.hasWarnings());
     }
 
     @Test
@@ -127,7 +139,7 @@ public class ValidateMojoTest {
         assertNotNull("Expected a MojoFailureException.", exception);
         assertEquals("Expected 1 file to process.", 1, processedFiles.size());
         assertTrue("Expected warning.sly to be one of the processed files.", processedFiles.contains(new File(baseDir, WARNING_SLY)));
-        assertEquals("Expected compilation warnings.", true, validateMojo.hasWarnings());
+        assertTrue("Expected compilation warnings.", validateMojo.hasWarnings());
     }
 
     @Test
@@ -158,8 +170,7 @@ public class ValidateMojoTest {
         assertTrue("Expected script.html to be one of the processed files.", processedFiles.contains(new File(baseDir,
                 SCRIPT_HTML)));
         String generatedSourceCode = FileUtils.readFileToString(new File(baseDir,
-                "target/generated-sources/htl/apps/projects/script_html.java"), Charset
-                .forName("UTF-8"));
+                "target/generated-sources/htl/apps/projects/script_html.java"), StandardCharsets.UTF_8);
         assertTrue(generatedSourceCode.contains("org.apache.sling.settings.SlingSettingsService.class"));
         assertTrue(generatedSourceCode.contains("apps.projects.Pojo"));
     }
@@ -174,8 +185,7 @@ public class ValidateMojoTest {
         assertTrue("Expected script.html to be one of the processed files.", processedFiles.contains(new File(baseDir,
                 SCRIPT_HTML)));
         String generatedSourceCode = FileUtils.readFileToString(new File(baseDir,
-                "target/generated-sources/htl/org/apache/sling/scripting/sightly/apps/projects/script_html.java"), Charset
-                .forName("UTF-8"));
+                "target/generated-sources/htl/org/apache/sling/scripting/sightly/apps/projects/script_html.java"), StandardCharsets.UTF_8);
         assertTrue(generatedSourceCode.contains("org.apache.sling.settings.SlingSettingsService.class"));
         assertTrue(generatedSourceCode.contains("apps.projects.Pojo"));
     }
@@ -198,8 +208,7 @@ public class ValidateMojoTest {
         assertTrue("Expected script.html to be one of the processed files.", processedFiles.contains(new File(baseDir,
                 SCRIPT_HTML)));
         String generatedSourceCode = FileUtils.readFileToString(new File(baseDir,
-                "target/generated-sources/htl/apps/projects/script_html.java"), Charset
-                .forName("UTF-8"));
+                "target/generated-sources/htl/apps/projects/script_html.java"), StandardCharsets.UTF_8);
         assertFalse(generatedSourceCode.contains("import org.apache.sling.settings.SlingSettingsService;"));
         assertTrue(generatedSourceCode.contains("apps.projects.Pojo"));
     }
@@ -241,10 +250,37 @@ public class ValidateMojoTest {
         assertFalse("Did not expect compilation errors.", validateMojo.hasErrors());
     }
 
+    @Test
+    public void testDataSlyTestConstantValues() throws Exception {
+        DefaultBuildContext context = spy(new DefaultBuildContext());
+        File baseDir = new File(System.getProperty("basedir"));
+        ValidateMojo validateMojo = getMojo(baseDir, DATA_SLY_TEST_CONSTANT_VALUES_POM_XML, context);
+        Exception exception = null;
+        try {
+            validateMojo.execute();
+        } catch (MojoFailureException e) {
+            exception = e;
+        }
+        List<File> processedFiles = validateMojo.getProcessedFiles();
+        assertNotNull("Expected a MojoFailureException.", exception);
+        assertEquals("Expected 1 file to process.", 1, processedFiles.size());
+        assertTrue("Expected " + DATA_SLY_TEST_CONSTANT_VALUES_SLY + " to be the processed file.", processedFiles.contains(new File(baseDir,
+                DATA_SLY_TEST_CONSTANT_VALUES_SLY)));
+        assertTrue("Expected compilation warnings.", validateMojo.hasWarnings());
+        verify(context, times(5)).addMessage(any(), anyInt(), anyInt(), anyString(), eq(BuildContext.SEVERITY_WARNING),
+                isNull());
+    }
+
     private ValidateMojo getMojo(File baseDir, String pomFile) throws Exception {
-        SilentLog log = new SilentLog();
-        DefaultBuildContext buildContext = new DefaultBuildContext();
+        return getMojo(baseDir, pomFile, null);
+    }
 
+    private ValidateMojo getMojo(File baseDir, String pomFile, DefaultBuildContext buildContext) throws Exception {
+        SilentLog log = new SilentLog();
+        if (buildContext == null) {
+            buildContext = new DefaultBuildContext();
+        }
+        buildContext.enableLogging(log);
         // use lookupConfiguredMojo to also consider default values (https://issues.apache.org/jira/browse/MPLUGINTESTING-23)
         // similar to MojoRule#lookupConfiguredMojo(File, String) but with custom pom file name
         MavenProject project = readMavenProject(baseDir, pomFile);
@@ -252,7 +288,6 @@ public class ValidateMojoTest {
         MojoExecution execution = mojoRule.newMojoExecution("validate");
         ValidateMojo validateMojo = (ValidateMojo) mojoRule.lookupConfiguredMojo(session, execution);
         validateMojo.setLog(log);
-        buildContext.enableLogging(log);
         validateMojo.setBuildContext(buildContext);
         return validateMojo;
     }
diff --git a/src/test/resources/test-project/data-sly-test-constant-values.pom.xml b/src/test/resources/test-project/data-sly-test-constant-values.pom.xml
new file mode 100644
index 0000000..ce86780
--- /dev/null
+++ b/src/test/resources/test-project/data-sly-test-constant-values.pom.xml
@@ -0,0 +1,53 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.apache.sling</groupId>
+    <artifactId>htl-maven-plugin-it-data-sly-test-string-constant</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>pom</packaging>
+
+    <name>HTL Maven Plugin IT - Fail On Warnings</name>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.sling</groupId>
+                <artifactId>htl-maven-plugin</artifactId>
+                <configuration>
+                    <sourceDirectory>src/main/resources</sourceDirectory>
+                    <!-- only the warning.sly file will be compiled -->
+                    <includes>
+                        <include>**/data-sly-test-constant-values.sly</include>
+                    </includes>
+                    <failOnWarnings>true</failOnWarnings>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>validate-scripts</id>
+                        <goals>
+                            <goal>validate</goal>
+                        </goals>
+                        <phase>compile</phase>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/src/test/resources/test-project/src/main/resources/apps/projects/data-sly-test-constant-values.sly b/src/test/resources/test-project/src/main/resources/apps/projects/data-sly-test-constant-values.sly
new file mode 100644
index 0000000..62485ca
--- /dev/null
+++ b/src/test/resources/test-project/src/main/resources/apps/projects/data-sly-test-constant-values.sly
@@ -0,0 +1,23 @@
+<!--/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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.
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
+<sly data-sly-test="${'a'}"></sly>
+<sly data-sly-test="${0}"></sly>
+<sly data-sly-test="${true}"></sly>
+<sly data-sly-test="${}"></sly>
+<sly data-sly-test="${properties}}"></sly>