Version checkDbVersion(DatabaseMetaData metaData, Version minSupported) throws SQLException { int major = metaData.getDatabaseMajorVersion(); int minor = metaData.getDatabaseMinorVersion(); Version version = Version.create(major, minor, 0); if (version.compareTo(minSupported) < 0) { throw MessageException.of(String.format( "Unsupported %s version: %s. Minimal supported version is %s.", getId(), version, minSupported)); } return version; } }
@Test public void test_create() { assertVersion(Version.create(1, 2), 1, 2, 0, 0, ""); assertVersion(Version.create(1, 2, 3), 1, 2, 3, 0, ""); assertVersion(Version.create(1, 2, 0, ""), 1, 2, 0, 0, ""); assertVersion(Version.create(1, 2, 3, "build1"), 1, 2, 3, 0, "build1"); assertThat(Version.create(1, 2, 3, "build1").toString()).isEqualTo("1.2.3-build1"); }
public Optional<UpdateCenter> getUpdateCenter(boolean refreshUpdateCenter) { Optional<UpdateCenter> updateCenter = centerClient.getUpdateCenter(refreshUpdateCenter); if (updateCenter.isPresent()) { org.sonar.api.utils.Version fullVersion = sonarRuntime.getApiVersion(); org.sonar.api.utils.Version semanticVersion = org.sonar.api.utils.Version.create(fullVersion.major(), fullVersion.minor(), fullVersion.patch()); return Optional.of(updateCenter.get().setInstalledSonarVersion(Version.create(semanticVersion.toString())).registerInstalledPlugins( installedPluginReferentialFactory.getInstalledPluginReferential()) .setDate(centerClient.getLastRefreshDate())); } return Optional.absent(); } }
@Test public void test_getVersion() { Version version = Version.create(6, 1); when(runtime.getApiVersion()).thenReturn(version); assertThat(underTest.getVersion()).isEqualTo(version.toString()); } }
@Test public void load_from_database_if_server_is_startup_follower() { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(6, 1), SonarQubeSide.SERVER); when(webServer.isStartupLeader()).thenReturn(false); testLoadingFromDatabase(runtime, false); }
private void test(SonarQubeSide side) { underTest = new ServerIdManager(serverIdChecksum, serverIdFactory, dbClient, SonarRuntimeImpl.forSonarQube(Version.create(6, 7), side), webServer); underTest.start(); } }
@Test public void load_from_database_if_compute_engine_of_startup_leader_server() { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(6, 1), SonarQubeSide.COMPUTE_ENGINE); testLoadingFromDatabase(runtime, true); }
@Test public void load_from_database_if_compute_engine_of_startup_follower_server() { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(6, 1), SonarQubeSide.COMPUTE_ENGINE); testLoadingFromDatabase(runtime, false); }
@Test public void generate_SERVER_STARTIME_but_do_not_persist_it_if_server_is_startup_leader() { when(system.now()).thenReturn(A_DATE); SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(6, 1), SonarQubeSide.SERVER); when(webServer.isStartupLeader()).thenReturn(true); StartupMetadata metadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); assertThat(metadata.getStartedAt()).isEqualTo(A_DATE); assertNotPersistedProperty(CoreProperties.SERVER_STARTTIME); // keep a cache StartupMetadata secondMetadata = underTest.provide(system, runtime, webServer, dbTester.getDbClient()); assertThat(secondMetadata).isSameAs(metadata); }
@Test public void fail_to_load_from_database_if_properties_are_not_persisted() { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.create(6, 1), SonarQubeSide.COMPUTE_ENGINE); when(webServer.isStartupLeader()).thenReturn(false); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Property sonar.core.startTime is missing in database"); underTest.provide(system, runtime, webServer, dbTester.getDbClient()); }
@Before public void setUp() { XooRulesDefinition def = new XooRulesDefinition(SonarRuntimeImpl.forSonarQube(Version.create(7, 3), SonarQubeSide.SCANNER)); context = new RulesDefinition.Context(); def.define(context); }
if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 6))) { context.addExtension(XooBuiltInQualityProfilesDefinition.class); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 4))) { context.addExtension(DeprecatedGlobalSensor.class); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 6))) { context.addExtensions( GlobalProjectSensor.class, XooIgnoreCommand.class); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 2))) { context.addExtensions( OneExternalIssuePerLineSensor.class, SignificantCodeSensor.class); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 3))) { context.addExtension(HotspotSensor.class);
private void createIssues(InputFile file, SensorContext context, String repo) { RuleKey ruleKey = RuleKey.of(repo, RULE_KEY); String severity = context.settings().getString(FORCE_SEVERITY_PROPERTY); for (int line = 1; line <= file.lines(); line++) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) .overrideSeverity(severity != null ? Severity.valueOf(severity) : null); if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(5, 5))) { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } else { newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); } newIssue.save(); } }
.setDebtRemediationFunction(hotspot.debtRemediationFunctions().constantPerIssue("2min")); if (version != null && version.isGreaterThanOrEqual(Version.create(7, 3))) { hotspot .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3)
Version checkDbVersion(DatabaseMetaData metaData, Version minSupported) throws SQLException { int major = metaData.getDatabaseMajorVersion(); int minor = metaData.getDatabaseMinorVersion(); Version version = Version.create(major, minor, 0); if (version.compareTo(minSupported) < 0) { throw MessageException.of(String.format( "Unsupported %s version: %s. Minimal supported version is %s.", getId(), version, minSupported)); } return version; } }
private static void importReport(File reportPath, SensorContext context, Set<String> unresolvedInputFiles) { try (InputStream in = new FileInputStream(reportPath)) { LOG.info("Importing {}", reportPath); boolean engineIdIsSupported = context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 4)); BanditJsonReportReader.read(in, issue -> saveIssue(context, issue, unresolvedInputFiles, engineIdIsSupported)); } catch (IOException | ParseException | RuntimeException e) { LOG.error("No issues information will be saved as the report file '{}' can't be read. " + e.getClass().getSimpleName() + ": " + e.getMessage(), reportPath, e); } }
private static void importReport(File reportPath, SensorContext context, Set<String> unresolvedInputFiles) { try (InputStream in = new FileInputStream(reportPath)) { LOG.info("Importing {}", reportPath); boolean engineIdIsSupported = context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(7, 4)); BanditJsonReportReader.read(in, issue -> saveIssue(context, issue, unresolvedInputFiles, engineIdIsSupported)); } catch (IOException | ParseException | RuntimeException e) { LOG.error("No issues information will be saved as the report file '{}' can't be read. " + e.getClass().getSimpleName() + ": " + e.getMessage(), reportPath, e); } }
public Optional<UpdateCenter> getUpdateCenter(boolean refreshUpdateCenter) { Optional<UpdateCenter> updateCenter = centerClient.getUpdateCenter(refreshUpdateCenter); if (updateCenter.isPresent()) { org.sonar.api.utils.Version fullVersion = sonarRuntime.getApiVersion(); org.sonar.api.utils.Version semanticVersion = org.sonar.api.utils.Version.create(fullVersion.major(), fullVersion.minor(), fullVersion.patch()); return Optional.of(updateCenter.get().setInstalledSonarVersion(Version.create(semanticVersion.toString())).registerInstalledPlugins( installedPluginReferentialFactory.getInstalledPluginReferential()) .setDate(centerClient.getLastRefreshDate())); } return Optional.absent(); } }
@SuppressWarnings("unchecked") @Test public void test() { Version v60 = Version.create(6, 0); assertThat(extensions(SonarRuntimeImpl.forSonarQube(v60, SonarQubeSide.SERVER))).hasSize(7); }
static SonarComponents sonarComponents(File file) { SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7))); context.setSettings(new MapSettings().setProperty("sonar.java.failOnException", true)); SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null) { @Override public boolean reportAnalysisError(RecognitionException re, File file) { return false; } }; sonarComponents.setSensorContext(context); context.fileSystem().add(new TestInputFileBuilder("", file.getPath()).setCharset(StandardCharsets.UTF_8).build()); return sonarComponents; }