@Test public void test_non_empty_file() { SourceFile file = JavaAstScanner.scanSingleFile( new File("src/test/files/checks/NonEmptyFile.java"), new VisitorsBridge(check)); CheckMessagesVerifier.verify(file.getCheckMessages()).noMore(); }
@Test public void detected() { SourceFile file = JavaAstScanner.scanSingleFile( new File("src/test/files/checks/ReturnEmptyArrayyNotNullCheck.java"), new ReturnEmptyArrayyNotNullCheck()); checkMessagesVerifier .verify(file.getCheckMessages()) .next() .atLine(16) .withMessage("Return an empty array instead of null.") .next() .atLine(25) .next() .atLine(35) .next() .atLine(40) .next() .atLine(44) .next() .atLine(48) .next() .atLine(53) .withMessage("Return an empty collection instead of null.") .next() .atLine(57) .withMessage("Return an empty array instead of null.") .next() .atLine(61); }
@Test public void shouldDetectEmptyFiles() { SourceFile file = (SourceFile) squid.search("org/foo/CommentedOutFile.java"); assertThat(file.getCheckMessages().size(), is(1)); CheckMessage message = file.getCheckMessages().iterator().next(); assertThat(message.getLine(), nullValue()); assertThat(message.getDefaultMessage(), is("This Java file is empty")); }
@Override public void visitFile(SourceFile sourceFile) { for (Integer line : sourceFile.getNoSonarTagLines()) { CheckMessage message = new CheckMessage( this, "Is //NOSONAR used to exclude false-positive or to hide real quality flaw ?"); message.setBypassExclusion(true); message.setLine(line); sourceFile.log(message); } }
@Test public void check_with_exclusion() { check.exclude = "com.sun.imageio,com.sun.jersey"; SourceFile file = JavaAstScanner.scanSingleFile( new File("src/test/files/checks/SunPackagesUsedCheck.java"), check); checkMessagesVerifier .verify(file.getCheckMessages()) .next() .atLine(10) .withMessage("Replace this usage of Sun classes by ones from the Java API."); }
@Test public void test() { SourceFile file = FlexAstScanner.scanSingleFile(new File("src/test/resources/checks/StarTypeUse.as"), check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(5) .withMessage("Remove usage of this \"star\" type") .next() .atLine(6) .noMore(); }
private void save(Collection<SourceCode> squidSourceFiles) { for (SourceCode squidSourceFile : squidSourceFiles) { SourceFile squidFile = (SourceFile) squidSourceFile; File sonarFile = File.fromIOFile(new java.io.File(squidFile.getKey()), project); saveMeasures(sonarFile, squidFile); saveFunctionsComplexityDistribution(sonarFile, squidFile); saveFilesComplexityDistribution(sonarFile, squidFile); saveViolations(sonarFile, squidFile); } }
@Test public void test() { ElseIfWithoutElseCheck check = new ElseIfWithoutElseCheck(); SourceFile file = JavaScriptAstScanner.scanSingleFile( new File("src/test/resources/checks/elseIfWithoutElse.js"), check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(15) .withMessage("End this if...else if construct by an else clause.") .noMore(); }
@Test public void check() { XPathCheck check = new XPathCheck(); check.xpathQuery = "//IDENTIFIER[string-length(@tokenValue) >= 10]"; check.message = "Avoid identifiers which are too long!"; SourceFile file = scanFile("/checks/xpath.cs", check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(7) .withMessage("Avoid identifiers which are too long!") .noMore(); }
@Test public void check() { XPathCheck check = new XPathCheck(); check.xpathQuery = "//IDENTIFIER[string-length(@tokenValue) >= 10]"; check.message = "Avoid identifiers which are too long!"; SourceFile file = CSharpAstScanner.scanSingleFile(new File("src/test/resources/checks/xpath.cs"), check); checkMessagesVerifier .verify(file.getCheckMessages()) .next() .atLine(7) .withMessage("Avoid identifiers which are too long!"); }
@Test public void detected() { SourceFile file = JavaAstScanner.scanSingleFile( new File("src/test/files/checks/RightCurlyBraceDifferentLineAsNextBlockCheck.java"), new RightCurlyBraceDifferentLineAsNextBlockCheck()); checkMessagesVerifier .verify(file.getCheckMessages()) .next() .atLine(5) .withMessage("Move this \"else\" keyword to a new dedicated line.") .next() .atLine(14) .withMessage("Move this \"catch\" keyword to a new dedicated line."); }
private void saveFilesComplexityDistribution(File sonarFile, SourceFile squidFile) { RangeDistributionBuilder complexityDistribution = new RangeDistributionBuilder( CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, FILES_DISTRIB_BOTTOM_LIMITS); complexityDistribution.add(squidFile.getDouble(FlexMetric.COMPLEXITY)); context.saveMeasure( sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY)); }
@Test public void test() { OneStatementPerLineCheck check = new OneStatementPerLineCheck(); SourceFile file = PythonAstScanner.scanSingleFile( new File("src/test/resources/checks/oneStatementPerLine.py"), check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(1) .withMessage( "At most one statement is allowed per line, but 2 statements were found on this line.") .next() .atLine(2) .withMessage( "At most one statement is allowed per line, but 3 statements were found on this line.") .noMore(); }
@Test public void detected() { SourceFile file = JavaAstScanner.scanSingleFile( new File("src/test/files/checks/SunPackagesUsedCheck.java"), check); checkMessagesVerifier .verify(file.getCheckMessages()) .next() .atLine(1) .withMessage("Replace this usage of Sun classes by ones from the Java API.") .next() .atLine(2) .next() .atLine(7) .next() .atLine(8) .next() .atLine(10); }
private void saveViolations(File sonarFile, SourceFile squidFile) { Collection<CheckMessage> messages = squidFile.getCheckMessages(); if (messages != null) { for (CheckMessage message : messages) { Violation violation = Violation.create(annotationCheckFactory.getActiveRule(message.getCheck()), sonarFile) .setLineId(message.getLine()) .setMessage(message.getText(Locale.ENGLISH)); context.saveViolation(violation); } } }
@Test public void test() { ActionScript2Check check = new ActionScript2Check(); SourceFile file = FlexAstScanner.scanSingleFile( new File("src/test/resources/checks/ActionScript2.as"), check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(1) .withMessage("'intrinsic' not available in ActionScript 3.0") .next() .atLine(2) .withMessage("Operator '<>' not available in ActionScript 3.0") .next() .atLine(4) .withMessage("Operator 'not' not available in ActionScript 3.0") .next() .atLine(6) .withMessage("'set variable statement' not available in ActionScript 3.0") .noMore(); }
@Test public void test() { ParenthesesCheck check = new ParenthesesCheck(); SourceFile file = JavaScriptAstScanner.scanSingleFile( new File("src/test/resources/checks/parentheses.js"), check); CheckMessagesVerifier.verify(file.getCheckMessages()) .next() .atLine(2) .next() .atLine(8) .next() .atLine(12) .next() .atLine(15) .next() .atLine(23) .next() .atLine(26) .next() .atLine(30) .noMore(); }
private void saveMeasures(File sonarFile, SourceFile squidFile) { context.saveMeasure(sonarFile, CoreMetrics.FILES, squidFile.getDouble(FlexMetric.FILES)); context.saveMeasure(sonarFile, CoreMetrics.LINES, squidFile.getDouble(FlexMetric.LINES)); context.saveMeasure( sonarFile, CoreMetrics.NCLOC, squidFile.getDouble(FlexMetric.LINES_OF_CODE)); context.saveMeasure( sonarFile, CoreMetrics.COMMENT_LINES, squidFile.getDouble(FlexMetric.COMMENT_LINES)); context.saveMeasure(sonarFile, CoreMetrics.CLASSES, squidFile.getDouble(FlexMetric.CLASSES)); context.saveMeasure( sonarFile, CoreMetrics.FUNCTIONS, squidFile.getDouble(FlexMetric.FUNCTIONS)); context.saveMeasure( sonarFile, CoreMetrics.STATEMENTS, squidFile.getDouble(FlexMetric.STATEMENTS)); context.saveMeasure( sonarFile, CoreMetrics.COMPLEXITY, squidFile.getDouble(FlexMetric.COMPLEXITY)); }
@Test public void shouldNotLogOnCorrectFiles() { SourceFile file = (SourceFile) squid.search("CommentedCode.java"); assertThat(file.getCheckMessages().size(), is(0)); }