Search in sources :

Example 6 with Settings

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

the class CoreTestDb method init.

private CoreTestDb init(@Nullable String schemaPath, boolean databaseToUpper) {
    Consumer<Settings> noExtraSettingsLoaded = settings -> {
    };
    Function<Settings, Database> databaseCreator = settings -> {
        String dialect = settings.getString("sonar.jdbc.dialect");
        // test relying on CoreTestDb can only run on H2
        if (dialect != null && !"h2".equals(dialect)) {
            throw new AssumptionViolatedException("This test is intended to be run on H2 only");
        }
        String name = "h2Tests-" + (schemaPath == null ? "empty" : DigestUtils.md5Hex(schemaPath));
        if (!databaseToUpper) {
            name = name + ";DATABASE_TO_UPPER=FALSE";
        }
        name = name + ";NON_KEYWORDS=VALUE";
        return new CoreH2Database(name);
    };
    Consumer<Database> databaseInitializer = database -> {
        if (schemaPath == null) {
            return;
        }
        ((CoreH2Database) database).executeScript(schemaPath);
    };
    BiConsumer<Database, Boolean> noPostStartAction = (db, created) -> {
    };
    init(noExtraSettingsLoaded, databaseCreator, databaseInitializer, noPostStartAction);
    return this;
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) Settings(org.sonar.api.config.internal.Settings) Connection(java.sql.Connection) MapSettings(org.sonar.api.config.internal.MapSettings) Function(java.util.function.Function) Consumer(java.util.function.Consumer) Loggers(org.sonar.api.utils.log.Loggers) SQLException(java.sql.SQLException) JDBC_USERNAME(org.sonar.process.ProcessProperties.Property.JDBC_USERNAME) ResultSet(java.sql.ResultSet) Objects.requireNonNull(java.util.Objects.requireNonNull) Statement(java.sql.Statement) BiConsumer(java.util.function.BiConsumer) DataSource(javax.sql.DataSource) DigestUtils(org.apache.commons.codec.digest.DigestUtils) SqTables(org.sonar.db.version.SqTables) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) AssumptionViolatedException(org.junit.AssumptionViolatedException) Settings(org.sonar.api.config.internal.Settings) MapSettings(org.sonar.api.config.internal.MapSettings)

Example 7 with Settings

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

the class CoreTestDb method init.

protected void init(Consumer<Settings> settingsLoader, Function<Settings, Database> databaseCreator, Consumer<Database> databaseInitializer, BiConsumer<Database, Boolean> extendedStart) {
    if (db == null) {
        Settings settings = new MapSettings().addProperties(System.getProperties());
        settingsLoader.accept(settings);
        logJdbcSettings(settings);
        db = databaseCreator.apply(settings);
        db.start();
        databaseInitializer.accept(db);
        Loggers.get(getClass()).debug("Test Database: " + db);
        String login = settings.getString(JDBC_USERNAME.getKey());
        extendedStart.accept(db, true);
    } else {
        extendedStart.accept(db, false);
    }
}
Also used : MapSettings(org.sonar.api.config.internal.MapSettings) Settings(org.sonar.api.config.internal.Settings) MapSettings(org.sonar.api.config.internal.MapSettings)

Example 8 with Settings

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

the class TestDbImpl method init.

private void init(@Nullable String schemaPath, MyBatisConfExtension[] confExtensions) {
    Consumer<Settings> loadOrchestratorSettings = OrchestratorSettingsUtils::loadOrchestratorSettings;
    Function<Settings, Database> databaseCreator = settings -> {
        String dialect = settings.getString("sonar.jdbc.dialect");
        if (dialect != null && !"h2".equals(dialect)) {
            return new DefaultDatabase(new LogbackHelper(), settings);
        }
        return SQDatabase.newH2Database("h2Tests" + DigestUtils.md5Hex(StringUtils.defaultString(schemaPath)), schemaPath == null);
    };
    Consumer<Database> schemaPathExecutor = database -> {
        if (schemaPath == null) {
            return;
        }
        // scripts are assumed to be using H2 specific syntax, ignore the test if not on H2
        if (!database.getDialect().getId().equals("h2")) {
            database.stop();
            throw new AssumptionViolatedException("This test is intended to be run on H2 only");
        }
        ((SQDatabase) database).executeScript(schemaPath);
    };
    BiConsumer<Database, Boolean> createMyBatis = (db, created) -> myBatis = newMyBatis(db, confExtensions);
    init(loadOrchestratorSettings, databaseCreator, schemaPathExecutor, createMyBatis);
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) AssumptionViolatedException(org.junit.AssumptionViolatedException) Settings(org.sonar.api.config.internal.Settings) H2(org.sonar.db.dialect.H2) Set(java.util.Set) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) DigestUtils(org.apache.commons.codec.digest.DigestUtils) LogbackHelper(org.sonar.process.logging.LogbackHelper) Nullable(javax.annotation.Nullable) LogbackHelper(org.sonar.process.logging.LogbackHelper) AssumptionViolatedException(org.junit.AssumptionViolatedException) Settings(org.sonar.api.config.internal.Settings)

Aggregations

Settings (org.sonar.api.config.internal.Settings)8 MapSettings (org.sonar.api.config.internal.MapSettings)4 Test (org.junit.Test)3 BiConsumer (java.util.function.BiConsumer)2 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 Nullable (javax.annotation.Nullable)2 DigestUtils (org.apache.commons.codec.digest.DigestUtils)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 Tuple.tuple (org.assertj.core.groups.Tuple.tuple)2 AssumptionViolatedException (org.junit.AssumptionViolatedException)2 BeforeClass (org.junit.BeforeClass)2 Rule (org.junit.Rule)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 Mockito.doThrow (org.mockito.Mockito.doThrow)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.when (org.mockito.Mockito.when)2