/** * Returns maximum array elements of the the field value from a given parent object * * @param object parent object (contains this field) * @return max array size if exist or 1 if there are no arrays */ public int getMaxArraySize(Object object) { if (object == null) { return 1; } if (object.getClass().isArray()) { int count = Array.getLength(object); int height = 0; for (int i = 0; i < count; i++) { height += getMaxArraySize(Array.get(object, i)); } return height == 0 ? 1 : height; } Object fieldValue = ExportUtils.fieldValue(object, getField()); return calcArraySizeForChild(fieldValue); }
private int getFieldWidth(List<FieldDescriptor> fields) { int colNum = 0; for (FieldDescriptor fieldDescriptor : fields) { colNum += fieldDescriptor.getLeafNodeCount(); } return colNum == 0 ? 1 : colNum; }
private int calcArraySizeForChild(Object fieldValue) { if (fieldValue == null) { return 1; } // In excel each element contains at least one cell even if it's empty. if (children == null) { return 1; } if (fieldValue.getClass().isArray()) { int size = 0; int count = Array.getLength(fieldValue); for (int i = 0; i < count; i++) { size += calcArraySizeForChild(Array.get(fieldValue, i)); } return size == 0 ? 1 : size; } else { int max = 1; for (FieldDescriptor child : children) { int childSize = child.getMaxArraySize(fieldValue); if (childSize > max) { max = childSize; } } return max; } }
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; }
Object fieldValue = value == null ? null : field.get(value, null); if (fieldValue != null && (!field.getType().isArray() || Array.getLength(fieldValue) > 0)) { List<FieldDescriptor> children = nonEmptyFieldsForFlatten(fieldType, childFieldValues); result.add(new FieldDescriptor(field, children)); break;
/** * Find all non empty fields from all test results. * If some field of any test result is not null, it will be included in result. * If some field is null in all test results, it will not be included in result. * WARNING: This method is very expensive! Don't invoke it too often. * * @param type Type of a checking object * @param values All possible values for a given type. Is got from test result. * @return all non empty fields from all test results (values). */ static List<FieldDescriptor> nonEmptyFields(IOpenClass type, List<?> values) { return nonEmptyFieldsForFlatten(type, ExportUtils.flatten(values)); }
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; }
private List<List<FieldDescriptor>> getAllNonEmptyFields(TestDescription[] descriptions) { TestDescription description = descriptions[0]; ParameterWithValueDeclaration[] executionParams = description.getExecutionParams(); List<List<FieldDescriptor>> result = new ArrayList<>(executionParams.length); for (int i = 0; i < executionParams.length; i++) { ParameterWithValueDeclaration param = executionParams[i]; result.add(FieldDescriptor.nonEmptyFields(param.getType(), valuesForAllCases(descriptions, i))); } return result; }
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(); } } }
/** * Returns leaf node count * * @return the count of leaf node or 1 if the node doesn't contain children */ public int getLeafNodeCount() { if (children == null) { return 1; } int width = 0; for (FieldDescriptor child : children) { width += child.getLeafNodeCount(); } return width; }
private int getLastColumn(TestUnitsResults test, List<List<FieldDescriptor>> nonEmptyFields) { int lastColumn = FIRST_COLUMN; // ID column TestSuite testSuite = test.getTestSuite(); ParameterWithValueDeclaration[] params = testSuite.getTest(0).getExecutionParams(); for (int i = 0; i < params.length; i++) { ParameterWithValueDeclaration param = params[i]; if (isHasPK(param)) { lastColumn++; // _PK_ column } List<FieldDescriptor> fields = nonEmptyFields.get(i); if (fields == null) { // Simple type lastColumn++; } else { for (FieldDescriptor field : fields) { lastColumn += field.getLeafNodeCount(); } } } return lastColumn; }