ResultSetDataWrapper implements DataWrapper { @Override public boolean addToTable(final TableCellWalker walker) throws IOException { int rowCount = 0; try { final ResultSetMetaData metadata = this.resultSet.getMetaData(); try { final int r1 = walker.rowIndex(); final int c1 = walker.colIndex(); final int columnCount = metadata.getColumnCount(); this.writeFirstLineDataTo(walker, metadata); if (this.resultSet.next()) { do { if (this.max == -1 || ++rowCount <= this.max) { walker.nextRow(); walker.to(c1); this.writeDataLineTo(walker, columnCount); } } while (this.resultSet.next()); } final boolean oneBlankLine = rowCount == 0 || rowCount > this.max; if (oneBlankLine) { walker.nextRow(); walker.to(c1); this.writeLastLineDataTo(walker, columnCount, rowCount); } if (this.autoFilter) { final int r2 = walker.rowIndex(); final int c2 = c1 + columnCount - 1; walker.getTable().addAutoFilter(this.rangeName, r1, c1, r2, c2); } walker.nextRow(); } catch (final SQLException e) { if (this.logger != null) { this.logger.log(Level.SEVERE, "Can't read ResultSet row", e); } throw new RuntimeException(e); } catch (final FastOdsException e) { throw new RuntimeException(e); } } catch (final SQLException e) { if (this.logger != null) { this.logger.log(Level.SEVERE, "Can't read ResultSet metadata", e); } throw new RuntimeException(e); } return rowCount > 0; } ResultSetDataWrapper(final Logger logger, final ToCellValueConverter converter,
                                final String rangeName, final ResultSet rs,
                                final TableCellStyle headCellStyle, final boolean autoFilter,
                                final Map<Integer, CellType> cellTypeByColIndex,
                                final CellValue nullValue, final int max); static ResultSetDataWrapperBuilder builder(final String rangeName, final ResultSet rs); @Override boolean addToTable(final TableCellWalker walker);  }
@Test @SuppressWarnings("deprecated") public final void testMax() throws IOException { final Collection<List<Object>> r = new ArrayList<List<Object>>(); for (int v = FROM; v < TO; v++) { final List<Object> l = Collections.<Object>singletonList(v); r.add(l); } final DataWrapper wrapper = this.createWrapper(Collections.singletonList("number"), r, 3); PowerMock.resetAll(); EasyMock.expect(this.walker.rowIndex()).andReturn(0); EasyMock.expect(this.walker.colIndex()).andReturn(10); this.walker.setStringValue("number"); this.walker.setStyle(this.tcls); this.walker.next(); for (int v = FROM; v < Math.min(FROM + 3, TO); v++) { this.walker.nextRow(); this.walker.to(10); this.walker.setCellValue(this.converter.from(v)); this.walker.next(); } this.walker.nextRow(); this.walker.to(10); this.walker.setStringValue("... (2 rows remaining)"); this.walker.next(); this.walker.nextRow(); PowerMock.replayAll(); wrapper.addToTable(this.walker); PowerMock.verifyAll(); }
@Test public final void testNoRow() throws IOException { final DataWrapper wrapper = this.createWrapper(Arrays.asList("number", "word"), Collections.<List<Object>>emptyList(), 100); PowerMock.resetAll(); EasyMock.expect(this.walker.rowIndex()).andReturn(0); EasyMock.expect(this.walker.colIndex()).andReturn(0); this.walker.setStringValue("number"); this.walker.setStyle(this.tcls); this.walker.next(); this.walker.setStringValue("word"); this.walker.setStyle(this.tcls); this.walker.next(); this.walker.nextRow(); this.walker.to(0); this.walker.setStringValue(""); this.walker.next(); this.walker.setStringValue(""); this.walker.next(); this.walker.nextRow(); PowerMock.replayAll(); wrapper.addToTable(this.walker); PowerMock.verifyAll(); }
@Test public final void testNullValue() throws IOException { final List<Object> l = new ArrayList<Object>(1); l.add(null); final DataWrapper wrapper = this.createWrapper(Collections.singletonList("value"), Collections.singletonList(l), 100); PowerMock.resetAll(); EasyMock.expect(this.walker.rowIndex()).andReturn(0); EasyMock.expect(this.walker.colIndex()).andReturn(0); this.walker.setStringValue("value"); this.walker.setStyle(this.tcls); this.walker.next(); this.walker.nextRow(); this.walker.to(0); this.walker.setCellValue(new StringValue("<NULL>")); this.walker.next(); this.walker.nextRow(); PowerMock.replayAll(); wrapper.addToTable(this.walker); PowerMock.verifyAll(); }
@Test public final void testOneRow() throws IOException { final DataWrapper wrapper = this.createWrapper(Arrays.asList("number", "word"), Collections.singletonList(Arrays.<Object>asList(7, "a")), 100); PowerMock.resetAll(); EasyMock.expect(this.walker.rowIndex()).andReturn(0); EasyMock.expect(this.walker.colIndex()).andReturn(0); this.walker.setStringValue("number"); this.walker.setStyle(this.tcls); this.walker.next(); this.walker.setStringValue("word"); this.walker.setStyle(this.tcls); this.walker.next(); this.walker.nextRow(); this.walker.to(0); this.walker.setCellValue(this.converter.from(7)); this.walker.next(); this.walker.setCellValue(this.converter.from("a")); this.walker.next(); this.walker.nextRow(); PowerMock.replayAll(); wrapper.addToTable(this.walker); PowerMock.verifyAll(); }