#544 run some checkstyle rules on sources generated by our tests

This commit is contained in:
Andreas Gudian 2015-05-15 01:37:41 +02:00 committed by sjaakd
parent 32828ff9eb
commit 4a2f0c01ec
6 changed files with 209 additions and 8 deletions

View File

@ -109,7 +109,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.jolira</groupId>
@ -473,12 +473,14 @@
<exclude>**/mapstruct.xml</exclude>
<exclude>**/toolchains-*.xml</exclude>
<exclude>**/eclipse-formatter-config.xml</exclude>
<exclude>**/checkstyle-for-generated-sources.xml</exclude>
<exclude>maven-settings.xml</exclude>
<exclude>readme.md</exclude>
<exclude>CONTRIBUTING.md</exclude>
<exclude>.gitattributes</exclude>
<exclude>.gitignore</exclude>
<exclude>.factorypath</exclude>
<exclude>.checkstyle</exclude>
</excludes>
</configuration>
<executions>

View File

@ -74,6 +74,12 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>6.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>

View File

@ -26,7 +26,7 @@
<#else>
<#if existingInstanceMapping>
${resultName}.clear();
return<#if returnType.name != "void"> ${resultName} </#if>;
return<#if returnType.name != "void"> ${resultName}</#if>;
<#else>
return <@returnObjectCreation/>;
</#if>
@ -78,4 +78,4 @@
</#if>()
</#if>
</@compress>
</#macro>
</#macro>

View File

@ -19,6 +19,7 @@
package org.mapstruct.ap.test.source.expressions.java;
import java.util.Arrays;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
@ -32,6 +33,6 @@ public interface SourceTargetListMapper {
SourceTargetListMapper INSTANCE = Mappers.getMapper( SourceTargetListMapper.class );
@Mapping( target = "list", expression = "java(Arrays.asList(\"test2\"))" )
@Mapping(target = "list", expression = "java(Arrays.asList( \"test2\" ))")
TargetList map( SourceList source );
}

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.testutil.runner;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@ -28,6 +29,7 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
@ -49,6 +51,14 @@ import org.mapstruct.ap.testutil.compilation.annotation.ProcessorOption;
import org.mapstruct.ap.testutil.compilation.annotation.ProcessorOptions;
import org.mapstruct.ap.testutil.compilation.model.CompilationOutcomeDescriptor;
import org.mapstruct.ap.testutil.compilation.model.DiagnosticDescriptor;
import org.xml.sax.InputSource;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.DefaultLogger;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import static org.fest.assertions.Assertions.assertThat;
@ -179,6 +189,49 @@ class CompilingStatement extends Statement {
}
assertDiagnostics( actualResult.getDiagnostics(), expectedResult.getDiagnostics() );
assertCheckstyleRules();
}
private void assertCheckstyleRules() throws Exception {
if ( sourceOutputDir != null ) {
Properties properties = new Properties();
properties.put( "checkstyle.cache.file", classOutputDir + "/checkstyle.cache" );
final Checker checker = new Checker();
checker.setModuleClassLoader( Checker.class.getClassLoader() );
checker.configure( ConfigurationLoader.loadConfiguration(
new InputSource( getClass().getClassLoader().getResourceAsStream(
"checkstyle-for-generated-sources.xml" ) ),
new PropertiesExpander( properties ),
true ) );
ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
checker.addListener( new DefaultLogger( ByteStreams.nullOutputStream(), true, errorStream, true ) );
int errors = checker.process( findGeneratedFiles( new File( sourceOutputDir ) ) );
if ( errors > 0 ) {
String errorLog = errorStream.toString( "UTF-8" );
assertThat( true ).describedAs( "Expected checkstyle compliant output, but got errors:\n" + errorLog )
.isEqualTo( false );
}
}
}
private static List<File> findGeneratedFiles(File file) {
final List<File> files = Lists.newLinkedList();
if ( file.canRead() ) {
if ( file.isDirectory() ) {
for ( File element : file.listFiles() ) {
files.addAll( findGeneratedFiles( element ) );
}
}
else if ( file.isFile() ) {
files.add( file );
}
}
return files;
}
private void assertDiagnostics(List<DiagnosticDescriptor> actualDiagnostics,
@ -229,8 +282,6 @@ class CompilingStatement extends Statement {
/**
* Returns the classes to be compiled for this test.
*
* @param testMethod The test method of interest
*
* @return A set containing the classes to be compiled for this test
*/
private Set<Class<?>> getTestClasses() {
@ -258,8 +309,6 @@ class CompilingStatement extends Statement {
/**
* Returns the processor options to be used this test.
*
* @param testMethod The test method of interest
*
* @return A list containing the processor options to be used for this test
*/
private List<String> getProcessorOptions() {

View File

@ -0,0 +1,143 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!-- Based on sun_checks.xml from the CheckStyle distribution -->
<module name="Checker">
<!--
If you set the basedir property below, then all reported file
names will be relative to the specified directory. See
http://checkstyle.sourceforge.net/5.x/config.html#Checker
<property name="basedir" value="${basedir}"/>
-->
<!-- Checks that each Java package has a Javadoc file used for commenting. -->
<!-- See http://checkstyle.sf.net/config_javadoc.html#JavadocPackage -->
<!--
<module name="JavadocPackage">
<property name="allowLegacy" value="true"/>
</module>
-->
<!-- Following interprets the header file as regular expressions. -->
<!-- <module name="RegexpHeader"/> -->
<module name="FileTabCharacter">
<property name="eachLine" value="true"/>
</module>
<!-- Not using NewLineAtEndOfFile as it doesn't work across all platforms
in conjunction with git's automatic handling of line endings. This rule fails
if there is a non-whitespace character at the end of the file; Together with
the next rule which forbids trailing whitespace characters this enforces a
new line at the end of the file -->
<module name="RegexpMultiline">
<property name="format" value="\S\z" />
<property name="message" value="There is no new line at the end of file" />
</module>
<module name="RegexpSingleline">
<!-- \s matches whitespace character, $ matches end of line. -->
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces"/>
</module>
<module name="TreeWalker">
<property name="cacheFile" value="${checkstyle.cache.file}"/>
<!-- Checks for Naming Conventions. -->
<!-- See http://checkstyle.sf.net/config_naming.html -->
<!-- <module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/> -->
<!-- Checks for imports -->
<!-- See http://checkstyle.sf.net/config_import.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="true"/>
</module>
<!-- Checks for whitespace -->
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
<module name="EmptyForIteratorPad">
<property name="option" value="space" />
</module>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter">
<property name="tokens" value="BNOT, DEC, DOT, INC, LNOT, UNARY_MINUS, UNARY_PLUS"/>
</module>
<module name="NoWhitespaceBefore"/>
<!-- <module name="OperatorWrap"/> -->
<module name="ParenPad">
<property name="tokens" value="CTOR_CALL, METHOD_CALL, SUPER_CTOR_CALL"/>
<property name="option" value="space"/>
</module>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<!-- Modifier Checks -->
<!-- See http://checkstyle.sf.net/config_modifiers.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>
<!-- Checks for blocks. You know, those {}'s -->
<!-- See http://checkstyle.sf.net/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly">
<property name="option" value="alone"/>
</module>
<!-- Checks for common coding problems -->
<!-- See http://checkstyle.sf.net/config_coding.html -->
<!-- <module name="AvoidInlineConditionals"/> -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<!-- <module name="HiddenField"/> -->
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<!-- <module name="MagicNumber"/> -->
<module name="MissingSwitchDefault"/>
<!-- <module name="RedundantThrows"/> -->
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
<!-- Checks for class design -->
<!-- See http://checkstyle.sf.net/config_design.html -->
<module name="VisibilityModifier">
<property name="packageAllowed" value="true" />
<property name="protectedAllowed" value="true" />
</module>
<!-- Miscellaneous other checks. -->
<!-- See http://checkstyle.sf.net/config_misc.html -->
<module name="ArrayTypeStyle"/>
<!-- <module name="FinalParameters"/> -->
<!-- <module name="TodoComment"/> -->
<module name="UpperEll"/>
</module>
</module>