Search in sources :

Example 21 with Configuration

use of org.sonar.api.config.Configuration in project sonarqube by SonarSource.

the class DefaultConfigurationTest method accessingPropertySetPropertiesShouldBeConsistentWithDeclaration.

@Test
public void accessingPropertySetPropertiesShouldBeConsistentWithDeclaration() {
    Configuration config = new DefaultConfiguration(new PropertyDefinitions(System2.INSTANCE, Arrays.asList(PropertyDefinition.builder("props").fields(PropertyFieldDefinition.build("foo1").name("Foo1").build(), PropertyFieldDefinition.build("foo2").name("Foo2").build()).build())), new Encryption(null), ImmutableMap.of("props", "1,2", "props.1.foo1", "a", "props.1.foo2", "b")) {
    };
    assertThat(config.get("props")).hasValue("1,2");
    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Access to the multi-values/property set property 'props' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.");
    logTester.clear();
    assertThat(config.getStringArray("props")).containsExactly("1", "2");
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
}
Also used : Configuration(org.sonar.api.config.Configuration) PropertyDefinitions(org.sonar.api.config.PropertyDefinitions) Encryption(org.sonar.api.config.internal.Encryption) Test(org.junit.Test)

Example 22 with Configuration

use of org.sonar.api.config.Configuration in project sonarqube by SonarSource.

the class DefaultConfigurationTest method getDefaultValues.

@Test
public void getDefaultValues() {
    Configuration config = new DefaultConfiguration(new PropertyDefinitions(System2.INSTANCE, Arrays.asList(PropertyDefinition.builder("single").multiValues(false).defaultValue("default").build(), PropertyDefinition.builder("multiA").multiValues(true).defaultValue("foo,bar").build())), new Encryption(null), ImmutableMap.of()) {
    };
    assertThat(config.get("multiA")).hasValue("foo,bar");
    assertThat(config.getStringArray("multiA")).containsExactly("foo", "bar");
    assertThat(config.get("single")).hasValue("default");
    assertThat(config.getStringArray("single")).containsExactly("default");
}
Also used : Configuration(org.sonar.api.config.Configuration) PropertyDefinitions(org.sonar.api.config.PropertyDefinitions) Encryption(org.sonar.api.config.internal.Encryption) Test(org.junit.Test)

Example 23 with Configuration

use of org.sonar.api.config.Configuration in project sonarqube by SonarSource.

the class CoreExtensionsInstallerTest method install_provides_Configuration_from_container_when_getBootConfiguration_is_called.

@Test
public void install_provides_Configuration_from_container_when_getBootConfiguration_is_called() {
    CoreExtension coreExtension1 = newCoreExtension();
    CoreExtension coreExtension2 = newCoreExtension();
    when(coreExtensionRepository.loadedCoreExtensions()).thenReturn(Stream.of(coreExtension1, coreExtension2));
    Configuration configuration = new MapSettings().asConfig();
    ExtensionContainer container = mock(ExtensionContainer.class);
    when(container.getComponentByType(Configuration.class)).thenReturn(configuration);
    underTest.install(container, noExtensionFilter(), noAdditionalSideFilter());
    verify(coreExtension1).load(contextCaptor.capture());
    verify(coreExtension2).load(contextCaptor.capture());
    assertThat(contextCaptor.getAllValues()).extracting(CoreExtension.Context::getBootConfiguration).containsOnly(configuration);
}
Also used : Configuration(org.sonar.api.config.Configuration) MapSettings(org.sonar.api.config.internal.MapSettings) ExtensionContainer(org.sonar.core.platform.ExtensionContainer) Test(org.junit.Test)

Example 24 with Configuration

use of org.sonar.api.config.Configuration in project sonar-java by SonarSource.

the class JavaSquidSensorTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock(SensorContextTester contextTester) {
    Configuration settings = new MapSettings().asConfig();
    DefaultFileSystem fs = contextTester.fileSystem();
    JavaTestClasspath javaTestClasspath = new JavaTestClasspath(settings, fs);
    JavaClasspath javaClasspath = new JavaClasspath(settings, fs);
    FileLinesContext fileLinesContext = mock(FileLinesContext.class);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
    SonarComponents sonarComponents = spy(new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory));
    sonarComponents.setSensorContext(contextTester);
    BadMethodNameCheck check = new BadMethodNameCheck();
    when(sonarComponents.checkClasses()).thenReturn(new JavaCheck[] { check });
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) JavaClasspath(org.sonar.java.JavaClasspath) BadMethodNameCheck(org.sonar.java.checks.naming.BadMethodNameCheck) Configuration(org.sonar.api.config.Configuration) MapSettings(org.sonar.api.config.internal.MapSettings) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) JavaTestClasspath(org.sonar.java.JavaTestClasspath) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 25 with Configuration

use of org.sonar.api.config.Configuration in project sonarqube by SonarSource.

the class LiveMeasureComputerImpl method refreshComponentsOnSameProject.

private Optional<QGChangeEvent> refreshComponentsOnSameProject(DbSession dbSession, List<ComponentDto> touchedComponents) {
    // load all the components to be refreshed, including their ancestors
    List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
    ComponentDto branchComponent = findBranchComponent(components);
    BranchDto branch = loadBranch(dbSession, branchComponent);
    ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
    Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
    if (!lastAnalysis.isPresent()) {
        return Optional.empty();
    }
    QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
    Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
    List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
    Map<String, MetricDto> metricsPerId = metrics.stream().collect(uniqueIndex(MetricDto::getUuid));
    List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
    List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerId.keySet());
    // previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
    Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);
    Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, branchComponent);
    DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
    MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
    FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
    long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
    components.forEach(c -> {
        IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeak));
        for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
            // use formulas when the leak period is defined, it's a PR, or the formula is not about the leak period
            if (shouldUseLeakFormulas(lastAnalysis.get(), branch) || !formula.isOnLeak()) {
                context.change(c, formula);
                try {
                    formula.compute(context, issueCounter);
                } catch (RuntimeException e) {
                    throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
                }
            }
        }
    });
    EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
    // persist the measures that have been created or updated
    matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE).forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m));
    projectIndexer.commitAndIndexComponents(dbSession, singleton(branchComponent), ProjectIndexer.Cause.MEASURE_CHANGE);
    return Optional.of(new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) Configuration(org.sonar.api.config.Configuration) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) QGChangeEvent(org.sonar.server.qualitygate.changeevent.QGChangeEvent) QualityGate(org.sonar.server.qualitygate.QualityGate) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) MetricDto(org.sonar.db.metric.MetricDto) EvaluatedQualityGate(org.sonar.server.qualitygate.EvaluatedQualityGate) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) Metric(org.sonar.api.measures.Metric) DebtRatingGrid(org.sonar.server.measure.DebtRatingGrid)

Aggregations

Configuration (org.sonar.api.config.Configuration)47 Test (org.junit.Test)40 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 ComponentDto (org.sonar.db.component.ComponentDto)12 QGChangeEvent (org.sonar.server.qualitygate.changeevent.QGChangeEvent)7 MapSettings (org.sonar.api.config.internal.MapSettings)5 SnapshotDto (org.sonar.db.component.SnapshotDto)4 PropertyDefinitions (org.sonar.api.config.PropertyDefinitions)3 Encryption (org.sonar.api.config.internal.Encryption)3 Metric (org.sonar.api.measures.Metric)3 DefaultBranchImpl (org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl)3 HashSet (java.util.HashSet)2 DbClient (org.sonar.db.DbClient)2 BranchDto (org.sonar.db.component.BranchDto)2 LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)2 MetricDto (org.sonar.db.metric.MetricDto)2 ProjectDto (org.sonar.db.project.ProjectDto)2 EvaluatedQualityGate (org.sonar.server.qualitygate.EvaluatedQualityGate)2 QualityGate (org.sonar.server.qualitygate.QualityGate)2 File (java.io.File)1