From 6bc9570f65ba805a8c66926f472ff6741082b0f7 Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Thu, 29 Aug 2013 12:51:34 +0000 Subject: [PATCH] Merged BRANCHES/DEV/V4.1-BUG-FIX to HEAD: 54639: As part of work on ALF-19783, I have added support for versioned nodes to the TemporaryNodes JUnit rule. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@54641 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../util/test/junitrules/TemporaryNodes.java | 35 ++++++++ .../test/junitrules/TemporaryNodesTest.java | 79 ++++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodes.java b/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodes.java index c0b440f491..151cf5cad1 100644 --- a/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodes.java +++ b/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodes.java @@ -44,6 +44,7 @@ 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.version.VersionService; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.apache.commons.logging.Log; @@ -90,6 +91,7 @@ public class TemporaryNodes extends ExternalResource final DictionaryService dictionaryService = springContext.getBean("DictionaryService", DictionaryService.class); final NodeService nodeService = springContext.getBean("NodeService", NodeService.class); final SiteService siteService = springContext.getBean("SiteService", SiteService.class); + final VersionService versionService = springContext.getBean("VersionService", VersionService.class); // Run as system to ensure all non-system nodes can be deleted irrespective of which user created them. AuthenticationUtil.runAs(new RunAsWork() @@ -113,6 +115,14 @@ public class TemporaryNodes extends ExternalResource NodeRef workingCopy = cociService.getWorkingCopy(node); cociService.cancelCheckout(workingCopy); } + + // If it's been versioned, then we need to clean out the version history too. + if (versionService.isVersioned(node)) + { + log.debug("Deleting version history of temporary node " + nodeService.getProperty(node, ContentModel.PROP_NAME)); + versionService.deleteVersionHistory(node); + } + log.debug("Deleting temporary node " + nodeService.getProperty(node, ContentModel.PROP_NAME)); // Site nodes are a special case which must be deleted through the SiteService. @@ -277,8 +287,27 @@ public class TemporaryNodes extends ExternalResource * @param nodeCmName the cm:name of the new node * @param nodeCreator the username of the person who will create the node * @return the newly created NodeRef. + * @since 4.1.7 */ public NodeRef createQuickFile(final String mimetype, final NodeRef parentNode, final String nodeCmName, final String nodeCreator) + { + return createQuickFile(mimetype, parentNode, nodeCmName, nodeCreator, false); + } + + /** + * This method creates a cm:content NodeRef whose content is taken from an Alfresco 'quick' file and adds it to the internal + * list of NodeRefs to be tidied up by the rule. + * This method will be run in its own transaction and will be run with the specified user as the fully authenticated user, + * thus ensuring the named user is the cm:creator of the new node. + * + * @param mimetype the MimeType of the content to put in the new node. + * @param parentNode the parent node + * @param nodeCmName the cm:name of the new node + * @param nodeCreator the username of the person who will create the node + * @param isVersionable should the new node be {@link ContentModel#ASPECT_VERSIONABLE versionable}? + * @return the newly created NodeRef. + */ + public NodeRef createQuickFile(final String mimetype, final NodeRef parentNode, final String nodeCmName, final String nodeCreator, final boolean isVersionable) { final RetryingTransactionHelper transactionHelper = (RetryingTransactionHelper) appContextRule.getApplicationContext().getBean("retryingTransactionHelper"); @@ -291,6 +320,12 @@ public class TemporaryNodes extends ExternalResource { final NodeRef result = createNode(nodeCmName, parentNode, ContentModel.TYPE_CONTENT); + if (isVersionable) + { + NodeService nodeService = appContextRule.getApplicationContext().getBean("nodeService", NodeService.class); + nodeService.addAspect(result, ContentModel.ASPECT_VERSIONABLE, null); + } + File quickFile = loadQuickFile(getQuickResource(mimetype)); ContentService contentService = appContextRule.getApplicationContext().getBean("contentService", ContentService.class); diff --git a/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodesTest.java b/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodesTest.java index eac99866b2..2b3fa0760a 100644 --- a/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodesTest.java +++ b/source/test-java/org/alfresco/util/test/junitrules/TemporaryNodesTest.java @@ -44,6 +44,8 @@ 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.cmr.version.Version; +import org.alfresco.service.cmr.version.VersionService; import org.alfresco.service.namespace.QName; import org.junit.Before; import org.junit.BeforeClass; @@ -88,6 +90,7 @@ public class TemporaryNodesTest private static NodeService NODE_SERVICE; private static SiteService SITE_SERVICE; private static RetryingTransactionHelper TRANSACTION_HELPER; + private static VersionService VERSION_SERVICE; private static NodeRef COMPANY_HOME; @@ -97,10 +100,11 @@ public class TemporaryNodesTest @BeforeClass public static void initStaticData() throws Exception { 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); + 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); + VERSION_SERVICE = APP_CONTEXT_INIT.getApplicationContext().getBean("VersionService", VersionService.class); Repository repositoryHelper = APP_CONTEXT_INIT.getApplicationContext().getBean("repositoryHelper", Repository.class); COMPANY_HOME = repositoryHelper.getCompanyHome(); @@ -198,6 +202,75 @@ public class TemporaryNodesTest }); } + /** + * This test ensures that any temporary nodes which are versioned, have all their various versions cleaned up too. + * @since 4.1.7 + */ + @Test public void ensureVersionedNodesAreFullyCleanedUp() 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. + final TemporaryNodes myTemporaryNodes = new TemporaryNodes(APP_CONTEXT_INIT); + + // Currently this is a no-op, but just in case that changes. + myTemporaryNodes.before(); + + + final List nodesThatShouldBeDeletedByRule = new ArrayList(); + + // Create a versioned, temporary node + final NodeRef versionedTempNode = myTemporaryNodes.createQuickFile(MimetypeMap.MIMETYPE_TEXT_PLAIN, + COMPANY_HOME, + "versionableNode", + TEST_USER1.getUsername(), + true); + + nodesThatShouldBeDeletedByRule.add(versionedTempNode); + + // Create various versions + TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback() + { + public Void execute() throws Throwable + { + assertTrue("The test node was not versioned as it should be.", VERSION_SERVICE.isVersioned(versionedTempNode)); + + Version v1 = VERSION_SERVICE.createVersion(versionedTempNode, null); + Version v2 = VERSION_SERVICE.createVersion(versionedTempNode, null); + Version v3 = VERSION_SERVICE.createVersion(versionedTempNode, null); + + // Ensure that these version nodes are all cleaned up too. + for (Version v : new Version[] {v1, v2, v3}) + { + nodesThatShouldBeDeletedByRule.add(v.getFrozenStateNodeRef()); + } + + return null; + } + }); + + // Now trigger the Rule's cleanup behaviour. + myTemporaryNodes.after(); + + // and ensure that the nodes are all gone. + TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback() + { + public Void execute() throws Throwable + { + for (NodeRef node : nodesThatShouldBeDeletedByRule) + { + if (NODE_SERVICE.exists(node)) + { + fail("Node '" + NODE_SERVICE.getProperty(node, ContentModel.PROP_NAME) + "' still exists."); + } + } + return null; + } + }); + } + /** Site nodes are a special case as they can only be deleted through the SiteService. */ @Test public void ensureSiteNodesAreCleanedUp() throws Throwable {