/** SONAR-6787 */ @Test public void ensure_differential_period_4_and_5_defined_at_project_level_is_taken_into_account() throws Exception { orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY); setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period4", "30"); setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period5", "previous_analysis"); // Execute an analysis in the past to have a past snapshot without any issues orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty"); orchestrator.executeBuild( SonarRunner.create(projectDir("shared/xoo-sample")) .setProperty("sonar.projectDate", formatDate(addDays(new Date(), -60)))); // Second analysis -> issues will be created orchestrator .getServer() .restoreProfile(FileLocation.ofClasspath("/measureHistory/one-issue-per-line-profile.xml")); orchestrator .getServer() .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line"); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); // New technical debt only comes from new issues Resource newTechnicalDebt = orchestrator .getServer() .getWsClient() .find( ResourceQuery.createForMetrics( "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") .setIncludeTrends(true)); List<Measure> measures = newTechnicalDebt.getMeasures(); assertThat(measures.get(0).getVariation4()).isEqualTo(17); assertThat(measures.get(0).getVariation5()).isEqualTo(17); // Third analysis, with exactly the same profile -> no new issues so no new technical debt orchestrator .getServer() .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line"); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); newTechnicalDebt = orchestrator .getServer() .getWsClient() .find( ResourceQuery.createForMetrics( "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt") .setIncludeTrends(true)); // No variation => measure is purged assertThat(newTechnicalDebt).isNull(); }
private SonarRunner configureRunner(String projectDir, String... props) throws IOException { SonarRunner runner = SonarRunner.create( ItUtils.projectDir(projectDir), "sonar.working.directory", temp.newFolder().getAbsolutePath(), "sonar.report.export.path", "sonar-report.json", "sonar.userHome", temp.newFolder().getAbsolutePath()); runner.setProperties(props); return runner; }
@Test public void test() throws Exception { assertTrue( "SonarQube 5.1 is the minimum version to generate the issues report, change your orchestrator.properties", orchestrator.getConfiguration().getSonarVersion().isGreaterThanOrEquals("5.1")); File litsDifferencesFile = FileLocation.of("target/differences").getFile(); SonarRunner build = SonarRunner.create(FileLocation.of("../sources/src").getFile()) .setProjectKey("project") .setProjectName("project") .setProjectVersion("1") .setLanguage("js") .setSourceDirs("./") .setSourceEncoding("utf-8") .setProfile("rules") .setProperty("sonar.analysis.mode", "preview") .setProperty("sonar.issuesReport.html.enable", "true") .setProperty( "dump.old", FileLocation.of("src/test/expected").getFile().getAbsolutePath()) .setProperty("dump.new", FileLocation.of("target/actual").getFile().getAbsolutePath()) .setProperty("lits.differences", litsDifferencesFile.getAbsolutePath()) .setProperty("sonar.cpd.skip", "true") .setEnvironmentVariable("SONAR_RUNNER_OPTS", "-Xmx1024m"); orchestrator.executeBuild(build); assertThat(Files.toString(litsDifferencesFile, StandardCharsets.UTF_8)).isEmpty(); }
@Test public void invalid_incremental_mode() throws IOException { restoreProfile("one-issue-per-line.xml"); orchestrator.getServer().provisionProject("sample", "xoo-sample"); orchestrator .getServer() .associateProjectToQualityProfile("sample", "xoo", "one-issue-per-line"); SonarRunner runner = configureRunner("shared/xoo-sample"); runner.setProperty("sonar.analysis.mode", "incremental"); thrown.expect(BuildFailureException.class); BuildResult res = orchestrator.executeBuild(runner); assertThat(res.getLogs()) .contains("Invalid analysis mode: incremental. This mode was removed in SonarQube 5.2"); }
@BeforeClass public static void scanStruts() { orchestrator.resetData(); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-multi-modules-sample"))); createUser("user-measure-filters", "User Measure Filters"); }
@Test public void should_exclude_source_files() { assumeTrue(AndroidTestSuite.isAtLeastPlugin1_1()); SonarRunner analysis = SonarRunner.create() .setProfile("it-profile") .setProjectName("SonarAndroidSample") .setProjectKey("SonarAndroidSample") .setProjectVersion("1.0") .setSourceDirs("app/src/main") .setProjectDir(new File("projects/SonarAndroidSample")) .setProperty("skipTests", "true") .setProperty("sonar.global.exclusions", "**/TestViolations.java") .setProperty("sonar.android.lint.report", "lint-results.xml") .setProperty("sonar.import_unknown_files", "true"); orchestrator.executeBuild(analysis); Resource project = sonar.find(ResourceQuery.createForMetrics("SonarAndroidSample", "violations")); int expectedViolations = 0; if (AndroidTestSuite.sonarqube_version_is_after_5_1()) { // After version 5.1 xml files will be indexed thanks to sonar.import_unknown_files parameter // and so issues can be reported on them. expectedViolations = 4; } assertThat(project.getMeasureIntValue("violations")).isEqualTo(expectedViolations); }
@Test @Ignore("Deactivated awaiting resolution of http://jira.sonarsource.com/browse/JC-145") public void should_run_lint_after_export_and_import_results() throws Exception { assumeTrue(AndroidTestSuite.isAtLeastPlugin1_1()); String response = exportProfile("it-profile"); File baseDir = new File("projects/SonarAndroidSample/app"); FileUtils.write(new File(baseDir, "lint.xml"), response, Charsets.UTF_8); ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "gradle lint"); pb.directory(baseDir); pb.inheritIO(); Process gradleProcess = pb.start(); int exitStatus = gradleProcess.waitFor(); if (exitStatus != 0) { fail("Failed to execute gradle lint."); } SonarRunner analysis = SonarRunner.create() .setProfile("it-profile") .setProjectName("SonarAndroidSample2") .setProjectKey("SonarAndroidSample2") .setProjectVersion("1.0") .setSourceDirs("src/main") .setProjectDir(baseDir) .setProperty("sonar.android.lint.report", "lint-report-build.xml") .setProperty("sonar.import_unknown_files", "true"); orchestrator.executeBuild(analysis); Resource project = sonar.find(ResourceQuery.createForMetrics("SonarAndroidSample2", "violations")); assertThat(project.getMeasureIntValue("violations")).isEqualTo(2); }
/** SONAR-7093 */ @Test public void ensure_leak_period_defined_at_project_level_is_taken_into_account() throws Exception { orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY); // Set a global property and a project property to ensure project property is used setServerProperty(orchestrator, "sonar.timemachine.period1", "previous_analysis"); setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period1", "30"); // Execute an analysis in the past to have a past snapshot without any issues orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty"); orchestrator.executeBuild( SonarRunner.create(projectDir("shared/xoo-sample")) .setProperty("sonar.projectDate", formatDate(addDays(new Date(), -15)))); // Second analysis -> issues will be created orchestrator .getServer() .restoreProfile(FileLocation.ofClasspath("/measureHistory/one-issue-per-line-profile.xml")); orchestrator .getServer() .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line"); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); // Third analysis -> There's no new issue from previous analysis orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); // Project should have 17 new issues for period 1 Resource newTechnicalDebt = orchestrator .getServer() .getWsClient() .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations").setIncludeTrends(true)); List<Measure> measures = newTechnicalDebt.getMeasures(); assertThat(measures.get(0).getVariation1()).isEqualTo(17); // Check on ui that it's possible to define leak period on project new SeleneseTest( Selenese.builder() .setHtmlTestsInClasspath( "define-leak-period-on-project", "/measureHistory/DifferentialPeriodsTest/define-leak-period-on-project.html") .build()) .runOn(orchestrator); }
private static SonarRunner analyzeProject( File projectDir, String projectKey, boolean create, String... additionalProperties) { if (create) { orchestrator.getServer().provisionProject(projectKey, projectKey); orchestrator .getServer() .associateProjectToQualityProfile(projectKey, "xoo", "xoo-duplication-profile"); } SonarRunner sonarRunner = SonarRunner.create(projectDir); ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); for (int i = 0; i < additionalProperties.length; i += 2) { builder.put(additionalProperties[i], additionalProperties[i + 1]); } SonarRunner scan = sonarRunner.setDebugLogs(true).setProperties(builder.build()); orchestrator.executeBuild(scan); return scan; }
/** SONAR-4700 */ @Test public void not_display_periods_selection_dropdown_on_first_analysis() { orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY); orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty"); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); // Use old way to execute Selenium because 'assertSelectOptions' action is not supported by // SeleneseTest orchestrator.executeSelenese( Selenese.builder() .setHtmlTestsInClasspath( "not-display-periods-selection-dropdown-on-first-analysis", "/measureHistory/DifferentialPeriodsTest/not-display-periods-selection-dropdown-on-dashboard.html") .build()); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); orchestrator.executeSelenese( Selenese.builder() .setHtmlTestsInClasspath( "display-periods-selection-dropdown-after-first-analysis", "/measureHistory/DifferentialPeriodsTest/display-periods-selection-dropdown-on-dashboard.html") .build()); }
/** SONAR-926 SONAR-5069 */ @Test public void test_sonar_runner_inspection() { orchestrator .getServer() .restoreProfile( FileLocation.ofClasspath("/analysis/MultiLanguageTest/one-issue-per-line.xml")); orchestrator .getServer() .restoreProfile( FileLocation.ofClasspath("/analysis/MultiLanguageTest/one-issue-per-line-xoo2.xml")); orchestrator.getServer().provisionProject("multi-language-sample", "multi-language-sample"); orchestrator .getServer() .associateProjectToQualityProfile("multi-language-sample", "xoo", "one-issue-per-line"); orchestrator .getServer() .associateProjectToQualityProfile( "multi-language-sample", "xoo2", "one-issue-per-line-xoo2"); SonarRunner build = SonarRunner.create().setProjectDir(ItUtils.projectDir("analysis/xoo-multi-languages")); BuildResult result = orchestrator.executeBuild(build); assertThat(result.getLogs()).contains("2 files indexed"); assertThat(result.getLogs()).contains("Quality profile for xoo: one-issue-per-line"); assertThat(result.getLogs()).contains("Quality profile for xoo2: one-issue-per-line-xoo2"); // modules Resource project = getResource("multi-language-sample", "files", "violations"); assertThat(project.getMeasureIntValue("files")).isEqualTo(2); assertThat(project.getMeasureIntValue("violations")).isEqualTo(26); Resource xooFile = getResource("multi-language-sample:src/sample/Sample.xoo", "violations"); assertThat(xooFile.getMeasureIntValue("violations")).isEqualTo(13); Resource xoo2File = getResource("multi-language-sample:src/sample/Sample.xoo2", "violations"); assertThat(xoo2File.getMeasureIntValue("violations")).isEqualTo(13); }
private void scan(String... properties) { SonarRunner build = SonarRunner.create(ItUtils.projectDir("exclusions/exclusions")).setProperties(properties); orchestrator.executeBuild(build); }
private SonarRunner createBuild(String testReportPath) { return SonarRunner.create() .setProjectDir(new File("projects/nosetests_project")) .setProperty("sonar.python.xunit.reportPath", testReportPath); }
@Before public void inspectProject() { orchestrator.resetData(); orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample"))); }