[MCHECKSTYLE-337] checkstyle:check only supports xml output format, but the docs say it supports plain as well
Submitted by: Stig Rohde Døssing

Adding support for 'plain' output file format in the 'check' goal. It is still not possible to use it in combination with 'skipExec' because the plain format does not interact fully with 'violationIgnore' (which can contain fully qualified class name, when Checkstyle plain format only has the class simple name).

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-checkstyle-plugin@1797596 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/MCHECKSTYLE-337/invoker.properties b/src/it/MCHECKSTYLE-337/invoker.properties
new file mode 100644
index 0000000..407fb91
--- /dev/null
+++ b/src/it/MCHECKSTYLE-337/invoker.properties
@@ -0,0 +1,20 @@
+# 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.
+
+invoker.goals = verify
+invoker.buildResult = failure
+invoker.mavenOpts = -Duser.language=en -Duser.country=US -Duser.variant=US
diff --git a/src/it/MCHECKSTYLE-337/pom.xml b/src/it/MCHECKSTYLE-337/pom.xml
new file mode 100644
index 0000000..3dbbf1a
--- /dev/null
+++ b/src/it/MCHECKSTYLE-337/pom.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.maven.plugins.checkstyle</groupId>
+  <artifactId>MCHECKSTYLE-337</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <url>http://maven.apache.org/</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <checkstyle.violation.ignore>NewlineAtEndOfFile</checkstyle.violation.ignore>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <version>@pom.version@</version>
+        <executions>
+          <execution>
+            <id>check</id>
+            <goals>
+              <goal>check</goal>
+            </goals>
+            <configuration>
+              <outputFile>${project.build.directory}/checkstyle-result.txt</outputFile>
+              <outputFileFormat>plain</outputFileFormat>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/MCHECKSTYLE-337/src/main/java/org/MyClass.java b/src/it/MCHECKSTYLE-337/src/main/java/org/MyClass.java
new file mode 100644
index 0000000..666efd9
--- /dev/null
+++ b/src/it/MCHECKSTYLE-337/src/main/java/org/MyClass.java
@@ -0,0 +1,27 @@
+package org;
+
+/*
+ * 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.
+ */
+
+/**
+ * My class.
+ */
+public class MyClass
+{
+}
diff --git a/src/it/MCHECKSTYLE-337/src/main/java/org/package-info.java b/src/it/MCHECKSTYLE-337/src/main/java/org/package-info.java
new file mode 100644
index 0000000..6d0409e
--- /dev/null
+++ b/src/it/MCHECKSTYLE-337/src/main/java/org/package-info.java
@@ -0,0 +1,23 @@
+/**
+ * Nothing very important here, only a file for checkstyle rule PackageInfo.
+ */
+package org;
+
+/*
+ * 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.
+ */
diff --git a/src/it/MCHECKSTYLE-337/verify.groovy b/src/it/MCHECKSTYLE-337/verify.groovy
new file mode 100644
index 0000000..5bb7ba8
--- /dev/null
+++ b/src/it/MCHECKSTYLE-337/verify.groovy
@@ -0,0 +1,26 @@
+
+/*
+ * 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.
+ */
+def buildLog = new File( basedir, 'build.log' )
+
+assert buildLog.text.contains( "You have 1 Checkstyle violation" )
+
+def checkstyleResult = new File( basedir, "target/checkstyle-result.txt" )
+
+assert checkstyleResult.text.contains( "'{' at column 1 should be on the previous line. [LeftCurly]" )
diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
index bd568d0..1367762 100644
--- a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
+++ b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java
@@ -485,6 +485,8 @@
 
     private ByteArrayOutputStream stringOutputStream;
 
+    private File outputXmlFile;
+
     /** {@inheritDoc} */
     public void execute()
         throws MojoExecutionException, MojoFailureException
@@ -496,6 +498,8 @@
             return;
         }
 
+        outputXmlFile = outputFile;
+
         if ( !skipExec )
         {
             if ( checkstyleRules != null )
@@ -564,19 +568,19 @@
             }
         }
 
-        if ( !"xml".equals( outputFileFormat ) )
+        if ( !"xml".equals( outputFileFormat ) && skipExec )
         {
             throw new MojoExecutionException( "Output format is '" + outputFileFormat
-                + "', checkstyle:check requires format to be 'xml'." );
+                + "', checkstyle:check requires format to be 'xml' when using skipExec." );
         }
 
-        if ( !outputFile.exists() )
+        if ( !outputXmlFile.exists() )
         {
             getLog().info( "Unable to perform checkstyle:check, unable to find checkstyle:checkstyle outputFile." );
             return;
         }
 
-        try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( outputFile ) ) )
+        try ( Reader reader = new BufferedReader( ReaderFactory.newXmlReader( outputXmlFile ) ) )
         {
             XmlPullParser xpp = new MXParser();
             xpp.setInput( reader );
@@ -601,8 +605,8 @@
         }
         catch ( IOException | XmlPullParserException e )
         {
-            throw new MojoExecutionException( "Unable to read Checkstyle results xml: " + outputFile.getAbsolutePath(),
-                                              e );
+            throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
+                + outputXmlFile.getAbsolutePath(), e );
         }
     }
 
@@ -799,7 +803,22 @@
             }
             else if ( "plain".equals( outputFileFormat ) )
             {
-                listener = new DefaultLogger( out, true );
+                try
+                {
+                    // Write a plain output file to the standard output file,
+                    // and write an XML output file to the temp directory that can be used to count violations
+                    outputXmlFile = File.createTempFile( "checkstyle-result", ".xml" );
+                    outputXmlFile.deleteOnExit();
+                    OutputStream xmlOut = getOutputStream( outputXmlFile );
+                    CompositeAuditListener compoundListener = new CompositeAuditListener();
+                    compoundListener.addListener( new XMLLogger( xmlOut, true ) );
+                    compoundListener.addListener( new DefaultLogger( out, true ) );
+                    listener = compoundListener;
+                }
+                catch ( IOException e )
+                {
+                    throw new MojoExecutionException( "Unable to create temporary file", e );
+                }
             }
             else
             {
diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/CompositeAuditListener.java b/src/main/java/org/apache/maven/plugins/checkstyle/CompositeAuditListener.java
new file mode 100644
index 0000000..505e63b
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/checkstyle/CompositeAuditListener.java
@@ -0,0 +1,96 @@
+package org.apache.maven.plugins.checkstyle;
+
+/*
+ * 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.
+ */
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.puppycrawl.tools.checkstyle.api.AuditEvent;
+import com.puppycrawl.tools.checkstyle.api.AuditListener;
+
+/**
+ * AuditListener that forwards events to a list of other AuditListeners
+ */
+public class CompositeAuditListener
+    implements AuditListener
+{
+
+    private final List<AuditListener> delegates = new ArrayList<>();
+
+    public void addListener( AuditListener listener )
+    {
+        delegates.add( listener );
+    }
+
+    @Override
+    public void auditStarted( AuditEvent event )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.auditStarted( event );
+        }
+    }
+
+    @Override
+    public void auditFinished( AuditEvent event )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.auditFinished( event );
+        }
+    }
+
+    @Override
+    public void fileStarted( AuditEvent event )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.fileStarted( event );
+        }
+    }
+
+    @Override
+    public void fileFinished( AuditEvent event )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.fileFinished( event );
+        }
+    }
+
+    @Override
+    public void addError( AuditEvent event )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.addError( event );
+        }
+    }
+
+    @Override
+    public void addException( AuditEvent event, Throwable throwable )
+    {
+        for ( AuditListener listener : delegates )
+        {
+            listener.addException( event, throwable );
+        }
+    }
+
+}
diff --git a/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojoTest.java b/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojoTest.java
index d86335a..73d3dc6 100644
--- a/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojoTest.java
+++ b/src/test/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojoTest.java
@@ -26,6 +26,7 @@
 import org.apache.maven.plugin.Mojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
 import org.apache.maven.plugins.checkstyle.CheckstyleViolationCheckMojo;
@@ -65,7 +66,7 @@
         }
     }
 
-    public void testInvalidFormat()
+    public void testInvalidFormatWithSkipExec()
         throws Exception
     {
         File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-config.xml" );
@@ -106,6 +107,46 @@
         mojo.execute();
     }
 
+    private void doTestPlainOutputFile( boolean failsOnError )
+        throws Exception
+    {
+        File pluginXmlFile = new File( getBasedir(), "src/test/plugin-configs/check-plugin-plain-output.xml" );
+
+        Mojo mojo = lookupMojo( "check", pluginXmlFile );
+
+        assertNotNull( "Mojo found.", mojo );
+
+        PluginDescriptor descriptorStub = new PluginDescriptor();
+        descriptorStub.setGroupId( "org.apache.maven.plugins" );
+        descriptorStub.setArtifactId( "maven-checkstyle-plugin" );
+        setVariableValueToObject( mojo, "plugin", descriptorStub );
+
+        setVariableValueToObject( mojo, "failsOnError", failsOnError );
+
+        mojo.execute();
+    }
+
+    public void testPlainOutputFileFailOnError()
+        throws Exception
+    {
+        try
+        {
+            doTestPlainOutputFile( true );
+
+            fail( "Must fail on violations" );
+        }
+        catch ( MojoExecutionException e )
+        {
+            // expected
+        }
+    }
+
+    public void testPlainOutputFile()
+        throws Exception
+    {
+        doTestPlainOutputFile( false );
+    }
+
     public void testNoFail()
         throws Exception
     {
diff --git a/src/test/plugin-configs/check-plugin-plain-output.xml b/src/test/plugin-configs/check-plugin-plain-output.xml
new file mode 100644
index 0000000..4d5e29c
--- /dev/null
+++ b/src/test/plugin-configs/check-plugin-plain-output.xml
@@ -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.
+-->
+
+<project>
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-checkstyle-plugin</artifactId>
+        <configuration>
+          <configLocation>sun_checks.xml</configLocation>
+          <cacheFile>${basedir}/target/test-harness/checkstyle/check-plugin-plain-output/checkstyle-cachefile</cacheFile>
+          <outputFile>${basedir}/target/test-harness/checkstyle/check-plugin-plain-output/checkstyle-result.txt</outputFile>
+          <outputFileFormat>plain</outputFileFormat>
+          <failsOnError>true</failsOnError>
+          <sourceDirectories>
+            <sourceDirectory>${basedir}/src/test/test-sources</sourceDirectory>
+          </sourceDirectories>
+          <project implementation="org.apache.maven.plugins.checkstyle.stubs.MinMavenProjectStub"/>
+          <consoleOutput>false</consoleOutput>
+          <encoding>UTF-8</encoding>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>