Merged BRANCHES/DEV/V4.1-BUG-FIX to HEAD:

41476: Enhancing the TemporaryNodes @Rule to allow cleanup of site nodes.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@41477 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Neil McErlean
2012-09-11 15:31:07 +00:00
parent e7bbba4366
commit b0afffa6ad
2 changed files with 66 additions and 2 deletions

View File

@@ -29,14 +29,18 @@ import org.alfresco.model.ContentModel;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.site.SiteModel;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
@@ -79,9 +83,11 @@ public class TemporaryNodes extends ExternalResource
final RetryingTransactionHelper transactionHelper = springContext.getBean("retryingTransactionHelper", RetryingTransactionHelper.class);
final CheckOutCheckInService cociService = springContext.getBean("CheckOutCheckInService", CheckOutCheckInService.class);
final DictionaryService dictionaryService = springContext.getBean("DictionaryService", DictionaryService.class);
final NodeService nodeService = springContext.getBean("NodeService", NodeService.class);
final SiteService siteService = springContext.getBean("SiteService", SiteService.class);
// Run as admin to ensure all non-system nodes can be deleted irrespecive of which user created them.
// Run as system to ensure all non-system nodes can be deleted irrespective of which user created them.
AuthenticationUtil.runAs(new RunAsWork<Void>()
{
@Override public Void doWork() throws Exception
@@ -104,7 +110,18 @@ public class TemporaryNodes extends ExternalResource
cociService.cancelCheckout(workingCopy);
}
log.debug("Deleting temporary node " + nodeService.getProperty(node, ContentModel.PROP_NAME));
nodeService.deleteNode(node);
// Site nodes are a special case which must be deleted through the SiteService.
final QName nodeType = nodeService.getType(node);
if (nodeType.equals(SiteModel.TYPE_SITE) || dictionaryService.isSubClass(nodeType, SiteModel.TYPE_SITE))
{
SiteInfo siteInfo = siteService.getSite(node);
siteService.deleteSite(siteInfo.getShortName());
}
else
{
nodeService.deleteNode(node);
}
}
}

View File

@@ -39,6 +39,9 @@ import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.cmr.site.SiteVisibility;
import org.alfresco.service.namespace.QName;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -81,6 +84,7 @@ public class TemporaryNodesTest
private static CheckOutCheckInService COCI_SERVICE;
private static ContentService CONTENT_SERVICE;
private static NodeService NODE_SERVICE;
private static SiteService SITE_SERVICE;
private static RetryingTransactionHelper TRANSACTION_HELPER;
private static NodeRef COMPANY_HOME;
@@ -93,6 +97,7 @@ public class TemporaryNodesTest
COCI_SERVICE = APP_CONTEXT_INIT.getApplicationContext().getBean("checkOutCheckInService", CheckOutCheckInService.class);
CONTENT_SERVICE = APP_CONTEXT_INIT.getApplicationContext().getBean("contentService", ContentService.class);
NODE_SERVICE = APP_CONTEXT_INIT.getApplicationContext().getBean("nodeService", NodeService.class);
SITE_SERVICE = APP_CONTEXT_INIT.getApplicationContext().getBean("siteService", SiteService.class);
TRANSACTION_HELPER = APP_CONTEXT_INIT.getApplicationContext().getBean("retryingTransactionHelper", RetryingTransactionHelper.class);
Repository repositoryHelper = APP_CONTEXT_INIT.getApplicationContext().getBean("repositoryHelper", Repository.class);
@@ -190,4 +195,46 @@ public class TemporaryNodesTest
}
});
}
/** Site nodes are a special case as they can only be deleted through the SiteService. */
@Test public void ensureSiteNodesAreCleanedUp() throws Throwable
{
// Note that because we need to test that the Rule's 'after' behaviour has worked correctly, we cannot
// use the Rule that has been declared in the normal way - otherwise nothing would be cleaned up until
// after our test method.
// Therefore we have to manually poke the Rule to get it to cleanup during test execution.
// NOTE! This is *not* how a JUnit Rule would normally be used.
TemporaryNodes myTemporaryNodes = new TemporaryNodes(APP_CONTEXT_INIT);
// Currently this is a no-op, but just in case that changes.
myTemporaryNodes.before();
// and ensure that the site node is gone.
SiteInfo createdSite = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<SiteInfo>()
{
public SiteInfo execute() throws Throwable
{
return SITE_SERVICE.createSite("sitePreset", "siteShortName", "site title", "site description", SiteVisibility.PUBLIC);
}
});
final NodeRef siteNodeRef = createdSite.getNodeRef();
myTemporaryNodes.addNodeRef(siteNodeRef);
// Now trigger the Rule's cleanup behaviour.
myTemporaryNodes.after();
// and ensure that the site node is gone.
TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback<Void>()
{
public Void execute() throws Throwable
{
if (NODE_SERVICE.exists(siteNodeRef))
{
fail("Node '" + NODE_SERVICE.getProperty(siteNodeRef, ContentModel.PROP_NAME) + "' still exists.");
}
return null;
}
});
}
}