@Test
 public void determineDriver() {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setUrl("jdbc:mysql://mydb");
   assertThat(properties.getDriverClassName()).isNull();
   assertThat(properties.determineDriverClassName()).isEqualTo("com.mysql.jdbc.Driver");
 }
 @Test
 public void determinePassword() throws Exception {
   DataSourceProperties properties = new DataSourceProperties();
   properties.afterPropertiesSet();
   assertThat(properties.getPassword()).isNull();
   assertThat(properties.determinePassword()).isEqualTo("");
 }
 @Test
 public void determineUrl() throws Exception {
   DataSourceProperties properties = new DataSourceProperties();
   properties.afterPropertiesSet();
   assertThat(properties.getUrl()).isNull();
   assertThat(properties.determineUrl()).isEqualTo(EmbeddedDatabaseConnection.H2.getUrl());
 }
 @Test
 public void determineUsernameWithExplicitConfig() throws Exception {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setUsername("foo");
   properties.afterPropertiesSet();
   assertThat(properties.getUsername()).isEqualTo("foo");
   assertThat(properties.determineUsername()).isEqualTo("foo");
 }
 @Test
 public void determineUrlWithExplicitConfig() throws Exception {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setUrl("jdbc:mysql://mydb");
   properties.afterPropertiesSet();
   assertThat(properties.getUrl()).isEqualTo("jdbc:mysql://mydb");
   assertThat(properties.determineUrl()).isEqualTo("jdbc:mysql://mydb");
 }
 @Test
 public void determineDriverWithExplicitConfig() {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setUrl("jdbc:mysql://mydb");
   properties.setDriverClassName("org.hsqldb.jdbcDriver");
   assertThat(properties.getDriverClassName()).isEqualTo("org.hsqldb.jdbcDriver");
   assertThat(properties.determineDriverClassName()).isEqualTo("org.hsqldb.jdbcDriver");
 }
 @Test
 public void determineCredentialsForDataScripts() {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setDataUsername("foo");
   properties.setDataPassword("bar");
   assertThat(properties.getDataUsername()).isEqualTo("foo");
   assertThat(properties.getDataPassword()).isEqualTo("bar");
 }
 @Test
 public void determinePasswordWithExplicitConfig() throws Exception {
   DataSourceProperties properties = new DataSourceProperties();
   properties.setPassword("bar");
   properties.afterPropertiesSet();
   assertThat(properties.getPassword()).isEqualTo("bar");
   assertThat(properties.determinePassword()).isEqualTo("bar");
 }
 /*
  * (non-javadoc)
  *
  * Use Tomcat 7 DBCP because GAE restricts usage on
  * java.lang.management.ManagementFactory
  *
  */
 @Bean
 public DataSource datasource() {
   BasicDataSource datasource = new BasicDataSource();
   datasource.setDriverClassName(properties.getDriverClassName());
   datasource.setUrl(properties.getUrl());
   datasource.setUsername(properties.getUsername());
   datasource.setPassword(properties.getPassword());
   datasource.setMaxActive(12);
   return datasource;
 }
  @Test
  public void determineUrlWithGenerateUniqueName() throws Exception {
    DataSourceProperties properties = new DataSourceProperties();
    properties.setGenerateUniqueName(true);
    properties.afterPropertiesSet();
    assertThat(properties.determineUrl()).isEqualTo(properties.determineUrl());

    DataSourceProperties properties2 = new DataSourceProperties();
    properties2.setGenerateUniqueName(true);
    properties2.afterPropertiesSet();
    assertThat(properties.determineUrl()).isNotEqualTo(properties2.determineUrl());
  }
 /**
  * Initialize OracleDataSource manually because datasource property
  * spring.datasource.type=oracle.jdbc.pool.OracleDataSource is not correctly handled by spring
  * https://github.com/spring-projects/spring-boot/issues/6027#issuecomment-221582708
  *
  * @param properties
  * @return
  * @throws SQLException
  */
 @Bean
 public DataSource dataSource(final DataSourceProperties properties) throws SQLException {
   final PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
   dataSource.setUser(properties.getUsername());
   dataSource.setPassword(properties.getPassword());
   dataSource.setURL(properties.getUrl());
   dataSource.setFastConnectionFailoverEnabled(true);
   dataSource.setMaxPoolSize(20);
   dataSource.setMinPoolSize(5);
   dataSource.setMaxIdleTime(5);
   dataSource.setValidateConnectionOnBorrow(true);
   dataSource.setMaxStatements(10);
   dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
   return dataSource;
 }
  @Bean
  public SpringLiquibase liquibase(
      DataSource dataSource,
      DataSourceProperties dataSourceProperties,
      LiquibaseProperties liquibaseProperties) {

    // Use liquibase.integration.spring.SpringLiquibase if you don't want Liquibase to start
    // asynchronously
    SpringLiquibase liquibase = new AsyncSpringLiquibase();
    liquibase.setDataSource(dataSource);
    liquibase.setChangeLog("classpath:config/liquibase/master.xml");
    liquibase.setContexts(liquibaseProperties.getContexts());
    liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
    liquibase.setDropFirst(liquibaseProperties.isDropFirst());
    liquibase.setShouldRun(liquibaseProperties.isEnabled());
    if (env.acceptsProfiles(Constants.SPRING_PROFILE_FAST)) {
      if ("org.h2.jdbcx.JdbcDataSource".equals(dataSourceProperties.getDriverClassName())) {
        liquibase.setShouldRun(true);
        log.warn(
            "Using '{}' profile with H2 database in memory is not optimal, you should consider switching to"
                + " MySQL or Postgresql to avoid rebuilding your database upon each start.",
            Constants.SPRING_PROFILE_FAST);
      } else {
        liquibase.setShouldRun(false);
      }
    } else {
      log.debug("Configuring Liquibase");
    }
    return liquibase;
  }
  @Bean(destroyMethod = "close")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource(
      DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Datasource");
    if (dataSourceProperties.getUrl() == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
    config.addDataSourceProperty("url", dataSourceProperties.getUrl());
    if (dataSourceProperties.getUsername() != null) {
      config.addDataSourceProperty("user", dataSourceProperties.getUsername());
    } else {
      config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
    }
    if (dataSourceProperties.getPassword() != null) {
      config.addDataSourceProperty("password", dataSourceProperties.getPassword());
    } else {
      config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
    }

    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }
  @Bean(destroyMethod = "close")
  @ConditionalOnExpression(
      "#{!environment.acceptsProfiles('cloud') && !environment.acceptsProfiles('heroku')}")
  public DataSource dataSource(
      DataSourceProperties dataSourceProperties, JHipsterProperties jHipsterProperties) {
    log.debug("Configuring Datasource");
    if (dataSourceProperties.getUrl() == null) {
      log.error(
          "Your database connection pool configuration is incorrect! The application"
              + " cannot start. Please check your Spring profile, current profiles are: {}",
          Arrays.toString(env.getActiveProfiles()));

      throw new ApplicationContextException("Database connection pool is not configured correctly");
    }
    HikariConfig config = new HikariConfig();
    config.setDataSourceClassName(dataSourceProperties.getDriverClassName());
    config.addDataSourceProperty("url", dataSourceProperties.getUrl());
    if (dataSourceProperties.getUsername() != null) {
      config.addDataSourceProperty("user", dataSourceProperties.getUsername());
    } else {
      config.addDataSourceProperty("user", ""); // HikariCP doesn't allow null user
    }
    if (dataSourceProperties.getPassword() != null) {
      config.addDataSourceProperty("password", dataSourceProperties.getPassword());
    } else {
      config.addDataSourceProperty("password", ""); // HikariCP doesn't allow null password
    }

    // MySQL optimizations, see https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
    if ("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
        .equals(dataSourceProperties.getDriverClassName())) {
      config.addDataSourceProperty(
          "cachePrepStmts", jHipsterProperties.getDatasource().isCachePrepStmts());
      config.addDataSourceProperty(
          "prepStmtCacheSize", jHipsterProperties.getDatasource().getPrepStmtCacheSize());
      config.addDataSourceProperty(
          "prepStmtCacheSqlLimit", jHipsterProperties.getDatasource().getPrepStmtCacheSqlLimit());
    }
    if (metricRegistry != null) {
      config.setMetricRegistry(metricRegistry);
    }
    return new HikariDataSource(config);
  }