From 48ab8265a79274b8f9f1078cb6303a98f7bf891e Mon Sep 17 00:00:00 2001 From: Mark Rogers Date: Tue, 28 Sep 2010 14:46:35 +0000 Subject: [PATCH] transferServiceTest: new test to repeatedly update content. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22751 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../transfer/TransferServiceImplTest.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java b/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java index 521fd1ad56..2c28e5d7a3 100644 --- a/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java +++ b/source/java/org/alfresco/repo/transfer/TransferServiceImplTest.java @@ -50,6 +50,8 @@ import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.security.authentication.AuthenticationComponent; import org.alfresco.repo.security.authentication.AuthenticationUtil; +import org.alfresco.repo.transaction.RetryingTransactionHelper; +import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transfer.manifest.TransferManifestNodeFactory; import org.alfresco.service.cmr.action.ActionService; import org.alfresco.service.cmr.lock.LockService; @@ -89,6 +91,7 @@ import org.alfresco.util.BaseAlfrescoSpringTest; import org.alfresco.util.GUID; import org.alfresco.util.Pair; import org.alfresco.util.PropertyMap; +import org.apache.abdera.i18n.text.io.CharsetSniffingInputStream.Encoding; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.support.DefaultTransactionDefinition; import org.springframework.util.ResourceUtils; @@ -7220,6 +7223,150 @@ public class TransferServiceImplTest extends BaseAlfrescoSpringTest } // end of testEmptyContent + + /** + * Test the transfer method with regard to a repeated update of content.by sending one node (CRUD). + * + * This is a unit test so it does some shenanigans to send to the same instance of alfresco. + */ + public void testRepeatUpdateOfContent() throws Exception + { + final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper(); + final String CONTENT_TITLE = "ContentTitle"; + final Locale CONTENT_LOCALE = Locale.GERMAN; + final String CONTENT_ENCODING = "UTF-8"; + + /** + * For unit test + * - replace the HTTP transport with the in-process transport + * - replace the node factory with one that will map node refs, paths etc. + * + * Fake Repository Id + */ + final TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService); + transferServiceImpl.setTransmitter(transmitter); + final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory); + transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory); + final List> pathMap = testNodeFactory.getPathMap(); + // Map company_home/guest_home to company_home so tranferred nodes and moved "up" one level. + pathMap.add(new Pair(PathHelper.stringToPath(GUEST_HOME_XPATH_QUERY), PathHelper.stringToPath(COMPANY_HOME_XPATH_QUERY))); + + DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A); + transferServiceImpl.setDescriptorService(mockedDescriptorService); + + final String targetName = "testRepeatUpdateOfContent"; + + class TestContext + { + TransferTarget transferMe; + NodeRef contentNodeRef; + NodeRef destNodeRef; + String contentString; + }; + + RetryingTransactionCallback setupCB = new RetryingTransactionCallback() + { + @Override + public TestContext execute() throws Throwable + { + TestContext testContext = new TestContext(); + + /** + * Get guest home + */ + String guestHomeQuery = "/app:company_home/app:guest_home"; + ResultSet guestHomeResult = searchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_XPATH, guestHomeQuery); + assertEquals("", 1, guestHomeResult.length()); + NodeRef guestHome = guestHomeResult.getNodeRef(0); + + /** + * Create a test node that we will read and write + */ + String name = GUID.generate(); + ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_CONTENT); + testContext.contentNodeRef = child.getChildRef(); + nodeService.setProperty(testContext.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE); + nodeService.setProperty(testContext.contentNodeRef, ContentModel.PROP_NAME, name); + + /** + * Make sure the transfer target exists and is enabled. + */ + if(!transferService.targetExists(targetName)) + { + testContext.transferMe = createTransferTarget(targetName); + } + else + { + testContext.transferMe = transferService.getTransferTarget(targetName); + } + transferService.enableTransferTarget(targetName, true); + return testContext; + } + }; + + final TestContext testContext = tran.doInTransaction(setupCB); + + RetryingTransactionCallback updateContentCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + ContentWriter writer = contentService.getWriter(testContext.contentNodeRef, ContentModel.PROP_CONTENT, true); + writer.setLocale(CONTENT_LOCALE); + writer.setEncoding(CONTENT_ENCODING); + writer.putContent(testContext.contentString); + return null; + } + }; + + RetryingTransactionCallback transferCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + TransferDefinition definition = new TransferDefinition(); + Setnodes = new HashSet(); + nodes.add(testContext.contentNodeRef); + definition.setNodes(nodes); + transferService.transfer(targetName, definition); + return null; + } + }; + + RetryingTransactionCallback checkTransferCB = new RetryingTransactionCallback() { + + @Override + public Void execute() throws Throwable + { + // Now validate that the target node exists and has similar properties to the source + NodeRef destNodeRef = testNodeFactory.getMappedNodeRef(testContext.contentNodeRef); + + ContentReader reader = contentService.getReader(destNodeRef, ContentModel.PROP_CONTENT); + assertNotNull("content reader is null", reader); + assertEquals("content encoding is wrong", reader.getEncoding(), CONTENT_ENCODING); + assertEquals("content locale is wrong", reader.getLocale(), CONTENT_LOCALE); + assertTrue("content does not exist", reader.exists()); + String contentStr = reader.getContentString(); + assertEquals("Content is wrong", contentStr, testContext.contentString); + + return null; + } + }; + + /** + * This is the test + */ + for(int i = 0; i < 6 ; i++) + { + logger.debug("testRepeatUpdateContent - iteration:" + i); + testContext.contentString = String.valueOf(i); + tran.doInTransaction(updateContentCB); + tran.doInTransaction(transferCB); + tran.doInTransaction(checkTransferCB); + } + } // test repeat update content + + private void createUser(String userName, String password) { if (this.authenticationService.authenticationExists(userName) == false)