Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
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 java.lang.reflect.Method;
import java.text.Normalizer;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.beanutils.PropertyUtils;
import org.specrunner.SpecRunnerServices;
import org.specrunner.context.IContext;
import org.specrunner.plugins.PluginException;
import org.specrunner.plugins.impl.AbstractPluginTable;
import org.specrunner.result.IResultSet;
import org.specrunner.result.Status;
import org.specrunner.util.UtilEvaluator;
import org.specrunner.util.UtilLog;
import org.specrunner.util.converter.ConverterException;
import org.specrunner.util.converter.IConverter;
import org.specrunner.util.converter.IConverterManager;
import org.specrunner.util.impl.CellAdapter;
import org.specrunner.util.impl.RowAdapter;
import org.specrunner.util.impl.TableAdapter;
/**
* Generic object plugin. To write object plugins override method
* isMapped() and action(...). i.e.
* SpecRunner-Hibernate3 extends the object manipulation to save data using
* Hibernate3 infra-structure.
*
* @author Thiago Santos
*
*/
public abstract class AbstractPluginObject extends AbstractPluginTable {
protected String type;
protected Class> typeInstance;
protected String creator;
protected IObjectCreator creatorInstance;
protected String reference;
protected String separator;
protected List fields = new LinkedList();
protected Map keysBefore = new HashMap();
protected Map instances = new HashMap();
/**
* The object type of the plugin. i.e.
* type='system.entity.Person'.
*
* @return The type.
*/
public String getType() {
return type;
}
/**
* Set the type.
*
* @param type
* A new type.
*/
public void setType(String type) {
this.type = type;
}
/**
* Gets the corresponding class to type.
*
* @return The class.
*/
public Class> getTypeInstance() {
return typeInstance;
}
/**
* Sets the instance type.
*
* @param typeInstance
* A new type.
*/
public void setTypeInstance(Class> typeInstance) {
this.typeInstance = typeInstance;
}
/**
* Returns the object creator of embeddable objects.
*
* @return The creator class name.
*/
public String getCreator() {
return creator;
}
/**
* Sets the creator class.
*
* @param creator
* A new creator class name.
*/
public void setCreator(String creator) {
this.creator = creator;
}
/**
* Object creator type.
*
* @return The creator instance.
*/
public IObjectCreator getCreatorInstance() {
return creatorInstance;
}
/**
* Sets the creator.
*
* @param creatorInstance
* A new creator instance.
*/
public void setCreatorInstance(IObjectCreator creatorInstance) {
this.creatorInstance = creatorInstance;
}
/**
* Sets the attribute which represents the keys of the entity. These fields
* are used to create the instance key. i.e. if
* reference='id,name' is used, the 'id' attribute and 'name'
* attribute are used as the object key, separated by 'separator' field.
*
* @return The list of attribute references in the expected order.
*/
public String getReference() {
return reference;
}
/**
* Sets the references.
*
* @param reference
* A new reference list.
*/
public void setReference(String reference) {
this.reference = reference;
}
/**
* Gets the string used to separate fields of reference. i.e. if
* reference='id,name' and separator='/', the
* object instance corresponding to a given line will be 'id/name'.
*
* @return The keys separator.
*/
public String getSeparator() {
return separator;
}
public void setSeparator(String separator) {
this.separator = separator;
}
@Override
public void initialize(IContext context) throws PluginException {
try {
if (type != null) {
typeInstance = Class.forName(type);
}
if (creator != null) {
creatorInstance = (IObjectCreator) Class.forName(creator).newInstance();
}
} catch (Exception e) {
if (UtilLog.LOG.isDebugEnabled()) {
UtilLog.LOG.debug(e.getMessage(), e);
}
throw new PluginException(e);
}
}
@Override
public void doEnd(IContext context, IResultSet result, TableAdapter table) throws PluginException {
if (isMapped()) {
PluginObjectManager.get().bind(this);
}
for (int i = 0; i < table.getRowCount(); i++) {
if (i == 0) {
try {
loadFields(context, table.getRow(i));
result.addResult(Status.SUCCESS, context.newBlock(table.getRow(i).getElement(), this));
} catch (Exception e) {
if (UtilLog.LOG.isDebugEnabled()) {
UtilLog.LOG.debug(e.getMessage(), e);
}
result.addResult(Status.FAILURE, context.newBlock(table.getRow(i).getElement(), this), e);
break;
}
} else {
try {
processLine(context, table.getRow(i), result);
} catch (Exception e) {
if (UtilLog.LOG.isDebugEnabled()) {
UtilLog.LOG.debug(e.getMessage(), e);
}
result.addResult(Status.FAILURE, context.newBlock(table.getRow(i).getElement(), this), e);
}
}
}
}
/**
* Load fields based on th tags.
*
* @param row
* @throws Exception
*/
protected void loadFields(IContext context, RowAdapter row) throws Exception {
int index = 0;
for (CellAdapter cell : row.getCells()) {
String name;
if (cell.hasAttribute("field")) {
name = cell.getAttribute("field");
} else {
name = normalize(cell.getValue());
}
StringTokenizer st = new StringTokenizer(name, ".");
String[] names = new String[st.countTokens()];
for (int i = 0; i < names.length; i++) {
names[i] = st.nextToken();
}
Class>[] types = new Class>[names.length];
Class> currentType = typeInstance;
for (int i = 0; i < types.length; i++) {
Method m = null;
try {
m = currentType.getMethod("get" + Character.toUpperCase(names[i].charAt(0)) + names[i].substring(1));
} catch (Exception e) {
m = currentType.getMethod("is" + Character.toUpperCase(names[i].charAt(0)) + names[i].substring(1));
}
if (m == null) {
throw new PluginException("Getter method for " + names[i] + " not found.");
}
types[i] = m.getReturnType();
currentType = types[i];
}
String def = null;
if (cell.hasAttribute("default")) {
def = cell.getAttribute("default");
}
String converter = cell.hasAttribute("converter") ? cell.getAttribute("converter") : null;
int i = 0;
List