Search in sources :

Example 46 with DiskSharedDevice

use of org.alfresco.jlan.server.filesys.DiskSharedDevice in project alfresco-repository by Alfresco.

the class ContentDiskDriverTest method testScenarioMacMountainLionKeynote_MNT_8558.

// testScenarioMacMountainLionPreview_MNT_317
/**
 * This test tries to simulate the cifs shuffling that is done
 * from Save from Mac Mountain Lion by Keynote when document is saved first time
 *
 * a) Temp file created in temporary folder (temp\test.key)
 * b) Original document renamed to backup copy name (test\test.key -> test\test~.key)
 * c) Temp file moved to original name (temp\test.key -> test\test.key)
 */
public void testScenarioMacMountainLionKeynote_MNT_8558() throws Exception {
    logger.debug("testScenarioMacMountainLionKeynote_MNT_8558");
    final String FILE_NAME = "test.key";
    final String BCKP_FILE_NAME = "test~.key";
    final String TEMP_FILE_NAME = "test.key";
    final String UPDATED_TEXT = "Mac Mountain Lion Keynote Updated Content";
    class TestContext {

        NetworkFile firstFileHandle;

        NetworkFile tempFileHandle;

        NodeRef testNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_ROOT_DIR = "\\ContentDiskDriverTest";
    final String TEST_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionKeynote";
    final String TEST_TEMP_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionKeynote\\.Temporary Items";
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    final TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    /**
     * Create a file in the test directory
     */
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the test directory we are going to use
             */
            FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createTempDirParams = new FileOpenParams(TEST_TEMP_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            driver.createDirectory(testSession, testConnection, createTempDirParams);
            /**
             * Create the file we are going to use
             */
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.firstFileHandle);
            String testContent = "Mac Mountain Lion Keynote Content";
            byte[] testContentBytes = testContent.getBytes();
            driver.writeFile(testSession, testConnection, testContext.firstFileHandle, testContentBytes, 0, testContentBytes.length, 0);
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            /**
             * Create the temp file we are going to use
             */
            FileOpenParams createTempFileParams = new FileOpenParams(TEST_TEMP_DIR + "\\" + TEMP_FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.tempFileHandle = driver.createFile(testSession, testConnection, createTempFileParams);
            assertNotNull(testContext.tempFileHandle);
            testContent = UPDATED_TEXT;
            testContentBytes = testContent.getBytes();
            driver.writeFile(testSession, testConnection, testContext.tempFileHandle, testContentBytes, 0, testContentBytes.length, 0);
            driver.closeFile(testSession, testConnection, testContext.tempFileHandle);
            /**
             * Also add versionable to target file
             */
            testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            nodeService.addAspect(testContext.testNodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    RetryingTransactionCallback<Void> renameFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + BCKP_FILE_NAME);
            return null;
        }
    };
    tran.doInTransaction(renameFileCB, false, true);
    RetryingTransactionCallback<Void> moveTempFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.renameFile(testSession, testConnection, TEST_TEMP_DIR + "\\" + TEMP_FILE_NAME, TEST_DIR + "\\" + FILE_NAME);
            return null;
        }
    };
    tran.doInTransaction(moveTempFileCB, false, true);
    RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            assertTrue("node is not versionable", nodeService.hasAspect(shuffledNodeRef, ContentModel.ASPECT_VERSIONABLE));
            assertEquals("shuffledNode ref is different", shuffledNodeRef, testContext.testNodeRef);
            assertEquals("Unexpected content size", contentService.getReader(shuffledNodeRef, ContentModel.PROP_CONTENT).getSize(), UPDATED_TEXT.length());
            return null;
        }
    };
    tran.doInTransaction(validateCB, false, true);
    // Make sure that during second rename test.key->test~.key deleted test~.key is not restored and version history doesn't lost.
    RetryingTransactionCallback<Void> prepareForSecondRunCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + BCKP_FILE_NAME);
            FileOpenParams createTempFileParams = new FileOpenParams(TEST_TEMP_DIR + "\\" + TEMP_FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.tempFileHandle = driver.createFile(testSession, testConnection, createTempFileParams);
            String testContent = UPDATED_TEXT;
            byte[] testContentBytes = testContent.getBytes();
            driver.writeFile(testSession, testConnection, testContext.tempFileHandle, testContentBytes, 0, testContentBytes.length, 0);
            driver.closeFile(testSession, testConnection, testContext.tempFileHandle);
            return null;
        }
    };
    tran.doInTransaction(prepareForSecondRunCB, false, true);
    tran.doInTransaction(renameFileCB, false, true);
    tran.doInTransaction(moveTempFileCB, false, true);
    tran.doInTransaction(validateCB, false, true);
}
Also used : SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Example 47 with DiskSharedDevice

use of org.alfresco.jlan.server.filesys.DiskSharedDevice in project alfresco-repository by Alfresco.

the class ContentDiskDriverTest method testMetadataExtraction.

/**
 * This test tries to simulate the shuffling that is done by MS Word 2003
 * with regard to metadata extraction.
 * <p>
 * 1: Setup an inbound rule for ContentMetadataExtractor.
 * 2: Write ContentDiskDriverTest1 file to ContentDiskDriver.docx
 * 3: Check metadata extraction for non update test
 * Simulate a WORD 2003 CIFS shuffle
 * 4: Write ContentDiskDriverTest2 file to ~WRD0003.TMP
 * 5: Rename ContentDiskDriver.docx to ~WRL0003.TMP
 * 6: Rename ~WRD0003.TMP to ContentDiskDriver.docx
 * 7: Check metadata extraction
 */
public void testMetadataExtraction() throws Exception {
    logger.debug("testMetadataExtraction");
    final String FILE_NAME = "ContentDiskDriver.docx";
    final String FILE_OLD_TEMP = "~WRL0003.TMP";
    final String FILE_NEW_TEMP = "~WRD0003.TMP";
    class TestContext {

        NodeRef testDirNodeRef;

        NodeRef testNodeRef;

        NetworkFile firstFileHandle;

        NetworkFile secondFileHandle;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testMetadataExtraction";
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    final TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    /**
     * Clean up just in case garbage is left from a previous run
     */
    RetryingTransactionCallback<Void> deleteGarbageDirCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteDirectory(testSession, testConnection, TEST_DIR);
            return null;
        }
    };
    try {
        tran.doInTransaction(deleteGarbageDirCB);
    } catch (Exception e) {
    // expect to go here
    }
    logger.debug("create Test directory" + TEST_DIR);
    RetryingTransactionCallback<Void> createTestDirCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the test directory we are going to use
             */
            FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            testContext.testDirNodeRef = getNodeForPath(testConnection, TEST_DIR);
            assertNotNull("testDirNodeRef is null", testContext.testDirNodeRef);
            UserTransaction txn = transactionService.getUserTransaction();
            return null;
        }
    };
    tran.doInTransaction(createTestDirCB);
    logger.debug("Create rule on test dir");
    RetryingTransactionCallback<Void> createRuleCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            Rule rule = new Rule();
            rule.setRuleType(RuleType.INBOUND);
            rule.applyToChildren(true);
            rule.setRuleDisabled(false);
            rule.setTitle("Extract Metadata from content");
            rule.setDescription("ContentDiskDriverTest");
            Map<String, Serializable> props = new HashMap<String, Serializable>(1);
            Action extractAction = actionService.createAction("extract-metadata", props);
            ActionCondition noCondition1 = actionService.createActionCondition(NoConditionEvaluator.NAME);
            extractAction.addActionCondition(noCondition1);
            ActionCondition noCondition2 = actionService.createActionCondition(NoConditionEvaluator.NAME);
            CompositeAction compAction = actionService.createCompositeAction();
            compAction.setTitle("Extract Metadata");
            compAction.setDescription("Content Disk Driver Test - Extract Metadata");
            compAction.addAction(extractAction);
            compAction.addActionCondition(noCondition2);
            rule.setAction(compAction);
            ruleService.saveRule(testContext.testDirNodeRef, rule);
            logger.debug("rule created");
            return null;
        }
    };
    tran.doInTransaction(createRuleCB, false, true);
    /**
     * Create a file in the test directory
     */
    logger.debug("create test file in test directory");
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the file we are going to use to test
             */
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.firstFileHandle);
            // now load up the node with lots of other stuff that we will test to see if it gets preserved during the
            // shuffle.
            testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            assertNotNull("testContext.testNodeRef is null", testContext.testNodeRef);
            // test non CM namespace property
            nodeService.setProperty(testContext.testNodeRef, TransferModel.PROP_ENABLED, true);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    logger.debug("step b: write content to test file");
    /**
     * Write ContentDiskDriverTest1.docx to the test file,
     */
    RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTest1.docx");
            assertNotNull("unable to find test resource filesys/ContentDiskDriverTest1.docx", fileResource);
            writeResourceToNetworkFile(fileResource, testContext.firstFileHandle);
            logger.debug("close the file, firstFileHandle");
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            return null;
        }
    };
    tran.doInTransaction(writeFileCB, false, true);
    logger.debug("Step c: validate metadata has been extracted.");
    /**
     * c: check simple case of meta-data extraction has worked.
     */
    RetryingTransactionCallback<Void> validateFirstExtractionCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            Map<QName, Serializable> props = nodeService.getProperties(testContext.testNodeRef);
            assertTrue("Enabled property has been lost", props.containsKey(TransferModel.PROP_ENABLED));
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            assertEquals("size is wrong", 11302, data.getSize());
            assertEquals("mimeType is wrong", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", data.getMimetype());
            // These metadata values should be extracted.
            assertEquals("description is not correct", "This is a test file", nodeService.getProperty(testContext.testNodeRef, ContentModel.PROP_DESCRIPTION));
            assertEquals("title is not correct", "ContentDiskDriverTest", nodeService.getProperty(testContext.testNodeRef, ContentModel.PROP_TITLE));
            assertEquals("author is not correct", "mrogers", nodeService.getProperty(testContext.testNodeRef, ContentModel.PROP_AUTHOR));
            return null;
        }
    };
    tran.doInTransaction(validateFirstExtractionCB, false, true);
    /**
     * d: Save the new file as an update file in the test directory
     */
    logger.debug("Step d: create update file in test directory " + FILE_NEW_TEMP);
    RetryingTransactionCallback<Void> createUpdateFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the file we are going to use to test
             */
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NEW_TEMP, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.secondFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.secondFileHandle);
            return null;
        }
    };
    tran.doInTransaction(createUpdateFileCB, false, true);
    RetryingTransactionCallback<Void> writeFile2CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTest2.docx");
            assertNotNull("unable to find test resource filesys/ContentDiskDriverTest2.docx", fileResource);
            byte[] buffer = new byte[1000];
            InputStream is = fileResource.getInputStream();
            try {
                long offset = 0;
                int i = is.read(buffer, 0, buffer.length);
                while (i > 0) {
                    testContext.secondFileHandle.writeFile(buffer, i, 0, offset);
                    offset += i;
                    i = is.read(buffer, 0, buffer.length);
                }
            } finally {
                is.close();
            }
            driver.closeFile(testSession, testConnection, testContext.secondFileHandle);
            return null;
        }
    };
    tran.doInTransaction(writeFile2CB, false, true);
    /**
     * rename the old file
     */
    logger.debug("move old file out of the way.");
    RetryingTransactionCallback<Void> renameOldFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + FILE_OLD_TEMP);
            return null;
        }
    };
    tran.doInTransaction(renameOldFileCB, false, true);
    /**
     * Check the old file has gone.
     */
    RetryingTransactionCallback<Void> validateOldFileGoneCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            try {
                driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME);
            } catch (IOException e) {
            // expect to go here since previous step renamed the file.
            }
            return null;
        }
    };
    tran.doInTransaction(validateOldFileGoneCB, false, true);
    // /**
    // * Check metadata extraction on intermediate new file
    // */
    // RetryingTransactionCallback<Void> validateIntermediateCB = new RetryingTransactionCallback<Void>() {
    // 
    // @Override
    // public Void execute() throws Throwable
    // {
    // NodeRef updateNodeRef = driver.getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NEW_TEMP);
    // 
    // Map<QName, Serializable> props = nodeService.getProperties(updateNodeRef);
    // 
    // // These metadata values should be extracted from file2.
    // assertEquals("intermediate file description is not correct", "Content Disk Test 2", props.get(ContentModel.PROP_DESCRIPTION));
    // assertEquals("intermediate file title is not correct", "Updated", props.get(ContentModel.PROP_TITLE));
    // assertEquals("intermediate file author is not correct", "mrogers", props.get(ContentModel.PROP_AUTHOR));
    // 
    // return null;
    // }
    // };
    // 
    // tran.doInTransaction(validateIntermediateCB, true, true);
    /**
     * Move the new file into place, stuff should get shuffled
     */
    logger.debug("move new file into place.");
    RetryingTransactionCallback<Void> moveNewFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NEW_TEMP, TEST_DIR + "\\" + FILE_NAME);
            return null;
        }
    };
    tran.doInTransaction(moveNewFileCB, false, true);
    logger.debug("validate update has run correctly.");
    RetryingTransactionCallback<Void> validateUpdateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            Map<QName, Serializable> props = nodeService.getProperties(shuffledNodeRef);
            // Check trx:enabled has been shuffled and not lost.
            assertTrue("node does not contain shuffled ENABLED property", props.containsKey(TransferModel.PROP_ENABLED));
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            assertEquals("mimeType is wrong", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", data.getMimetype());
            assertEquals("size is wrong", 11265, data.getSize());
            return null;
        }
    };
    tran.doInTransaction(validateUpdateCB, true, true);
}
Also used : Serializable(java.io.Serializable) CompositeAction(org.alfresco.service.cmr.action.CompositeAction) FileAction(org.alfresco.jlan.server.filesys.FileAction) Action(org.alfresco.service.cmr.action.Action) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) HashMap(java.util.HashMap) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) ContentData(org.alfresco.service.cmr.repository.ContentData) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) UserTransaction(javax.transaction.UserTransaction) SrvSession(org.alfresco.jlan.server.SrvSession) QName(org.alfresco.service.namespace.QName) InputStream(java.io.InputStream) IOException(java.io.IOException) DeviceContextException(org.alfresco.jlan.server.core.DeviceContextException) FileExistsException(org.alfresco.jlan.server.filesys.FileExistsException) FileNotFoundException(java.io.FileNotFoundException) PermissionDeniedException(org.alfresco.jlan.server.filesys.PermissionDeniedException) AccessDeniedException(org.alfresco.jlan.server.filesys.AccessDeniedException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) ActionCondition(org.alfresco.service.cmr.action.ActionCondition) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) CompositeAction(org.alfresco.service.cmr.action.CompositeAction) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) Rule(org.alfresco.service.cmr.rule.Rule) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Example 48 with DiskSharedDevice

use of org.alfresco.jlan.server.filesys.DiskSharedDevice in project alfresco-repository by Alfresco.

the class ContentDiskDriverTest method testEmptyContent.

// testZeroByteRules
/**
 * Test that files can be created with empty content and that
 * existing content can be over-wrriten by empty content.
 */
public void testEmptyContent() throws Exception {
    logger.debug("testEmptyContent");
    final String FILE_NAME_ZERO = "Zero.docx";
    final String FILE_NAME_NON_ZERO = "NonZero.docx";
    class TestContext {

        NodeRef testDirNodeRef;

        NodeRef testZeroNodeRef;

        NodeRef testNonZeroNodeRef;

        NetworkFile firstFileHandle;

        NetworkFile secondFileHandle;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testEmptyContent";
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    final TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    /**
     * Clean up just in case garbage is left from a previous run
     */
    RetryingTransactionCallback<Void> deleteGarbageDirCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteDirectory(testSession, testConnection, TEST_DIR);
            return null;
        }
    };
    try {
        tran.doInTransaction(deleteGarbageDirCB);
    } catch (Exception e) {
    // expect to go here
    }
    logger.debug("create Test directory" + TEST_DIR);
    RetryingTransactionCallback<Void> createTestDirCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the test directory we are going to use
             */
            FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            testContext.testDirNodeRef = getNodeForPath(testConnection, TEST_DIR);
            assertNotNull("testDirNodeRef is null", testContext.testDirNodeRef);
            return null;
        }
    };
    tran.doInTransaction(createTestDirCB);
    /**
     * Create a file in the test directory
     */
    logger.debug("create test file in test directory");
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the zero byte file we are going to use to test
             */
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME_ZERO, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.firstFileHandle);
            testContext.testZeroNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME_ZERO);
            assertNotNull("testContext.testNodeRef is null", testContext.testZeroNodeRef);
            logger.debug("close the file, firstFileHandle");
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            /**
             * Create the non zero byte file we are going to use to test
             */
            FileOpenParams createFileParams2 = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME_NON_ZERO, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.secondFileHandle = driver.createFile(testSession, testConnection, createFileParams2);
            assertNotNull(testContext.secondFileHandle);
            testContext.testNonZeroNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME_NON_ZERO);
            assertNotNull("testContext.testNodeRef is null", testContext.testNonZeroNodeRef);
            // Write hello world into the second file
            byte[] stuff = "Hello World".getBytes();
            driver.writeFile(testSession, testConnection, testContext.secondFileHandle, stuff, 0, stuff.length, 0);
            logger.debug("close the second non zero file, secondFileHandle");
            driver.closeFile(testSession, testConnection, testContext.secondFileHandle);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    /**
     * d: check both files have content properties.
     */
    RetryingTransactionCallback<Void> checkContentPropsCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            assertNotNull("content missing create non zero file.", nodeService.getProperty(testContext.testNonZeroNodeRef, ContentModel.PROP_CONTENT));
            assertNotNull("content missing create zero byte file.", nodeService.getProperty(testContext.testZeroNodeRef, ContentModel.PROP_CONTENT));
            return null;
        }
    };
    tran.doInTransaction(checkContentPropsCB, false, true);
    RetryingTransactionCallback<Void> truncateFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Truncate the non zero byte file we are going to use to test
             */
            FileOpenParams createFileParams2 = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME_NON_ZERO, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.secondFileHandle = driver.openFile(testSession, testConnection, createFileParams2);
            assertNotNull(testContext.secondFileHandle);
            testContext.testNonZeroNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME_NON_ZERO);
            assertNotNull("testContext.testNodeRef is null", testContext.testNonZeroNodeRef);
            driver.truncateFile(testSession, testConnection, testContext.secondFileHandle, 0);
            logger.debug("close the second non zero file, secondFileHandle");
            driver.closeFile(testSession, testConnection, testContext.secondFileHandle);
            return null;
        }
    };
    tran.doInTransaction(truncateFileCB, false, true);
    /**
     * d: check both files have content properties.
     */
    RetryingTransactionCallback<Void> checkContentProps2CB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ContentReader reader = contentService.getReader(testContext.testNonZeroNodeRef, ContentModel.PROP_CONTENT);
            String s = reader.getContentString();
            assertEquals("content not truncated", "", s);
            ContentReader reader2 = contentService.getReader(testContext.testZeroNodeRef, ContentModel.PROP_CONTENT);
            String s2 = reader2.getContentString();
            assertEquals("content not empty", "", s2);
            return null;
        }
    };
    tran.doInTransaction(checkContentProps2CB, false, true);
}
Also used : SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) ContentReader(org.alfresco.service.cmr.repository.ContentReader) DeviceContextException(org.alfresco.jlan.server.core.DeviceContextException) FileExistsException(org.alfresco.jlan.server.filesys.FileExistsException) FileNotFoundException(java.io.FileNotFoundException) PermissionDeniedException(org.alfresco.jlan.server.filesys.PermissionDeniedException) AccessDeniedException(org.alfresco.jlan.server.filesys.AccessDeniedException) IOException(java.io.IOException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Example 49 with DiskSharedDevice

use of org.alfresco.jlan.server.filesys.DiskSharedDevice in project alfresco-repository by Alfresco.

the class ContentDiskDriverTest method testScenarioMountainLionWord2011EditByEditor_ALF_16257.

// testScenarioMacLionTextEditByEditorForAlf16257
/**
 * 0. MacWord1.docx exist in folder where user has Editor permissions
 * 1. as Editor rename MacWord1.docx to backup Word Work File L_5.tmp
 * 2. as Editor create temporary file in temporary directory and move it to working dir
 * 3. as Editor rename Word Work File D_2.tmp to MacWord1.docx
 */
public void testScenarioMountainLionWord2011EditByEditor_ALF_16257() throws Exception {
    logger.debug("testScenarioMountainLionWord2011 Edit By Editor ALF-16257");
    final String FILE_NAME = "MacWord1.docx";
    final String FILE_OLD_TEMP = "Word Work File L_5.tmp";
    final String FILE_NEW_TEMP = "Word Work File D_2.tmp";
    class TestContext {

        NetworkFile firstFileHandle;

        String mimetype;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testALF16257Word";
    // need to match with interimPattern
    final String TEST_TEMP_DIR = TEST_DIR + "\\.TemporaryItems";
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    final TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    /**
     * Clean up just in case garbage is left from a previous run
     */
    RetryingTransactionCallback<Void> deleteGarbageFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME);
            return null;
        }
    };
    try {
        tran.doInTransaction(deleteGarbageFileCB);
    } catch (Exception e) {
    // expect to go here
    }
    logger.debug("a) create new file");
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Create the test directory we are going to use
             */
            FileOpenParams createRootDirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createDirParams = new FileOpenParams(TEST_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            FileOpenParams createTempDirParams = new FileOpenParams(TEST_TEMP_DIR, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            driver.createDirectory(testSession, testConnection, createTempDirParams);
            /**
             * Create the file we are going to test
             */
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.firstFileHandle);
            ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTest3.doc");
            assertNotNull("unable to find test resource filesys/ContentDiskDriverTest3.doc", fileResource);
            writeResourceToNetworkFile(fileResource, testContext.firstFileHandle);
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            NodeRef file1NodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            nodeService.addAspect(file1NodeRef, ContentModel.ASPECT_VERSIONABLE, null);
            // Apply 'Editor' role for test user to test folder
            permissionService.setPermission(getNodeForPath(testConnection, TEST_DIR), ContentDiskDriverTest.TEST_USER_AUTHORITY, PermissionService.EDITOR, true);
            // Apply full control on temporary directory
            permissionService.setPermission(getNodeForPath(testConnection, TEST_TEMP_DIR), PermissionService.ALL_AUTHORITIES, PermissionService.ALL_PERMISSIONS, true);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    /**
     * b) rename the old file, should fire doubleRenameShuffle scenario
     */
    logger.debug("b) rename old file");
    RunAsWork<Void> renameOldFileCB = new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            NodeRef file1NodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            Map<QName, Serializable> props = nodeService.getProperties(file1NodeRef);
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            testContext.mimetype = data.getMimetype();
            driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, TEST_DIR + "\\" + FILE_OLD_TEMP);
            return null;
        }
    };
    doTransactionWorkAsEditor(renameOldFileCB, tran);
    /**
     * c) as Editor Save the new file in .TemporaryItems
     * and move it to working directory (should be detected by scenario)
     * Write ContentDiskDriverTest3.doc,
     */
    logger.debug("c) create temp file in temp dir");
    RunAsWork<Void> writeFileCB = new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            FileOpenParams createFileParams = new FileOpenParams(TEST_TEMP_DIR + "\\" + FILE_NEW_TEMP, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            ClassPathResource fileResource = new ClassPathResource("filesys/ContentDiskDriverTest3.doc");
            assertNotNull("unable to find test resource filesys/ContentDiskDriverTest3.doc", fileResource);
            writeResourceToNetworkFile(fileResource, testContext.firstFileHandle);
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            driver.renameFile(testSession, testConnection, TEST_TEMP_DIR + "\\" + FILE_NEW_TEMP, TEST_DIR + "\\" + FILE_NEW_TEMP);
            return null;
        }
    };
    doTransactionWorkAsEditor(writeFileCB, tran);
    /**
     * d) Move the new file into place, stuff should get shuffled
     */
    logger.debug("d) move new file into place");
    RunAsWork<Void> moveNewFileCB = new RunAsWork<Void>() {

        @Override
        public Void doWork() throws Exception {
            driver.renameFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NEW_TEMP, TEST_DIR + "\\" + FILE_NAME);
            return null;
        }
    };
    doTransactionWorkAsEditor(moveNewFileCB, tran);
    /**
     * e) Delete the old file
     */
    logger.debug("e) delete the old file");
    RetryingTransactionCallback<Void> deleteOldFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_OLD_TEMP);
            return null;
        }
    };
    tran.doInTransaction(deleteOldFileCB, false, true);
    logger.debug("e) validate results");
    logger.debug("f) validate results");
    /**
     * Now validate everything is correct
     */
    RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            Map<QName, Serializable> props = nodeService.getProperties(shuffledNodeRef);
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            // assertNotNull("data is null", data);
            // assertEquals("size is wrong", 123904, data.getSize());
            NodeRef file1NodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            assertTrue("file has lost versionable aspect", nodeService.hasAspect(file1NodeRef, ContentModel.ASPECT_VERSIONABLE));
            assertEquals("mimeType is wrong", testContext.mimetype, data.getMimetype());
            return null;
        }
    };
    tran.doInTransaction(validateCB, true, true);
    logger.debug("end testScenarioMountainLionWord2011 Edit By Editor ALF-16257");
}
Also used : Serializable(java.io.Serializable) SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) QName(org.alfresco.service.namespace.QName) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) DeviceContextException(org.alfresco.jlan.server.core.DeviceContextException) FileExistsException(org.alfresco.jlan.server.filesys.FileExistsException) FileNotFoundException(java.io.FileNotFoundException) PermissionDeniedException(org.alfresco.jlan.server.filesys.PermissionDeniedException) AccessDeniedException(org.alfresco.jlan.server.filesys.AccessDeniedException) IOException(java.io.IOException) ClassPathResource(org.springframework.core.io.ClassPathResource) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Example 50 with DiskSharedDevice

use of org.alfresco.jlan.server.filesys.DiskSharedDevice in project alfresco-repository by Alfresco.

the class ContentDiskDriverTest method testScenarioDeleteViaNodeService.

// testScenarioSmultronSave
/**
 * This time we create a file through the ContentDiskDriver and then delete it
 * through the repo.   We check its no longer found by the driver.
 */
public void testScenarioDeleteViaNodeService() throws Exception {
    logger.debug("testScenarioDeleteViaNodeService");
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    SrvSession testSession = new TestSrvSession(666, testServer, "test", "remoteName");
    DiskSharedDevice share = getDiskSharedDevice();
    TreeConnection testConnection = testServer.getTreeConnection(share);
    final RetryingTransactionHelper tran = transactionService.getRetryingTransactionHelper();
    int openAction = FileAction.CreateNotExist;
    final String FILE_NAME = "testDeleteFileViaNodeService.new";
    final String FILE_PATH = "\\" + FILE_NAME;
    FileOpenParams params = new FileOpenParams(FILE_PATH, openAction, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    final NetworkFile file = driver.createFile(testSession, testConnection, params);
    assertNotNull("file is null", file);
    assertFalse("file is read only, should be read-write", file.isReadOnly());
    RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            byte[] stuff = "Hello World".getBytes();
            file.writeFile(stuff, stuff.length, 0, 0);
            file.close();
            NodeRef companyHome = repositoryHelper.getCompanyHome();
            NodeRef newNode = nodeService.getChildByName(companyHome, ContentModel.ASSOC_CONTAINS, FILE_NAME);
            assertNotNull("can't find new node", newNode);
            return null;
        }
    };
    tran.doInTransaction(writeFileCB, false, true);
    /**
     * Step 1: Delete the new node via the node service
     */
    RetryingTransactionCallback<Void> deleteNodeCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef companyHome = repositoryHelper.getCompanyHome();
            NodeRef newNode = nodeService.getChildByName(companyHome, ContentModel.ASSOC_CONTAINS, FILE_NAME);
            assertNotNull("can't find new node", newNode);
            nodeService.deleteNode(newNode);
            return null;
        }
    };
    tran.doInTransaction(deleteNodeCB, false, true);
    try {
        getNodeForPath(testConnection, FILE_PATH);
        fail("getNode for path unexpectedly succeeded");
    } catch (IOException ie) {
    // expect to go here
    }
    /**
     * Delete file by path - file should no longer exist
     */
    try {
        driver.deleteFile(testSession, testConnection, FILE_PATH);
        fail("delete unexpectedly succeeded");
    } catch (IOException ie) {
    // expect to go here
    }
}
Also used : SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) IOException(java.io.IOException) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) NodeRef(org.alfresco.service.cmr.repository.NodeRef) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Aggregations

DiskSharedDevice (org.alfresco.jlan.server.filesys.DiskSharedDevice)50 SrvSession (org.alfresco.jlan.server.SrvSession)47 ServerConfiguration (org.alfresco.jlan.server.config.ServerConfiguration)47 TreeConnection (org.alfresco.jlan.server.filesys.TreeConnection)47 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)46 NodeRef (org.alfresco.service.cmr.repository.NodeRef)46 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)45 FileOpenParams (org.alfresco.jlan.server.filesys.FileOpenParams)44 NetworkFile (org.alfresco.jlan.server.filesys.NetworkFile)44 IOException (java.io.IOException)31 Serializable (java.io.Serializable)31 QName (org.alfresco.service.namespace.QName)26 DeviceContextException (org.alfresco.jlan.server.core.DeviceContextException)25 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)25 FileNotFoundException (java.io.FileNotFoundException)24 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)24 PermissionDeniedException (org.alfresco.jlan.server.filesys.PermissionDeniedException)24 ContentData (org.alfresco.service.cmr.repository.ContentData)14 ClassPathResource (org.springframework.core.io.ClassPathResource)12 InputStream (java.io.InputStream)7