org.assertj.db.output.impl.HtmlOutput 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.Iterator;
import java.util.List;
/**
* Implementation of html output of assertj-db.
*
* @author Régis Pouiller
* @since 1.1.0
*/
enum HtmlOutput implements Output {
/**
* Singleton instance.
*/
INSTANCE;
/**
* Returns the html document for output.
* @param info Writable information about an assertion.
* @param content Content of the html document.
* @return The html document.
*/
private static String getHtml(WritableAssertionInfo info, String content) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("description ");
stringBuilder.append("");
stringBuilder.append(info.descriptionText());
stringBuilder.append("
");
stringBuilder.append(content);
stringBuilder.append("");
return stringBuilder.toString();
}
/**
* {@inheritDoc}
*/
@Override
public String getTableOutput(WritableAssertionInfo info, Table table) {
List pksNameList = table.getPksNameList();
List columnsNameList = table.getColumnsNameList();
List rowsList = table.getRowsList();
Row[] rows = rowsList.toArray(new Row[rowsList.size()]);
List typesList = OutputType.getTypesList(rows);
StringBuilder[] pksValueStringBuilders = OutputType.getPksValueStringBuilder(rows);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("
PRIMARY
KEY");
stringBuilder.append(" ");
int index = 0;
for (String columnName : columnsNameList) {
String pk = "";
if (pksNameList.contains(columnName)) {
pk = "*";
}
stringBuilder.append("");
stringBuilder.append(pk);
stringBuilder.append("
");
stringBuilder.append(columnName);
stringBuilder.append("
");
if (index < typesList.size()) {
stringBuilder.append(typesList.get(index));
}
stringBuilder.append("
Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
index++;
}
stringBuilder.append(" ");
index = 0;
for (Row row : table.getRowsList()) {
stringBuilder.append("");
stringBuilder.append("Index :");
stringBuilder.append(index);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(pksValueStringBuilders[index]);
stringBuilder.append(" ");
for (Value value : row.getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
stringBuilder.append(" ");
index++;
}
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getRequestOutput(WritableAssertionInfo info, Request request) {
List pksNameList = request.getPksNameList();
List columnsNameList = request.getColumnsNameList();
List rowsList = request.getRowsList();
Row[] rows = rowsList.toArray(new Row[rowsList.size()]);
List typesList = OutputType.getTypesList(rows);
StringBuilder[] pksValueStringBuilders = OutputType.getPksValueStringBuilder(rows);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("
PRIMARY
KEY");
stringBuilder.append(" ");
int index = 0;
for (String columnName : columnsNameList) {
String pk = "";
if (pksNameList.contains(columnName)) {
pk = "*";
}
stringBuilder.append("");
stringBuilder.append(pk);
stringBuilder.append("
");
stringBuilder.append(columnName);
stringBuilder.append("
");
if (index < typesList.size()) {
stringBuilder.append(typesList.get(index));
}
stringBuilder.append("
Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
index++;
}
stringBuilder.append(" ");
index = 0;
for (Row row : request.getRowsList()) {
stringBuilder.append("");
stringBuilder.append("Index :");
stringBuilder.append(index);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(pksValueStringBuilders[index]);
stringBuilder.append(" ");
for (Value value : row.getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
stringBuilder.append(" ");
index++;
}
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getChangesOutput(WritableAssertionInfo info, Changes changes) {
StringBuilder stringBuilder = new StringBuilder();
List changesList = changes.getChangesList();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("
TYPE");
stringBuilder.append(" ");
stringBuilder.append("
");
stringBuilder.append(changesList.size() > 0 ? changesList.get(0).getDataType() : "");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("
PRIMARY
KEY");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append(" ");
int index1 = 0;
for (Change change : changes.getChangesList()) {
ChangeType changeType = change.getChangeType();
String dataName = OutputType.getDataName(change);
List columnsNameList = change.getColumnsNameList();
Row rowAtStartPoint = change.getRowAtStartPoint();
Row rowAtEndPoint = change.getRowAtEndPoint();
StringBuilder[] pksValueStringBuilders = OutputType.getPksValueStringBuilder(change);
List typesList = OutputType.getTypesList(rowAtStartPoint, rowAtEndPoint);
stringBuilder.append("");
stringBuilder.append("Index : ");
stringBuilder.append(index1);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(changeType);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(dataName);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(pksValueStringBuilders[0]);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
int index = 0;
for (String columnName : columnsNameList) {
String pk = "";
if (change.getPksNameList().contains(columnName)) {
pk = "*";
}
stringBuilder.append("");
stringBuilder.append(pk);
stringBuilder.append("
");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(typesList.get(index));
stringBuilder.append("
Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
index++;
}
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("At start point");
stringBuilder.append(" ");
if (change.getRowAtStartPoint() == null) {
Iterator iterator = change.getColumnsNameList().iterator();
while (iterator.hasNext()) {
iterator.next();
stringBuilder.append("");
stringBuilder.append(" ");
}
} else {
for (Value value : change.getRowAtStartPoint().getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
}
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("At end point");
stringBuilder.append(" ");
if (change.getRowAtEndPoint() == null) {
Iterator iterator = change.getColumnsNameList().iterator();
while (iterator.hasNext()) {
iterator.next();
stringBuilder.append("");
stringBuilder.append(" ");
}
} else {
for (Value value : change.getRowAtEndPoint().getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
}
stringBuilder.append(" ");
stringBuilder.append("
");
stringBuilder.append(" ");
stringBuilder.append(" ");
index1++;
}
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getChangeOutput(WritableAssertionInfo info, Change change) {
ChangeType changeType = change.getChangeType();
DataType dataType = change.getDataType();
String dataName = OutputType.getDataName(change);
List columnsNameList = change.getColumnsNameList();
Row rowAtStartPoint = change.getRowAtStartPoint();
Row rowAtEndPoint = change.getRowAtEndPoint();
StringBuilder[] pksValueStringBuilders = OutputType.getPksValueStringBuilder(change);
List typesList = OutputType.getTypesList(rowAtStartPoint, rowAtEndPoint);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("
TYPE");
stringBuilder.append(" ");
stringBuilder.append("
");
stringBuilder.append(dataType);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("
PRIMARY
KEY");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(changeType);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(dataName);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(pksValueStringBuilders[0]);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
int index = 0;
for (String columnName : columnsNameList) {
String pk = "";
if (change.getPksNameList().contains(columnName)) {
pk = "*";
}
stringBuilder.append("");
stringBuilder.append(pk);
stringBuilder.append("
");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(typesList.get(index));
stringBuilder.append("
Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
index++;
}
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("At start point");
stringBuilder.append(" ");
if (change.getRowAtStartPoint() == null) {
Iterator iterator = change.getColumnsNameList().iterator();
while (iterator.hasNext()) {
iterator.next();
stringBuilder.append("");
stringBuilder.append(" ");
}
} else {
for (Value value : change.getRowAtStartPoint().getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
}
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("At end point");
stringBuilder.append(" ");
if (change.getRowAtEndPoint() == null) {
Iterator iterator = change.getColumnsNameList().iterator();
while (iterator.hasNext()) {
iterator.next();
stringBuilder.append("");
stringBuilder.append(" ");
}
} else {
for (Value value : change.getRowAtEndPoint().getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
}
stringBuilder.append(" ");
stringBuilder.append("
");
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getRowOutput(WritableAssertionInfo info, Row row) {
if (row == null) {
return getHtml(info, "Row does not exist");
}
List columnsNameList = row.getColumnsNameList();
List typesList = OutputType.getTypesList(row);
StringBuilder[] pksValueStringBuilders = OutputType.getPksValueStringBuilder(row);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("
PRIMARY
KEY");
stringBuilder.append(" ");
int index = 0;
for (String columnName : columnsNameList) {
String pk = "";
if (row.getPksNameList().contains(columnName)) {
pk = "*";
}
stringBuilder.append("");
stringBuilder.append(pk);
stringBuilder.append("
");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(typesList.get(index));
stringBuilder.append("
Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
index++;
}
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(pksValueStringBuilders[0]);
stringBuilder.append(" ");
for (Value value : row.getValuesList()) {
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
}
stringBuilder.append(" ");
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getColumnOutput(WritableAssertionInfo info, Column column) {
String columnName = column.getName();
List valuesList = column.getValuesList();
Value[] values = valuesList.toArray(new Value[valuesList.size()]);
String type = OutputType.getType(values);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(type);
stringBuilder.append(" ");
stringBuilder.append(" ");
int index = 0;
for (Value value : values) {
stringBuilder.append("");
stringBuilder.append("Index : ");
stringBuilder.append(index);
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
stringBuilder.append(" ");
index++;
}
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getChangeColumnOutput(WritableAssertionInfo info, String columnName,
Value valueAtStartPoint, Value valueAtEndPoint) {
String typeAtStartPoint = OutputType.getType(valueAtStartPoint);
String typeAtEndPoint = OutputType.getType(valueAtEndPoint);
String type = valueAtStartPoint.getValue() != null ? typeAtStartPoint : typeAtEndPoint;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(type);
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("At start point ");
stringBuilder.append("");
stringBuilder.append(OutputType.getText(valueAtStartPoint));
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("At end point ");
stringBuilder.append("");
stringBuilder.append(OutputType.getText(valueAtEndPoint));
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
/**
* {@inheritDoc}
*/
@Override
public String getValueOutput(WritableAssertionInfo info, Value value) {
String columnName = value.getColumnName();
String type = OutputType.getType(value);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(columnName);
stringBuilder.append("
");
stringBuilder.append(type);
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("");
stringBuilder.append("");
stringBuilder.append(OutputType.getText(value));
stringBuilder.append(" ");
stringBuilder.append(" ");
stringBuilder.append("
");
return getHtml(info, stringBuilder.toString());
}
}