@Test public void testDropIndexOnTable() throws SQLException { Connection connection = null; Statement statement = null; try { ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true); connection = spy(connectionManager.getConnection(null)); statement = spy(connection.createStatement()); when(connection.createStatement()).thenReturn(statement); DBQueries.createIndexTable(NAME, Integer.class, String.class, connection); DBQueries.createIndexOnTable(NAME, connection); assertObjectExistenceInSQLIteMasterTable(TABLE_NAME, "table", true, connectionManager); assertObjectExistenceInSQLIteMasterTable(INDEX_NAME, "index", true, connectionManager); DBQueries.dropIndexOnTable(NAME, connection); assertObjectExistenceInSQLIteMasterTable(TABLE_NAME, "table", true, connectionManager); assertObjectExistenceInSQLIteMasterTable(INDEX_NAME, "index", false, connectionManager); verify(statement, times(3)).close(); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }
public void assertObjectExistenceInSQLIteMasterTable( final String name, final String type, boolean exists, final ConnectionManager connectionManager) { Connection connection = null; PreparedStatement statement = null; try { connection = connectionManager.getConnection(null); statement = connection.prepareStatement("SELECT name FROM sqlite_master WHERE type=?"); statement.setString(1, type); java.sql.ResultSet indices = statement.executeQuery(); boolean found = false; StringBuilder objectsFound = new StringBuilder(); String next; while (indices.next()) { next = indices.getString(1); objectsFound.append("'").append(next).append("' "); if (name.equals(next)) { found = true; } } if (exists) Assert.assertTrue( "Object '" + name + "' must exists in 'sqlite_master' but it doesn't. found: " + found + ". Objects found: " + objectsFound, found); else Assert.assertFalse( "Object '" + name + "' must NOT exists in 'sqlite_master' but it does. found: " + found + " Objects found: " + objectsFound, found); } catch (Exception e) { throw new IllegalStateException( "Unable to verify existence of the object '" + name + "' in the 'sqlite_master' table", e); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }
/** * Test issue 161, fetch trigger generated keys from Oracle. Fixed by calling * java.sql.Connection.prepareStatement(String, String[]) overload. * * @throws SQLException */ @Test public void testCreateQueryWithKeyNames() throws SQLException { DataSource dataSource = mock(DataSource.class); java.sql.Connection jdbcCon = mock(java.sql.Connection.class); when(jdbcCon.isClosed()).thenReturn(false); when(dataSource.getConnection()).thenReturn(jdbcCon); Sql2o sql2o = new Sql2o(dataSource); Connection con = sql2o.open(); final Query query = con.createQuery("sql", "colname"); verify(jdbcCon, times(1)).prepareStatement(anyString(), new String[] {anyString()}); }
void initWithTestData(final ConnectionManager connectionManager) { createSchema(connectionManager); Connection connection = null; Statement statement = null; try { connection = connectionManager.getConnection(null); statement = connection.createStatement(); assertEquals(statement.executeUpdate("INSERT INTO " + TABLE_NAME + " values (1, 'abs')"), 1); assertEquals(statement.executeUpdate("INSERT INTO " + TABLE_NAME + " values (1, 'gps')"), 1); assertEquals( statement.executeUpdate("INSERT INTO " + TABLE_NAME + " values (2, 'airbags')"), 1); assertEquals(statement.executeUpdate("INSERT INTO " + TABLE_NAME + " values (3, 'abs')"), 1); } catch (Exception e) { throw new IllegalStateException("Unable to initialize test database", e); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }
public void assertQueryResultSet( final String query, final List<DBQueries.Row<Integer, String>> rows, final ConnectionManager connectionManager) throws SQLException { Connection connection = null; Statement statement = null; try { connection = connectionManager.getConnection(null); statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query); assertResultSetOrderAgnostic(resultSet, rows); } catch (Exception e) { throw new IllegalStateException("Unable to verify resultSet", e); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }
void createSchema(final ConnectionManager connectionManager) { Connection connection = null; Statement statement = null; try { connection = connectionManager.getConnection(null); statement = connection.createStatement(); assertEquals( statement.executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT)"), 0); assertEquals( statement.executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + "(value)"), 0); } catch (Exception e) { throw new IllegalStateException("Unable to create test database schema", e); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }
@Test public void testClearIndexTable() throws SQLException { Connection connection = null; Statement statement = null; try { ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true); createSchema(connectionManager); assertObjectExistenceInSQLIteMasterTable(TABLE_NAME, "table", true, connectionManager); assertObjectExistenceInSQLIteMasterTable(INDEX_NAME, "index", true, connectionManager); connection = spy(connectionManager.getConnection(null)); statement = spy(connection.createStatement()); when(connection.createStatement()).thenReturn(statement); DBQueries.clearIndexTable(NAME, connection); List<DBQueries.Row<Integer, String>> expectedRows = Collections.emptyList(); assertQueryResultSet("SELECT * FROM " + TABLE_NAME, expectedRows, connectionManager); verify(statement, times(1)).close(); } finally { DBUtils.closeQuietly(connection); DBUtils.closeQuietly(statement); } }