use of java.sql.Statement in project HikariCP by brettwooldridge.
the class MockDataSource method createMockConnection.
public static Connection createMockConnection() throws SQLException {
// Setup mock connection
final Connection mockConnection = mock(Connection.class);
// Autocommit is always true by default
when(mockConnection.getAutoCommit()).thenReturn(true);
// Handle Connection.createStatement()
Statement statement = mock(Statement.class);
when(mockConnection.createStatement()).thenReturn(statement);
when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement);
when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement);
when(mockConnection.isValid(anyInt())).thenReturn(true);
// Handle Connection.prepareStatement()
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);
when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), any(int[].class))).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), any(String[].class))).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
return null;
}
}).doNothing().when(mockPreparedStatement).setInt(anyInt(), anyInt());
ResultSet mockResultSet = mock(ResultSet.class);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.getString(anyInt())).thenReturn("aString");
when(mockResultSet.next()).thenReturn(true);
// Handle Connection.prepareCall()
CallableStatement mockCallableStatement = mock(CallableStatement.class);
when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement);
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
return mockConnection;
}
use of java.sql.Statement in project HikariCP by brettwooldridge.
the class ExceptionTest method testUseAfterClose.
@Test
public void testUseAfterClose() throws SQLException {
try (Connection connection = ds.getConnection()) {
assertNotNull(connection);
connection.close();
try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) {
fail();
} catch (SQLException e) {
assertSame("Connection is closed", e.getMessage());
}
}
}
use of java.sql.Statement in project flyway by flyway.
the class VerticaMigrationMediumTest method emptySearchPath.
@Test
public void emptySearchPath() {
Flyway flyway1 = new Flyway();
DriverDataSource driverDataSource = (DriverDataSource) dataSource;
flyway1.setDataSource(new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, driverDataSource.getUrl(), driverDataSource.getUser(), driverDataSource.getPassword(), null) {
@Override
public Connection getConnection() throws SQLException {
Connection connection = super.getConnection();
Statement statement = null;
try {
statement = connection.createStatement();
statement.execute("SET search_path = v_catalog");
} finally {
JdbcUtils.closeStatement(statement);
}
return connection;
}
});
flyway1.setLocations(getBasedir());
flyway1.setSchemas("public");
flyway1.migrate();
}
use of java.sql.Statement in project flyway by flyway.
the class PostgreSQLMigrationMediumTest method emptySearchPath.
@Test
public void emptySearchPath() {
Flyway flyway1 = new Flyway();
DriverDataSource driverDataSource = (DriverDataSource) dataSource;
flyway1.setDataSource(new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, driverDataSource.getUrl(), driverDataSource.getUser(), driverDataSource.getPassword(), null) {
@Override
public Connection getConnection() throws SQLException {
Connection connection = super.getConnection();
Statement statement = null;
try {
statement = connection.createStatement();
statement.execute("SELECT set_config('search_path', '', false)");
} finally {
JdbcUtils.closeStatement(statement);
}
return connection;
}
});
flyway1.setLocations(getBasedir());
flyway1.setSchemas("public");
flyway1.migrate();
}
use of java.sql.Statement in project gephi-plugins-bootcamp by gephi.
the class SQLiteDatabaseExporter method execute.
@Override
public boolean execute() {
Connection connection = null;
try {
if (path.getParentFile().exists()) {
//Create connection
SQLiteDriver sQLiteDriver = Lookup.getDefault().lookup(SQLiteDriver.class);
String connectionUrl = SQLUtils.getUrl(sQLiteDriver, path.getAbsolutePath(), 0, "");
connection = sQLiteDriver.getConnection(connectionUrl, "", "");
//Create statement and create nodes and egdes table
Statement statement = connection.createStatement();
// set timeout to 30 sec.
statement.setQueryTimeout(30);
statement.executeUpdate("drop table if exists nodes");
statement.executeUpdate("drop table if exists edges");
statement.executeUpdate("create table nodes (id string, label string)");
statement.executeUpdate("create table edges (source string, target string, weight real)");
//Get the current graph in the defined workspace
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getGraphModel(workspace);
Graph graph = graphModel.getGraphVisible();
//Count the number of tasks (nodes + edges) and start the progress
int tasks = graph.getNodeCount() + graph.getEdgeCount();
Progress.start(progress, tasks);
//Export nodes. Progress is incremented at each step.
for (Node n : graph.getNodes().toArray()) {
String id = n.getId().toString();
String label = n.getLabel();
statement.executeUpdate("insert into nodes values('" + id + "', '" + label + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Export edges. Progress is incremented at each step.
for (Edge e : graph.getEdges().toArray()) {
String sourceId = e.getSource().getId().toString();
String targetId = e.getTarget().getId().toString();
String weight = String.valueOf(e.getWeight());
statement.executeUpdate("insert into edges values('" + sourceId + "', '" + targetId + "', '" + weight + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Finish progress
Progress.finish(progress);
return true;
} else {
throw new FileNotFoundException(path.getAbsolutePath() + " does not exist");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// connection close failed.
System.err.println(e);
}
}
}
Aggregations