Table { public String toHtml() { StringBuffer sb = new StringBuffer("<table"); if (id != null) { sb.append(" id=\"" + id + "\""); } sb.append(">\n"); if (caption != null) { sb.append(String.format("<caption>%s</caption>\n", caption)); } for (TableRow tr : this.rows) { sb.append(tr.toHtml()); sb.append("\n"); } sb.append("</table>"); return sb.toString(); } Table();  Table(String id, List<TableRow> rows, String caption); String getCaption(); String getId(); List<TableRow> getRows(); void setCaption(String caption); void setId(String id); void setRows(List<TableRow> rows); String toHtml(); @Override String toString();  }
@Test public void testToHtml() { assertEquals("<table>\n</table>",new Table().toHtml()); assertEquals("<table id=\"foo\">\n</table>",new Table("foo",rows,null).toHtml()); assertEquals("<table id=\"foo\">\n<caption>This is a table</caption>\n</table>",new Table("foo",rows,caption).toHtml()); rows.add(rowOne); String tr1 = rowOne.toHtml() + "\n"; assertEquals("<table id=\"foo\">\n<caption>This is a table</caption>\n" + tr1 + "</table>",new Table("foo",rows,caption).toHtml()); rows.add(rowTwo); String tr2 = rowTwo.toHtml() + "\n"; assertEquals("<table id=\"foo\">\n<caption>This is a table</caption>\n" + tr1 + tr2 + "</table>",new Table("foo",rows,caption).toHtml()); }