-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1054 from guwirth/add-xpath
add XPath check again
- Loading branch information
Showing
6 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
cxx-checks/src/main/java/org/sonar/cxx/checks/XPathCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import com.sonar.sslr.api.AstNode; | ||
import org.sonar.check.Priority; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
import org.sonar.squidbridge.checks.AbstractXPathCheck; | ||
import com.sonar.sslr.api.Grammar; | ||
import org.sonar.api.utils.PathUtils; | ||
import org.sonar.api.utils.WildcardPattern; | ||
import org.sonar.squidbridge.annotations.NoSqale; | ||
import org.sonar.squidbridge.annotations.RuleTemplate; | ||
|
||
@Rule( | ||
key = "XPath", | ||
name = "XPath rule", | ||
priority = Priority.MAJOR) | ||
@RuleTemplate | ||
@NoSqale | ||
public class XPathCheck extends AbstractXPathCheck<Grammar> { | ||
|
||
private static final String DEFAULT_MATCH_FILE_PATTERN = ""; | ||
private static final boolean DEFAULT_INVERT_FILE_PATTERN = false; | ||
private static final String DEFAULT_XPATH_QUERY = ""; | ||
private static final String DEFAULT_MESSAGE = "The XPath expression matches this piece of code"; | ||
|
||
@RuleProperty( | ||
key = "matchFilePattern", | ||
description = "Ant-style matching patterns for path", | ||
defaultValue = DEFAULT_MATCH_FILE_PATTERN) | ||
public String matchFilePattern = DEFAULT_MATCH_FILE_PATTERN; | ||
|
||
@RuleProperty( | ||
key = "invertFilePattern", | ||
description = "Invert file pattern comparison", | ||
defaultValue = "" + DEFAULT_INVERT_FILE_PATTERN) | ||
public boolean invertFilePattern = DEFAULT_INVERT_FILE_PATTERN; | ||
|
||
@RuleProperty( | ||
key = "xpathQuery", | ||
description = "The XPath query", | ||
type = "TEXT", | ||
defaultValue = DEFAULT_XPATH_QUERY) | ||
public String xpathQuery = DEFAULT_XPATH_QUERY; | ||
|
||
@RuleProperty( | ||
key = "message", | ||
description = "The violation message", | ||
defaultValue = DEFAULT_MESSAGE) | ||
public String message = DEFAULT_MESSAGE; | ||
|
||
@Override | ||
public String getXPathQuery() { | ||
return xpathQuery; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
@Override | ||
public void visitFile(AstNode fileNode) { | ||
if (fileNode != null) { | ||
if (!matchFilePattern.isEmpty()) { | ||
WildcardPattern pattern = WildcardPattern.create(matchFilePattern); | ||
String path = PathUtils.sanitize(getContext().getFile().getPath()); | ||
if (!compare(invertFilePattern, pattern.match(path))) { | ||
return; | ||
} | ||
} | ||
super.visitFile(fileNode); | ||
} | ||
} | ||
|
||
private boolean compare(boolean invert, boolean condition) { | ||
return invert ? !condition : condition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
cxx-checks/src/test/java/org/sonar/cxx/checks/XPathCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Sonar C++ Plugin (Community) | ||
* Copyright (C) 2010-2016 SonarOpenCommunity | ||
* http://github.com/SonarOpenCommunity/sonar-cxx | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.cxx.checks; | ||
|
||
import java.io.IOException; | ||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.junit.Test; | ||
import org.sonar.cxx.CxxAstScanner; | ||
import org.sonar.squidbridge.api.SourceFile; | ||
import org.sonar.squidbridge.checks.CheckMessagesVerifier; | ||
|
||
public class XPathCheckTest { | ||
|
||
@Test | ||
public void xpathWithoutFilePattern() throws UnsupportedEncodingException, IOException { | ||
XPathCheck check = new XPathCheck(); | ||
check.xpathQuery = "//declaration"; | ||
check.message = "Avoid declarations!! "; | ||
|
||
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", "."); | ||
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(1).withMessage(check.message) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void xpathWithFilePattern1() throws UnsupportedEncodingException, IOException { | ||
XPathCheck check = new XPathCheck(); | ||
check.matchFilePattern = "/**/*.cc"; // all files with .cc file extension | ||
check.xpathQuery = "//declaration"; | ||
check.message = "Avoid declarations!! "; | ||
|
||
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", "."); | ||
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(1).withMessage(check.message) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void xpathWithFilePattern2() throws UnsupportedEncodingException, IOException { | ||
XPathCheck check = new XPathCheck(); | ||
check.matchFilePattern = "/**/test/**/xpath.cc"; // all files with filename xpath.cc in a subdirectory with name test | ||
check.xpathQuery = "//declaration"; | ||
check.message = "Avoid declarations!! "; | ||
|
||
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", "."); | ||
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(1).withMessage(check.message) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void xpathWithFilePattern3() throws UnsupportedEncodingException, IOException { | ||
XPathCheck check = new XPathCheck(); | ||
check.matchFilePattern = "/**/*.xxx"; // all files with .xxx file extension | ||
check.xpathQuery = "//declaration"; | ||
check.message = "Avoid declarations!! "; | ||
|
||
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", "."); | ||
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.noMore(); | ||
} | ||
|
||
@Test | ||
public void xpathWithFilePatternInvert() throws UnsupportedEncodingException, IOException { | ||
XPathCheck check = new XPathCheck(); | ||
check.matchFilePattern = "/**/*.xxx"; // all files with not .xxx file extension | ||
check.invertFilePattern = true; | ||
check.xpathQuery = "//declaration"; | ||
check.message = "Avoid declarations!! "; | ||
|
||
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", "."); | ||
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check); | ||
CheckMessagesVerifier.verify(file.getCheckMessages()) | ||
.next().atLine(1).withMessage(check.message) | ||
.noMore(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters