org.assertj.db.output.impl.PlainOutput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assertj-db Show documentation
Show all versions of assertj-db Show documentation
AssertJ-DB - Rich and fluent assertions for testing with database
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.db.output.impl;
import org.assertj.core.api.WritableAssertionInfo;
import org.assertj.db.type.*;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of plain output of assertj-db.
*
* @author Régis Pouiller
* @since 1.1.0
*/
enum PlainOutput implements Output {
/**
* Singleton instance.
*/
INSTANCE;
/**
* End of a line.
*/
private final static String EOL = String.format("%n");
/**
* Returns the text representing a object.
*
* @param object The object
* @return The text.
*/
private static String getText(Object object) {
return "" + object;
}
/**
* Returns the size of the column corresponding to the maximum in the length of the column name,
* the length of the text of the index and the length of the texts representing the values.
*
* @param columnName The column name.
* @param type The text representing the type.
* @param index The index of the column.
* @param values The values.
* @return The size.
*/
private static int getColumnSize(String columnName, String type, Integer index, Value... values) {
int size = ("" + columnName).length();
int typeSize = type.length();
if (typeSize > size) {
size = typeSize;
}
if (index != null) {
int indexSize = ("Index : " + index).length();
if (indexSize > size) {
size = indexSize;
}
}
for (Value value : values) {
int valueSize = OutputType.getText(value).length();
if (valueSize > size) {
size = valueSize;
}
}
return size + 2;
}
/**
* Returns the size of the column corresponding to the maximum in the length of the column name,
* the length of the text of the index and the length of the texts representing the objects.
*
* @param columnName The column name.
* @param objects The objects.
* @return The size.
*/
private static int getColumnSize(String columnName, Object... objects) {
int size = ("" + columnName).length();
for (Object object : objects) {
int valueSize = getText(object).length();
if (valueSize > size) {
size = valueSize;
}
}
return size + 2;
}
/**
* Returns a {@code StringBuilder} representing a text containing the {@code text} in parameter
* and the remaining space to corresponding to the {@code size} in parameter is filled with spaces.
*
* @param text The text.
* @param size The size.
* @return The text after filling.
*/
private static StringBuilder getFilledText(String text, int size) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(" ").append(text);
while (stringBuilder.length() < size) {
stringBuilder.append(" ");
}
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a line for a cell corresponding to the {@code size} in parameter.
*
* @param size The size.
* @return The line.
*/
private static StringBuilder getCellLine(int size) {
StringBuilder stringBuilder = new StringBuilder();
while (stringBuilder.length() < size) {
stringBuilder.append('-');
}
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the {@code row} in parameter.
*
* @param sizesList The list of sizes.
* @param row The row
* @param otherColumnsContent Other content in the column (var-args) : the columns before the values.
* @return The line.
*/
private static StringBuilder getCompleteRow(List sizesList, Row row, Object... otherColumnsContent) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (Integer size : sizesList) {
if (index < otherColumnsContent.length) {
stringBuilder.append(getFilledText("" + otherColumnsContent[index], size));
stringBuilder.append("|");
}
else {
if (row != null) {
Value value = row.getValuesList().get(index - otherColumnsContent.length);
stringBuilder.append(getFilledText(OutputType.getText(value), size)).append("|");
}
else {
stringBuilder.append(getFilledText("", size)).append("|");
}
}
index++;
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the {@code sizes} in parameter.
*
* @param sizesList The list of sizes.
* @param otherColumnsContent Other content in the column (var-args) : the columns before the values.
* @return The line.
*/
private static StringBuilder getCompleteLine(List sizesList, Object... otherColumnsContent) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (int size : sizesList) {
if (index < otherColumnsContent.length) {
stringBuilder.append(getFilledText("" + otherColumnsContent[index], size));
} else {
stringBuilder.append(getCellLine(size));
}
stringBuilder.append("|");
index++;
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns the columns sizes in array.
*
* @param columnSizesList List of column sizes (the columns with the values).
* @param sizes Sizes (var-args) : the columns before the values.
* @return An array with the sizes.
*/
private List getSizesList(List columnSizesList, Integer... sizes) {
List sizesList = new ArrayList<>();
for (Integer size : sizes) {
sizesList.add(size);
}
if (columnSizesList != null) {
for (Integer size : columnSizesList) {
sizesList.add(size);
}
}
return sizesList;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the indication about the primary key.
*
* @param sizesList The list of sizes of the column.
* @param pksNameList The list of the primary key name.
* @param columnsNameList The list of the column name.
* @return The output.
*/
private static StringBuilder getCompletePrimaryKey(List sizesList, List pksNameList,
List columnsNameList) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (; index < sizesList.size() - columnsNameList.size(); index++) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText("", columnSize));
stringBuilder.append("|");
}
for (String columnName : columnsNameList) {
Integer columnSize = sizesList.get(index);
String pk = "";
if (pksNameList != null && pksNameList.contains(columnName)) {
pk = "*";
}
stringBuilder.append(getFilledText(pk, columnSize)).append("|");
index++;
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the indication about the primary key.
*
* @param sizesList The list of sizes of the column.
* @param columnsNameList The list of the column name.
* @param otherColumnsContent Other content in the column (var-args) : the columns before the values.
* @return The output.
*/
private static StringBuilder getCompleteColumnName(List sizesList, List columnsNameList,
String... otherColumnsContent) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (String content : otherColumnsContent) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText(content, columnSize));
stringBuilder.append("|");
index++;
}
for (String columnName : columnsNameList) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText(columnName, columnSize)).append("|");
index++;
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the indication about the type.
*
* @param sizesList The list of sizes of the column.
* @param typesList The list of the type.
* @param otherColumnsContent Other content in the column (var-args) : the columns before the values.
* @return The output.
*/
private static StringBuilder getCompleteType(List sizesList, List typesList,
String... otherColumnsContent) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (String content : otherColumnsContent) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText(content, columnSize));
stringBuilder.append("|");
index++;
}
for (; index < sizesList.size(); index++) {
int index1 = index - otherColumnsContent.length;
Integer columnSize = sizesList.get(index);
String type = index1 < typesList.size() ? typesList.get(index1) : "";
stringBuilder.append(getFilledText(type, columnSize)).append("|");
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns a {@code StringBuilder} representing a complete line corresponding to the indication about the index.
*
* @param sizesList The list of sizes of the column.
* @param numberOfAdditionalColumns The number of additional columns.
* @return The output.
*/
private static StringBuilder getCompleteIndex(List sizesList, int numberOfAdditionalColumns) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("|");
int index = 0;
for (; index < numberOfAdditionalColumns; index++) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText("", columnSize));
stringBuilder.append("|");
}
for (; index < sizesList.size(); index++) {
Integer columnSize = sizesList.get(index);
stringBuilder.append(getFilledText("Index : " + (index - numberOfAdditionalColumns), columnSize)).append("|");
}
stringBuilder.append(EOL);
return stringBuilder;
}
/**
* Returns the size of the column of index.
* @param size The size of the rows/changes.
* @return The size.
*/
private static Integer getIndexColumnSize(int size) {
return getColumnSize("", "Index : " + (size - 1));
}
/**
* Returns the size of the column of change type.
* @param changes The changes.
* @return The size.
*/
private static Integer getChangeTypeColumnSize(Change... changes) {
int size = 0;
for (Change change : changes) {
ChangeType changeType = change.getChangeType();
int changeTypeColumnSize = getColumnSize("TYPE", changeType);
if (size < changeTypeColumnSize) {
size = changeTypeColumnSize;
}
}
return size;
}
/**
* Returns the size of the column of change type.
* @param changes The changes.
* @return The size.
*/
private static Integer getDataTypeColumnSize(Change... changes) {
int size = 0;
for (Change change : changes) {
DataType dataType = change.getDataType();
String dataName = OutputType.getDataName(change);
int dataTypeColumnSize = getColumnSize("" + dataType, dataName);
if (size < dataTypeColumnSize) {
size = dataTypeColumnSize;
}
}
return size;
}
/**
* Returns the sizes for the columns corresponding to the size of the columns name.
*
* @param columnsNameList The list of column names.
* @return The labels.
*/
private static List getColumnSizesList(List columnsNameList) {
List columnSizesList = new ArrayList<>();
int index = 0;
for (String columnName : columnsNameList) {
int columnSize = getColumnSize(columnName, "", index);
columnSizesList.add(columnSize);
index++;
}
return columnSizesList;
}
/**
* Returns the sizes for the columns corresponding to the size of the values of the column.
*
* @param rows The rows.
* @return The labels.
*/
private static List getColumnSizesList(Row... rows) {
List columnSizesList = new ArrayList<>();
Row row0 = null;
for (Row row : rows) {
if (row != null) {
row0 = row;
break;
}
}
List columnsNameList = row0.getColumnsNameList();
int index = 0;
for (String columnName : columnsNameList) {
List