private Object execute(String assertion, Query query,
String singleStringQuery, boolean hasOrdering,
Object parameters, Object expectedResult, boolean positive) {
Object result = null;
PersistenceManager pm = getPM();
Transaction tx = pm.currentTransaction();
tx.begin();
try {
try {
if (parameters == null) {
result = query.execute();
} else if (parameters instanceof Object[]) {
result = query.executeWithArray((Object[])parameters);
} else if (parameters instanceof Map) {
result = query.executeWithMap((Map)parameters);
} else if (parameters instanceof List) {
List list = (List) parameters;
switch (list.size()) {
case 1:
result = query.execute(list.get(0));
break;
case 2:
result = query.execute(list.get(0), list.get(1));
break;
case 3:
result = query.execute(
list.get(0), list.get(1), list.get(2));
break;
default:
throw new IllegalArgumentException(
"Argument parameters is a list " +
"and must have 1, 2, or 3 elements.");
}
} else {
throw new IllegalArgumentException("Argument parameters " +
"must be instance of List, Map, Object[], or null.");
}
if (logger.isDebugEnabled()) {
logger.debug("Query result: " + ConversionHelper.
convertObjectArrayElements(result));
}
if (positive) {
if (hasOrdering) {
checkQueryResultWithOrder(assertion, result,
expectedResult);
} else {
checkQueryResultWithoutOrder(assertion, result,
expectedResult);
}
} else {
fail(assertion, "Query must throw JDOUserException: " +
singleStringQuery);
}
} finally {
query.close(result);
}
} catch (JDOUserException e) {
if (positive) {
throw e;
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
return result;
}