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