Search in sources :

Example 16 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project gocd by gocd.

the class DatabaseFixture method query.

public static Object[][] query(String query, H2Database h2Database) {
    BasicDataSource source = h2Database.createDataSource();
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        con = source.getConnection();
        stmt = con.createStatement();
        rs = stmt.executeQuery(query);
        int columnCount = rs.getMetaData().getColumnCount();
        List<Object[]> objects = new ArrayList<>();
        while (rs.next()) {
            Object[] values = new Object[columnCount];
            for (int i = 0; i < values.length; i++) {
                values[i] = rs.getObject(i + 1);
            }
            objects.add(values);
        }
        return objects.toArray(new Object[0][0]);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            assert stmt != null;
            stmt.close();
            con.close();
            assert rs != null;
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Example 17 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project gocd by gocd.

the class H2DatabaseTest method shouldMigrateBuildbufferToTextColumn.

@Test
public void shouldMigrateBuildbufferToTextColumn() throws SQLException, IOException {
    h2Database.startDatabase();
    h2Database.upgrade();
    BasicDataSource dataSource = h2Database.createDataSource();
    assertColumnType(dataSource, "modifications", "comment", DatabaseFixture.LONGVARCHAR);
    assertColumnType(dataSource, "properties", "value", "VARCHAR(255)");
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Test(org.junit.Test)

Example 18 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project gocd by gocd.

the class H2DatabaseTest method shouldThrowUpWhenBackupFails.

@Test
public void shouldThrowUpWhenBackupFails() throws Exception {
    File destDir = new File(".");
    SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    Database database = new H2Database(systemEnvironment);
    Database spy = spy(database);
    BasicDataSource dataSource = mock(BasicDataSource.class);
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);
    doReturn(dataSource).when(spy).createDataSource();
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.createStatement()).thenReturn(statement);
    when(statement.execute(anyString())).thenThrow(new SQLException("i failed"));
    try {
        spy.backup(destDir);
    } catch (RuntimeException e) {
        assertThat(e.getMessage(), is("i failed"));
    }
    verify(statement).execute(anyString());
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Database(com.thoughtworks.go.database.Database) Connection(java.sql.Connection) File(java.io.File) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Test(org.junit.Test)

Example 19 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project gocd by gocd.

the class H2DatabaseTest method shouldBackupDatabase.

@Test
public void shouldBackupDatabase() throws Exception {
    File destDir = new File(".");
    SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    Database database = new H2Database(systemEnvironment);
    Database spy = spy(database);
    BasicDataSource dataSource = mock(BasicDataSource.class);
    Connection connection = mock(Connection.class);
    Statement statement = mock(Statement.class);
    doReturn(dataSource).when(spy).createDataSource();
    when(dataSource.getConnection()).thenReturn(connection);
    when(connection.createStatement()).thenReturn(statement);
    when(statement.execute(anyString())).thenReturn(true);
    spy.backup(destDir);
    verify(statement).execute(anyString());
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) Statement(java.sql.Statement) Database(com.thoughtworks.go.database.Database) Connection(java.sql.Connection) File(java.io.File) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) Test(org.junit.Test)

Example 20 with BasicDataSource

use of org.apache.commons.dbcp.BasicDataSource in project kylo by Teradata.

the class RefreshableDataSource method create.

private DataSource create() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setDriverClassLoader(driverClassLoader);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}
Also used : BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Aggregations

BasicDataSource (org.apache.commons.dbcp.BasicDataSource)141 Connection (java.sql.Connection)25 Test (org.junit.Test)13 SQLException (java.sql.SQLException)12 Properties (java.util.Properties)12 DataSource (javax.sql.DataSource)10 Platform (org.apache.ddlutils.Platform)8 Database (org.apache.ddlutils.model.Database)8 DdlGenerator (siena.jdbc.ddl.DdlGenerator)8 Statement (java.sql.Statement)7 Bean (org.springframework.context.annotation.Bean)6 ResultSet (java.sql.ResultSet)5 Before (org.junit.Before)5 JdbcPersistenceManager (siena.jdbc.JdbcPersistenceManager)5 Config (com.typesafe.config.Config)4 File (java.io.File)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Test (org.testng.annotations.Test)3 PostgresqlPersistenceManager (siena.jdbc.PostgresqlPersistenceManager)3