Пример #1
0
  public void assertResultSetOrderAgnostic(
      final ResultSet resultSet, final List<DBQueries.Row<Integer, String>> rows) {

    try {
      List<DBQueries.Row<Integer, String>> actual =
          new ArrayList<DBQueries.Row<Integer, String>>(rows.size());
      while (resultSet.next()) {
        actual.add(new DBQueries.Row<Integer, String>(resultSet.getInt(1), resultSet.getString(2)));
      }

      Comparator<DBQueries.Row<Integer, String>> comparator =
          new Comparator<DBQueries.Row<Integer, String>>() {
            @Override
            public int compare(
                DBQueries.Row<Integer, String> o1, DBQueries.Row<Integer, String> o2) {
              int objectKeyComparison = o1.getObjectKey().compareTo(o2.getObjectKey());
              if (objectKeyComparison != 0) {
                return objectKeyComparison;
              }
              return o1.getValue().compareTo(o2.getValue());
            }
          };

      Collections.sort(actual, comparator);
      Collections.sort(rows, comparator);

      Assert.assertEquals(rows, actual);
    } catch (Exception e) {
      throw new IllegalStateException("Unable to verify resultSet", e);
    }
  }
 @Test
 public void forceNewGameWillRestartTableAndStartNewGame() {
   Map<BigDecimal, BigDecimal> playerIdToAccountIdOverrides = new HashMap<>();
   playerIdToAccountIdOverrides.put(PLAYER_ID, BigDecimal.valueOf(202020));
   com.yazino.game.api.GameStatus status = new GameStatus(new PrintlnStatus());
   com.yazino.game.api.ExecutionResult result =
       new com.yazino.game.api.ExecutionResult.Builder(gameRules, status).build();
   when(gameRules.startNewGame(isA(com.yazino.game.api.GameCreationContext.class)))
       .thenReturn(result);
   when(auditor.newLabel()).thenReturn("X");
   host = gameHost(new InMemoryGameRepository(gameRules));
   table.setTableStatus(TableStatus.closed);
   table.setCurrentGame(null);
   Collection<com.yazino.game.api.PlayerAtTableInformation> playersAtTable =
       Arrays.asList(
           new com.yazino.game.api.PlayerAtTableInformation(
               new com.yazino.game.api.GamePlayer(PLAYER1_ID, null, "One"),
               Collections.<String, String>emptyMap()),
           new com.yazino.game.api.PlayerAtTableInformation(
               new com.yazino.game.api.GamePlayer(PLAYER2_ID, null, "Two"),
               Collections.<String, String>emptyMap()));
   host.forceNewGame(table, playersAtTable, playerIdToAccountIdOverrides);
   ArgumentCaptor<com.yazino.game.api.GameCreationContext> contextCaptor =
       ArgumentCaptor.forClass(com.yazino.game.api.GameCreationContext.class);
   verify(gameRules).startNewGame(contextCaptor.capture());
   assertEquals(TableStatus.open, table.getTableStatus());
   assertEquals(status, table.getCurrentGame());
   assertEquals(playersAtTable, contextCaptor.getValue().getPlayersAtTableInformation());
 }
Пример #3
0
  @Test
  public void errorResponseThrowsHttpError() throws Exception {
    given(connection.getURL()).willReturn(new URL(URL));
    given(connection.getResponseCode()).willReturn(400);
    Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>();
    given(connection.getHeaderFields())
        .willReturn(responseHeaderFields, Collections.<String, List<String>>emptyMap());

    Request request = new Request("GET", URL, Collections.<Header>emptyList(), null);

    Response response = underTest.execute(request);

    assertThat(response.getStatus()).isEqualTo(400);
  }
 @Test
 public void testProcessTransactionResultCorrectlyCallsAuditOnSuccessWithNonNullExecutionResult()
     throws com.yazino.game.api.GameException {
   final String auditLabel = "test424243542354";
   gameStatus = mock(com.yazino.game.api.GameStatus.class);
   when(gameRules.canBeClosed(gameStatus)).thenReturn(false);
   when(gameRules.isComplete(gameStatus)).thenReturn(false);
   whenPlayers();
   when(gameRules.getPlayerInformation(gameStatus))
       .thenReturn(Collections.<com.yazino.game.api.PlayerAtTableInformation>emptyList());
   when(gameRules.getNumberOfSeatsTaken(gameStatus)).thenReturn(0);
   table.setCurrentGame(gameStatus);
   table.setGameId(GAME_ID);
   final com.yazino.game.api.TransactionResult txResult =
       new com.yazino.game.api.TransactionResult("foo", true, null, null, null, null);
   final com.yazino.game.api.ExecutionResult executionResult =
       new com.yazino.game.api.ExecutionResult.Builder(gameRules, gameStatus).build();
   when(gameRules.processTransactionResult(
           isA(com.yazino.game.api.ExecutionContext.class), eq(txResult)))
       .thenReturn(executionResult);
   auditor = mock(Auditor.class);
   when(auditor.newLabel()).thenReturn(auditLabel);
   host = gameHost(new InMemoryGameRepository(gameRules));
   host.processTransactionResult(table, GAME_ID, txResult);
 }
Пример #5
0
  @Test
  public void postAndGet() throws Exception {
    given(connection.getURL()).willReturn(new URL(URL), new URL(URL + "/secret"));
    given(connection.getResponseCode()).willReturn(302, 200);
    Map<String, List<String>> responseHeaderFields = new LinkedHashMap<String, List<String>>();
    responseHeaderFields.put("Location", Arrays.asList("http://example.com/secret"));
    given(connection.getHeaderFields())
        .willReturn(responseHeaderFields, Collections.<String, List<String>>emptyMap());

    Request request = new Request("POST", URL, Collections.<Header>emptyList(), null);

    Response response = underTest.execute(request);

    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getUrl()).isEqualTo(URL + "/secret");
  }
  private void addPlayersToGame(
      com.yazino.game.api.GameStatus gameStatus, com.yazino.game.api.GamePlayer... players) {
    Collection<com.yazino.game.api.PlayerAtTableInformation> playerAtTableInformationCollection =
        new ArrayList<>();
    for (com.yazino.game.api.GamePlayer player : players) {
      playerAtTableInformationCollection.add(
          new com.yazino.game.api.PlayerAtTableInformation(
              player, Collections.<String, String>emptyMap()));
      if (table.playerAtTable(player.getId()) == null) {
        if (player.getId().equals(PLAYER_ID)) {
          table.addPlayerToTable(
              new PlayerInformation(
                  PLAYER_ID, PLAYER_NAME, PLAYER_ACCOUNT_ID, BigDecimal.TEN, BigDecimal.ZERO));
        } else if (player.getId().equals(PLAYER1_ID)) {
          table.addPlayerToTable(
              new PlayerInformation(
                  PLAYER1_ID, "player1", PLAYER1_ACCOUNT_ID, BigDecimal.TEN, BigDecimal.ZERO));
        } else if (player.getId().equals(PLAYER2_ID)) {
          table.addPlayerToTable(
              new PlayerInformation(
                  PLAYER2_ID, "player2", PLAYER2_ACCOUNT_ID, BigDecimal.TEN, BigDecimal.ZERO));
        } else {
          table.addPlayerToTable(
              new PlayerInformation(
                  player.getId(),
                  player.getName(),
                  player.getId(),
                  BigDecimal.TEN,
                  BigDecimal.ZERO));
        }
      }
    }

    when(gameRules.getPlayerInformation(gameStatus)).thenReturn(playerAtTableInformationCollection);
  }
Пример #7
0
  @Test
  public void shouldSetTimerToFlushFilesToDisk_AtSpecifiedInterval() {
    final TimerTask[] tasks = new TimerTask[1];
    final EntryRepoFactory repoFactory = mock(EntryRepoFactory.class);
    final Timer timer =
        new Timer() {
          @Override
          public void schedule(TimerTask task, long delay, long period) {
            if (task instanceof TlbServerInitializer.SyncToDisk) {
              tasks[0] = task;
              assertThat(delay, is(0l));
              assertThat(period, is(2l * TlbServerInitializer.MILLS_PER_MINUTE)); // every 2 mins
            }
          }
        };

    new TlbServerInitializer(
        new SystemEnvironment(
            Collections.singletonMap(
                TlbConstants.Server.TLB_SYNC_TO_DISK_INTERVAL_IN_MINS.key, "2")),
        timer) {
      @Override
      EntryRepoFactory repoFactory() {
        return repoFactory;
      }
    }.init();

    verify(repoFactory).registerExitHook();
    verifyNoMoreInteractions(repoFactory);

    assertThat(tasks[0], is(notNullValue()));
  }
 @Before
 public void inContext() {
   updatedFiles = newArrayList();
   core = new DefaultInfinitestCore(mock(TestRunner.class), new ControlledEventQueue());
   testDetector = mock(TestDetector.class);
   when(testDetector.getCurrentTests()).thenReturn(Collections.<String>emptySet());
   core.setTestDetector(testDetector);
 }
Пример #9
0
  public void assertResultSetObjectKeysOrderAgnostic(
      final ResultSet resultSet, final List<Integer> objectKeys) {
    try {
      List<Integer> actual = new ArrayList<Integer>(objectKeys.size());
      while (resultSet.next()) {
        actual.add(resultSet.getInt(1));
      }

      Collections.sort(actual);
      Collections.sort(objectKeys);

      Assert.assertEquals(objectKeys, actual);

    } catch (Exception e) {
      throw new IllegalStateException("Unable to verify resultSet", e);
    }
  }
 @Test
 public void shutdownReturnsInvestedAmountsOnlyOnceForPlayer() throws WalletServiceException {
   com.yazino.game.api.GamePlayer player1 =
       new com.yazino.game.api.GamePlayer(BigDecimal.valueOf(1), null, "p1");
   com.yazino.game.api.PlayerAtTableInformation p1 =
       new com.yazino.game.api.PlayerAtTableInformation(
           player1, BigDecimal.valueOf(10), Collections.<String, String>emptyMap());
   PlayerInformation pi1 =
       new PlayerInformation(
           player1.getId(),
           player1.getName(),
           BigDecimal.valueOf(501),
           BigDecimal.TEN,
           BigDecimal.valueOf(0));
   com.yazino.game.api.PlayerAtTableInformation p2 =
       new com.yazino.game.api.PlayerAtTableInformation(
           player1, BigDecimal.valueOf(15), Collections.<String, String>emptyMap());
   PlayerInformation pi2 =
       new PlayerInformation(
           player1.getId(),
           player1.getName(),
           BigDecimal.valueOf(501),
           BigDecimal.TEN,
           BigDecimal.valueOf(0));
   table.addPlayerToTable(pi1);
   table.addPlayerToTable(pi2);
   Collection<com.yazino.game.api.PlayerAtTableInformation> players = Arrays.asList(p1, p2);
   when(gameRules.getPlayerInformation(gameStatus)).thenReturn(players);
   table.setCurrentGame(gameStatus);
   List<HostDocument> documents = gameHost(gameRules).shutdown(table);
   assertEquals(1, documents.size());
   verify(bufferedGameHostWallet)
       .post(
           table.getTableId(),
           table.getGameId(),
           pi1,
           BigDecimal.valueOf(25),
           com.yazino.game.api.TransactionType.Return,
           AUDIT_LABEL,
           "Shutdown refund",
           NEW_UUID);
   verify(bufferedGameHostWallet).flush();
   verifyNoMoreInteractions(bufferedGameHostWallet);
 }
Пример #11
0
  @Test
  public void testInit_EmptyCollection() throws Exception {

    // Mock
    ConnectionManager connectionManager = mock(ConnectionManager.class);

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        new SQLiteIndex<String, Car, Integer>(
            Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager);

    carFeaturesOffHeapIndex.init(Collections.<Car>emptySet(), new QueryOptions());
    verify(connectionManager, times(0)).getConnection(any(SQLiteIndex.class));
  }
  @Test
  public void shouldAlsoLookForClassesInClassDirectories() throws Exception {
    newDir = new File("tempClassDir");
    List<File> buildPaths = asList(newDir);
    ClasspathProvider classpath =
        new StandaloneClasspath(
            Collections.<File>emptyList(),
            buildPaths,
            FakeEnvironments.systemClasspath() + pathSeparator + newDir.getAbsolutePath());

    String classname = "org.fakeco.Foobar2";
    createClass(classname);

    builder = new JavaClassBuilder(classpath);
    JavaClass javaClass = builder.createClass(classname);
    assertEquals(classname, javaClass.getName());
    assertFalse(javaClass.isATest());
  }
Пример #13
0
  @Test
  public void testBulkRemove() throws SQLException {

    Connection connection = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      initWithTestData(connectionManager);

      List<DBQueries.Row<Integer, String>> expectedRows =
          new ArrayList<DBQueries.Row<Integer, String>>(2);
      expectedRows.add(new DBQueries.Row<Integer, String>(2, "airbags"));
      expectedRows.add(new DBQueries.Row<Integer, String>(3, "abs"));

      connection = connectionManager.getConnection(null);
      DBQueries.bulkRemove(Collections.singletonList(1), NAME, connection);
      assertQueryResultSet("SELECT * FROM " + TABLE_NAME, expectedRows, connectionManager);

    } finally {
      DBUtils.closeQuietly(connection);
    }
  }
 @Test
 public void shutdownIgnoresNullInvestedAmounts() {
   com.yazino.game.api.PlayerAtTableInformation p1 =
       new com.yazino.game.api.PlayerAtTableInformation(
           new com.yazino.game.api.GamePlayer(BigDecimal.valueOf(1), SESSION_ID, "p1"),
           null,
           Collections.<String, String>emptyMap());
   PlayerInformation pi1 =
       new PlayerInformation(
           BigDecimal.valueOf(1),
           "p1",
           BigDecimal.valueOf(501),
           BigDecimal.TEN,
           BigDecimal.valueOf(0));
   table.addPlayerToTable(pi1);
   Collection<com.yazino.game.api.PlayerAtTableInformation> players = Arrays.asList(p1);
   when(gameRules.getPlayerInformation(gameStatus)).thenReturn(players);
   table.setCurrentGame(gameStatus);
   List<HostDocument> documents = gameHost(gameRules).shutdown(table);
   assertEquals(1, documents.size());
   verify(bufferedGameHostWallet).flush();
   verifyNoMoreInteractions(bufferedGameHostWallet);
 }
Пример #15
0
  @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);
    }
  }
Пример #16
0
/**
 * Unit tests for {@link SQLiteIndex}
 *
 * @author Silvano Riz
 */
public class SQLiteIndexTest {

  private static final String TABLE_NAME = "cqtbl_features";
  private static final String INDEX_NAME = "cqidx_features_value";

  public static final SimpleAttribute<Car, Integer> OBJECT_TO_ID = Car.CAR_ID;

  public static final SimpleAttribute<Integer, Car> ID_TO_OBJECT =
      new SimpleAttribute<Integer, Car>("carFromId") {
        public Car getValue(Integer carId, QueryOptions queryOptions) {
          return null;
        }
      };

  public static List<Car> data =
      Arrays.asList(
          new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps")),
          new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags")),
          new Car(3, "Toyota", "Prius", Car.Color.BLACK, 3, 9700.00, Arrays.asList("abs")),
          new Car(4, "Fiat", "Panda", Car.Color.BLUE, 5, 5600.00, Collections.<String>emptyList()),
          new Car(5, "Fiat", "Punto", Car.Color.BLUE, 5, 5600.00, Arrays.asList("gps")));

  @Rule
  public TemporaryDatabase.TemporaryInMemoryDatabase temporaryInMemoryDatabase =
      new TemporaryDatabase.TemporaryInMemoryDatabase();

  @Test
  public void testNewStandalone() throws Exception {

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        SQLiteIndex.onAttribute(
            Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, mock(ConnectionManager.class));

    assertNotNull(carFeaturesOffHeapIndex);
  }

  @Test
  public void testNewNonStandalone() throws Exception {

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        SQLiteIndex.onAttribute(Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT);

    assertNotNull(carFeaturesOffHeapIndex);
  }

  @Test
  public void testGetConnectionManager_Standalone() {

    ConnectionManager connectionManager = mock(ConnectionManager.class);

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        new SQLiteIndex<String, Car, Integer>(
            Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager);

    assertEquals(
        connectionManager, carFeaturesOffHeapIndex.getConnectionManager(new QueryOptions()));
  }

  @Test
  public void testGetConnectionManager_NonStandalone() {

    ConnectionManager connectionManager = mock(ConnectionManager.class);
    QueryOptions queryOptions = mock(QueryOptions.class);
    when(queryOptions.get(ConnectionManager.class)).thenReturn(connectionManager);

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        new SQLiteIndex<String, Car, Integer>(Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, null);

    assertEquals(connectionManager, carFeaturesOffHeapIndex.getConnectionManager(queryOptions));
  }

  @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 testNotifyObjectsAdded() 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("INSERT OR IGNORE INTO " + TABLE_NAME + " values(?, ?);"))
        .thenReturn(preparedStatement);
    when(preparedStatement.executeBatch()).thenReturn(new int[] {2});
    // The objects to add
    Set<Car> addedObjects = new HashSet<Car>(2);
    addedObjects.add(
        new Car(1, "Ford", "Focus", Car.Color.BLUE, 5, 9000.50, Arrays.asList("abs", "gps")));
    addedObjects.add(
        new Car(2, "Honda", "Civic", Car.Color.RED, 5, 5000.00, Arrays.asList("airbags")));

    // Create the index and cal the addAll
    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        new SQLiteIndex<String, Car, Integer>(
            Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager);
    carFeaturesOffHeapIndex.addAll(addedObjects, 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(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(connection1, 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();
  }

  @Test
  public void testInit_EmptyCollection() throws Exception {

    // Mock
    ConnectionManager connectionManager = mock(ConnectionManager.class);

    SQLiteIndex<String, Car, Integer> carFeaturesOffHeapIndex =
        new SQLiteIndex<String, Car, Integer>(
            Car.FEATURES, OBJECT_TO_ID, ID_TO_OBJECT, connectionManager);

    carFeaturesOffHeapIndex.init(Collections.<Car>emptySet(), new QueryOptions());
    verify(connectionManager, times(0)).getConnection(any(SQLiteIndex.class));
  }

  @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 testNewResultSet_Size() 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(DISTINCT 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, connectionManager)
            .retrieve(equal(Car.FEATURES, "abs"), new QueryOptions());

    Assert.assertNotNull(carsWithAbs);
    int size = carsWithAbs.size();

    assertEquals(3, size);
    verify(connection, times(1)).close();
  }

  @Test
  public void testNewResultSet_GetRetrievalCost() throws Exception {

    // Mocks
    ConnectionManager connectionManager = mock(ConnectionManager.class);

    // 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());

    assertEquals(SQLiteIndex.INDEX_RETRIEVAL_COST, carsWithAbs.getRetrievalCost());
  }

  @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 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(expected = IllegalStateException.class)
  public void testNewResultSet_Iterator_Exception_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).thenThrow(new SQLException("SQL exception"));
    when(idToObject.getValue(1, queryOptions)).thenReturn(data.get(0));

    // Iterator
    try {
      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<Car> carsWithAbsIterator = carsWithAbs.iterator();
      Assert.assertNotNull(carsWithAbsIterator.next());
      carsWithAbsIterator.next(); // Should throw exception!

    } finally {
      verify(connection, times(1)).close();
      verify(preparedStatement, times(1)).close();
      verify(resultSet, 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_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 testRowIterable() {

    Iterable<DBQueries.Row<Integer, String>> rows =
        SQLiteIndex.rowIterable(data, Car.CAR_ID, Car.FEATURES, null);
    Assert.assertNotNull(rows);

    Iterator<DBQueries.Row<Integer, String>> rowsIterator = rows.iterator();
    Assert.assertNotNull(rowsIterator);
    assertTrue(rowsIterator.hasNext());
    assertEquals(new DBQueries.Row<Integer, String>(1, "abs"), rowsIterator.next());
    assertTrue(rowsIterator.hasNext());
    assertEquals(new DBQueries.Row<Integer, String>(1, "gps"), rowsIterator.next());
    assertTrue(rowsIterator.hasNext());
    assertEquals(new DBQueries.Row<Integer, String>(2, "airbags"), rowsIterator.next());
    assertTrue(rowsIterator.hasNext());
    assertEquals(new DBQueries.Row<Integer, String>(3, "abs"), rowsIterator.next());
    assertTrue(rowsIterator.hasNext());
    assertEquals(new DBQueries.Row<Integer, String>(5, "gps"), rowsIterator.next());
    assertFalse(rowsIterator.hasNext());
  }

  @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());
  }

  @Test
  public void testGetDistinctKeys_AllAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());

    List<String> expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius",
            "Taurus");
    List<String> actual = Lists.newArrayList(offHeapIndex.getDistinctKeys(noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_AllDescending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());

    List<String> expected =
        Arrays.asList(
            "Taurus", "Prius", "M6", "Insight", "Hilux", "Fusion", "Focus", "Civic", "Avensis",
            "Accord");
    List<String> actual =
        Lists.newArrayList(offHeapIndex.getDistinctKeysDescending(noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_GreaterThanExclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius",
            "Taurus");
    actual =
        Lists.newArrayList(offHeapIndex.getDistinctKeys("", false, null, true, noQueryOptions()));
    assertEquals(expected, actual);

    expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius",
            "Taurus");
    actual =
        Lists.newArrayList(offHeapIndex.getDistinctKeys("A", false, null, true, noQueryOptions()));
    assertEquals(expected, actual);

    expected =
        Arrays.asList(
            "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius", "Taurus");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys("Accord", false, null, true, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_GreaterThanInclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius",
            "Taurus");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys("Accord", true, null, true, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_LessThanExclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected = Arrays.asList();
    actual =
        Lists.newArrayList(offHeapIndex.getDistinctKeys(null, true, "", false, noQueryOptions()));
    assertEquals(expected, actual);

    expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius",
            "Taurus");
    actual =
        Lists.newArrayList(offHeapIndex.getDistinctKeys(null, true, "Z", false, noQueryOptions()));
    assertEquals(expected, actual);

    expected =
        Arrays.asList("Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys(null, true, "Prius", false, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_LessThanInclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected =
        Arrays.asList(
            "Accord", "Avensis", "Civic", "Focus", "Fusion", "Hilux", "Insight", "M6", "Prius");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys(null, true, "Prius", true, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_BetweenExclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected = Arrays.asList("Focus", "Fusion", "Hilux");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys("Civic", false, "Insight", false, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_BetweenInclusiveAscending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected = Arrays.asList("Civic", "Focus", "Fusion", "Hilux", "Insight");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeys("Civic", true, "Insight", true, noQueryOptions()));
    assertEquals(expected, actual);
  }

  @Test
  public void testGetDistinctKeys_BetweenInclusiveDescending() {
    ConnectionManager connectionManager = temporaryInMemoryDatabase.getConnectionManager(true);
    SQLiteIndex<String, Car, Integer> offHeapIndex =
        SQLiteIndex.onAttribute(
            Car.MODEL,
            Car.CAR_ID,
            new SimpleAttribute<Integer, Car>() {
              @Override
              public Car getValue(Integer carId, QueryOptions queryOptions) {
                return CarFactory.createCar(carId);
              }
            },
            connectionManager);
    offHeapIndex.addAll(CarFactory.createCollectionOfCars(10), QueryFactory.noQueryOptions());
    List<String> expected, actual;

    expected = Arrays.asList("Insight", "Hilux", "Fusion", "Focus", "Civic");
    actual =
        Lists.newArrayList(
            offHeapIndex.getDistinctKeysDescending(
                "Civic", true, "Insight", true, noQueryOptions()));
    assertEquals(expected, actual);
  }
}
  @Before
  public void setUp() throws com.yazino.game.api.GameException, WalletServiceException {
    MockitoAnnotations.initMocks(this);

    timeSource = new SettableTimeSource(System.currentTimeMillis());

    command = new CommandWrapper(TABLE_ID, GAME_ID, A_PLAYER.getId(), SESSION_ID, "T");
    command.setRequestId(UUID);
    playerStatisticEventsPublisher = new InMemoryPlayerStatisticEventsPublisher();

    table =
        new Table(
            new com.yazino.game.api.GameType(GAME_TYPE, "Test", Collections.<String>emptySet()),
            BigDecimal.ONE,
            "test",
            true);
    table.setTableId(TABLE_ID);
    table.setTableStatus(TableStatus.open);
    table.setVariationProperties(new HashMap<String, String>());

    when(bufferedGameHostWalletFactory.create(table.getTableId(), command.getRequestId()))
        .thenReturn(bufferedGameHostWallet);
    when(bufferedGameHostWalletFactory.create(table.getTableId()))
        .thenReturn(bufferedGameHostWallet);

    when(auditor.newLabel()).thenReturn(AUDIT_LABEL);
    when(bufferedGameHostWallet.getBalance(Mockito.isA(BigDecimal.class)))
        .thenReturn(BigDecimal.ZERO);

    when(uuidSource.getNewUUID()).thenReturn(NEW_UUID);
    when(auditor.newLabel()).thenReturn(AUDIT_LABEL);
    when(bufferedGameHostWallet.getBalance(Mockito.isA(BigDecimal.class)))
        .thenReturn(ACCOUNT_BALANCE);

    when(gameRules.getGameType()).thenReturn("TEST");

    when(playerRepository.findSummaryByPlayerAndSession(PLAYER_ID, SESSION_ID))
        .thenReturn(
            aPlayerSessionSummary(PLAYER_ID, "Player", PLAYER_ACCOUNT_ID, BigDecimal.valueOf(100)));
    when(playerRepository.findSummaryByPlayerAndSession(PLAYER_ID, null))
        .thenReturn(
            aPlayerSessionSummary(PLAYER_ID, "Player", PLAYER_ACCOUNT_ID, BigDecimal.valueOf(100)));
    when(playerRepository.findSummaryByPlayerAndSession(PLAYER1_ID, null))
        .thenReturn(
            aPlayerSessionSummary(
                PLAYER1_ID, "Player1", PLAYER1_ACCOUNT_ID, BigDecimal.valueOf(200)));
    when(playerRepository.findSummaryByPlayerAndSession(PLAYER2_ID, null))
        .thenReturn(
            aPlayerSessionSummary(
                PLAYER2_ID, "Player2", PLAYER2_ACCOUNT_ID, BigDecimal.valueOf(300)));

    when(chatRepository.getOrCreateForLocation(TABLE_ID.toPlainString()))
        .thenReturn(new ChatChannel(TABLE_ID.toPlainString()));
  }