org.unitils.reflectionassert.report.impl.TreeDifferenceView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unitils Show documentation
Show all versions of unitils Show documentation
Unitils provides utilities to further simplify unit-testing with JUnit, DBUnit, EasyMock
Hibernate and Spring. The goal is to make unit-testing easy and maintainable by offering
utilities such as automatic DB-schema maintainance and equality assertion through reflection.
/*
* Copyright 2008, Unitils.org
*
* 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.
*/
package org.unitils.reflectionassert.report.impl;
import org.unitils.reflectionassert.difference.*;
import org.unitils.reflectionassert.report.DifferenceView;
import static org.unitils.reflectionassert.report.impl.DefaultDifferenceReport.MatchType.NO_MATCH;
import org.unitils.core.util.ObjectFormatter;
import static org.apache.commons.lang.ClassUtils.getShortClassName;
import java.util.List;
import java.util.Map;
/**
* Formatter that will output all objects in the difference tree. For an unordered collection difference,
* the best matching differences are taken.
*
* @author Tim Ducheyne
* @author Filip Neven
*/
public class TreeDifferenceView implements DifferenceView {
/**
* Formatter for object values.
*/
protected ObjectFormatter objectFormatter = new ObjectFormatter();
/**
* The visitor for visiting the difference tree
*/
protected TreeDifferenceFormatterVisitor treeDifferenceFormatterVisitor = new TreeDifferenceFormatterVisitor();
/**
* Creates a string representation of the given difference tree.
*
* @param difference The root difference, not null
* @return The string representation, not null
*/
public String createView(Difference difference) {
return difference.accept(treeDifferenceFormatterVisitor, null);
}
/**
* Creates a string representation of a simple difference.
*
* @param difference The difference, not null
* @param fieldName The current fieldName, null for root
* @return The string representation, not null
*/
protected String formatDifference(Difference difference, String fieldName) {
return formatValues(fieldName, difference.getLeftValue(), difference.getRightValue());
}
/**
* Creates a string representation of an object difference.
*
* @param objectDifference The difference, not null
* @param fieldName The current fieldName, null for root
* @return The string representation, not null
*/
protected String formatDifference(ObjectDifference objectDifference, String fieldName) {
StringBuilder result = new StringBuilder();
result.append(formatDifference((Difference) objectDifference, fieldName));
for (Map.Entry fieldDifference : objectDifference.getFieldDifferences().entrySet()) {
String innerFieldName = createFieldName(fieldName, fieldDifference.getKey(), true);
result.append(fieldDifference.getValue().accept(treeDifferenceFormatterVisitor, innerFieldName));
}
return result.toString();
}
protected String formatDifference(ClassDifference classDifference, String fieldName) {
StringBuilder result = new StringBuilder();
result.append((fieldName == null) ? "" : fieldName + ":");
result.append("Expected: object of type ").append(getShortClassName(classDifference.getLeftClass()));
result.append(", actual: object of type ").append(getShortClassName(classDifference.getRightClass())).append("\n");
return result.toString();
}
/**
* Creates a string representation of a collection difference.
*
* @param collectionDifference The difference, not null
* @param fieldName The current fieldName, null for root
* @return The string representation, not null
*/
protected String formatDifference(CollectionDifference collectionDifference, String fieldName) {
StringBuilder result = new StringBuilder();
result.append(formatDifference((Difference) collectionDifference, fieldName));
for (Map.Entry elementDifferences : collectionDifference.getElementDifferences().entrySet()) {
String innerFieldName = createFieldName(fieldName, "[" + elementDifferences.getKey() + "]", false);
result.append(elementDifferences.getValue().accept(treeDifferenceFormatterVisitor, innerFieldName));
}
List> leftList = collectionDifference.getLeftList();
List> rightList = collectionDifference.getRightList();
for (Integer leftIndex : collectionDifference.getLeftMissingIndexes()) {
String innerFieldName = createFieldName(fieldName, "[" + leftIndex + "]", false);
result.append(formatValues(innerFieldName, leftList.get(leftIndex), ""));
}
for (Integer rightIndex : collectionDifference.getRightMissingIndexes()) {
String innerFieldName = createFieldName(fieldName, "[" + rightIndex + "]", false);
result.append(formatValues(innerFieldName, "", rightList.get(rightIndex)));
}
return result.toString();
}
/**
* Creates a string representation of a map difference.
*
* @param mapDifference The difference, not null
* @param fieldName The current fieldName, null for root
* @return The string representation, not null
*/
protected String formatDifference(MapDifference mapDifference, String fieldName) {
StringBuilder result = new StringBuilder();
result.append(formatDifference((Difference) mapDifference, fieldName));
for (Map.Entry