public void write(SXSSFSheet sheet, List<TestUnitsResults> tests) { if (tests.isEmpty()) { return; } int rowNum = FIRST_ROW; int colNum = FIRST_COLUMN; for (TestUnitsResults test : tests) { if (test.getTestSuite().getNumberOfTests() == 0) { continue; } Row row = sheet.createRow(rowNum); String testName = getTestName(test); createCell(row, colNum, "Parameters of " + testName, styles.parametersInfo); rowNum += 2; // Skip one row // Finding non empty fields from the test results is very expensive. Find them only once and then reuse // everywhere where needed. List<List<FieldDescriptor>> nonEmptyFields = getAllNonEmptyFields(test.getTestSuite().getTests()); // Create header final Cursor start = new Cursor(rowNum, colNum); Cursor lowestRight = writeHeaderForFields(sheet, start, test, nonEmptyFields); rowNum = lowestRight.getRowNum() + 1; colNum = FIRST_COLUMN; rowNum = writeValuesForFields(sheet, new Cursor(rowNum, colNum), test, nonEmptyFields); rowNum += SPACE_BETWEEN_RESULTS; } }
private int getRowHeight(Object value, List<FieldDescriptor> fields) { if (value == null || fields == null) { return 1; } if (value.getClass().isArray()) { int count = Array.getLength(value); int height = 0; for (int i = 0; i < count; i++) { height += getRowHeight(Array.get(value, i), fields); } return height == 0 ? 1 : height; } int maxSize = 1; for (FieldDescriptor fieldDescriptor : fields) { int size = fieldDescriptor.getMaxArraySize(value); if (size > maxSize) { maxSize = size; } } return maxSize; }
int rowNum = start.getRowNum(); int colNum = FIRST_COLUMN; int lastColNum = getLastColumn(test, nonEmptyFields); int maxHeight = getMaxHeight(description, nonEmptyFields); tasks.add(new WriteTask(new Cursor(rowNum, colNum++), description.getId(), styles.parameterValue, maxHeight)); if (isHasPK(parameter)) { IOpenField keyField = parameter.getKeyField(); Object id = ExportUtils.fieldValue(parameter.getValue(), keyField); int count = Array.getLength(id); for (int i = 0; i < count; i++) { int height = getRowHeight(Array.get(value, i), fields); tasks.add(new WriteTask(new Cursor(pkRow, colNum), Array.get(id, i), addValueTasks(tasks, new Cursor(rowNum, colNum), fields, value, maxHeight); colNum += getFieldWidth(fields); Cursor cursor = performWrite(sheet, new Cursor(rowNum, FIRST_COLUMN), tasks, lastColNum);
private Cursor writeHeaderForFields(SXSSFSheet sheet, Cursor start, TestUnitsResults test, List<List<FieldDescriptor>> nonEmptyFields) { TreeSet<WriteTask> tasks = new TreeSet<>(); int rowNum = start.getRowNum(); int colNum = start.getColNum(); tasks.add(new WriteTask(new Cursor(rowNum, colNum++), "ID", styles.header)); TestSuite testSuite = test.getTestSuite(); ParameterWithValueDeclaration[] params = testSuite.getTest(0).getExecutionParams(); for (int i = 0; i < params.length; i++) { ParameterWithValueDeclaration param = params[i]; boolean hasPK = isHasPK(param); List<FieldDescriptor> fields = nonEmptyFields.get(i); if (fields == null || fields.isEmpty()) { tasks.add(new WriteTask(new Cursor(rowNum, colNum++), param.getName(), styles.header)); continue; } String prefix = param.getName() + "."; if (hasPK) { tasks.add(new WriteTask(new Cursor(rowNum, colNum++), prefix + "_PK_", styles.header)); } colNum = addHeaderTasks(tasks, new Cursor(rowNum, colNum), fields, prefix); } return performWrite(sheet, start, tasks, getLastColumn(test, nonEmptyFields)); }
private void addValueTasks(TreeSet<WriteTask> tasks, Cursor cursor, List<FieldDescriptor> fields, Object value, int rowHeight) { int colNum = cursor.getColNum(); int rowNum = cursor.getRowNum(); if (value != null && value.getClass().isArray()) { int count = Array.getLength(value); int heightLeft = rowHeight; for (int i = 0; i < count; i++) { Object elem = Array.get(value, i); int height = getRowHeight(elem, fields); if (i < count - 1) { addValueTasks(tasks, new Cursor(rowNum, colNum), fields, elem, height); heightLeft -= height; } else { addValueTasks(tasks, new Cursor(rowNum, colNum), fields, elem, heightLeft); } rowNum += height; } } else { for (FieldDescriptor fieldDescriptor : fields) { Object fieldValue = ExportUtils.fieldValue(value, fieldDescriptor.getField()); List<FieldDescriptor> children = fieldDescriptor.getChildren(); if (children == null) { tasks.add(new WriteTask(new Cursor(rowNum, colNum), fieldValue, styles.parameterValue, rowHeight)); } else { addValueTasks(tasks, new Cursor(rowNum, colNum), children, fieldValue, rowHeight); } colNum += fieldDescriptor.getLeafNodeCount(); } } }
try { styles = new Styles(workbook); ParameterExport parameterExport = new ParameterExport(styles); parameterExport.write(sheet, resultsList);
styleEmptyCells(row, start.getColNum(), lastCellNum); row = sheet.createRow(rowNum); lowestRowNum = rowNum; createCell(row, colNum, task.getValue(), task.getStyle()); styleEmptyCells(row, start.getColNum(), lastCellNum);
private void styleEmptyCells(Row row, int firstCellNum, int lastCellNum) { for (int i = firstCellNum; i <= lastCellNum; i++) { Cell cell = row.getCell(i); if (cell == null) { createCell(row, i, null, styles.parameterAbsent); } } }
private int addHeaderTasks(TreeSet<WriteTask> tasks, Cursor cursor, List<FieldDescriptor> fields, String prefix) { int colNum = cursor.getColNum(); int rowNum = cursor.getRowNum(); for (FieldDescriptor fieldDescriptor : fields) { String fieldName = fieldDescriptor.getField().getName(); int width = fieldDescriptor.getLeafNodeCount(); if (fieldDescriptor.getChildren() == null) { tasks.add(new WriteTask(new Cursor(rowNum, colNum), prefix + fieldName, styles.header)); } else { addHeaderTasks(tasks, new Cursor(rowNum, colNum), fieldDescriptor.getChildren(), prefix + fieldName + "."); } colNum += width; } return colNum; }
private int getMaxHeight(TestDescription description, List<List<FieldDescriptor>> nonEmptyFields) { int maxHeight = 1; ParameterWithValueDeclaration[] executionParams = description.getExecutionParams(); for (int i = 0; i < executionParams.length; i++) { ParameterWithValueDeclaration param = executionParams[i]; List<FieldDescriptor> fields = nonEmptyFields.get(i); int rowHeight = getRowHeight(param.getValue(), fields); if (rowHeight > maxHeight) { maxHeight = rowHeight; } } return maxHeight; }