pmd (build): make sure the rulesets are sorted alphabetically
* fix TODO - use compiled regex pattern
This commit is contained in:
@ -5,6 +5,7 @@ package net.sourceforge.pmd.build.filefilter;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -15,8 +16,13 @@ import java.io.FilenameFilter;
|
||||
public class RulesetFilenameFilter implements FilenameFilter {
|
||||
|
||||
// FUTURE: Make this somehow configurable ? Turn into an array passed by constructor ?
|
||||
// TODO: move to compiled regex to improve perf.
|
||||
private static final String[] patterns = { "^[0-9][0-9].*\\.xml", "^.*dogfood.*\\.xml", "^all-.*\\.xml", "^migrating_.*\\.xml", "^pmdspecific.xml"} ;
|
||||
private static final Pattern EXCLUDE = Pattern.compile(
|
||||
"(^[0-9][0-9].*\\.xml)" +
|
||||
"|(^.*dogfood.*\\.xml)" +
|
||||
"|(^all-.*\\.xml)" +
|
||||
"|(^migrating_.*\\.xml)" +
|
||||
"|(^pmdspecific.xml)"
|
||||
);
|
||||
|
||||
public boolean accept(File file, String name) {
|
||||
if ( doesNotMatchExcludeNames(name) )
|
||||
@ -26,10 +32,6 @@ public class RulesetFilenameFilter implements FilenameFilter {
|
||||
}
|
||||
|
||||
private boolean doesNotMatchExcludeNames(String name) {
|
||||
for ( String pattern : patterns ) {
|
||||
if ( name.matches(pattern))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return !EXCLUDE.matcher(name).matches();
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,10 @@ import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import net.sourceforge.pmd.build.PmdBuildException;
|
||||
|
||||
@ -28,20 +30,20 @@ public final class FileUtil {
|
||||
|
||||
public static String pathToParent = "..";
|
||||
|
||||
private FileUtil() {};
|
||||
private FileUtil() {}
|
||||
|
||||
public static Set<File> listFilesFrom(File dir) {
|
||||
public static List<File> listFilesFrom(File dir) {
|
||||
return filterFilesFrom(dir, null);
|
||||
}
|
||||
|
||||
public static Set<File> filterFilesFrom(File dir, FilenameFilter filter) {
|
||||
Set<File> filteredFiles = new HashSet<File>(0);
|
||||
public static List<File> filterFilesFrom(File dir, FilenameFilter filter) {
|
||||
List<File> filteredFiles = new LinkedList<File>();
|
||||
if ( dir != null ) {
|
||||
File[] files = dir.listFiles(filter);
|
||||
if ( files != null && files.length > 0 )
|
||||
for ( int fileIterator = 0 ; fileIterator < files.length ; fileIterator++ )
|
||||
filteredFiles.add(files[fileIterator]);
|
||||
filteredFiles.addAll(Arrays.asList(files));
|
||||
}
|
||||
Collections.sort(filteredFiles);
|
||||
return filteredFiles;
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,13 @@
|
||||
|
||||
package net.sourceforge.pmd.ant;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import net.sourceforge.pmd.TestBase;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@ -22,5 +27,10 @@ public class PmdBuildTaskTest extends TestBase {
|
||||
task.setSiteXml(TEST_DIR + "site/site.pre.xml");
|
||||
task.setSiteXmlTarget(TEST_DIR + "site/site.xml");
|
||||
task.execute();
|
||||
|
||||
String site = IOUtils.toString(new File(TEST_DIR + "site/site.xml").toURI());
|
||||
assertTrue(site.contains("<item name=\"Basic\""));
|
||||
assertTrue(site.contains("<item name=\"Code Size\""));
|
||||
assertTrue(site.indexOf("<item name=\"Basic\"") < site.indexOf("<item name=\"Code Size\""));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
/**
|
||||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
|
||||
*/
|
||||
package net.sourceforge.pmd.build;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import net.sourceforge.pmd.build.filefilter.RulesetFilenameFilter;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RulesetFilenameFilterTest {
|
||||
|
||||
@Test
|
||||
public void testAccept() {
|
||||
RulesetFilenameFilter filter = new RulesetFilenameFilter();
|
||||
File directory = new File(".");
|
||||
|
||||
assertTrue(filter.accept(directory, "codesize.xml"));
|
||||
|
||||
assertFalse(filter.accept(directory, "some-text-file.txt"));
|
||||
assertFalse(filter.accept(directory, "all-java.xml"));
|
||||
assertFalse(filter.accept(directory, "dogfood.xml"));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0"?>
|
||||
|
||||
<ruleset name="Code Size"
|
||||
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
|
||||
|
||||
<description>
|
||||
The Code Size ruleset contains rules that find problems related to code size or complexity.
|
||||
</description>
|
||||
|
||||
<rule name="NPathComplexity"
|
||||
since="3.9"
|
||||
message="The method {0}() has an NPath complexity of {1}"
|
||||
class="net.sourceforge.pmd.lang.java.rule.codesize.NPathComplexityRule"
|
||||
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#NPathComplexity">
|
||||
<description>
|
||||
The NPath complexity of a method is the number of acyclic execution paths through that method.
|
||||
A threshold of 200 is generally considered the point where measures should be taken to reduce
|
||||
complexity and increase readability.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<example>
|
||||
<![CDATA[
|
||||
void bar() { // this is something more complex than it needs to be,
|
||||
if (y) { // it should be broken down into smaller methods or functions
|
||||
for (j = 0; j < m; j++) {
|
||||
if (j > r) {
|
||||
doSomething();
|
||||
while (f < 5 ) {
|
||||
anotherThing();
|
||||
f -= 27;
|
||||
}
|
||||
} else {
|
||||
tryThis();
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( r - n > 45) {
|
||||
while (doMagic()) {
|
||||
findRabbits();
|
||||
}
|
||||
}
|
||||
try {
|
||||
doSomethingDangerous();
|
||||
} catch (Exception ex) {
|
||||
makeAmends();
|
||||
} finally {
|
||||
dontDoItAgain();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
</ruleset>
|
Reference in New Issue
Block a user