Search in sources :

Example 11 with PGSimpleDataSource

use of org.postgresql.ds.PGSimpleDataSource in project jdbi by jdbi.

the class JdbiExternalPostgresExtension method createDataSource.

@Override
protected DataSource createDataSource() throws Exception {
    final PGSimpleDataSource datasource = new PGSimpleDataSource();
    datasource.setServerNames(new String[] { hostname });
    if (port != null) {
        datasource.setPortNumbers(new int[] { port });
    }
    datasource.setUser(username);
    datasource.setPassword(password);
    datasource.setDatabaseName(database);
    datasource.setApplicationName(this.getClass().getSimpleName());
    return datasource;
}
Also used : PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource)

Example 12 with PGSimpleDataSource

use of org.postgresql.ds.PGSimpleDataSource in project sis by apache.

the class TestDatabase method createOnPostgreSQL.

/**
 * Creates a connection to an existing PostgreSQL database.
 * This method returns only if all the following conditions are true:
 *
 * <ol>
 *   <li>{@link TestCase#RUN_EXTENSIVE_TESTS} is {@code true} (for reducing the risk of messing with user installation).</li>
 *   <li>A PostgreSQL server is running on the local host and listening to the default port.</li>
 *   <li>A database named {@value #NAME} exists.</li>
 *   <li>A role with Unix user name exists and can connect to the database without password.</li>
 *   <li>The database does not contain any schema of the given name.</li>
 * </ol>
 *
 * If the {@code create} argument is {@code false}, then the callers is responsible for creating the schema
 * soon after this method call. That schema will be deleted by {@link #close()}.
 *
 * @param  schema  temporary schema to create. Shall not contain {@code '_'} or {@code '%'} characters.
 * @param  create  whether the schema should be created by this method.
 * @return connection to a PostgreSQL database
 * @throws SQLException if an error occurred while connecting to the database or creating the schema.
 *
 * @see <a href="http://sis.apache.org/source.html#postgres">Configuring PostgreSQL for Apache SIS tests</a>
 *
 * @since 1.0
 */
public static TestDatabase createOnPostgreSQL(final String schema, final boolean create) throws SQLException {
    assumeTrue("Extensive tests not enabled.", TestCase.RUN_EXTENSIVE_TESTS);
    final PGSimpleDataSource ds = new PGSimpleDataSource();
    // Server default to "localhost".
    ds.setDatabaseName(NAME);
    ds.setApplicationName("Apache SIS test database");
    ds.setCurrentSchema(schema);
    // For avoiding warning when no PostgreSQL server is running.
    ds.setProperty(PGProperty.LOGGER_LEVEL, "OFF");
    /*
         * Current version does not use pooling on the assumption that connections to local host are fast enough.
         * We verify that the schema does not exist, even if the `create` argument is `false`, because we assume
         * that the test is going to create the schema soon (see the contract documented in method javadoc).
         * This is also a safety for avoiding to delete a schema that was not created by the test case.
         */
    try (Connection c = ds.getConnection()) {
        try (ResultSet reflect = c.getMetaData().getSchemas(null, schema)) {
            if (reflect.next()) {
                throw new SQLDataException("Schema \"" + schema + "\" already exists in \"" + NAME + "\".");
            }
        }
        if (create) {
            try (Statement s = c.createStatement()) {
                s.execute("CREATE SCHEMA \"" + schema + '"');
            }
        }
    } catch (SQLException e) {
        final String state = e.getSQLState();
        assumeFalse("This test needs a PostgreSQL server running on the local host.", "08001".equals(state));
        assumeFalse("This test needs a PostgreSQL database named \"" + NAME + "\".", "3D000".equals(state));
        throw e;
    }
    return new TestDatabase(ds) {

        @Override
        public void close() throws SQLException {
            final PGSimpleDataSource ds = (PGSimpleDataSource) source;
            try (Connection c = ds.getConnection()) {
                try (Statement s = c.createStatement()) {
                    /*
                         * Prevents test to hang indefinitely if connections are not properly released in test cases.
                         * If the limit (in seconds) is exceeded, an SQLTimeoutException is thrown and test fails.
                         */
                    s.setQueryTimeout(10);
                    s.execute("DROP SCHEMA \"" + ds.getCurrentSchema() + "\" CASCADE");
                }
            }
        }
    };
}
Also used : SQLDataException(java.sql.SQLDataException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource)

Example 13 with PGSimpleDataSource

use of org.postgresql.ds.PGSimpleDataSource in project micrometer by micrometer-metrics.

the class PostgreSQLDatabaseMetricsIntegrationTest method createDataSource.

private DataSource createDataSource() {
    final PGSimpleDataSource dataSource = new PGSimpleDataSource();
    dataSource.setURL(postgres.getJdbcUrl());
    dataSource.setUser(postgres.getUsername());
    dataSource.setPassword(postgres.getPassword());
    dataSource.setDatabaseName(postgres.getDatabaseName());
    return dataSource;
}
Also used : PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource)

Example 14 with PGSimpleDataSource

use of org.postgresql.ds.PGSimpleDataSource in project trellis by trellis-ldp.

the class DBTestUtils method initialize.

static void initialize() {
    postgres.start();
    final PGSimpleDataSource ds = new PGSimpleDataSource();
    ds.setURL(postgres.getJdbcUrl());
    ds.setUser(postgres.getUsername());
    ds.setPassword(postgres.getPassword());
    try {
        // Set up database migrations
        try (final Connection c = ds.getConnection()) {
            final Liquibase liquibase = new Liquibase("org/trellisldp/jdbc/migrations.yml", new ClassLoaderResourceAccessor(), new JdbcConnection(c));
            final Contexts ctx = null;
            liquibase.update(ctx);
        }
    } catch (final SQLException | LiquibaseException ex) {
        LOGGER.error("Error setting up tests", ex);
    }
    datasource = ds;
}
Also used : Liquibase(liquibase.Liquibase) SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) JdbcConnection(liquibase.database.jvm.JdbcConnection) LiquibaseException(liquibase.exception.LiquibaseException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Contexts(liquibase.Contexts)

Example 15 with PGSimpleDataSource

use of org.postgresql.ds.PGSimpleDataSource in project jobrunr by jobrunr.

the class PostgresJacksonBackgroundJobContainer method initStorageProvider.

@Override
protected StorageProvider initStorageProvider(JdbcDatabaseContainer sqlContainer) {
    PGSimpleDataSource dataSource = new PGSimpleDataSource();
    dataSource.setURL(sqlContainer.getJdbcUrl());
    dataSource.setUser(sqlContainer.getUsername());
    dataSource.setPassword(sqlContainer.getPassword());
    return SqlStorageProviderFactory.using(dataSource);
}
Also used : PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource)

Aggregations

PGSimpleDataSource (org.postgresql.ds.PGSimpleDataSource)83 Test (org.junit.Test)16 Connection (java.sql.Connection)7 TypeLiteral (com.google.inject.TypeLiteral)5 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)5 AccountImpl (org.eclipse.che.account.spi.AccountImpl)5 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)5 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)5 TckResourcesCleaner (org.eclipse.che.commons.test.tck.TckResourcesCleaner)5 DBInitializer (org.eclipse.che.core.db.DBInitializer)5 SchemaInitializer (org.eclipse.che.core.db.schema.SchemaInitializer)5 FlywaySchemaInitializer (org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer)5 SHA512PasswordEncryptor (org.eclipse.che.security.SHA512PasswordEncryptor)5 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 AliasService (org.apache.knox.gateway.services.security.AliasService)4 UserDevfileImpl (org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl)4 AccountDao (org.eclipse.che.account.spi.AccountDao)3 JpaAccountDao (org.eclipse.che.account.spi.jpa.JpaAccountDao)3