@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); } }
public static <K, V> Map<K, V> resultSetToMap(final ResultSet resultSet) { try { final Map<K, V> map = new LinkedHashMap<K, V>(); while (resultSet.next()) { K key = (K) resultSet.getObject(1); V value = (V) resultSet.getObject(2); map.put(key, value); } return map; } catch (Exception e) { throw new IllegalStateException("Unable to transform the resultSet into a Map", e); } }
@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); } }