
org.specrunner.objects.AbstractPluginObjectCompare Maven / Gradle / Ivy
/*
SpecRunner - Acceptance Test Driven Development Tool
Copyright (C) 2011-2012 Thiago Santos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
*/
package org.specrunner.objects;
import org.apache.commons.beanutils.PropertyUtils;
import org.specrunner.SpecRunnerServices;
import org.specrunner.context.IContext;
import org.specrunner.plugins.PluginException;
import org.specrunner.result.IResultSet;
import org.specrunner.result.Status;
import org.specrunner.util.UtilLog;
import org.specrunner.util.aligner.IStringAligner;
import org.specrunner.util.aligner.IStringAlignerFactory;
import org.specrunner.util.aligner.impl.DefaultAlignmentException;
import org.specrunner.util.comparer.IComparator;
import org.specrunner.util.comparer.IComparatorManager;
import org.specrunner.util.impl.CellAdapter;
import org.specrunner.util.impl.RowAdapter;
/**
* Performs object after some system iteraction.
*
* @author Thiago Santos
*
*/
public abstract class AbstractPluginObjectCompare extends AbstractPluginObject {
@Override
public boolean isMapped() {
return false;
}
@Override
protected void action(IContext context, Object instance, RowAdapter row, IResultSet result) throws Exception {
if (UtilLog.LOG.isDebugEnabled()) {
UtilLog.LOG.debug("KEYS>" + reference);
}
try {
Object base = selectUnique(context, instance, row, result);
if (base == null) {
Exception e = new PluginException("This register is not present in object repository. XML:" + row.getElement().toXML());
for (int i = 0; i < row.getCellsCount(); i++) {
result.addResult(i == 0 ? Status.FAILURE : Status.WARNING, context.newBlock(row.getCell(i).getElement(), this), i == 0 ? e : null);
}
} else {
compare(context, base, instance, row, result);
}
} finally {
release();
}
}
/**
* Performs a select on object repository to compare with the reference.
*
* @param context
* The test context.
* @param instance
* The object to be compared with repository version.
* @param row
* The row which was the source for object creation.
* @param result
* The result set.
* @return The corresponding object from repository.
* @throws Exception
* On selecion errors.
*/
public abstract Object selectUnique(IContext context, Object instance, RowAdapter row, IResultSet result) throws Exception;
/**
* Release comparation resources. i.e. For Hibernate repositories free
* sessions in use for comparation.
*
* @throws Exception
* On release errors.
*/
public abstract void release() throws Exception;
/**
* Default comparation processing.
*
* @param context
* The test context.
* @param base
* The object version from repository.
* @param instance
* The object version from specification.
* @param row
* The row which give origin to the 'instance'.
* @param result
* The result set.
* @throws Exception
* On comparation errors.
*/
public void compare(IContext context, Object base, Object instance, RowAdapter row, IResultSet result) throws Exception {
for (Field f : fields) {
Object currentInstance = instance;
for (int i = 0; i < f.getNames().length; i++) {
currentInstance = PropertyUtils.getProperty(currentInstance, f.getNames()[i]);
if (currentInstance == null) {
break;
}
}
Object currentBase = base;
for (int i = 0; i < f.getNames().length; i++) {
currentBase = PropertyUtils.getProperty(currentBase, f.getNames()[i]);
if (currentBase == null) {
break;
}
}
// lookup for best match comparator
IComparatorManager cf = SpecRunnerServices.get(IComparatorManager.class);
IComparator comparator = null;
// specific cell comparator
CellAdapter cell = row.getCell(f.getIndex());
if (cell.hasAttribute("comparator")) {
comparator = cf.get(cell.getAttribute("comparator"));
}
// by column comparator
if (comparator == null && f.getComparator() != null) {
comparator = cf.get(f.getComparator());
}
// by object type
if (comparator == null) {
Class> type = currentInstance.getClass();
comparator = cf.get(type);
}
// by default
if (comparator == null) {
comparator = cf.getDefaultComparator();
}
if (comparator.equals(currentInstance, currentBase)) {
result.addResult(Status.SUCCESS, context.newBlock(row.getCell(f.getIndex()).getElement(), this));
} else {
IStringAligner aligner = SpecRunnerServices.get(IStringAlignerFactory.class).align(String.valueOf(currentInstance), String.valueOf(currentBase));
result.addResult(Status.FAILURE, context.newBlock(row.getCell(f.getIndex()).getElement(), this), new DefaultAlignmentException(aligner));
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy