Skip to content

Commit

Permalink
#877 updated tests added ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyk committed May 7, 2018
1 parent fe71001 commit 9e97c38
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
18 changes: 18 additions & 0 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,22 @@
</properties>
<priority>3</priority>
</rule>
<rule name="ProhibitFilesCreateFileInTests"
message="Files.createFile should not be used in tests, replace them with @Rule TemporaryFolder"
language="java"
class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Files.createFile shouldn't be used in tests.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[ends-with(@Image, 'Test')]//PrimaryPrefix/Name[@Image='Files.createFile']
]]>
</value>
</property>
</properties>
<priority>3</priority>
</rule>
</ruleset>
36 changes: 33 additions & 3 deletions qulice-pmd/src/test/java/com/qulice/pmd/PmdValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public final class PmdValidatorTest {
private static final String STATIC_METHODS =
"Public static methods are prohibited";

/**
* Error text for Files.createFile.
*/
private static final String FILES_CREATE_ERROR =
// @checkstyle LineLength (1 line)
"Files.createFile should not be used in tests, replace them with @Rule TemporaryFolder";

/**
* PmdValidator can find violations in Java file(s).
* @throws Exception If something wrong happens inside.
Expand Down Expand Up @@ -306,12 +313,35 @@ public void forbidsCodeInConstructor()
}

/**
* PmdValidator forbids usage of Files.createFile.
* PmdValidator forbids usage of Files.createFile in tests.
* @throws Exception If something wrong happens inside.
*/
@Test
public void forbidsFilesCreateFile() throws Exception {
new PmdAssert("FilesCreateFileTest.java",Matchers.is(false), Matchers.containsString("Files.createFile should not be used in tests, replace them with @Rule TemporaryFolder")).validate();
public void forbidsFilesCreateFileInTests() throws Exception {
new PmdAssert(
"FilesCreateFileTest.java",
Matchers.is(false),
Matchers.containsString(
PmdValidatorTest.FILES_CREATE_ERROR
)
).validate();
}

/**
* PmdValidator allows usage of Files.createFile outside of tests.
* @throws Exception If something wrong happens inside.
*/
@Test
public void forbidsFilesCreateFileOutsideOfTests() throws Exception {
new PmdAssert(
"FilesCreateFileOther.java",
Matchers.is(true),
Matchers.not(
Matchers.containsString(
PmdValidatorTest.FILES_CREATE_ERROR
)
)
).validate();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package foo;

import java.nio.file.Files;
import java.nio.file.Paths;

public final class FilesCreateFileOther {
public void other() {
Files.createFile(Paths.get("test"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package foo;

import java.nio.file.Files;
import java.nio.file.Paths;

public final class FilesCreateFileTest {
@Test
public void test() {
Files.createFile(Paths.get("test"));
}
}

0 comments on commit 9e97c38

Please sign in to comment.