SQLiteIndex extends AbstractAttributeIndex<A, O> implements SortedKeyStatisticsAttributeIndex<A, O>, NonHeapTypeIndex { @Override public ResultSet<O> retrieve(final Query<O> query, final QueryOptions queryOptions) { final ConnectionManager connectionManager = getConnectionManager(queryOptions); final CloseableResourceGroup closeableResourceGroup = CloseableRequestResources.forQueryOptions(queryOptions).addGroup(); if (query instanceof FilterQuery){ @SuppressWarnings("unchecked") final FilterQuery<O, A> filterQuery = (FilterQuery<O, A>)query; return new ResultSet<O>() { @Override public Iterator<O> iterator() { final Connection searchConnection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); final java.sql.ResultSet searchResultSet = DBQueries.getAllIndexEntries(tableName, searchConnection); closeableResourceGroup.add(DBUtils.wrapAsCloseable(searchResultSet)); return new LazyIterator<O>() { @Override protected O computeNext() { try { while (true) { if (!searchResultSet.next()) { close(); return endOfData(); } final K objectKey = DBUtils.getValueFromResultSet(1, searchResultSet, primaryKeyAttribute.getAttributeType()); final A objectValue = DBUtils.getValueFromResultSet(2, searchResultSet, attribute.getAttributeType()); if (filterQuery.matchesValue(objectValue, queryOptions)) { return foreignKeyAttribute.getValue(objectKey, queryOptions); } } } catch (Exception e) { endOfData(); close(); throw new IllegalStateException("Unable to retrieve the ResultSet item.", e); } } }; } @Override public boolean contains(O object) { Connection connection = null; java.sql.ResultSet searchResultSet = null; try { connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); searchResultSet = DBQueries.getIndexEntryByObjectKey(primaryKeyAttribute.getValue(object, queryOptions), tableName, connection); return lazyMatchingValuesIterable(searchResultSet).iterator().hasNext(); }finally { DBUtils.closeQuietly(searchResultSet); } } @Override public boolean matches(O object) { return query.matches(object, queryOptions); } @Override public Query<O> getQuery() { return query; } @Override public QueryOptions getQueryOptions() { return queryOptions; } @Override public int getRetrievalCost() { return INDEX_RETRIEVAL_COST_FILTERING; } @Override public int getMergeCost() { final Connection connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); return DBQueries.count(has(primaryKeyAttribute), tableName, connection); } @Override public int size() { Connection connection = null; java.sql.ResultSet searchResultSet = null; try { connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); searchResultSet = DBQueries.getAllIndexEntries(tableName, connection); final Iterable<K> iterator = lazyMatchingValuesIterable(searchResultSet); return IteratorUtil.countElements(iterator); }finally { DBUtils.closeQuietly(searchResultSet); } } @Override public void close() { closeableResourceGroup.close(); } Iterable<K> lazyMatchingValuesIterable(final java.sql.ResultSet searchResultSet) { return new Iterable<K>() { @Override public Iterator<K> iterator() { return new LazyIterator<K>() { K currentKey = null; @Override protected K computeNext() { try { while (true) { if (!searchResultSet.next()) { close(); return endOfData(); } final K objectKey = DBUtils.getValueFromResultSet(1, searchResultSet, primaryKeyAttribute.getAttributeType()); if (currentKey == null || !currentKey.equals(objectKey)) { final A objectValue = DBUtils.getValueFromResultSet(2, searchResultSet, attribute.getAttributeType()); if (filterQuery.matchesValue(objectValue, queryOptions)) { currentKey = objectKey; return objectKey; } } } } catch (Exception e) { endOfData(); close(); throw new IllegalStateException("Unable to retrieve the ResultSet item.", e); } } }; } }; } }; }else { return new ResultSet<O>() { @Override public Iterator<O> iterator() { final Connection searchConnection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); final java.sql.ResultSet searchResultSet = DBQueries.search(query, tableName, searchConnection); closeableResourceGroup.add(DBUtils.wrapAsCloseable(searchResultSet)); return new LazyIterator<O>() { @Override protected O computeNext() { try { if (!searchResultSet.next()) { close(); return endOfData(); } final K objectKey = DBUtils.getValueFromResultSet(1, searchResultSet, primaryKeyAttribute.getAttributeType()); return foreignKeyAttribute.getValue(objectKey, queryOptions); } catch (Exception e) { endOfData(); close(); throw new IllegalStateException("Unable to retrieve the ResultSet item.", e); } } }; } @Override public int getRetrievalCost() { return INDEX_RETRIEVAL_COST; } @Override public int getMergeCost() { final Connection connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); return DBQueries.count(query, tableName, connection); } @Override public boolean contains(O object) { final K objectKey = primaryKeyAttribute.getValue(object, queryOptions); final Connection connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); return DBQueries.contains(objectKey, query, tableName, connection); } @Override public boolean matches(O object) { return query.matches(object, queryOptions); } @Override public int size() { final Connection connection = connectionManager.getConnection(SQLiteIndex.this, queryOptions); boolean attributeHasAtMostOneValue = (attribute instanceof SimpleAttribute || attribute instanceof SimpleNullableAttribute); boolean queryIsADisjointInQuery = query instanceof In && ((In) query).isDisjoint(); if (queryIsADisjointInQuery || attributeHasAtMostOneValue) { return DBQueries.count(query, tableName, connection); }else{ return DBQueries.countDistinct(query, tableName, connection); } } @Override public void close() { closeableResourceGroup.close(); } @Override public Query<O> getQuery() { return query; } @Override public QueryOptions getQueryOptions() { return queryOptions; } }; } } SQLiteIndex(final Attribute<O, A> attribute,
                       final SimpleAttribute<O, K> primaryKeyAttribute,
                       final SimpleAttribute<K, O> foreignKeyAttribute,
                       final String tableNameSuffix); @Override boolean supportsQuery(Query<O> query, QueryOptions queryOptions); @Override boolean isMutable(); @Override boolean isQuantized(); @Override Index<O> getEffectiveIndex(); @Override ResultSet<O> retrieve(final Query<O> query, final QueryOptions queryOptions); @Override boolean addAll(final ObjectSet<O> objectSet, final QueryOptions queryOptions); @Override boolean removeAll(final ObjectSet<O> objectSet, final QueryOptions queryOptions); @Override void clear(QueryOptions queryOptions); @Override void init(ObjectStore<O> objectStore, QueryOptions queryOptions); @Override void destroy(QueryOptions queryOptions); @Override CloseableIterable<A> getDistinctKeys(final QueryOptions queryOptions); @Override CloseableIterable<A> getDistinctKeys(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive, final QueryOptions queryOptions); @Override CloseableIterable<A> getDistinctKeysDescending(QueryOptions queryOptions); @Override CloseableIterable<A> getDistinctKeysDescending(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive, QueryOptions queryOptions); @Override Integer getCountOfDistinctKeys(QueryOptions queryOptions); @Override CloseableIterable<KeyStatistics<A>> getStatisticsForDistinctKeysDescending(QueryOptions queryOptions); @Override CloseableIterable<KeyStatistics<A>> getStatisticsForDistinctKeys(QueryOptions queryOptions); @Override CloseableIterable<KeyValue<A, O>> getKeysAndValues(final QueryOptions queryOptions); @Override CloseableIterable<KeyValue<A, O>> getKeysAndValues(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive, final QueryOptions queryOptions); @Override CloseableIterable<KeyValue<A, O>> getKeysAndValuesDescending(QueryOptions queryOptions); @Override CloseableIterable<KeyValue<A, O>> getKeysAndValuesDescending(A lowerBound, boolean lowerInclusive, A upperBound, boolean upperInclusive, QueryOptions queryOptions); @Override Integer getCountForKey(A key, QueryOptions queryOptions); @Override boolean equals(Object o); @Override int hashCode(); static SQLiteIndex<A, O, K> onAttribute(final Attribute<O, A> attribute,
                                                                                   final SimpleAttribute<O, K> objectKeyAttribute,
                                                                                   final SimpleAttribute<K, O> foreignKeyAttribute);  }
@Test public void testNewResultSet_Size() throws Exception{ ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.prepareStatement("SELECT COUNT(1) AS countDistinct FROM (SELECT objectKey FROM " + TABLE_NAME + " WHERE value = ? GROUP BY objectKey);")).thenReturn(preparedStatement); when(preparedStatement.executeQuery()).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(preparedStatement); when(resultSet.next()).thenReturn(true); when(resultSet.getInt(1)).thenReturn(3); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); int size = carsWithAbs.size(); assertEquals(3, size); verify(connection, times(0)).close(); }
@Test public void testNewResultSet_GetRetrievalCost() throws Exception{ ConnectionManager connectionManager = mock(ConnectionManager.class); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertEquals(SQLiteIndex.INDEX_RETRIEVAL_COST, carsWithAbs.getRetrievalCost()); }
@Test public void testNewResultSet_GetMergeCost() throws Exception{ ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).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); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); int size = carsWithAbs.getMergeCost(); assertEquals(3, size); verify(connection, times(0)).close(); }
@Test public void testNewResultSet_Contains() throws Exception{ 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); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connectionContains).thenReturn(connectionDoNotContain); when(connectionContains.prepareStatement("SELECT objectKey FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ? LIMIT 1;")).thenReturn(preparedStatementContains); when(connectionDoNotContain.prepareStatement("SELECT objectKey FROM " + TABLE_NAME + " WHERE value = ? AND objectKey = ? LIMIT 1;")).thenReturn(preparedStatementDoNotContains); when(preparedStatementContains.executeQuery()).thenReturn(resultSetContains); when(preparedStatementDoNotContains.executeQuery()).thenReturn(resultSetDoNotContain); when(resultSetContains.next()).thenReturn(true).thenReturn(false); when(resultSetDoNotContain.next()).thenReturn(false); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); boolean resultContains = carsWithAbs.contains(data.get(0)); assertTrue(resultContains); verify(connectionContains, times(0)).close(); boolean resultDoNotContain = carsWithAbs.contains(data.get(1)); assertFalse(resultDoNotContain); verify(connectionDoNotContain, times(0)).close(); }
@Test(expected = IllegalStateException.class) public void testNewResultSet_Iterator_Exception_Close() throws Exception{ 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); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.prepareStatement("SELECT DISTINCT objectKey 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).thenThrow(new SQLException("SQL exception")); when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0)); try { ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); Iterator<Car> carsWithAbsIterator = carsWithAbs.iterator(); assertNotNull(carsWithAbsIterator.next()); carsWithAbsIterator.next(); }finally { verify(connection, times(0)).close(); verify(preparedStatement, times(1)).close(); verify(resultSet, times(1)).close(); } }
@Test public void testNewResultSet_Iterator_Close() throws Exception{ 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); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.prepareStatement("SELECT DISTINCT objectKey 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(eq(1), anyQueryOptions())).thenReturn(data.get(0)); when(idToObject.getValue(eq(3), anyQueryOptions())).thenReturn(data.get(2)); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); Iterator carsWithAbsIterator = carsWithAbs.iterator(); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertFalse(carsWithAbsIterator.hasNext()); verify(connection, times(0)).close(); verify(preparedStatement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test public void testNewResultSet_Close() throws Exception{ 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); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.prepareStatement("SELECT DISTINCT objectKey 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(eq(1), anyQueryOptions())).thenReturn(data.get(0)); when(idToObject.getValue(eq(3), anyQueryOptions())).thenReturn(data.get(2)); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(equal(Car.FEATURES, "abs"), createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); Iterator carsWithAbsIterator = carsWithAbs.iterator(); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); carsWithAbs.close(); verify(connection, times(0)).close(); verify(preparedStatement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test(expected = IllegalStateException.class) public void testNewResultSet_FilterQuery_Iterator_Exception_Close() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Statement statement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); @SuppressWarnings("unchecked") SimpleAttribute<Integer, Car> idToObject = (SimpleAttribute<Integer, Car>)mock(SimpleAttribute.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery("SELECT objectKey, value FROM " + TABLE_NAME + " ORDER BY objectKey;")).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(statement); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getInt(1)).thenReturn(1).thenThrow(new SQLException("SQL exception")); when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0)); try { ResultSet<Car> cars = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(cars); Iterator<Car> carsWithAbsIterator = cars.iterator(); assertNotNull(carsWithAbsIterator.next()); carsWithAbsIterator.next(); }finally { verify(connection, times(0)).close(); verify(statement, times(1)).close(); verify(resultSet, times(1)).close(); } }
@Test public void testNewResultSet_FilterQuery_Iterator_Close() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Statement statement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); @SuppressWarnings("unchecked") SimpleAttribute<Integer, Car> idToObject = (SimpleAttribute<Integer, Car>)mock(SimpleAttribute.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery("SELECT objectKey, value FROM " + TABLE_NAME + " ORDER BY objectKey;")).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(statement); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getInt(1)).thenReturn(1).thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4).thenReturn(5); when(resultSet.getString(2)).thenReturn("abs").thenReturn("gps").thenReturn("airbags").thenReturn("abs").thenReturn("").thenReturn("gps"); when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0)); when(idToObject.getValue(eq(3), anyQueryOptions())).thenReturn(data.get(2)); when(idToObject.getValue(eq(5), anyQueryOptions())).thenReturn(data.get(4)); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); Iterator carsWithAbsIterator = carsWithAbs.iterator(); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); assertFalse(carsWithAbsIterator.hasNext()); verify(connection, times(0)).close(); verify(statement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test public void testNewResultSet_FilterQuery_Close() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Statement statement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); @SuppressWarnings("unchecked") SimpleAttribute<Integer, Car> idToObject = (SimpleAttribute<Integer, Car>)mock(SimpleAttribute.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery("SELECT objectKey, value FROM " + TABLE_NAME + " ORDER BY objectKey;")).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(statement); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getInt(1)).thenReturn(1).thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4).thenReturn(5); when(resultSet.getString(2)).thenReturn("abs").thenReturn("gps").thenReturn("airbags").thenReturn("abs").thenReturn("").thenReturn("gps"); when(idToObject.getValue(eq(1), anyQueryOptions())).thenReturn(data.get(0)); when(idToObject.getValue(eq(3), anyQueryOptions())).thenReturn(data.get(2)); when(idToObject.getValue(eq(5), anyQueryOptions())).thenReturn(data.get(4)); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, idToObject, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); Iterator carsWithAbsIterator = carsWithAbs.iterator(); assertTrue(carsWithAbsIterator.hasNext()); assertNotNull(carsWithAbsIterator.next()); carsWithAbs.close(); verify(connection, times(0)).close(); verify(statement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test public void testNewResultSet_FilterQuery_GetMergeCost() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); PreparedStatement preparedStatement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.prepareStatement("SELECT COUNT(objectKey) FROM " + TABLE_NAME + " ;")).thenReturn(preparedStatement); when(preparedStatement.executeQuery()).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(preparedStatement); when(resultSet.next()).thenReturn(true); when(resultSet.getInt(1)).thenReturn(3); ResultSet<Car> cars = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(cars); int mergeCost = cars.getMergeCost(); assertEquals(3, mergeCost); verify(connection, times(0)).close(); }
@Test public void testNewResultSet_FilterQuery_GetRetrievalCost(){ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertEquals(carsWithAbs.getRetrievalCost(), SQLiteIndex.INDEX_RETRIEVAL_COST_FILTERING); }
@Test public void testNewResultSet_FilterQuery_Contains() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connectionContains = mock(Connection.class); Connection connectionDoNotContain = mock(Connection.class); Connection connectionNoRows = mock(Connection.class); PreparedStatement preparedStatementContains = mock(PreparedStatement.class); PreparedStatement preparedStatementDoNotContains = mock(PreparedStatement.class); PreparedStatement preparedStatementNoRows = mock(PreparedStatement.class); java.sql.ResultSet resultSetContains = mock(java.sql.ResultSet.class); java.sql.ResultSet resultSetDoNotContain = mock(java.sql.ResultSet.class); java.sql.ResultSet resultSetNoRows = mock(java.sql.ResultSet.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connectionContains).thenReturn(connectionDoNotContain).thenReturn(connectionNoRows); when(connectionContains.prepareStatement("SELECT objectKey, value FROM " + TABLE_NAME + " WHERE objectKey = ?")).thenReturn(preparedStatementContains); when(connectionDoNotContain.prepareStatement("SELECT objectKey, value FROM " + TABLE_NAME + " WHERE objectKey = ?")).thenReturn(preparedStatementDoNotContains); when(connectionNoRows.prepareStatement("SELECT objectKey, value FROM " + TABLE_NAME + " WHERE objectKey = ?")).thenReturn(preparedStatementNoRows); when(preparedStatementContains.executeQuery()).thenReturn(resultSetContains); when(preparedStatementDoNotContains.executeQuery()).thenReturn(resultSetDoNotContain); when(preparedStatementNoRows.executeQuery()).thenReturn(resultSetNoRows); when(resultSetContains.next()).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSetContains.getInt(1)).thenReturn(1).thenReturn(1); when(resultSetContains.getString(2)).thenReturn("abs").thenReturn("gps"); when(resultSetDoNotContain.next()).thenReturn(true).thenReturn(false); when(resultSetDoNotContain.getInt(1)).thenReturn(2); when(resultSetDoNotContain.getString(2)).thenReturn("airbags"); when(resultSetNoRows.next()).thenReturn(false); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); boolean resultContains = carsWithAbs.contains(data.get(0)); assertTrue(resultContains); verify(connectionContains, times(0)).close(); boolean resultDoNotContain = carsWithAbs.contains(data.get(1)); assertFalse(resultDoNotContain); verify(connectionDoNotContain, times(0)).close(); boolean resultNoRows = carsWithAbs.contains(CarFactory.createCar(100)); assertFalse(resultNoRows); verify(connectionNoRows, times(0)).close(); }
@Test public void testNewResultSet_FilterQuery_Size() throws Exception{ FilterQuery<Car, String> filterQuery = mockFilterQuery(); ConnectionManager connectionManager = mock(ConnectionManager.class); Connection connection = mock(Connection.class); Statement statement = mock(PreparedStatement.class); java.sql.ResultSet resultSet = mock(java.sql.ResultSet.class); when(connectionManager.getConnection(any(SQLiteIndex.class), anyQueryOptions())).thenReturn(connection); when(connection.createStatement()).thenReturn(statement); when(statement.executeQuery("SELECT objectKey, value FROM " + TABLE_NAME + " ORDER BY objectKey;")).thenReturn(resultSet); when(resultSet.getStatement()).thenReturn(statement); when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); when(resultSet.getInt(1)).thenReturn(1).thenReturn(1).thenReturn(2).thenReturn(3).thenReturn(4).thenReturn(5); when(resultSet.getString(2)).thenReturn("abs").thenReturn("gps").thenReturn("airbags").thenReturn("abs").thenReturn("").thenReturn("gps"); ResultSet<Car> carsWithAbs = new SQLiteIndex<String, Car, Integer>( Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, "") .retrieve(filterQuery, createQueryOptions(connectionManager)); assertNotNull(carsWithAbs); int size = carsWithAbs.size(); assertEquals(3, size); verify(connection, times(0)).close(); }