Improve JDeprScanConsumer

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@1812898 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/projects/jdeprscan-release8/invoker.properties b/src/it/projects/jdeprscan-release8/invoker.properties
index cb52a4c..c99dc4e 100644
--- a/src/it/projects/jdeprscan-release8/invoker.properties
+++ b/src/it/projects/jdeprscan-release8/invoker.properties
@@ -14,4 +14,5 @@
 # KIND, either express or implied.  See the License for the

 # specific language governing permissions and limitations

 # under the License.

+invoker.buildResult = failure

 invoker.goals = verify
\ No newline at end of file
diff --git a/src/it/projects/jdeprscan-release9/invoker.properties b/src/it/projects/jdeprscan-release9/invoker.properties
index cb52a4c..c99dc4e 100644
--- a/src/it/projects/jdeprscan-release9/invoker.properties
+++ b/src/it/projects/jdeprscan-release9/invoker.properties
@@ -14,4 +14,5 @@
 # KIND, either express or implied.  See the License for the

 # specific language governing permissions and limitations

 # under the License.

+invoker.buildResult = failure

 invoker.goals = verify
\ No newline at end of file
diff --git a/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java b/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java
index d712345..a40977c 100644
--- a/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jdeprscan/AbstractJDeprScanMojo.java
@@ -35,7 +35,6 @@
 import org.apache.maven.plugin.MojoFailureException;

 import org.apache.maven.plugins.annotations.Component;

 import org.apache.maven.plugins.annotations.Parameter;

-import org.apache.maven.plugins.jdeprscan.consumers.JDeprScanConsumer;

 import org.apache.maven.toolchain.Toolchain;

 import org.apache.maven.toolchain.ToolchainManager;

 import org.codehaus.plexus.util.StringUtils;

@@ -55,9 +54,6 @@
     @Parameter( defaultValue = "${session}", readonly = true, required = true )

     private MavenSession session;

     

-    

-    @Parameter()

-

     @Component

     private ToolchainManager toolchainManager;

 

@@ -82,7 +78,18 @@
 

         addJDeprScanOptions( cmd );

 

-        executeJDeprScanCommandLine( cmd, new JDeprScanConsumer() );

+        executeJDeprScanCommandLine( cmd, getConsumer() );

+        

+        verify();

+    }

+    

+    protected CommandLineUtils.StringStreamConsumer getConsumer()

+    {

+      return null;    

+    }

+    

+    protected void verify() throws MojoExecutionException

+    {

     }

     

     protected abstract boolean isForRemoval();

@@ -192,13 +199,13 @@
 

             String output = ( StringUtils.isEmpty( out.getOutput() ) ? null : '\n' + out.getOutput().trim() );

 

+            if ( StringUtils.isNotEmpty( output ) )

+            {

+                getLog().info( output );

+            }

+

             if ( exitCode != 0 )

             {

-                if ( StringUtils.isNotEmpty( output ) )

-                {

-                    getLog().info( output );

-                }

-

                 StringBuilder msg = new StringBuilder( "\nExit code: " );

                 msg.append( exitCode );

                 if ( StringUtils.isNotEmpty( err.getOutput() ) )

@@ -211,10 +218,6 @@
                 throw new MojoExecutionException( msg.toString() );

             }

 

-            if ( StringUtils.isNotEmpty( output ) )

-            {

-                getLog().info( output );

-            }

         }

         catch ( CommandLineException e )

         {

diff --git a/src/main/java/org/apache/maven/plugins/jdeprscan/BaseJDeprScanMojo.java b/src/main/java/org/apache/maven/plugins/jdeprscan/BaseJDeprScanMojo.java
index 97cb8f8..763d90e 100644
--- a/src/main/java/org/apache/maven/plugins/jdeprscan/BaseJDeprScanMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jdeprscan/BaseJDeprScanMojo.java
@@ -23,15 +23,19 @@
 import java.nio.file.Files;

 import java.nio.file.Path;

 import java.util.Collection;

+import java.util.Map;

+import java.util.Set;

 

 import org.apache.maven.artifact.DependencyResolutionRequiredException;

 import org.apache.maven.plugin.MojoExecutionException;

 import org.apache.maven.plugin.MojoFailureException;

 

 import org.apache.maven.plugins.annotations.Parameter;

+import org.apache.maven.plugins.jdeprscan.consumers.JDeprScanConsumer;

 import org.apache.maven.project.MavenProject;

 import org.codehaus.plexus.util.StringUtils;

 import org.codehaus.plexus.util.cli.Commandline;

+import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;

 

 /**

  * Base class for all explicit jdeprscan mojos

@@ -63,13 +67,15 @@
     @Parameter

     private String release;

     

+    private JDeprScanConsumer consumer = new JDeprScanConsumer();

+    

     @Override

     public void execute()

         throws MojoExecutionException, MojoFailureException

     {

         if ( !Files.exists( getClassesDirectory() ) )

         {

-            getLog().debug( "No classes to analyze" );

+            getLog().debug( "No classes to scan" );

             return;

         }

         super.execute();

@@ -87,6 +93,12 @@
     }

     

     @Override

+    protected StringStreamConsumer getConsumer()

+    {

+        return consumer;

+    }

+    

+    @Override

     protected final void addJDeprScanOptions( Commandline cmd ) throws MojoFailureException

     {

         super.addJDeprScanOptions( cmd );

@@ -117,7 +129,46 @@
         

         cmd.createArg().setFile( getClassesDirectory().toFile() );

     }

-    

+

+    @Override

+    protected void verify() throws MojoExecutionException

+    {

+        if ( !( consumer.getDeprecatedClasses().isEmpty() && consumer.getDeprecatedMethods().isEmpty() ) )

+        {

+            if ( !consumer.getDeprecatedClasses().isEmpty() )

+            {

+                getLog().warn( "Found usage of deprecated classes:" );

+                

+                for ( Map.Entry<String, Set<String>> classes : consumer.getDeprecatedClasses().entrySet() )

+                {

+                    getLog().warn( "class " + classes.getKey() + " uses deprecated class(es)" );

+                    for ( String deprClass : classes.getValue() )

+                    {

+                        getLog().warn( "  * " + deprClass );

+                    }

+                }

+            }

+            

+            if ( !consumer.getDeprecatedMethods().isEmpty() )

+            {

+                getLog().warn( "Found usage of deprecated methods:" );

+                

+                for ( Map.Entry<String, Set<String>> classes : consumer.getDeprecatedMethods().entrySet() )

+                {

+                    getLog().warn( "class " + classes.getKey() + " uses deprecated method(s)" );

+                    for ( String deprMethod : classes.getValue() )

+                    {

+                        getLog().warn( "  * " + deprMethod );

+                    }

+                }

+            }

+            

+            if ( failOnWarning )

+            {

+                throw new MojoExecutionException( "JDeprScan detected usage of deprecated classes/methods" );

+            }

+        }

+    }

 

     protected abstract Path getClassesDirectory();

     

diff --git a/src/main/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumer.java b/src/main/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumer.java
index 3983efd..1a5d51c 100644
--- a/src/main/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumer.java
+++ b/src/main/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumer.java
@@ -19,6 +19,13 @@
  * under the License.

  */

 

+import java.util.HashMap;

+import java.util.HashSet;

+import java.util.Map;

+import java.util.Set;

+import java.util.regex.Matcher;

+import java.util.regex.Pattern;

+

 import org.codehaus.plexus.util.cli.CommandLineUtils;

 import org.codehaus.plexus.util.cli.StreamConsumer;

 

@@ -33,4 +40,55 @@
     implements StreamConsumer

 {

 

+    private Map<String, Set<String>> deprecatedClasses = new HashMap<>();

+

+    private Map<String, Set<String>> deprecatedMethods = new HashMap<>();

+

+    public static final Pattern DEPRECATED_CLASS = Pattern.compile( "^class (\\S+) uses deprecated class (\\S+)" );

+

+    public static final Pattern DEPRECATED_METHOD = Pattern.compile( "^class (\\S+) uses deprecated method (\\S+)" );

+

+    public Map<String, Set<String>> getDeprecatedClasses()

+    {

+        return deprecatedClasses;

+    }

+    

+    public Map<String, Set<String>> getDeprecatedMethods()

+    {

+        return deprecatedMethods;

+    }

+    

+    @Override

+    public void consumeLine( String line )

+    {

+        super.consumeLine( line );

+

+        Matcher matcher;

+        

+        matcher = DEPRECATED_CLASS.matcher( line );

+        if ( matcher.find() )

+        {

+            Set<String> dc = deprecatedClasses.get( matcher.group( 1 ) );

+            if ( dc == null )

+            {

+                dc = new HashSet<>();

+                deprecatedClasses.put( matcher.group( 1 ), dc );

+            }

+            dc.add( matcher.group( 2 ) );

+            return;

+        }

+        

+        matcher = DEPRECATED_METHOD.matcher( line );

+        if ( matcher.find() )

+        {

+            Set<String> dm = deprecatedMethods.get( matcher.group( 1 ) );

+            if ( dm == null )

+            {

+                dm = new HashSet<>();

+                deprecatedMethods.put( matcher.group( 1 ), dm );

+            }

+            dm.add( matcher.group( 2 ) );

+            return;

+        }

+    }

 }

diff --git a/src/test/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumerTest.java b/src/test/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumerTest.java
new file mode 100644
index 0000000..0126ddb
--- /dev/null
+++ b/src/test/java/org/apache/maven/plugins/jdeprscan/consumers/JDeprScanConsumerTest.java
@@ -0,0 +1,63 @@
+package org.apache.maven.plugins.jdeprscan.consumers;

+

+/*

+ * 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 static org.junit.Assert.assertEquals;

+

+import java.util.Collections;

+import java.util.Set;

+

+import org.junit.Before;

+import org.junit.Test;

+

+public class JDeprScanConsumerTest

+{

+    private JDeprScanConsumer consumer;

+    

+    @Before

+    public void setUp()

+    {

+        consumer = new JDeprScanConsumer();

+    }

+

+    @Test

+    public void testDeprecatedClass()

+    {

+        consumer.consumeLine( "class o/a/m/p/j/its/Deprecations uses deprecated class java/rmi/RMISecurityManager " );

+        

+        assertEquals( consumer.getDeprecatedClasses().size(), 1 );

+        assertEquals( consumer.getDeprecatedMethods().size(), 0 );

+        

+        Set<String> classes = consumer.getDeprecatedClasses().get( "o/a/m/p/j/its/Deprecations" );

+        assertEquals( Collections.singleton( "java/rmi/RMISecurityManager" ), classes );

+    }

+

+    @Test

+    public void testDeprecatedMethod()

+    {

+        consumer.consumeLine( "class o/a/m/p/j/its/Deprecations uses deprecated method java/lang/Boolean::<init>(Z)V" );

+        

+        assertEquals( consumer.getDeprecatedClasses().size(), 0 );

+        assertEquals( consumer.getDeprecatedMethods().size(), 1 );

+        

+        Set<String> methods = consumer.getDeprecatedMethods().get( "o/a/m/p/j/its/Deprecations" );

+        assertEquals( Collections.singleton( "java/lang/Boolean::<init>(Z)V" ), methods );

+    }

+}