DS-3086: Fixed the unit tests

This commit is contained in:
Tom Desair
2016-04-08 11:21:11 +02:00
committed by dylan
parent 8db0faed33
commit ec8eee1ba5
6 changed files with 36 additions and 49 deletions

View File

@@ -74,7 +74,7 @@ public class MetadataValue implements ReloadableEntity<Integer>
*/ */
protected MetadataValue() protected MetadataValue()
{ {
id = 0;
} }
/** /**

View File

@@ -201,7 +201,7 @@ public class ItemDAOImpl extends AbstractHibernateDSODAO<Item> implements ItemDA
@Override @Override
public Iterator<Item> findByAuthorityValue(Context context, MetadataField metadataField, String authority, boolean inArchive) throws SQLException { public Iterator<Item> findByAuthorityValue(Context context, MetadataField metadataField, String authority, boolean inArchive) throws SQLException {
Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalueq = :authority"); Query query = createQuery(context, "SELECT item FROM Item as item join item.metadata metadatavalue WHERE item.inArchive=:in_archive AND metadatavalue.metadataField = :metadata_field AND metadatavalue.authority = :authority");
query.setParameter("in_archive", inArchive); query.setParameter("in_archive", inArchive);
query.setParameter("metadata_field", metadataField); query.setParameter("metadata_field", metadataField);
query.setParameter("authority", authority); query.setParameter("authority", authority);

View File

@@ -618,7 +618,7 @@ public class Context
* If a context is garbage-collected, we roll back and free up the * If a context is garbage-collected, we roll back and free up the
* database connection if there is one. * database connection if there is one.
*/ */
if (dbConnection.isTransActionAlive()) if (dbConnection != null && dbConnection.isTransActionAlive())
{ {
abort(); abort();
} }

View File

@@ -11,14 +11,13 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils; import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dspace.content.MetadataField; import org.dspace.content.MetadataField;
import org.dspace.core.Context;
import org.dspace.core.AbstractHibernateDSODAO; import org.dspace.core.AbstractHibernateDSODAO;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson; import org.dspace.eperson.EPerson;
import org.dspace.eperson.Group; import org.dspace.eperson.Group;
import org.dspace.eperson.dao.EPersonDAO; import org.dspace.eperson.dao.EPersonDAO;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.Query; import org.hibernate.Query;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Restrictions;
import java.sql.SQLException; import java.sql.SQLException;
@@ -41,35 +40,25 @@ public class EPersonDAOImpl extends AbstractHibernateDSODAO<EPerson> implements
@Override @Override
public EPerson findByEmail(Context context, String email) throws SQLException public EPerson findByEmail(Context context, String email) throws SQLException
{ {
Query query = createQuery(context, // All email addresses are stored as lowercase, so ensure that the email address is lowercased for the lookup
"SELECT p " + Criteria criteria = createCriteria(context, EPerson.class);
"FROM EPerson p " + criteria.add(Restrictions.eq("email", email.toLowerCase()));
"WHERE p.email = :email" ); return uniqueResult(criteria);
query.setParameter("email", email.toLowerCase());
return uniqueResult(query);
} }
@Override @Override
public EPerson findByNetid(Context context, String netid) throws SQLException public EPerson findByNetid(Context context, String netid) throws SQLException
{ {
Query query = createQuery(context, Criteria criteria = createCriteria(context, EPerson.class);
"SELECT p " + criteria.add(Restrictions.eq("netid", netid));
"FROM EPerson p " + return uniqueResult(criteria);
"WHERE p.netid = :netid" );
query.setParameter("netid", netid);
return uniqueResult(query);
} }
@Override @Override
public List<EPerson> search(Context context, String query, List<MetadataField> queryFields, List<MetadataField> sortFields, int offset, int limit) throws SQLException public List<EPerson> search(Context context, String query, List<MetadataField> queryFields, List<MetadataField> sortFields, int offset, int limit) throws SQLException
{ {
String queryString = "SELECT " + EPerson.class.getSimpleName().toLowerCase() + " FROM EPerson as " + EPerson.class.getSimpleName().toLowerCase() + " "; String queryString = "SELECT " + EPerson.class.getSimpleName().toLowerCase() + " FROM EPerson as " + EPerson.class.getSimpleName().toLowerCase() + " ";
if(query != null) query= "%"+query.toLowerCase()+"%"; if(query != null) query= "%"+query.toLowerCase()+"%";
Query hibernateQuery = getSearchQuery(context, queryString, query, queryFields, sortFields, null); Query hibernateQuery = getSearchQuery(context, queryString, query, queryFields, sortFields, null);
@@ -110,37 +99,36 @@ public class EPersonDAOImpl extends AbstractHibernateDSODAO<EPerson> implements
@Override @Override
public List<EPerson> findByGroups(Context context, Set<Group> groups) throws SQLException { public List<EPerson> findByGroups(Context context, Set<Group> groups) throws SQLException {
Criteria criteria = createCriteria(context, EPerson.class); Query query = createQuery(context,
criteria.createAlias("groups", "g"); "SELECT DISTINCT e FROM EPerson e " +
Disjunction orRestriction = Restrictions.or(); "JOIN e.groups g " +
for(Group group : groups) "WHERE g.id IN (:idList)");
{
orRestriction.add(Restrictions.eq("g.id", group.getID()));
}
criteria.add(orRestriction);
return list(criteria);
}
List<UUID> idList = new ArrayList<>(groups.size());
for (Group group : groups) {
idList.add(group.getID());
}
query.setParameterList("idList", idList);
return list(query);
}
@Override @Override
public List<EPerson> findWithPasswordWithoutDigestAlgorithm(Context context) throws SQLException { public List<EPerson> findWithPasswordWithoutDigestAlgorithm(Context context) throws SQLException {
Query query = createQuery(context, Criteria criteria = createCriteria(context, EPerson.class);
"SELECT p " + criteria.add(Restrictions.and(
"FROM EPerson p " + Restrictions.isNotNull("password"),
"WHERE p.password IS NOT NULL AND p.digestAlgorithm IS NULL " ); Restrictions.isNull("digestAlgorithm")
));
return list(query); return list(criteria);
} }
@Override @Override
public List<EPerson> findNotActiveSince(Context context, Date date) throws SQLException { public List<EPerson> findNotActiveSince(Context context, Date date) throws SQLException {
Query query = createQuery(context, Criteria criteria = createCriteria(context, EPerson.class);
"SELECT p " + criteria.add(Restrictions.le("lastActive", date));
"FROM EPerson p " + return list(criteria);
"WHERE p.lastActive <= :date " );
query.setParameter("date", date);
return list(query);
} }
protected Query getSearchQuery(Context context, String queryString, String queryParam, List<MetadataField> queryFields, List<MetadataField> sortFields, String sortField) throws SQLException { protected Query getSearchQuery(Context context, String queryString, String queryParam, List<MetadataField> queryFields, List<MetadataField> sortFields, String sortField) throws SQLException {
@@ -174,7 +162,7 @@ public class EPersonDAOImpl extends AbstractHibernateDSODAO<EPerson> implements
@Override @Override
public List<EPerson> findAllSubscribers(Context context) throws SQLException { public List<EPerson> findAllSubscribers(Context context) throws SQLException {
return list(createQuery(context, "SELECT DISTINCT e from Subscription s JOIN s.ePerson e ")); return list(createQuery(context, "SELECT DISTINCT e from Subscription s join s.ePerson e"));
} }
@Override @Override

View File

@@ -42,7 +42,7 @@ public class HandleDAOImpl extends AbstractHibernateDAO<Handle> implements Handl
Query query = createQuery(context, Query query = createQuery(context,
"SELECT h " + "SELECT h " +
"FROM Handle h " + "FROM Handle h " +
"JOIN FETCH h.dso " + "LEFT JOIN FETCH h.dso " +
"WHERE h.dso.id = :id "); "WHERE h.dso.id = :id ");
query.setParameter("id", dso.getID()); query.setParameter("id", dso.getID());
@@ -57,7 +57,7 @@ public class HandleDAOImpl extends AbstractHibernateDAO<Handle> implements Handl
Query query = createQuery(context, Query query = createQuery(context,
"SELECT h " + "SELECT h " +
"FROM Handle h " + "FROM Handle h " +
"JOIN FETCH h.dso " + "LEFT JOIN FETCH h.dso " +
"WHERE h.handle = :handle "); "WHERE h.handle = :handle ");
query.setParameter("handle", handle); query.setParameter("handle", handle);

View File

@@ -90,7 +90,6 @@ public class InstallItemTest extends AbstractUnitTest
context.turnOffAuthorisationSystem(); context.turnOffAuthorisationSystem();
communityService.delete(context, owningCommunity); communityService.delete(context, owningCommunity);
context.restoreAuthSystemState(); context.restoreAuthSystemState();
context.commit();
} catch (SQLException | AuthorizeException | IOException ex) { } catch (SQLException | AuthorizeException | IOException ex) {
log.error("SQL Error in destroy", ex); log.error("SQL Error in destroy", ex);
fail("SQL Error in destroy: " + ex.getMessage()); fail("SQL Error in destroy: " + ex.getMessage());