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);
}
}
}
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)");
}
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());
}
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());
}
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;
}
Aggregations