Search in sources :

Example 1 with Database

use of com.thoughtworks.go.database.Database 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 2 with Database

use of com.thoughtworks.go.database.Database 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)

Aggregations

Database (com.thoughtworks.go.database.Database)2 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)2 File (java.io.File)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 Test (org.junit.Test)2 SQLException (java.sql.SQLException)1