mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Unit test Suite for DAOs and some extra content clean tests and fallout
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14838 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
58
source/java/org/alfresco/repo/domain/DomainTestSuite.java
Normal file
58
source/java/org/alfresco/repo/domain/DomainTestSuite.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2009 Alfresco Software Limited.
|
||||
*
|
||||
* 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 2
|
||||
* 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.domain;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.alfresco.repo.domain.contentdata.ContentDataDAOTest;
|
||||
import org.alfresco.repo.domain.encoding.EncodingDAOTest;
|
||||
import org.alfresco.repo.domain.hibernate.HibernateSessionHelperTest;
|
||||
import org.alfresco.repo.domain.locks.LockDAOTest;
|
||||
import org.alfresco.repo.domain.mimetype.MimetypeDAOTest;
|
||||
|
||||
/**
|
||||
* Suite for domain-related tests.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class DomainTestSuite extends TestSuite
|
||||
{
|
||||
public static Test suite()
|
||||
{
|
||||
TestSuite suite = new TestSuite();
|
||||
|
||||
suite.addTestSuite(ContentDataDAOTest.class);
|
||||
suite.addTestSuite(EncodingDAOTest.class);
|
||||
suite.addTestSuite(HibernateSessionHelperTest.class);
|
||||
suite.addTestSuite(LockDAOTest.class);
|
||||
suite.addTestSuite(MimetypeDAOTest.class);
|
||||
suite.addTestSuite(LocaleDAOTest.class);
|
||||
suite.addTestSuite(PropertyValueTest.class);
|
||||
suite.addTestSuite(QNameDAOTest.class);
|
||||
|
||||
return suite;
|
||||
}
|
||||
}
|
@@ -26,12 +26,16 @@ package org.alfresco.repo.domain.contentdata;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import org.alfresco.repo.cache.SimpleCache;
|
||||
import org.alfresco.repo.content.cleanup.EagerContentStoreCleaner;
|
||||
import org.alfresco.repo.domain.LocaleDAO;
|
||||
import org.alfresco.repo.domain.encoding.EncodingDAO;
|
||||
import org.alfresco.repo.domain.mimetype.MimetypeDAO;
|
||||
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
|
||||
import org.alfresco.repo.transaction.TransactionListenerAdapter;
|
||||
import org.alfresco.repo.transaction.TransactionalResourceHelper;
|
||||
import org.alfresco.service.cmr.repository.ContentData;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -52,9 +56,13 @@ import org.springframework.dao.ConcurrencyFailureException;
|
||||
*/
|
||||
public abstract class AbstractContentDataDAOImpl implements ContentDataDAO
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(AbstractContentDataDAOImpl.class);
|
||||
|
||||
/**
|
||||
* Content URL IDs to delete before final commit.
|
||||
*/
|
||||
private static final String KEY_PRE_COMMIT_CONTENT_URL_DELETIONS = "AbstractContentDataDAOImpl.PreCommitContentUrlDeletions";
|
||||
private static final Long CACHE_NULL_LONG = Long.MIN_VALUE;
|
||||
|
||||
private static Log logger = LogFactory.getLog(AbstractContentDataDAOImpl.class);
|
||||
|
||||
private MimetypeDAO mimetypeDAO;
|
||||
private EncodingDAO encodingDAO;
|
||||
@@ -100,21 +108,22 @@ public abstract class AbstractContentDataDAOImpl implements ContentDataDAO
|
||||
*/
|
||||
protected void registerNewContentUrl(String contentUrl)
|
||||
{
|
||||
if (contentStoreCleaner != null)
|
||||
{
|
||||
contentStoreCleaner.registerNewContentUrl(contentUrl);
|
||||
}
|
||||
contentStoreCleaner.registerNewContentUrl(contentUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register orphaned content for post-commit handling
|
||||
* A <b>content_url</b> entity was dereferenced. This makes no assumptions about the
|
||||
* current references - dereference deletion is handled in the commit phase.
|
||||
*/
|
||||
protected void registerOrphanedContentUrl(String contentUrl)
|
||||
protected void registerDereferenceContentUrl(String contentUrl)
|
||||
{
|
||||
if (contentStoreCleaner != null)
|
||||
Set<String> contentUrls = TransactionalResourceHelper.getSet(KEY_PRE_COMMIT_CONTENT_URL_DELETIONS);
|
||||
if (contentUrls.size() == 0)
|
||||
{
|
||||
contentStoreCleaner.registerOrphanedContentUrl(contentUrl);
|
||||
ContentUrlDeleteTransactionListener listener = new ContentUrlDeleteTransactionListener();
|
||||
AlfrescoTransactionSupport.bindListener(listener);
|
||||
}
|
||||
contentUrls.add(contentUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -288,6 +297,13 @@ public abstract class AbstractContentDataDAOImpl implements ContentDataDAO
|
||||
*/
|
||||
protected abstract ContentUrlEntity getContentUrlEntity(String contentUrl);
|
||||
|
||||
/**
|
||||
* @param contentUrl the URL of the <b>content url</b> entity
|
||||
* @return Return the entity or <tt>null</tt> if it doesn't exist or is still
|
||||
* referenced by a <b>content_data</b> entity
|
||||
*/
|
||||
protected abstract ContentUrlEntity getContentUrlEntityUnreferenced(String contentUrl);
|
||||
|
||||
/**
|
||||
* Delete the entity with the given ID
|
||||
* @return Returns the number of rows deleted
|
||||
@@ -315,4 +331,38 @@ public abstract class AbstractContentDataDAOImpl implements ContentDataDAO
|
||||
* @return Returns the number of rows deleted
|
||||
*/
|
||||
protected abstract int deleteContentDataEntity(Long id);
|
||||
|
||||
/**
|
||||
* Transactional listener that deletes unreferenced <b>content_url</b> entities.
|
||||
*
|
||||
* @author Derek Hulley
|
||||
*/
|
||||
public class ContentUrlDeleteTransactionListener extends TransactionListenerAdapter
|
||||
{
|
||||
@Override
|
||||
public void beforeCommit(boolean readOnly)
|
||||
{
|
||||
// Ignore read-only
|
||||
if (readOnly)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Set<String> contentUrls = TransactionalResourceHelper.getSet(KEY_PRE_COMMIT_CONTENT_URL_DELETIONS);
|
||||
for (String contentUrl : contentUrls)
|
||||
{
|
||||
ContentUrlEntity contentUrlEntity = getContentUrlEntityUnreferenced(contentUrl);
|
||||
if (contentUrlEntity == null)
|
||||
{
|
||||
// It is still referenced, so ignore it
|
||||
continue;
|
||||
}
|
||||
// It needs to be deleted
|
||||
Long contentUrlId = contentUrlEntity.getId();
|
||||
deleteContentUrlEntity(contentUrlId);
|
||||
// Pop this in the queue for deletion from the content store
|
||||
contentStoreCleaner.registerOrphanedContentUrl(contentUrl);
|
||||
}
|
||||
contentUrls.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -46,10 +46,10 @@ public class ContentDataDAOImpl extends AbstractContentDataDAOImpl
|
||||
{
|
||||
private static final String SELECT_CONTENT_URL_BY_ID = "select.ContentUrlById";
|
||||
private static final String SELECT_CONTENT_URL_BY_KEY = "select.ContentUrlByKey";
|
||||
private static final String SELECT_CONTENT_URL_BY_KEY_UNREFERENCED = "select.ContentUrlByKeyUnreferenced";
|
||||
private static final String SELECT_CONTENT_URLS = "select.ContentUrls";
|
||||
private static final String SELECT_CONTENT_DATA_BY_ID = "select.ContentDataById";
|
||||
private static final String SELECT_CONTENT_DATA_BY_NODE_AND_QNAME = "select.ContentDataByNodeAndQName";
|
||||
private static final String SELECT_CONTENT_DATA_BY_URL_ID = "select.ContentDataByContentUrlId";
|
||||
private static final String INSERT_CONTENT_URL = "insert.ContentUrl";
|
||||
private static final String INSERT_CONTENT_DATA = "insert.ContentData";
|
||||
private static final String DELETE_CONTENT_DATA = "delete.ContentData";
|
||||
@@ -105,6 +105,16 @@ public class ContentDataDAOImpl extends AbstractContentDataDAOImpl
|
||||
return template.delete(DELETE_CONTENT_URL, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContentUrlEntity getContentUrlEntityUnreferenced(String contentUrl)
|
||||
{
|
||||
ContentUrlEntity contentUrlEntity = new ContentUrlEntity();
|
||||
contentUrlEntity.setContentUrl(contentUrl);
|
||||
contentUrlEntity = (ContentUrlEntity) template.queryForObject(SELECT_CONTENT_URL_BY_KEY_UNREFERENCED, contentUrlEntity);
|
||||
// Done
|
||||
return contentUrlEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContentDataEntity createContentDataEntity(
|
||||
Long contentUrlId,
|
||||
@@ -166,22 +176,13 @@ public class ContentDataDAOImpl extends AbstractContentDataDAOImpl
|
||||
}
|
||||
// Only check the content URLs if one is present
|
||||
String contentUrl = contentDataEntity.getContentUrl();
|
||||
Long contentUrlId = contentDataEntity.getContentUrlId();
|
||||
// Delete the ContentData entity
|
||||
deleteContentData(id);
|
||||
// Check if the content URL was orphaned
|
||||
if (contentUrlId != null)
|
||||
if (contentUrl != null)
|
||||
{
|
||||
params.clear();
|
||||
params.put("id", contentUrlId);
|
||||
@SuppressWarnings("unchecked")
|
||||
List<ContentDataEntity> contentDataEntities = (List<ContentDataEntity>) template.queryForList(SELECT_CONTENT_DATA_BY_URL_ID, params);
|
||||
// If there is still ContentData associated with the content URL, then leave it
|
||||
if (contentDataEntities.size() == 0)
|
||||
{
|
||||
// Orphaned
|
||||
registerOrphanedContentUrl(contentUrl);
|
||||
}
|
||||
// It has been dereferenced and may be orphaned - we'll check later
|
||||
registerDereferenceContentUrl(contentUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,10 @@ import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.domain.AuditableProperties;
|
||||
import org.alfresco.repo.domain.Node;
|
||||
@@ -11,16 +15,32 @@ import org.alfresco.repo.domain.QNameDAO;
|
||||
import org.alfresco.repo.domain.Server;
|
||||
import org.alfresco.repo.domain.Store;
|
||||
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.util.BaseSpringTest;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.ApplicationContextHelper;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.engine.EntityKey;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.orm.hibernate3.SessionFactoryUtils;
|
||||
|
||||
public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
public class HibernateSessionHelperTest extends TestCase
|
||||
{
|
||||
|
||||
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
|
||||
|
||||
protected void onTearDownInTransaction()
|
||||
private UserTransaction txn;
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
|
||||
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
|
||||
TransactionService transactionService = serviceRegistry.getTransactionService();
|
||||
txn = transactionService.getUserTransaction();
|
||||
txn.begin();
|
||||
|
||||
// force a flush to ensure that the database updates succeed
|
||||
try
|
||||
{
|
||||
@@ -33,6 +53,28 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
}
|
||||
}
|
||||
|
||||
private Session getSession()
|
||||
{
|
||||
return SessionFactoryUtils.getSession(sessionFactory, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown()
|
||||
{
|
||||
if (txn != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
txn.rollback();
|
||||
}
|
||||
catch (Throwable e)
|
||||
{
|
||||
// Don't let this hide errors coming from the tests
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testSimpleMark()
|
||||
{
|
||||
assertEquals(0, getSession().getStatistics().getEntityCount());
|
||||
@@ -56,7 +98,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
|
||||
assertEquals(2, getSession().getStatistics().getEntityCount());
|
||||
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)getApplicationContext().getBean("hibernateSessionHelper");
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper) ctx.getBean("hibernateSessionHelper");
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
helper.mark();
|
||||
assertTrue(SessionSizeResourceManager.isDisableInTransaction());
|
||||
@@ -92,7 +134,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
assertEquals(0, getSession().getStatistics().getEntityCount());
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
|
||||
QNameDAO qnameDAO = (QNameDAO) getApplicationContext().getBean("qnameDAO");
|
||||
QNameDAO qnameDAO = (QNameDAO) ctx.getBean("qnameDAO");
|
||||
Long baseQNameId = qnameDAO.getOrCreateQName(ContentModel.TYPE_BASE).getFirst();
|
||||
|
||||
StoreImpl store = new StoreImpl();
|
||||
@@ -118,7 +160,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
|
||||
assertEquals(4, getSession().getStatistics().getEntityCount());
|
||||
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)getApplicationContext().getBean("hibernateSessionHelper");
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)ctx.getBean("hibernateSessionHelper");
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
helper.mark();
|
||||
assertTrue(SessionSizeResourceManager.isDisableInTransaction());
|
||||
@@ -284,7 +326,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
|
||||
assertEquals(2, getSession().getStatistics().getEntityCount());
|
||||
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)getApplicationContext().getBean("hibernateSessionHelper");
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)ctx.getBean("hibernateSessionHelper");
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
helper.mark("One");
|
||||
assertTrue(SessionSizeResourceManager.isDisableInTransaction());
|
||||
@@ -320,7 +362,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
assertEquals(0, getSession().getStatistics().getEntityCount());
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
|
||||
QNameDAO qnameDAO = (QNameDAO) getApplicationContext().getBean("qnameDAO");
|
||||
QNameDAO qnameDAO = (QNameDAO) ctx.getBean("qnameDAO");
|
||||
Long baseQNameId = qnameDAO.getOrCreateQName(ContentModel.TYPE_BASE).getFirst();
|
||||
|
||||
StoreImpl store = new StoreImpl();
|
||||
@@ -346,7 +388,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
|
||||
assertEquals(4, getSession().getStatistics().getEntityCount());
|
||||
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)getApplicationContext().getBean("hibernateSessionHelper");
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)ctx.getBean("hibernateSessionHelper");
|
||||
assertNull(helper.getCurrentMark());
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
helper.mark("One");
|
||||
@@ -504,7 +546,7 @@ public class HibernateSessionHelperTest extends BaseSpringTest
|
||||
|
||||
assertEquals(3, getSession().getStatistics().getEntityCount());
|
||||
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)getApplicationContext().getBean("hibernateSessionHelper");
|
||||
HibernateSessionHelper helper = (HibernateSessionHelper)ctx.getBean("hibernateSessionHelper");
|
||||
assertFalse(SessionSizeResourceManager.isDisableInTransaction());
|
||||
helper.mark("One");
|
||||
helper.mark("Two");
|
||||
|
Reference in New Issue
Block a user