Skip to content

Commit 4af434b

Browse files
Remove HibernateFlusher and HibernateDaoSupport dependency from unit tests
1 parent e3a460a commit 4af434b

File tree

14 files changed

+842
-920
lines changed

14 files changed

+842
-920
lines changed

src/main/java/org/wise/portal/dao/impl/AbstractHibernateDao.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2008-2022 Regents of the University of California (Regents).
2+
* Copyright (c) 2008-2025 Regents of the University of California (Regents).
33
* Created by WISE, Graduate School of Education, University of California, Berkeley.
44
*
55
* This software is distributed under the GNU General Public License, v3,
@@ -33,8 +33,6 @@
3333
import javax.persistence.criteria.CriteriaQuery;
3434

3535
import org.hibernate.SessionFactory;
36-
import org.springframework.beans.factory.annotation.Autowired;
37-
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
3836
import org.springframework.transaction.annotation.Transactional;
3937
import org.wise.portal.dao.ObjectNotFoundException;
4038
import org.wise.portal.dao.SimpleDao;
@@ -43,20 +41,14 @@
4341
* @author Cynick Young
4442
* @author Hiroki Terashima
4543
*/
46-
public abstract class AbstractHibernateDao<T> extends HibernateDaoSupport implements SimpleDao<T> {
44+
public abstract class AbstractHibernateDao<T> implements SimpleDao<T> {
4745

4846
@PersistenceContext
4947
protected EntityManager entityManager;
5048

5149
@Resource
5250
private SessionFactory sessionFactory;
5351

54-
@Autowired
55-
@Transactional
56-
public void init() {
57-
setSessionFactory(sessionFactory);
58-
}
59-
6052
@Transactional
6153
public void delete(T object) {
6254
sessionFactory.getCurrentSession().delete(object);

src/main/java/org/wise/portal/dao/project/impl/HibernateTagDao.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.wise.portal.dao.project.impl;
2525

2626
import java.util.List;
27+
import java.util.Set;
2728

2829
import javax.persistence.TypedQuery;
2930
import javax.persistence.criteria.CriteriaBuilder;
@@ -36,6 +37,7 @@
3637
import org.wise.portal.dao.project.TagDao;
3738
import org.wise.portal.domain.Tag;
3839
import org.wise.portal.domain.impl.TagImpl;
40+
import org.wise.portal.domain.project.impl.ProjectImpl;
3941
import org.wise.portal.domain.run.Run;
4042

4143
/**
@@ -54,26 +56,28 @@ protected Class<? extends Tag> getDataObjectClass() {
5456
*/
5557
@SuppressWarnings("unchecked")
5658
public Tag getTagByName(String name) {
57-
List<Tag> tags = (List<Tag>) this.getHibernateTemplate()
58-
.find("select tag from TagImpl tag where tag.name='" + name + "'");
59-
59+
CriteriaBuilder cb = getCriteriaBuilder();
60+
CriteriaQuery<TagImpl> cq = cb.createQuery(TagImpl.class);
61+
Root<TagImpl> tagRoot = cq.from(TagImpl.class);
62+
cq.select(tagRoot).where(cb.equal(tagRoot.get("name"), name));
63+
TypedQuery<TagImpl> query = entityManager.createQuery(cq);
64+
List<TagImpl> tags = query.getResultList();
6065
if (tags.size() == 0) {
6166
return null;
6267
} else {
6368
return tags.get(0);
6469
}
6570
}
6671

67-
/**
68-
*
69-
* @param tagId
70-
*/
71-
@SuppressWarnings("unchecked")
7272
public void removeIfOrphaned(Integer tagId) {
73-
List<Tag> projects = (List<Tag>) this.getHibernateTemplate()
74-
.find("select project from ProjectImpl project inner join project.tags tag where tag.id="
75-
+ tagId);
76-
73+
CriteriaBuilder cb = getCriteriaBuilder();
74+
CriteriaQuery<ProjectImpl> cq = cb.createQuery(ProjectImpl.class);
75+
Root<ProjectImpl> projectRoot = cq.from(ProjectImpl.class);
76+
Root<TagImpl> tagRoot = cq.from(TagImpl.class);
77+
cq.select(projectRoot).where(cb.and(cb.equal(tagRoot.get("id"), tagId),
78+
cb.isMember(tagRoot, projectRoot.<Set<TagImpl>> get("tags")))).distinct(true);
79+
TypedQuery<ProjectImpl> query = entityManager.createQuery(cq);
80+
List<ProjectImpl> projects = query.getResultList();
7781
if (projects.size() == 0) {
7882
try {
7983
Tag tag = this.getById(tagId);

src/main/java/org/wise/portal/presentation/web/controllers/admin/FindProjectRunsController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ protected String findRun(
6666
if ("runId".equals(runLookupType)) {
6767
runList = getRunListByRunId(Long.parseLong(runLookupValue));
6868
} else if ("projectId".equals(runLookupType)) {
69-
// can be re-written using JPA?
7069
runList = getRunListByProjectId(Long.parseLong(runLookupValue));
7170
} else if ("teacherUsername".equals(runLookupType)) {
7271
runList = getRunListByUsername(runLookupValue);

src/test/java/org/wise/portal/dao/AbstractTransactionalDaoTests.java

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -30,91 +30,90 @@
3030
* @author Cynick Young
3131
*/
3232
public abstract class AbstractTransactionalDaoTests<DAO extends SimpleDao<OBJECT>, OBJECT extends Persistable>
33-
extends AbstractTransactionalDbTests {
34-
35-
protected DAO dao;
36-
37-
protected OBJECT dataObject;
38-
39-
private static final Long NON_EXISTENT_PK = new Long(666);
40-
41-
/**
42-
* Test method for
43-
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#delete(java.lang.Object)}.
44-
*/
45-
public void testDelete() {
46-
this.verifyDataStoreIsEmpty();
47-
48-
// save and delete the data object using dao
49-
this.dao.save(this.dataObject);
50-
this.dao.delete(this.dataObject);
51-
52-
// * NOTE * must flush to test delete
53-
// see http://forum.springframework.org/showthread.php?t=18263 for
54-
// explanation
55-
this.toilet.flush();
56-
57-
this.verifyDataStoreIsEmpty();
58-
}
59-
60-
/**
61-
* Test method for
62-
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#save(java.lang.Object)}.
63-
*/
64-
public abstract void testSave();
65-
66-
/**
67-
* Test method for
68-
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#getList()}.
69-
*/
70-
public void testGetList() {
71-
this.verifyDataStoreIsEmpty();
72-
List<OBJECT> actualList = this.dao.getList();
73-
assertTrue(actualList.isEmpty());
74-
75-
this.dao.save(this.dataObject);
76-
List<?> expectedList = this.retrieveDataObjectListFromDb();
77-
assertEquals(1, expectedList.size());
78-
79-
actualList = this.dao.getList();
80-
assertEquals(1, actualList.size());
81-
assertEquals(this.dataObject, actualList.get(0));
82-
}
83-
84-
/**
85-
* Test method for
86-
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#getById(java.lang.Long)}.
87-
*/
88-
public void testGetById() throws Exception {
89-
this.verifyDataStoreIsEmpty();
90-
try {
91-
this.dao.getById(NON_EXISTENT_PK);
92-
fail("Expected ObjectNotFoundException");
93-
} catch (ObjectNotFoundException e) {
94-
95-
}
96-
this.dao.save(this.dataObject);
97-
List<OBJECT> actualList = this.dao.getList();
98-
OBJECT actualObject = actualList.get(0);
99-
100-
assertTrue(this.dataObject.getId() instanceof Long);
101-
assertEquals(actualObject, this.dao.getById((Long) this.dataObject
102-
.getId()));
103-
}
104-
105-
protected final void verifyDataStoreIsEmpty() {
106-
assertTrue(retrieveDataObjectListFromDb().isEmpty());
107-
}
108-
109-
protected abstract List<?> retrieveDataObjectListFromDb();
110-
111-
/**
112-
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction()
113-
*/
114-
@Override
115-
protected void onTearDownAfterTransaction() throws Exception {
116-
super.onTearDownAfterTransaction();
117-
this.dao = null;
118-
this.dataObject = null;
119-
}
33+
extends AbstractTransactionalDbTests {
34+
35+
protected DAO dao;
36+
37+
protected OBJECT dataObject;
38+
39+
private static final Long NON_EXISTENT_PK = new Long(666);
40+
41+
/**
42+
* Test method for
43+
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#delete(java.lang.Object)}.
44+
*/
45+
public void testDelete() {
46+
this.verifyDataStoreIsEmpty();
47+
48+
// save and delete the data object using dao
49+
this.dao.save(this.dataObject);
50+
this.dao.delete(this.dataObject);
51+
52+
// * NOTE * must flush to test delete
53+
// see http://forum.springframework.org/showthread.php?t=18263 for
54+
// explanation
55+
this.flush();
56+
57+
this.verifyDataStoreIsEmpty();
58+
}
59+
60+
/**
61+
* Test method for
62+
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#save(java.lang.Object)}.
63+
*/
64+
public abstract void testSave();
65+
66+
/**
67+
* Test method for
68+
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#getList()}.
69+
*/
70+
public void testGetList() {
71+
this.verifyDataStoreIsEmpty();
72+
List<OBJECT> actualList = this.dao.getList();
73+
assertTrue(actualList.isEmpty());
74+
75+
this.dao.save(this.dataObject);
76+
List<?> expectedList = this.retrieveDataObjectListFromDb();
77+
assertEquals(1, expectedList.size());
78+
79+
actualList = this.dao.getList();
80+
assertEquals(1, actualList.size());
81+
assertEquals(this.dataObject, actualList.get(0));
82+
}
83+
84+
/**
85+
* Test method for
86+
* {@link org.wise.portal.dao.impl.AbstractHibernateDao#getById(java.lang.Long)}.
87+
*/
88+
public void testGetById() throws Exception {
89+
this.verifyDataStoreIsEmpty();
90+
try {
91+
this.dao.getById(NON_EXISTENT_PK);
92+
fail("Expected ObjectNotFoundException");
93+
} catch (ObjectNotFoundException e) {
94+
95+
}
96+
this.dao.save(this.dataObject);
97+
List<OBJECT> actualList = this.dao.getList();
98+
OBJECT actualObject = actualList.get(0);
99+
100+
assertTrue(this.dataObject.getId() instanceof Long);
101+
assertEquals(actualObject, this.dao.getById((Long) this.dataObject.getId()));
102+
}
103+
104+
protected final void verifyDataStoreIsEmpty() {
105+
assertTrue(retrieveDataObjectListFromDb().isEmpty());
106+
}
107+
108+
protected abstract List<?> retrieveDataObjectListFromDb();
109+
110+
/**
111+
* @see org.springframework.test.AbstractTransactionalSpringContextTests#onTearDownAfterTransaction()
112+
*/
113+
@Override
114+
protected void onTearDownAfterTransaction() throws Exception {
115+
super.onTearDownAfterTransaction();
116+
this.dao = null;
117+
this.dataObject = null;
118+
}
120119
}

0 commit comments

Comments
 (0)