DataRowIterator implements Iterator<DataRow>, AutoCloseable { public RowAttribute[] calculateHeader() { if (currentRow == null) { return new RowAttribute[0]; } List<String> names = new ArrayList<>(currentRow.keySet()); Collections.sort(names); RowAttribute[] header = new RowAttribute[currentRow.size()]; for (int i = 0; i < header.length; i++) { String name = names.get(i); header[i] = new BaseRowAttribute(Object.class, name, ASTDbPath.DB_PREFIX + name, i); } return header; } DataRowIterator(ResultIterator<DataRow> delegate); @Override void close(); RowAttribute[] calculateHeader(); @Override boolean hasNext(); @Override DataRow next();  }
@Test public void testCalculateHeader() { DataRow row = new DataRow(5); row.put("A1", new Object()); row.put("A2", null); row.put("A0", "aaaa"); ResultIterator<DataRow> rows = mock(ResultIterator.class); when(rows.hasNextRow()).thenReturn(true, false); when(rows.nextRow()).thenReturn(row, (DataRow) null); DataRowIterator it = new DataRowIterator(rows); RowAttribute[] header = it.calculateHeader(); assertNotNull(header); assertEquals(3, header.length); assertEquals("A0", header[0].getSourceName()); assertEquals("db:A0", header[0].getTargetPath()); assertEquals("A1", header[1].getSourceName()); assertEquals("db:A1", header[1].getTargetPath()); assertEquals("A2", header[2].getSourceName()); assertEquals("db:A2", header[2].getTargetPath()); }
@Test public void testCalculateHeader_NoRows() { ResultIterator<DataRow> rows = mock(ResultIterator.class); when(rows.hasNextRow()).thenReturn(false, false); when(rows.nextRow()).thenReturn(null, (DataRow) null); DataRowIterator it = new DataRowIterator(rows); RowAttribute[] header = it.calculateHeader(); assertNotNull(header); assertEquals(0, header.length); }