PmdValidator implements ResourceValidator { @Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public Collection<Violation> validate(final Collection<File> files) { final SourceValidator validator = new SourceValidator(this.env); final Collection<DataSource> sources = new LinkedList<>(); for (final File file : files) { sources.add(new FileDataSource(file)); } final Collection<RuleViolation> breaches = validator.validate( sources, this.env.basedir().getPath() ); final Collection<Violation> violations = new LinkedList<>(); for (final RuleViolation breach : breaches) { violations.add( new Violation.Default( this.name(), breach.getRule().getName(), breach.getFilename(), String.format( "%d-%d", breach.getBeginLine(), breach.getEndLine() ), breach.getDescription() ) ); } return violations; } PmdValidator(final Environment env); @Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") Collection<Violation> validate(final Collection<File> files); @Override String name();  }
@Test public void findsProblemsInJavaFiles() throws Exception { final String file = "src/main/java/Main.java"; final Environment env = new Environment.Mock() .withFile(file, "class Main { int x = 0; }"); MatcherAssert.assertThat( new PmdValidator(env).validate( Collections.singletonList(new File(env.basedir(), file)) ), Matchers.not(Matchers.<Violation>empty()) ); }
@Test public void forbidsCodeInConstructor() throws Exception { final String file = "CodeInConstructor.java"; new PmdAssert( file, Matchers.is(false), RegexMatchers.containsPattern( String.format(PmdValidatorTest.CODE_IN_CON, file) ) ).validate(); }
@Test public void allowsLambdaInConstructor() throws Exception { final String file = "LambdaInConstructor.java"; new PmdAssert( file, new IsEqual<>(true), new IsNot<>( RegexMatchers.containsPattern( String.format(PmdValidatorTest.CODE_IN_CON, file) ) ) ).validate(); }
@Test public void forbidsFilesCreateFileInTests() throws Exception { new PmdAssert( "FilesCreateFileTest.java", Matchers.is(false), Matchers.containsString( PmdValidatorTest.FILES_CREATE_ERR ) ).validate(); }
@Test public void forbidsFilesCreateFileOutsideOfTests() throws Exception { new PmdAssert( "FilesCreateFileOther.java", Matchers.is(true), Matchers.not( Matchers.containsString( PmdValidatorTest.FILES_CREATE_ERR ) ) ).validate(); }
@Test public void acceptsCallToConstructorInConstructor() throws Exception { final String file = "CallToConstructorInConstructor.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( RegexMatchers.containsPattern( String.format(PmdValidatorTest.CODE_IN_CON, file) ) ) ).validate(); }
@Test public void acceptsCallToStaticFieldsInStaticWay() throws Exception { final String file = "StaticAccessToStaticFields.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( RegexMatchers.containsPattern( String.format(PmdValidatorTest.STATIC_ACCESS, file) ) ) ).validate(); }
@Test public void forbidsCallToStaticFieldsInNonStaticWay() throws Exception { final String file = "NonStaticAccessToStaticFields.java"; new PmdAssert( file, Matchers.is(false), RegexMatchers.containsPattern( String.format(PmdValidatorTest.STATIC_ACCESS, file) ) ).validate(); }
@Test public void forbidsNonPublicCloneMethod() throws Exception { new PmdAssert( "CloneMethodMustBePublic.java", Matchers.is(false), Matchers.containsString( String.format( PmdValidatorTest.BRACKETS, "CloneMethodMustBePublic" ) ) ).validate(); }
@Test public void forbidsCloneMethodReturnTypeNotMatchingClassName() throws Exception { new PmdAssert( "CloneMethodReturnTypeMustMatchClassName.java", Matchers.is(false), Matchers.containsString( String.format( PmdValidatorTest.BRACKETS, "CloneMethodReturnTypeMustMatchClassName" ) ) ).validate(); }
@Test public void forbidsNonSimplifiedTernaryOperators() throws Exception { new PmdAssert( "SimplifiedTernary.java", Matchers.is(false), Matchers.containsString( String.format( PmdValidatorTest.BRACKETS, "SimplifiedTernary" ) ) ).validate(); }
@Test @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void understandsMethodReferences() throws Exception { final String file = "src/main/java/Other.java"; final Environment env = new Environment.Mock().withFile( file, Joiner.on('\n').join( "import java.util.ArrayList;", "class Other {", " public static void test() {", " new ArrayList<String>().forEach(Other::other);", " }", " private static void other(String some) { "}" ) ); final Collection<Violation> violations = new PmdValidator(env).validate( Collections.singletonList(new File(env.basedir(), file)) ); MatcherAssert.assertThat( violations, Matchers.<Violation>empty() ); }
@Test public void prohibitsStaticImportsPlainAssertionsInTests() throws Exception { final String file = "PlainJUnitAssertionStaticImportBlock.java"; new PmdAssert( file, Matchers.is(false), Matchers.containsString( PmdValidatorTest.PLAIN_ASSERTIONS ) ).validate(); }
@Test public void prohibitsPlainJunitAssertionsInTestMethods() throws Exception { final String file = "PlainJUnitAssertionTestMethod.java"; new PmdAssert( file, Matchers.is(false), Matchers.containsString( PmdValidatorTest.PLAIN_ASSERTIONS ) ).validate(); }
@Test public void allowsAssertFail() throws Exception { final String file = "AllowAssertFail.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( Matchers.containsString( PmdValidatorTest.PLAIN_ASSERTIONS ) ) ).validate(); }
@Test public void allowsNonTransientFields() throws Exception { final String file = "AllowNonTransientFields.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( Matchers.containsString( "Found non-transient, non-static member." ) ) ).validate(); }
@Test public void prohibitsPublicStaticMethods() throws Exception { new PmdAssert( "StaticPublicMethod.java", Matchers.is(false), Matchers.containsString(PmdValidatorTest.STATIC_METHODS) ).validate(); }
@Test public void allowsPublicStaticMainMethod() throws Exception { new PmdAssert( "StaticPublicVoidMainMethod.java", Matchers.is(true), Matchers.not( Matchers.containsString(PmdValidatorTest.STATIC_METHODS) ) ).validate(); }
@Test public void allowsJunitFrameworkPublicStaticMethods() throws Exception { new PmdAssert( "JunitStaticPublicMethods.java", Matchers.is(true), Matchers.not( Matchers.containsString(PmdValidatorTest.STATIC_METHODS) ) ).validate(); }
@Test public void allowsDuplicateLiteralsInAnnotations() throws Exception { new PmdAssert( "AllowsDuplicateLiteralsInAnnotations.java", Matchers.is(true), Matchers.not( Matchers.containsString("AvoidDuplicateLiterals") ) ).validate(); }
@Test @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void doesNotComplainAboutConstantsInInnerClasses() throws Exception { final String file = "src/main/java/foo/Foo.java"; final Environment env = new Environment.Mock().withFile( file, Joiner.on('\n').join( "package foo;", "interface Foo {", " final class Bar implements Foo {", " private static final Pattern TEST =", " Pattern.compile(\"hey\");", " public String doSomething() {", " return Foo.Bar.TEST.toString();", " }", " }", "}" ) ); MatcherAssert.assertThat( new PmdValidator(env).validate( Collections.singletonList(new File(env.basedir(), file)) ), Matchers.<Violation>empty() ); }
@Test public void allowsFieldInitializationWhenConstructorIsMissing() throws Exception { final String file = "FieldInitNoConstructor.java"; new PmdAssert( file, Matchers.is(false), Matchers.not( RegexMatchers.containsPattern( String.format(PmdValidatorTest.NO_CON_INIT, file) ) ) ).validate(); }
@Test public void forbidsFieldInitializationWhenConstructorExists() throws Exception { final String file = "FieldInitConstructor.java"; new PmdAssert( file, Matchers.is(false), RegexMatchers.containsPattern( String.format(PmdValidatorTest.NO_CON_INIT, file) ) ).validate(); }
@Test public void allowsStaticFieldInitializationWhenConstructorExists() throws Exception { final String file = "StaticFieldInitConstructor.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( RegexMatchers.containsPattern( String.format(PmdValidatorTest.NO_CON_INIT, file) ) ) ).validate(); }
@Test public void forbidsFieldInitializationInSeveralConstructors() throws Exception { final String file = "FieldInitSeveralConstructors.java"; new PmdAssert( file, Matchers.is(false), RegexMatchers.containsPattern( String.format(PmdValidatorTest.MULT_CON_INIT, file) ) ).validate(); }
@Test public void allowsFieldInitializationInOneConstructor() throws Exception { final String file = "FieldInitOneConstructor.java"; new PmdAssert( file, Matchers.is(true), Matchers.not( RegexMatchers.containsPattern( String.format(PmdValidatorTest.MULT_CON_INIT, file) ) ) ).validate(); }
@Test public void forbidsUnnecessaryFinalModifier() throws Exception { final String file = "UnnecessaryFinalModifier.java"; new PmdAssert( file, Matchers.is(false), Matchers.containsString("Unnecessary modifier 'final'") ).validate(); }
@Test public void forbidsUselessParentheses() throws Exception { final String file = "UselessParentheses.java"; new PmdAssert( file, Matchers.is(false), Matchers.containsString("Useless parentheses") ).validate(); }