From 3daca86045397e5a3f546707d5eb38769acfeda8 Mon Sep 17 00:00:00 2001 From: Romain PELISSE Date: Tue, 29 Jan 2013 12:20:22 +0100 Subject: [PATCH] pmd: AntTask support for language - fix #1004 targetjdk isn't attribute of PMD task - introduce a new element, called , that replaces the already existing element --- .../java/net/sourceforge/pmd/ant/PMDTask.java | 21 +- .../sourceforge/pmd/ant/SourceLanguage.java | 33 ++++ .../java/net/sourceforge/pmd/ant/Version.java | 19 -- .../sourceforge/pmd/lang/LanguageVersion.java | 17 ++ pmd/src/site/xdocs/ant-task.xml | 4 +- .../net/sourceforge/pmd/ant/PMDTaskTest.java | 26 ++- .../net/sourceforge/pmd/ant/VersionTest.java | 25 --- .../resources/ant/ecmascript/fcoltable.js | 57 ++++++ .../resources/ant/ecmascript/sorttable.js | 184 ++++++++++++++++++ .../sourceforge/pmd/ant/xml/pmdtasktest.xml | 11 +- 10 files changed, 323 insertions(+), 74 deletions(-) create mode 100644 pmd/src/main/java/net/sourceforge/pmd/ant/SourceLanguage.java delete mode 100644 pmd/src/main/java/net/sourceforge/pmd/ant/Version.java delete mode 100644 pmd/src/test/java/net/sourceforge/pmd/ant/VersionTest.java create mode 100644 pmd/src/test/resources/ant/ecmascript/fcoltable.js create mode 100644 pmd/src/test/resources/ant/ecmascript/sorttable.js diff --git a/pmd/src/main/java/net/sourceforge/pmd/ant/PMDTask.java b/pmd/src/main/java/net/sourceforge/pmd/ant/PMDTask.java index a24dc59977..0435a24c91 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/ant/PMDTask.java +++ b/pmd/src/main/java/net/sourceforge/pmd/ant/PMDTask.java @@ -26,7 +26,6 @@ import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; import net.sourceforge.pmd.RuleSets; -import net.sourceforge.pmd.lang.Language; import net.sourceforge.pmd.lang.LanguageVersion; import net.sourceforge.pmd.renderers.AbstractRenderer; import net.sourceforge.pmd.renderers.Renderer; @@ -110,24 +109,10 @@ public class PMDTask extends Task { formatters.add(f); } - public void addConfiguredVersion(Version version) { - LanguageVersion languageVersion = LanguageVersion.findByTerseName(version.getTerseName()); + public void addConfiguredSourceLanguage(SourceLanguage version) { + LanguageVersion languageVersion = LanguageVersion.findVersionsForLanguageTerseName(version.getName(), version.getVersion()); if (languageVersion == null) { - StringBuilder buf = new StringBuilder("The element, if used, must be one of "); - boolean first = true; - for (Language language : Language.values()) { - if (language.getVersions().size() > 2) { - for (LanguageVersion v : language.getVersions()) { - if (!first) { - buf.append(", "); - } - buf.append('\'').append(v.getTerseName()).append('\''); - first = false; - } - } - } - buf.append('.'); - throw new BuildException(buf.toString()); + throw new BuildException("The following language is not supported:" + version + "."); } configuration.setDefaultLanguageVersion(languageVersion); } diff --git a/pmd/src/main/java/net/sourceforge/pmd/ant/SourceLanguage.java b/pmd/src/main/java/net/sourceforge/pmd/ant/SourceLanguage.java new file mode 100644 index 0000000000..e3fb3fb768 --- /dev/null +++ b/pmd/src/main/java/net/sourceforge/pmd/ant/SourceLanguage.java @@ -0,0 +1,33 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.ant; + +/** + * Stores LanguageVersion terse name value. + */ +public class SourceLanguage { + + private String name; + private String version; + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String toString() { + return ""; + } +} diff --git a/pmd/src/main/java/net/sourceforge/pmd/ant/Version.java b/pmd/src/main/java/net/sourceforge/pmd/ant/Version.java deleted file mode 100644 index 58eaa61a1d..0000000000 --- a/pmd/src/main/java/net/sourceforge/pmd/ant/Version.java +++ /dev/null @@ -1,19 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ -package net.sourceforge.pmd.ant; - -/** - * Stores LanguageVersion terse name value. - */ -public class Version { - private String terseName; - - public void addText(String text) { - this.terseName = text; - } - - public String getTerseName() { - return terseName; - } -} diff --git a/pmd/src/main/java/net/sourceforge/pmd/lang/LanguageVersion.java b/pmd/src/main/java/net/sourceforge/pmd/lang/LanguageVersion.java index ed6f6af823..c8e3dcc2b0 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/lang/LanguageVersion.java +++ b/pmd/src/main/java/net/sourceforge/pmd/lang/LanguageVersion.java @@ -200,6 +200,23 @@ public enum LanguageVersion { return versionsAvailable; } + /** + * A utility method to retrieve the appropriate enum, given the provided parameters + * + * @param languageTerseName The LanguageVersion terse name. + * @param languageVersion The version of the language requested. + * @return A list of versions associated with the terse name. + */ + public static LanguageVersion findVersionsForLanguageTerseName(String languageTerseName, String languageVersion) { + List versionsAvailable = findVersionsForLanguageTerseName(languageTerseName); + for ( LanguageVersion version : versionsAvailable ) { + if ( version.getVersion().equalsIgnoreCase(languageVersion) ) + return version; + } + return null; + } + + /** * Return a comma-separated list of LanguageVersion terse names. * @param languageVersions The language versions. diff --git a/pmd/src/site/xdocs/ant-task.xml b/pmd/src/site/xdocs/ant-task.xml index d16f228875..e88e830346 100644 --- a/pmd/src/site/xdocs/ant-task.xml +++ b/pmd/src/site/xdocs/ant-task.xml @@ -99,7 +99,7 @@

auxclasspath nested element - extra classpath used for Type Resolution rules.

-

version nested element - specify which version of JDK to (1.5, 1.6,...)

+

language nested element - specify which language (Java, Ecmascript, XML,...) and the associated version (1.5, 1.6,...)

ruleset nested element - another way to specify rulesets. Here's an example:

@@ -107,7 +107,7 @@ - 1.6 + rulesets/java/design.xml java-basic diff --git a/pmd/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java b/pmd/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java index e109cabc83..384d91bbba 100644 --- a/pmd/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java +++ b/pmd/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java @@ -40,14 +40,14 @@ public class PMDTaskTest extends BuildFileTest { public void testNestedRuleset() { executeTarget("testNestedRuleset"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); } @Test public void testFormatterWithProperties() { executeTarget("testFormatterWithProperties"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); assertOutputContaining("link_prefix"); assertOutputContaining("line_prefix"); } @@ -56,35 +56,35 @@ public class PMDTaskTest extends BuildFileTest { public void testAbstractNames() { executeTarget("testAbstractNames"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); } @Test public void testAbstractNamesInNestedRuleset() { executeTarget("testAbstractNamesInNestedRuleset"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); } @Test public void testCommaInRulesetfiles() { executeTarget("testCommaInRulesetfiles"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); } @Test public void testRelativeRulesets() { executeTarget("testRelativeRulesets"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at the"); } @Test public void testRelativeRulesetsInRulesetfiles() { executeTarget("testRelativeRulesetsInRulesetfiles"); assertOutputContaining("Avoid really long methods"); - assertOutputContaining("Deeply nested if"); + assertOutputContaining("Fields should be declared at"); } @Test @@ -94,7 +94,7 @@ public class PMDTaskTest extends BuildFileTest { @Test public void testInvalidLanguageVersion() { - expectBuildExceptionContaining("testInvalidLanguageVersion", "Fail requested.", "The element, if used, must be one of 'java 1.3', 'java 1.4', 'java 1.5', 'java 1.6', 'java 1.7'."); + expectBuildExceptionContaining("testInvalidLanguageVersion", "Fail requested.", "The following language is not supported:."); } @Test @@ -102,7 +102,15 @@ public class PMDTaskTest extends BuildFileTest { executeTarget("testExplicitRuleInRuleSet"); assertOutputContaining("Avoid really long methods"); } - + + @Test + public void testEcmascript() { + executeTarget("testEcmascript"); + assertOutputContaining("A 'return', 'break', 'continue', or 'throw' statement should be the last in a block."); + assertOutputContaining("Avoid using global variables"); + assertOutputContaining("Use ===/!== to compare with true/false or Numbers"); + } + public static junit.framework.Test suite() { return new junit.framework.JUnit4TestAdapter(PMDTaskTest.class); } diff --git a/pmd/src/test/java/net/sourceforge/pmd/ant/VersionTest.java b/pmd/src/test/java/net/sourceforge/pmd/ant/VersionTest.java deleted file mode 100644 index d225cd9141..0000000000 --- a/pmd/src/test/java/net/sourceforge/pmd/ant/VersionTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ -package net.sourceforge.pmd.ant; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import net.sourceforge.pmd.ant.Version; - -import org.junit.Test; - -public class VersionTest { - - @Test - public void testHappyPath() { - Version version = new Version(); - assertNull("default", version.getTerseName()); - version.addText("terseName"); - assertEquals("set terse name", "terseName", version.getTerseName()); - } - - public static junit.framework.Test suite() { - return new junit.framework.JUnit4TestAdapter(VersionTest.class); - } -} diff --git a/pmd/src/test/resources/ant/ecmascript/fcoltable.js b/pmd/src/test/resources/ant/ecmascript/fcoltable.js new file mode 100644 index 0000000000..573dfd2a9b --- /dev/null +++ b/pmd/src/test/resources/ant/ecmascript/fcoltable.js @@ -0,0 +1,57 @@ +function tablecollapse() +{ + /* Variables */ + var collapseClass='footcollapse'; + var collapsePic='arrow_up.gif'; + var expandPic='arrow_down.gif'; + var initialCollapse=true; + + // loop through all tables + var t=document.getElementsByTagName('table'); + var checktest= new RegExp("(^|\\s)" + collapseClass + "(\\s|$)"); + for (var i=0;i 0) { + var firstRow = table.rows[0]; + } + if (!firstRow) return; + + // We have a first row: assume it's the header, and make its contents clickable links + for (var i=0;i   '; + } +} + +function ts_getInnerText(el) { + if (typeof el == "string") return el; + if (typeof el == "undefined") { return el }; + if (el.innerText) return el.innerText; //Not needed but it is faster + var str = ""; + + var cs = el.childNodes; + var l = cs.length; + for (var i = 0; i < l; i++) { + switch (cs[i].nodeType) { + case 1: //ELEMENT_NODE + str += ts_getInnerText(cs[i]); + break; + case 3: //TEXT_NODE + str += cs[i].nodeValue; + break; + } + } + return str; +} + +function ts_resortTable(lnk) { + // get the span + var span; + for (var ci=0;ci - 42 + @@ -118,4 +118,13 @@ + + + + + + + + +