### Question:
GenericJdbcDatabaseAccessor implements DatabaseAccessor { @Override public int getTotalNumberOfRecords(Configuration conf) throws HiveJdbcDatabaseAccessException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { initializeDatabaseConnection(conf); String sql = JdbcStorageConfigManager.getQueryToExecute(conf); String countQuery = "SELECT COUNT(*) FROM (" + sql + ") tmptable"; LOGGER.info("Query to execute is [{}]", countQuery); conn = dbcpDataSource.getConnection(); ps = conn.prepareStatement(countQuery); rs = ps.executeQuery(); if (rs.next()) { return rs.getInt(1); } else { LOGGER.warn("The count query did not return any results.", countQuery); throw new HiveJdbcDatabaseAccessException("Count query did not return any results."); } } catch (HiveJdbcDatabaseAccessException he) { throw he; } catch (Exception e) { LOGGER.error("Caught exception while trying to get the number of records", e); throw new HiveJdbcDatabaseAccessException(e); } finally { cleanupResources(conn, ps, rs); } } GenericJdbcDatabaseAccessor(); @Override List<String> getColumnNames(Configuration conf); @Override int getTotalNumberOfRecords(Configuration conf); @Override JdbcRecordIterator getRecordIterator(Configuration conf, String partitionColumn, String lowerBound, String upperBound, int limit, int
          offset); RecordWriter getRecordWriter(TaskAttemptContext context); @Override Pair<String, String> getBounds(Configuration conf, String partitionColumn, boolean retrieveMin, boolean
          retrieveMax); @Override boolean needColumnQuote();  }### Answer:
@Test public void testGetTotalNumberOfRecords() throws HiveJdbcDatabaseAccessException { Configuration conf = buildConfiguration(); DatabaseAccessor accessor = DatabaseAccessorFactory.getAccessor(conf); int numRecords = accessor.getTotalNumberOfRecords(conf); assertThat(numRecords, is(equalTo(5))); }
@Test public void testGetTotalNumberOfRecords_whereClause() throws HiveJdbcDatabaseAccessException { Configuration conf = buildConfiguration(); conf.set(JdbcStorageConfig.QUERY.getPropertyName(), "select * from test_strategy where strategy_id = '5'"); DatabaseAccessor accessor = DatabaseAccessorFactory.getAccessor(conf); int numRecords = accessor.getTotalNumberOfRecords(conf); assertThat(numRecords, is(equalTo(1))); }
@Test public void testGetTotalNumberOfRecords_noRecords() throws HiveJdbcDatabaseAccessException { Configuration conf = buildConfiguration(); conf.set(JdbcStorageConfig.QUERY.getPropertyName(), "select * from test_strategy where strategy_id = '25'"); DatabaseAccessor accessor = DatabaseAccessorFactory.getAccessor(conf); int numRecords = accessor.getTotalNumberOfRecords(conf); assertThat(numRecords, is(equalTo(0))); }
@Test(expected = HiveJdbcDatabaseAccessException.class) public void testGetTotalNumberOfRecords_invalidQuery() throws HiveJdbcDatabaseAccessException { Configuration conf = buildConfiguration(); conf.set(JdbcStorageConfig.QUERY.getPropertyName(), "select * from strategyx where strategy_id = '5'"); DatabaseAccessor accessor = DatabaseAccessorFactory.getAccessor(conf); @SuppressWarnings("unused") int numRecords = accessor.getTotalNumberOfRecords(conf); }