DataTable { @Override public String toString() { StringBuilder sb = new StringBuilder(); int totalWidth = 0; int colCount = getColumnsCount(); List<Integer> colWidths = new ArrayList<>(); for (int i = 0; i < colCount; i++) { List<String> col = getColumn(i); int colWidth = 0; for (int j = 0; j < col.size(); j++) { colWidth = Math.max(colWidth, col.get(j).length()); } colWidths.add(colWidth); totalWidth += colWidth; } String line = "+" + StringUtils.repeat("-", totalWidth + colCount - 1) + "+"; for(int i = 0; i < getRowsCount(); i++) { List<String> row = getRow(i); sb.append("\n").append(line).append("\n"); for(int j = 0; j < colWidths.size(); j++) { String cellPadded = rightPad("", colWidths.get(j)); if(j < row.size()) { cellPadded = isolateRTLIfRequired(rightPad(row.get(j), colWidths.get(j))); } sb.append("|").append(cellPadded); } sb.append("|"); } sb.append("\n").append(line).append("\n"); return sb.toString(); } DataTable(int pageNumber);  DataTable(Collection<Integer> pageNumbers); DataTable addRow(String... dataRow); void addRow(List<String> dataRow); void addRows(List<List<String>> dataRows); List<String> headerRow(); List<String> headerRowIgnoreBlanks(); boolean hasSameHeaderAs(DataTable other); boolean hasSameHeaderBlanksIgnoredAs(DataTable other); boolean hasSameColumnCountAs(DataTable other); List<List<String>> getData(); TreeSet<Integer> getPageNumbers(); DataTable mergeWith(DataTable other); String getPagesAsString(); boolean hasData(); int getColumnsCount(); int getRowsCount(); List<String> getColumn(int c); List<String> getRow(int r); DataTable mergeColumns(int c1, int c2); void addBlankColumn(int index); @Override String toString();  }
@Test public void testToString() { DataTable data = new DataTable(1); data.addRow("Name", "Surname", "Age", "Sex"); data.addRow("John", "Doe", "14", "M"); data.addRow("Alexander", "Appleseed", "99", "M"); data.addRow("Alex", "Jones", "", "F"); data.addRow("Felix", "Fog"); String expected = "\n" + "+---------------------------+\n" + "|Name |Surname |Age|Sex|\n" + "+---------------------------+\n" + "|John |Doe |14 |M |\n" + "+---------------------------+\n" + "|Alexander|Appleseed|99 |M |\n" + "+---------------------------+\n" + "|Alex |Jones | |F |\n" + "+---------------------------+\n" + "|Felix |Fog | | |\n" + "+---------------------------+\n"; assertThat(data.toString(), is(expected)); }