Пример #1
0
  @Test
  public void testDropIndexOnTable() throws SQLException {

    Connection connection = null;
    Statement statement = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      connection = spy(connectionManager.getConnection(null));
      statement = spy(connection.createStatement());
      when(connection.createStatement()).thenReturn(statement);

      DBQueries.createIndexTable(NAME, Integer.class, String.class, connection);
      DBQueries.createIndexOnTable(NAME, connection);

      assertObjectExistenceInSQLIteMasterTable(TABLE_NAME, "table", true, connectionManager);
      assertObjectExistenceInSQLIteMasterTable(INDEX_NAME, "index", true, connectionManager);

      DBQueries.dropIndexOnTable(NAME, connection);
      assertObjectExistenceInSQLIteMasterTable(TABLE_NAME, "table", true, connectionManager);
      assertObjectExistenceInSQLIteMasterTable(INDEX_NAME, "index", false, connectionManager);

      verify(statement, times(3)).close();
    } finally {
      DBUtils.closeQuietly(connection);
      DBUtils.closeQuietly(statement);
    }
  }
Пример #2
0
 @Test
 public void testEnsureNotNegative_ValidCase() {
   IllegalStateException unexpected = null;
   try {
     DBQueries.ensureNotNegative(0);
     DBQueries.ensureNotNegative(1);
   } catch (IllegalStateException e) {
     unexpected = e;
   }
   assertNull(unexpected);
 }
Пример #3
0
  @Test
  public void getDistinctKeysAndCounts_SortByKeyDescending() {

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

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.getDistinctKeysAndCounts(true, NAME, connection);

      Map<String, Integer> resultSetToMap = resultSetToMap(resultSet);
      assertEquals(3, resultSetToMap.size());

      Iterator<Map.Entry<String, Integer>> entriesIterator = resultSetToMap.entrySet().iterator();

      Map.Entry entry = entriesIterator.next();
      assertEquals("gps", entry.getKey());
      assertEquals(1, entry.getValue());

      entry = entriesIterator.next();
      assertEquals("airbags", entry.getKey());
      assertEquals(1, entry.getValue());

      entry = entriesIterator.next();
      assertEquals("abs", entry.getKey());
      assertEquals(2, entry.getValue());

    } finally {
      DBUtils.closeQuietly(resultSet);
      DBUtils.closeQuietly(connection);
    }
  }
Пример #4
0
  @Test
  public void testContains() throws SQLException {
    Connection connection = null;

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

      Equal<Car, String> equal = equal(Car.FEATURES, "abs");

      connection = connectionManager.getConnection(null);
      Assert.assertTrue(DBQueries.contains(1, equal, NAME, connection));
      Assert.assertFalse(DBQueries.contains(4, equal, NAME, connection));

    } finally {
      DBUtils.closeQuietly(connection);
    }
  }
Пример #5
0
 @Test
 public void testEnsureNotNegative_InvalidCase() {
   IllegalStateException expected = null;
   try {
     DBQueries.ensureNotNegative(-1);
   } catch (IllegalStateException e) {
     expected = e;
   }
   assertNotNull(expected);
   assertEquals("Update returned error code: -1", expected.getMessage());
 }
Пример #6
0
  @Test
  public void getCountOfDistinctKeys() {

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

      connection = connectionManager.getConnection(null);
      int countOfDistinctKeys = DBQueries.getCountOfDistinctKeys(NAME, connection);
      assertEquals(3, countOfDistinctKeys);

    } finally {
      DBUtils.closeQuietly(connection);
    }
  }
Пример #7
0
  @Test
  public void testSearch_Has() throws SQLException {
    Connection connection = null;
    ResultSet resultSet = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      initWithTestData(connectionManager);

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.search(has(self(Car.class)), NAME, connection);
      assertResultSetObjectKeysOrderAgnostic(resultSet, Arrays.asList(1, 2, 3));

    } finally {
      DBUtils.closeQuietly(connection);
      DBUtils.closeQuietly(resultSet);
    }
  }
Пример #8
0
  @Test
  public void testSearch_StringStartsWith() throws SQLException {
    Connection connection = null;
    ResultSet resultSet = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      initWithTestData(connectionManager);

      StringStartsWith<Car, String> startsWith = startsWith(Car.FEATURES, "ab");

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.search(startsWith, NAME, connection);
      assertResultSetObjectKeysOrderAgnostic(resultSet, Arrays.asList(1, 3));

    } finally {
      DBUtils.closeQuietly(connection);
      DBUtils.closeQuietly(resultSet);
    }
  }
Пример #9
0
  @Test
  public void testGetIndexEntryByObjectKey() throws SQLException {

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

      connection = connectionManager.getConnection(null);
      ResultSet resultSet = DBQueries.getIndexEntryByObjectKey(3, NAME, connection);

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

      assertResultSetOrderAgnostic(resultSet, expectedRows);

    } finally {
      DBUtils.closeQuietly(connection);
    }
  }
Пример #10
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);
    }
  }
Пример #11
0
  @Test
  public void testBulkAdd() throws SQLException {
    Connection connection = null;
    try {
      ConnectionManager connectionManager = temporaryFileDatabase.getConnectionManager(true);
      createSchema(connectionManager);

      List<DBQueries.Row<Integer, String>> rowsToAdd =
          new ArrayList<DBQueries.Row<Integer, String>>(4);
      rowsToAdd.add(new DBQueries.Row<Integer, String>(1, "abs"));
      rowsToAdd.add(new DBQueries.Row<Integer, String>(1, "gps"));
      rowsToAdd.add(new DBQueries.Row<Integer, String>(2, "airbags"));
      rowsToAdd.add(new DBQueries.Row<Integer, String>(2, "abs"));

      connection = connectionManager.getConnection(null);
      DBQueries.bulkAdd(rowsToAdd, NAME, connection);
      assertQueryResultSet("SELECT * FROM " + TABLE_NAME, rowsToAdd, connectionManager);

    } finally {
      DBUtils.closeQuietly(connection);
    }
  }
Пример #12
0
  @Test
  public void getDistinctKeysAndCounts() {

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

      connection = connectionManager.getConnection(null);
      resultSet = DBQueries.getDistinctKeysAndCounts(false, NAME, connection);

      Map<String, Integer> resultSetToMap = resultSetToMap(resultSet);
      assertEquals(3, resultSetToMap.size());
      assertEquals(new Integer(2), resultSetToMap.get("abs"));
      assertEquals(new Integer(1), resultSetToMap.get("airbags"));
      assertEquals(new Integer(1), resultSetToMap.get("gps"));

    } finally {
      DBUtils.closeQuietly(resultSet);
      DBUtils.closeQuietly(connection);
    }
  }
Пример #13
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);
    }
  }