Skip to content

Commit

Permalink
Fix #5: check test classes (#80)
Browse files Browse the repository at this point in the history
* Bump enforcer-plugin version to support mvn -f or -pl

* Fix #5: Check test classes

* Fix #5: align phase and dep resolution to test class check
  • Loading branch information
famod authored Apr 7, 2020
1 parent 71db56b commit 6363af4
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,23 @@ public class CheckSignatureRule
*/
private String[] excludeDependencies = null;

/**
* Should test classes be checked.
*
* @parameter default-value="true"
* @since 1.19
*/
private boolean checkTestClasses = true;

public void execute( EnforcerRuleHelper helper )
throws EnforcerRuleException
{
try
{
File outputDirectory = new File( (String) helper.evaluate( "${project.build.outputDirectory}" ) );

File testOutputDirectory = new File( (String) helper.evaluate( "${project.build.testOutputDirectory}" ) );

ArtifactResolver resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );

MavenProject project = (MavenProject) helper.evaluate( "${project}" );
Expand Down Expand Up @@ -194,19 +204,22 @@ public void execute( EnforcerRuleHelper helper )
new SignatureChecker( new FileInputStream( a.getFile() ), ignoredPackages, logger );
signatureChecker.setCheckJars( false ); // don't want to descend into jar files that have been copied to
// the output directory as resources.
List<File> sourcePaths = new ArrayList<File>();
Iterator iterator = project.getCompileSourceRoots().iterator();
while ( iterator.hasNext() )
{
String path = (String) iterator.next();
sourcePaths.add( new File( path ) );
}
signatureChecker.setSourcePath( sourcePaths );

signatureChecker.setSourcePath( buildSourcePathList( project ) );

if ( annotations != null )
{
signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) );
}
signatureChecker.process( outputDirectory );

if ( checkTestClasses )
{
signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } );
}
else
{
signatureChecker.process( outputDirectory );
}

if ( signatureChecker.isSignatureBroken() )
{
Expand Down Expand Up @@ -365,4 +378,20 @@ private static String artifactId( Artifact artifact )
artifact.getClassifier() != null ? ":" + artifact.getClassifier() : "" ) + ":" + artifact.getBaseVersion();

}

@SuppressWarnings("unchecked")
private List<File> buildSourcePathList( MavenProject project )
{
List<String> compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() );
if ( checkTestClasses )
{
compileSourceRoots.addAll( project.getTestCompileSourceRoots() );
}
List<File> sourcePathList = new ArrayList<>( compileSourceRoots.size() );
for ( String compileSourceRoot : compileSourceRoots)
{
sourcePathList.add( new File( compileSourceRoot ) );
}
return sourcePathList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Checking a project against API signatures
....
<execution>
<id>check-signatures</id>
<phase>test</phase>
<phase>process-test-classes</phase>
<goals>
<goal>enforce</goal>
</goals>
Expand Down Expand Up @@ -158,7 +158,7 @@ public final class MapFactory {
....
<execution>
<id>check-signatures</id>
<phase>test</phase>
<phase>process-test-classes</phase>
<goals>
<goal>enforce</goal>
</goals>
Expand Down Expand Up @@ -217,7 +217,7 @@ public final class MapFactory {
....
<execution>
<id>check-signatures</id>
<phase>test</phase>
<phase>process-test-classes</phase>
<goals>
<goal>enforce</goal>
</goals>
Expand Down Expand Up @@ -359,7 +359,7 @@ public final class Someclass {
....
<execution>
<id>check-signatures</id>
<phase>test</phase>
<phase>process-test-classes</phase>
<goals>
<goal>enforce</goal>
</goals>
Expand Down
2 changes: 1 addition & 1 deletion animal-sniffer-enforcer-rule/src/site/apt/usage.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Usage
....
<execution>
<id>check-signatures</id>
<phase>test</phase>
<phase>process-test-classes</phase>
<goals>
<goal>enforce</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# The MIT License
#
# Copyright (c) 2009 codehaus.org.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
invoker.goals.1=verify -Danimal.sniffer.checkTestClasses=false

invoker.goals.2=verify
invoker.buildResult.2=failure
85 changes: 85 additions & 0 deletions animal-sniffer-maven-plugin/src/it/github-5-test-classes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright (c) 2009 codehaus.org.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localdomain.localhost</groupId>
<artifactId>test-classes-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Real Test</name>

<description>
Tests that verifies that test classes are checked.
</description>

<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>@mojo.java.target@</source>
<target>@mojo.java.target@</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>${pluginGroupId}</groupId>
<artifactId>${pluginArtifactId}</artifactId>
<version>${pluginVersion}</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java14</artifactId>
<version>1.0</version>
</signature>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<pluginGroupId>@project.groupId@</pluginGroupId>
<pluginArtifactId>@project.artifactId@</pluginArtifactId>
<pluginVersion>@project.version@</pluginVersion>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package localhost;

/*
* The MIT License
*
* Copyright (c) 2009, codehaus.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

public class Main
{
public static void main( String[] args )
{
if ( new java.util.concurrent.ConcurrentHashMap().isEmpty() )
{
System.out.println( "All is good" );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
*
* @author Kohsuke Kawaguchi
*/
@Mojo( name = "check", defaultPhase = LifecyclePhase.PROCESS_CLASSES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
@Mojo( name = "check", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
public class CheckSignatureMojo
extends AbstractMojo
{
Expand All @@ -72,10 +72,26 @@ public class CheckSignatureMojo
@Parameter( defaultValue = "${project.build.outputDirectory}", required = true, readonly = true )
protected File outputDirectory;

/**
* The directory for compiled test classes.
*
* @since 1.19
*/
@Parameter( defaultValue = "${project.build.testOutputDirectory}", required = true, readonly = true )
protected File testOutputDirectory;

/**
* Should test classes be checked.
*
* @since 1.19
*/
@Parameter( property = "animal.sniffer.checkTestClasses", defaultValue = "true" )
protected boolean checkTestClasses;

/**
* Signature module to use.
*/
@Parameter( required = true, property="animal.sniffer.signature" )
@Parameter( required = true, property = "animal.sniffer.signature" )
protected Signature signature;

/**
Expand Down Expand Up @@ -187,6 +203,7 @@ public void setSignature( String signatureId ) {
@Component
protected ArtifactFactory artifactFactory;

@Override
public void execute()
throws MojoExecutionException, MojoFailureException
{
Expand Down Expand Up @@ -250,21 +267,22 @@ public void execute()
new MavenLogger( getLog() ) );
signatureChecker.setCheckJars( false ); // don't want to decend into jar files that have been copied to
// the output directory as resources.
List<File> sourcePaths = new ArrayList<>();
Iterator<String> iterator = project.getCompileSourceRoots().iterator();
while ( iterator.hasNext() )
{
String path = iterator.next();
sourcePaths.add( new File( path ) );
}
signatureChecker.setSourcePath( sourcePaths );

signatureChecker.setSourcePath( buildSourcePathList() );

if ( annotations != null )
{
signatureChecker.setAnnotationTypes( Arrays.asList( annotations ) );
}

signatureChecker.process( outputDirectory );
if ( checkTestClasses )
{
signatureChecker.process( new File[] { outputDirectory, testOutputDirectory } );
}
else
{
signatureChecker.process( outputDirectory );
}

if ( signatureChecker.isSignatureBroken() )
{
Expand Down Expand Up @@ -394,4 +412,20 @@ private void apply( ClassFileVisitor v )
}
}
}

@SuppressWarnings("unchecked")
private List<File> buildSourcePathList( )
{
List<String> compileSourceRoots = new ArrayList<>( project.getCompileSourceRoots() );
if ( checkTestClasses )
{
compileSourceRoots.addAll( project.getTestCompileSourceRoots() );
}
List<File> sourcePathList = new ArrayList<>( compileSourceRoots.size() );
for ( String compileSourceRoot : compileSourceRoots)
{
sourcePathList.add( new File( compileSourceRoot ) );
}
return sourcePathList;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down

0 comments on commit 6363af4

Please sign in to comment.