@Test public void testNewResultSet_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()); // Do not continue with the iteration, but close carsWithAbs.close(); verify(connection, times(1)).close(); verify(preparedStatement, times(1)).close(); verify(resultSet, times(1)).close(); }
@Test public void testObjectKeyIterable() { Iterable<Integer> objectKeys = SQLiteIndex.objectKeyIterable(data, Car.CAR_ID, null); Assert.assertNotNull(objectKeys); Iterator<Integer> objectKeysIterator = objectKeys.iterator(); Assert.assertNotNull(objectKeysIterator); assertTrue(objectKeysIterator.hasNext()); assertEquals(new Integer(1), objectKeysIterator.next()); assertTrue(objectKeysIterator.hasNext()); assertEquals(new Integer(2), objectKeysIterator.next()); assertTrue(objectKeysIterator.hasNext()); assertEquals(new Integer(3), objectKeysIterator.next()); assertTrue(objectKeysIterator.hasNext()); assertEquals(new Integer(4), objectKeysIterator.next()); assertTrue(objectKeysIterator.hasNext()); assertEquals(new Integer(5), objectKeysIterator.next()); assertFalse(objectKeysIterator.hasNext()); }