|
| 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 | +} |
0 commit comments