Skip to content

Commit 3640987

Browse files
authored
Merge pull request #1054 from guwirth/add-xpath
add XPath check again
2 parents 28960c2 + 1e571f7 commit 3640987

File tree

6 files changed

+203
-7
lines changed

6 files changed

+203
-7
lines changed

cxx-checks/src/main/java/org/sonar/cxx/checks/CheckList.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ public static List<Class> getChecks() {
8383
ClassNameCheck.class,
8484
FileNameCheck.class,
8585
FunctionNameCheck.class,
86-
MethodNameCheck.class
86+
MethodNameCheck.class,
87+
// XPath
88+
XPathCheck.class
8789
));
8890
}
8991

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Sonar C++ Plugin (Community)
3+
* Copyright (C) 2010-2016 SonarOpenCommunity
4+
* http://github.com/SonarOpenCommunity/sonar-cxx
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.cxx.checks;
21+
22+
import com.sonar.sslr.api.AstNode;
23+
import org.sonar.check.Priority;
24+
import org.sonar.check.Rule;
25+
import org.sonar.check.RuleProperty;
26+
import org.sonar.squidbridge.checks.AbstractXPathCheck;
27+
import com.sonar.sslr.api.Grammar;
28+
import org.sonar.api.utils.PathUtils;
29+
import org.sonar.api.utils.WildcardPattern;
30+
import org.sonar.squidbridge.annotations.NoSqale;
31+
import org.sonar.squidbridge.annotations.RuleTemplate;
32+
33+
@Rule(
34+
key = "XPath",
35+
name = "XPath rule",
36+
priority = Priority.MAJOR)
37+
@RuleTemplate
38+
@NoSqale
39+
public class XPathCheck extends AbstractXPathCheck<Grammar> {
40+
41+
private static final String DEFAULT_MATCH_FILE_PATTERN = "";
42+
private static final boolean DEFAULT_INVERT_FILE_PATTERN = false;
43+
private static final String DEFAULT_XPATH_QUERY = "";
44+
private static final String DEFAULT_MESSAGE = "The XPath expression matches this piece of code";
45+
46+
@RuleProperty(
47+
key = "matchFilePattern",
48+
description = "Ant-style matching patterns for path",
49+
defaultValue = DEFAULT_MATCH_FILE_PATTERN)
50+
public String matchFilePattern = DEFAULT_MATCH_FILE_PATTERN;
51+
52+
@RuleProperty(
53+
key = "invertFilePattern",
54+
description = "Invert file pattern comparison",
55+
defaultValue = "" + DEFAULT_INVERT_FILE_PATTERN)
56+
public boolean invertFilePattern = DEFAULT_INVERT_FILE_PATTERN;
57+
58+
@RuleProperty(
59+
key = "xpathQuery",
60+
description = "The XPath query",
61+
type = "TEXT",
62+
defaultValue = DEFAULT_XPATH_QUERY)
63+
public String xpathQuery = DEFAULT_XPATH_QUERY;
64+
65+
@RuleProperty(
66+
key = "message",
67+
description = "The violation message",
68+
defaultValue = DEFAULT_MESSAGE)
69+
public String message = DEFAULT_MESSAGE;
70+
71+
@Override
72+
public String getXPathQuery() {
73+
return xpathQuery;
74+
}
75+
76+
@Override
77+
public String getMessage() {
78+
return message;
79+
}
80+
81+
@Override
82+
public void visitFile(AstNode fileNode) {
83+
if (fileNode != null) {
84+
if (!matchFilePattern.isEmpty()) {
85+
WildcardPattern pattern = WildcardPattern.create(matchFilePattern);
86+
String path = PathUtils.sanitize(getContext().getFile().getPath());
87+
if (!compare(invertFilePattern, pattern.match(path))) {
88+
return;
89+
}
90+
}
91+
super.visitFile(fileNode);
92+
}
93+
}
94+
95+
private boolean compare(boolean invert, boolean condition) {
96+
return invert ? !condition : condition;
97+
}
98+
}

cxx-checks/src/test/java/org/sonar/cxx/checks/CheckListTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public class CheckListTest {
2727

2828
@Test
2929
public void count() {
30-
assertThat(CheckList.getChecks().size()).isEqualTo(44);
30+
assertThat(CheckList.getChecks().size()).isEqualTo(45);
3131
}
3232
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Sonar C++ Plugin (Community)
3+
* Copyright (C) 2010-2016 SonarOpenCommunity
4+
* http://github.com/SonarOpenCommunity/sonar-cxx
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.cxx.checks;
21+
22+
import java.io.IOException;
23+
import java.io.UnsupportedEncodingException;
24+
25+
import org.junit.Test;
26+
import org.sonar.cxx.CxxAstScanner;
27+
import org.sonar.squidbridge.api.SourceFile;
28+
import org.sonar.squidbridge.checks.CheckMessagesVerifier;
29+
30+
public class XPathCheckTest {
31+
32+
@Test
33+
public void xpathWithoutFilePattern() throws UnsupportedEncodingException, IOException {
34+
XPathCheck check = new XPathCheck();
35+
check.xpathQuery = "//declaration";
36+
check.message = "Avoid declarations!! ";
37+
38+
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", ".");
39+
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
40+
CheckMessagesVerifier.verify(file.getCheckMessages())
41+
.next().atLine(1).withMessage(check.message)
42+
.noMore();
43+
}
44+
45+
@Test
46+
public void xpathWithFilePattern1() throws UnsupportedEncodingException, IOException {
47+
XPathCheck check = new XPathCheck();
48+
check.matchFilePattern = "/**/*.cc"; // all files with .cc file extension
49+
check.xpathQuery = "//declaration";
50+
check.message = "Avoid declarations!! ";
51+
52+
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", ".");
53+
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
54+
CheckMessagesVerifier.verify(file.getCheckMessages())
55+
.next().atLine(1).withMessage(check.message)
56+
.noMore();
57+
}
58+
59+
@Test
60+
public void xpathWithFilePattern2() throws UnsupportedEncodingException, IOException {
61+
XPathCheck check = new XPathCheck();
62+
check.matchFilePattern = "/**/test/**/xpath.cc"; // all files with filename xpath.cc in a subdirectory with name test
63+
check.xpathQuery = "//declaration";
64+
check.message = "Avoid declarations!! ";
65+
66+
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", ".");
67+
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
68+
CheckMessagesVerifier.verify(file.getCheckMessages())
69+
.next().atLine(1).withMessage(check.message)
70+
.noMore();
71+
}
72+
73+
@Test
74+
public void xpathWithFilePattern3() throws UnsupportedEncodingException, IOException {
75+
XPathCheck check = new XPathCheck();
76+
check.matchFilePattern = "/**/*.xxx"; // all files with .xxx file extension
77+
check.xpathQuery = "//declaration";
78+
check.message = "Avoid declarations!! ";
79+
80+
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", ".");
81+
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
82+
CheckMessagesVerifier.verify(file.getCheckMessages())
83+
.noMore();
84+
}
85+
86+
@Test
87+
public void xpathWithFilePatternInvert() throws UnsupportedEncodingException, IOException {
88+
XPathCheck check = new XPathCheck();
89+
check.matchFilePattern = "/**/*.xxx"; // all files with not .xxx file extension
90+
check.invertFilePattern = true;
91+
check.xpathQuery = "//declaration";
92+
check.message = "Avoid declarations!! ";
93+
94+
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/xpath.cc", ".");
95+
SourceFile file = CxxAstScanner.scanSingleFile(tester.cxxFile, tester.sensorContext, check);
96+
CheckMessagesVerifier.verify(file.getCheckMessages())
97+
.next().atLine(1).withMessage(check.message)
98+
.noMore();
99+
}
100+
}

pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,6 @@
226226
<groupId>org.codehaus.sonar.sslr</groupId>
227227
<artifactId>sslr-core</artifactId>
228228
</exclusion>
229-
<exclusion>
230-
<groupId>org.codehaus.sonar.sslr</groupId>
231-
<artifactId>sslr-xpath</artifactId>
232-
</exclusion>
233229
<exclusion>
234230
<groupId>org.codehaus.sonar</groupId>
235231
<artifactId>sonar-plugin-api</artifactId>

sonar-cxx-plugin/src/test/java/org/sonar/plugins/cxx/CxxRuleRepositoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public void rulesTest() {
3434
new CxxRuleRepository().define(context);
3535

3636
assertThat(context.repositories()).hasSize(1);
37-
assertThat(context.repository(CheckList.REPOSITORY_KEY).rules()).hasSize(44);
37+
assertThat(context.repository(CheckList.REPOSITORY_KEY).rules()).hasSize(45);
3838
}
3939
}

0 commit comments

Comments
 (0)