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 2018/09/12 13:04:53 UTC

[sling-htl-maven-plugin] 02/02: code cleaning

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

commit e7ada3f5aba0dc10a9310982371506a819263f9f
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed Sep 12 15:04:35 2018 +0200

    code cleaning
    
    * added jacoco to check code coverage (> 80% lines)
    * cleaned potential bugs reported by findbugs
---
 pom.xml                                            | 84 ++++++++++++++++++++++
 .../maven/htl/compiler/ScriptCompilationUnit.java  | 14 ++--
 .../apache/sling/maven/htl/ValidateMojoTest.java   | 24 +++++++
 .../generate-java-classes-ignore-imports.pom.xml   | 57 +++++++++++++++
 src/test/resources/test-project/skip.pom.xml       | 56 +++++++++++++++
 5 files changed, 227 insertions(+), 8 deletions(-)

diff --git a/pom.xml b/pom.xml
index 4500ba1..9704138 100644
--- a/pom.xml
+++ b/pom.xml
@@ -61,6 +61,8 @@
             12343846,12344079</site.jira.version.id>
         <maven.version>3.3.9</maven.version>
         <maven.site.path>${project.artifactId}-archives/${project.artifactId}-LATEST</maven.site.path>
+        <jacoco.version>0.8.1</jacoco.version>
+        <argLine/>
     </properties>
 
     <!-- force maven-plugin-testing-harness to use newer plexus container (https://issues.apache.org/jira/browse/MPLUGINTESTING-53) -->
@@ -178,6 +180,12 @@
             <artifactId>slf4j-simple</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.jacoco</groupId>
+            <artifactId>org.jacoco.agent</artifactId>
+            <version>${jacoco.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
@@ -236,6 +244,82 @@
                     <tryUpdate>true</tryUpdate>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>findbugs-maven-plugin</artifactId>
+                <version>3.0.3</version>
+                <configuration>
+                    <effort>Max</effort>
+                    <xmlOutput>true</xmlOutput>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>find-bugs</id>
+                        <phase>process-classes</phase>
+                        <goals>
+                            <goal>check</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.jacoco</groupId>
+                <artifactId>jacoco-maven-plugin</artifactId>
+                <version>${jacoco.version}</version>
+                <executions>
+                    <execution>
+                        <id>default-prepare-agent</id>
+                        <goals>
+                            <goal>prepare-agent</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>check-coverage</id>
+                        <goals>
+                            <goal>check</goal>
+                        </goals>
+                        <configuration>
+                            <haltOnFailure>true</haltOnFailure>
+                            <rules>
+                                <rule>
+                                    <element>BUNDLE</element>
+                                    <limits>
+                                        <limit>
+                                            <counter>INSTRUCTION</counter>
+                                            <value>COVEREDRATIO</value>
+                                            <minimum>0.80</minimum>
+                                        </limit>
+                                    </limits>
+                                </rule>
+                                <rule>
+                                    <element>CLASS</element>
+                                    <limits>
+                                        <limit>
+                                            <counter>INSTRUCTION</counter>
+                                            <value>COVEREDRATIO</value>
+                                            <minimum>0.80</minimum>
+                                        </limit>
+                                    </limits>
+                                </rule>
+                            </rules>
+                            <excludes>
+                                <exclude>org/apache/sling/maven/htl/HelpMojo.class</exclude>
+                            </excludes>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>report</id>
+                        <phase>prepare-package</phase>
+                        <goals>
+                            <goal>report</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 
diff --git a/src/main/java/org/apache/sling/maven/htl/compiler/ScriptCompilationUnit.java b/src/main/java/org/apache/sling/maven/htl/compiler/ScriptCompilationUnit.java
index e482689..9b58b82 100644
--- a/src/main/java/org/apache/sling/maven/htl/compiler/ScriptCompilationUnit.java
+++ b/src/main/java/org/apache/sling/maven/htl/compiler/ScriptCompilationUnit.java
@@ -20,11 +20,15 @@ package org.apache.sling.maven.htl.compiler;
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.Reader;
+import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.sling.scripting.sightly.compiler.CompilationUnit;
 
 public class ScriptCompilationUnit implements CompilationUnit {
@@ -36,7 +40,7 @@ public class ScriptCompilationUnit implements CompilationUnit {
     private static final int _16K = 16384;
 
     public ScriptCompilationUnit(File sourceDirectory, File script) throws FileNotFoundException {
-        reader = new BufferedReader(new FileReader(script), _16K);
+        reader = new BufferedReader(new InputStreamReader(new FileInputStream(script), StandardCharsets.UTF_8), _16K);
         this.sourceDirectory = sourceDirectory;
         this.script = script;
     }
@@ -55,12 +59,6 @@ public class ScriptCompilationUnit implements CompilationUnit {
     }
 
     public void dispose() {
-        try {
-            if (reader != null) {
-                reader.close();
-            }
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
+        IOUtils.closeQuietly(reader);
     }
 }
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 76cbcb8..691b110 100644
--- a/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java
+++ b/src/test/java/org/apache/sling/maven/htl/ValidateMojoTest.java
@@ -57,6 +57,8 @@ public class ValidateMojoTest {
     private static final String DEFAULT_INCLUDES_POM = "default-includes.pom.xml";
     private static final String GENERATE_JAVA_CLASSES_POM = "generate-java-classes.pom.xml";
     private static final String GENERATE_JAVA_CLASSES_WITH_PREFIX_POM = "generate-java-classes-with-prefix.pom.xml";
+    private static final String SKIP_POM = "skip.pom.xml";
+    private static final String GENERATE_JAVA_CLASSES_IGNORE_IMPORTS_POM = "generate-java-classes-ignore-imports.pom.xml";
 
     @Rule
     public MojoRule mojoRule = new MojoRule() {
@@ -171,6 +173,28 @@ public class ValidateMojoTest {
         assertTrue(generatedSourceCode.contains("org.apache.sling.settings.SlingSettingsService.class"));
         assertTrue(generatedSourceCode.contains("apps.projects.Pojo"));
     }
+
+    @Test
+    public void testSkip() throws Exception {
+        File baseDir = new File(System.getProperty("basedir"));
+        ValidateMojo validateMojo = getMojo(baseDir, SKIP_POM);
+        validateMojo.execute();
+        assertEquals(0, validateMojo.getProcessedFiles().size());
+    }
+
+    @Test
+    public void testGenerateJavaClassesIgnoreImports() throws Exception {
+        File baseDir = new File(System.getProperty("basedir"));
+        ValidateMojo validateMojo = getMojo(baseDir, GENERATE_JAVA_CLASSES_IGNORE_IMPORTS_POM);
+        validateMojo.execute();
+        List<File> processedFiles = validateMojo.getProcessedFiles();
+        assertEquals("Expected 1 files to process.", 1, processedFiles.size());
+        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"));
+        assertFalse(generatedSourceCode.contains("import org.apache.sling.settings.SlingSettingsService;"));
         assertTrue(generatedSourceCode.contains("apps.projects.Pojo"));
     }
 
diff --git a/src/test/resources/test-project/generate-java-classes-ignore-imports.pom.xml b/src/test/resources/test-project/generate-java-classes-ignore-imports.pom.xml
new file mode 100644
index 0000000..ca817ca
--- /dev/null
+++ b/src/test/resources/test-project/generate-java-classes-ignore-imports.pom.xml
@@ -0,0 +1,57 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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-generate-java-classes</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>HTL Maven Plugin IT - Generate Java Classes</name>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.sling</groupId>
+                <artifactId>htl-maven-plugin</artifactId>
+                <configuration>
+                    <sourceDirectory>src/main/resources</sourceDirectory>
+                    <!-- only the script.html file will be compiled -->
+                    <includes>
+                        <include>**/script.html</include>
+                    </includes>
+                    <failOnWarnings>true</failOnWarnings>
+                    <generateJavaClasses>true</generateJavaClasses>
+                    <ignoreImports>org.apache.sling.settings</ignoreImports>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>validate-scripts</id>
+                        <goals>
+                            <goal>validate</goal>
+                        </goals>
+                        <phase>generate-sources</phase>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/src/test/resources/test-project/skip.pom.xml b/src/test/resources/test-project/skip.pom.xml
new file mode 100644
index 0000000..3716fea
--- /dev/null
+++ b/src/test/resources/test-project/skip.pom.xml
@@ -0,0 +1,56 @@
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+  ~ 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-skip</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>HTL Maven Plugin IT - Generate Java Classes</name>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.sling</groupId>
+                <artifactId>htl-maven-plugin</artifactId>
+                <configuration>
+                    <sourceDirectory>src/main/resources</sourceDirectory>
+                    <!-- only the script.html file will be compiled -->
+                    <includes>
+                        <include>**/script.html</include>
+                    </includes>
+                    <failOnWarnings>true</failOnWarnings>
+                    <skip>true</skip>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>validate-scripts</id>
+                        <goals>
+                            <goal>validate</goal>
+                        </goals>
+                        <phase>generate-sources</phase>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>