AsciiTableFormatter extends AbstractTableFormatter { @Override public TableFormatter writeCell(String value, int colspan) { if (colspan > columns[column].getColspan() - cellIndex) { throw new IllegalArgumentException("You have exceded the authorized colspan"); } HorizontalAlignment horizontalAlignment = (colspan == 1) ? columns[column].getHorizontalAlignment() : HorizontalAlignment.CENTER; table.addCell(value, convertCellStyle(horizontalAlignment), colspan); rowCellIndex = (rowCellIndex + 1) % cellsCount; cellIndex = (cellIndex + colspan) % columns[column].getColspan(); if (cellIndex == 0) { column = (column + 1) % columns.length; } return this; } AsciiTableFormatter(Writer writer, String title, TableFormatterConfig config, Column... columns);  AsciiTableFormatter(String title, Column... columns);  AsciiTableFormatter(Writer writer, String title, Column... columns); @Override TableFormatter writeEmptyLine(); @Override TableFormatter writeComment(String comment); @Override TableFormatter writeCell(String value, int colspan); @Override void close();  }
@Test public void testUnauthorizedColspan() throws IOException { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("You have exceded the authorized colspan"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8); AsciiTableFormatter formatter = new AsciiTableFormatter(writer, null, config, new Column("column1").setColspan(4).setHorizontalAlignment(HorizontalAlignment.CENTER), new Column("column2").setColspan(2).setHorizontalAlignment(HorizontalAlignment.CENTER))) { formatter.writeCell("Line:1 Cell:1", 1) .writeCell("Line:1 Cell:2", 1) .writeCell("Line:1 Cell:3", 1) .writeCell("Line:1 Cell:4", 2) .writeCell("Line:1 Cell:5", 1); } }