@Test public void testNewResultSet_GetMergeCost() throws Exception { // Mocks ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))).thenReturn(connection); when(connection.prepareStatement( "SELECT COUNT(objectKey) FROM " + TABLE_NAME + " WHERE value = ?;")) .thenReturn(preparedStatement); when(preparedStatement.executeQuery()).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(preparedStatement); when(resultSet.next()).thenReturn(true); when(resultSet.getInt(1)).thenReturn(3); // Iterator ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager) .retrieve(equal(Car.FEATURES, "abs"), new QueryOptions()); Assert.assertNotNull(carsWithAbs); int size = carsWithAbs.getMergeCost(); assertEquals(3, size); verify(connection, times(1)).close(); }
@Test public void testInit_NonEmptyCollection() throws Exception { // Mock ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Connection connection1 = mock(Connection.class); Statement statement = mock(Statement.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection1.prepareStatement("INSERT OR IGNORE INTO " + TABLE_NAME + " values(?, ?);")) .thenReturn(preparedStatement); when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connection) .thenReturn(connection1); when(connectionManager.isApplyUpdateForIndexEnabled(any(SQLiteIndex.class))).thenReturn(true); when(connection.createStatement()).thenReturn(statement); when(preparedStatement.executeBatch()).thenReturn(new int[] {2}); // The objects to add Set<Car> initWithObjects = new HashSet<Car>(2); initWithObjects.add( new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps"))); initWithObjects.add( new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags"))); SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager); carFeaturesOffHeapIndex.init(initWithObjects, new QueryOptions()); // Verify verify(statement, times(1)) .executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT, PRIMARY KEY (objectKey, value)) WITHOUT ROWID;"); verify(statement, times(1)) .executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + " (value);"); verify(statement, times(2)).close(); verify(connection, times(1)).close(); verify(preparedStatement, times(2)).setObject(1, 1); verify(preparedStatement, times(1)).setObject(1, 2); verify(preparedStatement, times(1)).setObject(2, "abs"); verify(preparedStatement, times(1)).setObject(2, "gps"); verify(preparedStatement, times(1)).setObject(2, "airbags"); verify(preparedStatement, times(3)).addBatch(); verify(preparedStatement, times(1)).executeBatch(); verify(preparedStatement, times(1)).close(); verify(connection1, times(1)).close(); }
@Test public void testNotifyObjectsRemoved() throws Exception { // Mock ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Connection connection1 = mock(Connection.class); Statement statement = mock(Statement.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connection) .thenReturn(connection1); when(connectionManager.isApplyUpdateForIndexEnabled(any(SQLiteIndex.class))).thenReturn(true); when(connection.createStatement()).thenReturn(statement); when(connection1.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE objectKey = ?;")) .thenReturn(preparedStatement); when(preparedStatement.executeBatch()).thenReturn(new int[] {1}); // The objects to add Set<Car> removedObjects = new HashSet<Car>(2); removedObjects.add( new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps"))); removedObjects.add( new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags"))); @SuppressWarnings({"unchecked", "unused"}) SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager); carFeaturesOffHeapIndex.removeAll(removedObjects, new QueryOptions()); // Verify verify(statement, times(1)) .executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT, PRIMARY KEY (objectKey, value)) WITHOUT ROWID;"); verify(statement, times(1)) .executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + " (value);"); verify(connection, times(1)).close(); verify(preparedStatement, times(1)).setObject(1, 1); verify(preparedStatement, times(1)).setObject(1, 2); verify(preparedStatement, times(2)).addBatch(); verify(preparedStatement, times(1)).executeBatch(); verify(connection1, times(1)).close(); }
@Test public void testNewResultSet_Iterator_Close() throws Exception { QueryOptions queryOptions = new QueryOptions(); // Mocks ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); @SuppressWarnings("unchecked") SimpleAttribute<Integer, Car> idToObject = (SimpleAttribute<Integer, Car>) mock(SimpleAttribute.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))).thenReturn(connection); when(connection.prepareStatement( "SELECT DISTINCT objectKey, value FROM " + TABLE_NAME + " WHERE value = ?;")) .thenReturn(preparedStatement); when(preparedStatement.executeQuery()).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(preparedStatement); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getInt(1)).thenReturn(1).thenReturn(3); when(idToObject.getValue(1, queryOptions)).thenReturn(data.get(0)); when(idToObject.getValue(3, queryOptions)).thenReturn(data.get(2)); // Iterator ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, connectionManager) .retrieve(equal(Car.FEATURES, "abs"), queryOptions); Assert.assertNotNull(carsWithAbs); Iterator carsWithAbsIterator = carsWithAbs.iterator(); assertTrue(carsWithAbsIterator.hasNext()); Assert.assertNotNull(carsWithAbsIterator.next()); assertTrue(carsWithAbsIterator.hasNext()); Assert.assertNotNull(carsWithAbsIterator.next()); assertFalse(carsWithAbsIterator.hasNext()); // The end of the iteration should close the resources verify(connection, times(1)).close(); verify(preparedStatement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test public void testNewResultSet_Contains() throws Exception { // Mocks ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connectionContains = mock(Connection.class); Connection connectionDoNotContain = mock(Connection.class); PreparedStatement preparedStatementContains = mock(PreparedStatement.class); PreparedStatement preparedStatementDoNotContains = mock(PreparedStatement.class); java.sql.ResultSet resultSetContains = mock(java.sql.ResultSet.class); java.sql.ResultSet resultSetDoNotContain = mock(java.sql.ResultSet.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connectionContains) .thenReturn(connectionDoNotContain); when(connectionContains.prepareStatement( "SELECT COUNT(objectKey) FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ?;")) .thenReturn(preparedStatementContains); when(connectionDoNotContain.prepareStatement( "SELECT COUNT(objectKey) FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ?;")) .thenReturn(preparedStatementDoNotContains); when(preparedStatementContains.executeQuery()).thenReturn(resultSetContains); when(preparedStatementDoNotContains.executeQuery()).thenReturn(resultSetDoNotContain); when(resultSetContains.next()).thenReturn(true).thenReturn(false); when(resultSetContains.getInt(1)).thenReturn(1); when(resultSetDoNotContain.next()).thenReturn(true).thenReturn(false); when(resultSetDoNotContain.getInt(1)).thenReturn(0); // Iterator ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager) .retrieve(equal(Car.FEATURES, "abs"), new QueryOptions()); Assert.assertNotNull(carsWithAbs); boolean resultContains = carsWithAbs.contains(data.get(0)); assertTrue(resultContains); verify(connectionContains, times(1)).close(); boolean resultDoNotContain = carsWithAbs.contains(data.get(1)); assertFalse(resultDoNotContain); verify(connectionDoNotContain, times(1)).close(); }
@Test public void testNotifyObjectsCleared() throws Exception { // Mock ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Connection connection1 = mock(Connection.class); Statement statement = mock(Statement.class); Statement statement1 = mock(Statement.class); // Behaviour when(connectionManager.getConnection(any(SQLiteIndex.class))) .thenReturn(connection) .thenReturn(connection1); when(connectionManager.isApplyUpdateForIndexEnabled(any(SQLiteIndex.class))).thenReturn(true); when(connection.createStatement()).thenReturn(statement); when(connection1.createStatement()).thenReturn(statement1); @SuppressWarnings({"unchecked", "unused"}) SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager); carFeaturesOffHeapIndex.clear(new QueryOptions()); // Verify verify(statement, times(1)) .executeUpdate( "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (objectKey INTEGER, value TEXT, PRIMARY KEY (objectKey, value)) WITHOUT ROWID;"); verify(statement, times(1)) .executeUpdate( "CREATE INDEX IF NOT EXISTS " + INDEX_NAME + " ON " + TABLE_NAME + " (value);"); verify(connection, times(1)).close(); verify(statement1, times(1)).executeUpdate("DELETE FROM " + TABLE_NAME + ";"); verify(connection1, times(1)).close(); }