Search in sources :

Example 36 with SrvSession

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

the class ContentDiskDriverTest method testScenarioViSave.

// testScenarioEmacs save
/**
 * This test tries to simulate the cifs shuffling that is done to
 * support vi
 *
 * a) viTest.txt
 * b) Rename original file to viTest.txt~
 * c) Create viTest.txt
 * d) Delete viTest.txt~
 */
public void testScenarioViSave() throws Exception {
    logger.debug("testScenarioViSave");
    final String FILE_NAME = "viTest.txt";
    final String FILE_OLD_TEMP = "viTest.txt~";
    class TestContext {

        NetworkFile firstFileHandle;

        NetworkFile newFileHandle;

        // node ref of test.doc
        NodeRef testNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_ROOT_DIR = "\\ContentDiskDriverTest";
    final String TEST_DIR = "\\ContentDiskDriverTest\\testScenarioViSave";
    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);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            /**
             * 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);
            // no need to test lots of different properties, that's already been tested above
            testContext.testNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            nodeService.setProperty(testContext.testNodeRef, TransferModel.PROP_ENABLED, true);
            return null;
        }
    };
    tran.doInTransaction(createFileCB);
    /**
     * a) Write some content to the test file
     */
    RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            String testContent = "Emacs shuffle test";
            byte[] testContentBytes = testContent.getBytes();
            testContext.firstFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            return null;
        }
    };
    tran.doInTransaction(writeFileCB);
    /**
     * b) rename the 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);
    /**
     * c) Save the new file
     */
    RetryingTransactionCallback<Void> saveNewFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.newFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.newFileHandle);
            String testContent = "Vi shuffle test This is new content";
            byte[] testContentBytes = testContent.getBytes();
            testContext.newFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
            driver.closeFile(testSession, testConnection, testContext.newFileHandle);
            logger.debug("delete temporary file - which will trigger shuffle");
            driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_OLD_TEMP);
            return null;
        }
    };
    tran.doInTransaction(saveNewFileCB);
    RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef shuffledNodeRef = getNodeForPath(testConnection, TEST_DIR + "\\" + FILE_NAME);
            assertNotNull("shuffledNodeRef is null", shuffledNodeRef);
            Map<QName, Serializable> props = nodeService.getProperties(shuffledNodeRef);
            assertEquals("name wrong", FILE_NAME, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_NAME));
            assertTrue("node does not contain shuffled ENABLED property", props.containsKey(TransferModel.PROP_ENABLED));
            return null;
        }
    };
    tran.doInTransaction(validateCB);
}
Also used : Serializable(java.io.Serializable) SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) 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 37 with SrvSession

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

the class ContentDiskDriverTest method testScenarioMSWord2003SaveAsShuffle.

/**
 * Simulates a SaveAs from Word2003
 * 1. Create new document SAVEAS.DOC, file did not exist
 * 2. Create -WRDnnnn.TMP file, where 'nnnn' is a 4 digit sequence to make the name unique
 * 3. Rename SAVEAS.DOC to Backup of SAVEAS.wbk
 * 4. Rename -WRDnnnn.TMP to SAVEAS.DOC
 */
public void testScenarioMSWord2003SaveAsShuffle() throws Exception {
    logger.debug("testScenarioMSWord2003SaveShuffle");
    final String FILE_NAME = "SAVEAS.DOC";
    final String FILE_OLD_TEMP = "SAVEAS.wbk";
    final String FILE_NEW_TEMP = "~WRD0002.TMP";
    class TestContext {

        NetworkFile firstFileHandle;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testScenarioMSWord2003SaveAsShuffle";
    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);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            /**
             * 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);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    /**
     * b) Save the new file
     * Write ContentDiskDriverTest3.doc to the test file,
     */
    logger.debug("b) move new file into place");
    RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            FileOpenParams createFileParams = new FileOpenParams(TEST_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);
            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.firstFileHandle.writeFile(buffer, i, 0, offset);
                    offset += i;
                    i = is.read(buffer, 0, buffer.length);
                }
            } finally {
                is.close();
            }
            driver.closeFile(testSession, testConnection, testContext.firstFileHandle);
            return null;
        }
    };
    tran.doInTransaction(writeFileCB, false, true);
    /**
     * c) rename the old file
     */
    logger.debug("c) rename old file");
    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);
    /**
     * d) Move the new file into place, stuff should get shuffled
     */
    logger.debug("d) 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("e) 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", 26112, data.getSize());
            assertEquals("mimeType is wrong", "application/msword", data.getMimetype());
            return null;
        }
    };
    tran.doInTransaction(validateCB, true, true);
}
Also used : Serializable(java.io.Serializable) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentData(org.alfresco.service.cmr.repository.ContentData) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) SrvSession(org.alfresco.jlan.server.SrvSession) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) 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) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Example 38 with SrvSession

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

the class ContentDiskDriverTest method testScenarioOpenCloseFile.

/**
 * Test Open Close File Scenario
 *
 * 1) open(readOnly)
 * 2) open(readWrite)
 * 3) open(readWrite) - does nothing.
 * 4) close - does nothing
 * 5) close - does nothing
 * 6) close - updates the repo
 */
public void testScenarioOpenCloseFile() throws Exception {
    logger.debug("start of testScenarioOpenCloseFile");
    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();
    class TestContext {

        NodeRef testDirNodeRef;

        NodeRef targetNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    final String FILE_NAME = "testScenarioOpenFile.txt";
    final String FILE_PATH = TEST_ROOT_DOS_PATH + "\\" + FILE_NAME;
    FileOpenParams dirParams = new FileOpenParams(TEST_ROOT_DOS_PATH, 0, AccessMode.ReadOnly, FileAttribute.NTDirectory, 0);
    driver.createDirectory(testSession, testConnection, dirParams);
    testContext.testDirNodeRef = getNodeForPath(testConnection, TEST_ROOT_DOS_PATH);
    /**
     * 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, FILE_PATH);
            return null;
        }
    };
    try {
        tran.doInTransaction(deleteGarbageFileCB);
    } catch (Exception e) {
    // expect to go here
    }
    /**
     * Step 1: Now create the file through the node service and open it.
     */
    logger.debug("Step 1) Create File and Open file created by node service");
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            logger.debug("create file and close it immediatly");
            FileOpenParams createFileParams = new FileOpenParams(FILE_PATH, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            NetworkFile dummy = driver.createFile(testSession, testConnection, createFileParams);
            assertFalse("file is closed after create", dummy.isClosed());
            driver.closeFile(testSession, testConnection, dummy);
            logger.debug("after create and close");
            // assertTrue("file is not closed after close", dummy.isClosed());
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    testContext.targetNodeRef = getNodeForPath(testConnection, FILE_PATH);
    FileOpenParams openRO = new FileOpenParams(FILE_PATH, FileAction.CreateNotExist, AccessMode.ReadOnly, FileAttribute.NTNormal, 0);
    FileOpenParams openRW = new FileOpenParams(FILE_PATH, FileAction.CreateNotExist, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    /**
     * First open - read only
     */
    logger.debug("open file1 read only");
    NetworkFile file1 = driver.openFile(testSession, testConnection, openRO);
    assertNotNull(file1);
    assertFalse("file1 is closed", file1.isClosed());
    final String testString = "Yankee doodle went to town";
    byte[] stuff = testString.getBytes("UTF-8");
    /**
     * Negative test - file is open readOnly
     */
    try {
        driver.writeFile(testSession, testConnection, file1, stuff, 0, stuff.length, 0);
        fail("can write to a read only file!");
    } catch (Exception e) {
    // Expect to go here
    }
    logger.debug("open file 2 for read write");
    NetworkFile file2 = driver.openFile(testSession, testConnection, openRW);
    assertNotNull(file2);
    assertFalse("file is closed", file2.isClosed());
    /**
     * Write Some Content
     */
    driver.writeFile(testSession, testConnection, file2, stuff, 0, stuff.length, 0);
    NetworkFile file3 = driver.openFile(testSession, testConnection, openRW);
    assertNotNull(file3);
    logger.debug("first close");
    driver.closeFile(testSession, testConnection, file2);
    // assertTrue("node does not have no content aspect", nodeService.hasAspect(testContext.targetNodeRef, ContentModel.ASPECT_NO_CONTENT));
    logger.debug("second close");
    driver.closeFile(testSession, testConnection, file3);
    // //assertTrue("node does not have no content aspect", nodeService.hasAspect(testContext.targetNodeRef, ContentModel.ASPECT_NO_CONTENT));
    // logger.debug("this should be the last close");
    // driver.closeFile(testSession, testConnection, file1);
    // assertFalse("node still has no content aspect", nodeService.hasAspect(testContext.targetNodeRef, ContentModel.ASPECT_NO_CONTENT));
    /**
     * Step 2: Negative test - Close the file again - should do nothing quietly!
     */
    logger.debug("this is a negative test - should do nothing");
    driver.closeFile(testSession, testConnection, file1);
    logger.debug("now validate");
    RetryingTransactionCallback<Void> validateCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            Map<QName, Serializable> props = nodeService.getProperties(testContext.targetNodeRef);
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            assertNotNull("data is null", data);
            assertEquals("data wrong length", testString.length(), data.getSize());
            ContentReader reader = contentService.getReader(testContext.targetNodeRef, ContentModel.PROP_CONTENT);
            String s = reader.getContentString();
            assertEquals("content not written", testString, s);
            return null;
        }
    };
    tran.doInTransaction(validateCB, false, true);
}
Also used : Serializable(java.io.Serializable) SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) 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) 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 39 with SrvSession

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

the class ContentDiskDriverTest method testScenarioMacMountainLionPreview.

// Test Word 2011 Mountain Lion
/**
 * This test tries to simulate the cifs shuffling that is done
 * from Save from Mac Mountain Lion by Preview
 *
 * a) Temp file created in temporary folder (Crysanthemum.jpg)
 * b) Target file deleted by open / delete on close flag / close
 * c) Temp file moved to target file.
 */
public void testScenarioMacMountainLionPreview() throws Exception {
    logger.debug("testScenarioMountainLionPreview");
    final String FILE_NAME = "Crysanthemeum.jpg";
    final String TEMP_FILE_NAME = "Crysanthemeum.jpg";
    final String UPDATED_TEXT = "Mac Lion Preview Updated Content";
    class TestContext {

        NetworkFile firstFileHandle;

        NetworkFile tempFileHandle;

        // node ref Crysanthemenum.jpg
        NodeRef testNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_ROOT_DIR = "\\ContentDiskDriverTest";
    final String TEST_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionPreview";
    final String TEST_TEMP_DIR = "\\ContentDiskDriverTest\\testScenarioMountainLionPreview\\.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 Text";
            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);
    // /**
    // * b) Delete the target file by opening it and set the delete on close bit
    // */
    // RetryingTransactionCallback<Void> deleteTargetFileCB = new RetryingTransactionCallback<Void>() {
    // 
    // @Override
    // public Void execute() throws Throwable
    // {
    // FileOpenParams openFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NAME, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    // testContext.tempFileHandle = driver.openFile(testSession, testConnection, openFileParams);
    // FileInfo info = new FileInfo();
    // info.setFileInformationFlags(FileInfo.SetDeleteOnClose);
    // info.setDeleteOnClose(true);
    // testContext.tempFileHandle.setDeleteOnClose(true);
    // 
    // driver.setFileInformation(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME, info);
    // 
    // assertNotNull(testContext.tempFileHandle);
    // logger.debug("this close should result in a file being deleted");
    // driver.closeFile(testSession, testConnection,  testContext.tempFileHandle);
    // return null;
    // }
    // };
    // tran.doInTransaction(deleteTargetFileCB, false, true);
    /**
     * b) Delete the target file by a simple delete
     */
    RetryingTransactionCallback<Void> deleteTargetFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            driver.deleteFile(testSession, testConnection, TEST_DIR + "\\" + FILE_NAME);
            return null;
        }
    };
    tran.doInTransaction(deleteTargetFileCB, false, true);
    /**
     * c) Move the temp file into target directory
     */
    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);
    /**
     * Validate results.
     */
    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);
            return null;
        }
    };
    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 40 with SrvSession

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

the class ContentDiskDriverTest method testScenarioMSWord2003SaveShuffle.

// testRenameVersionable
/**
 * This test tries to simulate the shuffling that is done by MS Word 2003 upon file save
 *
 * a) TEST.DOC
 * b) Save to ~WRDnnnn.TMP
 * c) Delete ~WRLnnnn.TMP
 * d) Rename TEST.DOC ~WDLnnnn.TMP
 * e) Delete TEST.DOC
 * f) Rename ~WRDnnnn.TMP to TEST.DOC
 * g) Delete ~WRLnnnn.TMP
 *
 * We need to check that properties, aspects, primary assocs, secondary assocs, peer assocs, node type,
 * version history, creation date are maintained.
 */
public void testScenarioMSWord2003SaveShuffle() throws Exception {
    logger.debug("testScenarioMSWord2003SaveShuffle");
    final String FILE_NAME = "TEST.DOC";
    final String FILE_TITLE = "Test document";
    final String FILE_DESCRIPTION = "This is a test document to test CIFS shuffle";
    final String FILE_OLD_TEMP = "~WRL0002.TMP";
    final String FILE_NEW_TEMP = "~WRD0002.TMP";
    final QName RESIDUAL_MTTEXT = QName.createQName("{gsxhjsx}", "whatever");
    class TestContext {

        NetworkFile firstFileHandle;

        NetworkFile newFileHandle;

        NetworkFile oldFileHandle;

        // node ref of test.doc
        NodeRef testNodeRef;

        Serializable testCreatedDate;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testScenarioMSWord2003SaveShuffle";
    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
    }
    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);
            driver.createDirectory(testSession, testConnection, createRootDirParams);
            driver.createDirectory(testSession, testConnection, createDirParams);
            /**
             * 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);
            // 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);
            // test non CM namespace property
            nodeService.setProperty(testContext.testNodeRef, TransferModel.PROP_ENABLED, true);
            // test CM property not related to an aspect
            nodeService.setProperty(testContext.testNodeRef, ContentModel.PROP_ADDRESSEE, "Fred");
            nodeService.setProperty(testContext.testNodeRef, ContentModel.PROP_TITLE, FILE_TITLE);
            nodeService.setProperty(testContext.testNodeRef, ContentModel.PROP_DESCRIPTION, FILE_DESCRIPTION);
            /**
             * MLText value - also a residual value in a non cm namespace
             */
            MLText mltext = new MLText();
            mltext.addValue(Locale.FRENCH, "Bonjour");
            mltext.addValue(Locale.ENGLISH, "Hello");
            mltext.addValue(Locale.ITALY, "Buongiorno");
            mlAwareNodeService.setProperty(testContext.testNodeRef, RESIDUAL_MTTEXT, mltext);
            // classifiable chosen since its not related to any properties.
            nodeService.addAspect(testContext.testNodeRef, ContentModel.ASPECT_CLASSIFIABLE, null);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    /**
     * Write some content to the test file
     */
    RetryingTransactionCallback<Void> writeFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            String testContent = "MS Word 2003 shuffle test";
            byte[] testContentBytes = testContent.getBytes();
            testContext.firstFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
            testContext.firstFileHandle.close();
            testContext.testCreatedDate = nodeService.getProperty(testContext.testNodeRef, ContentModel.PROP_CREATED);
            MLText multi = (MLText) mlAwareNodeService.getProperty(testContext.testNodeRef, RESIDUAL_MTTEXT);
            multi.getValues();
            return null;
        }
    };
    tran.doInTransaction(writeFileCB, false, true);
    /**
     * b) Save the new file
     */
    RetryingTransactionCallback<Void> saveNewFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            FileOpenParams createFileParams = new FileOpenParams(TEST_DIR + "\\" + FILE_NEW_TEMP, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.newFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull(testContext.newFileHandle);
            String testContent = "MS Word 2003 shuffle test This is new content";
            byte[] testContentBytes = testContent.getBytes();
            testContext.newFileHandle.writeFile(testContentBytes, testContentBytes.length, 0, 0);
            testContext.newFileHandle.close();
            return null;
        }
    };
    tran.doInTransaction(saveNewFileCB, false, true);
    /**
     * rename the old file
     */
    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);
    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);
    logger.debug("Shuffle step next");
    /**
     * Move the new file into place, stuff should get shuffled
     */
    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("end of shuffle step");
    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);
            // Check trx:enabled has been shuffled.
            assertTrue("node does not contain shuffled ENABLED property", props.containsKey(TransferModel.PROP_ENABLED));
            // check my residual MLText has been transferred
            assertTrue(props.containsKey(RESIDUAL_MTTEXT));
            // Check the titled aspect is correct
            assertEquals("name wrong", FILE_NAME, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_NAME));
            assertEquals("title wrong", FILE_TITLE, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_TITLE));
            assertEquals("description wrong", FILE_DESCRIPTION, nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_DESCRIPTION));
            // ALF-7641 - CIFS shuffle, does not preseve MLText values.
            Map<QName, Serializable> mlProps = mlAwareNodeService.getProperties(shuffledNodeRef);
            MLText multi = (MLText) mlAwareNodeService.getProperty(shuffledNodeRef, RESIDUAL_MTTEXT);
            assertTrue("MLText has lost values", multi.getValues().size() > 2);
            // // ALF-7635 check auditable properties
            assertEquals("creation date not preserved", ((java.util.Date) testContext.testCreatedDate).getTime(), ((java.util.Date) nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_CREATED)).getTime());
            // ALF-7628 - preserve addressee and classifiable
            assertEquals("ADDRESSEE PROPERTY Not copied", "Fred", nodeService.getProperty(shuffledNodeRef, ContentModel.PROP_ADDRESSEE));
            assertTrue("CLASSIFIABLE aspect not present", nodeService.hasAspect(shuffledNodeRef, ContentModel.ASPECT_CLASSIFIABLE));
            // ALF-7584 - preserve node ref.
            assertEquals("noderef changed", testContext.testNodeRef, shuffledNodeRef);
            return null;
        }
    };
    tran.doInTransaction(validateCB, true, true);
    /**
     * Clean up just in case garbage is left from a previous run
     */
    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);
}
Also used : Serializable(java.io.Serializable) SrvSession(org.alfresco.jlan.server.SrvSession) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) QName(org.alfresco.service.namespace.QName) ServerConfiguration(org.alfresco.jlan.server.config.ServerConfiguration) 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) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileOpenParams(org.alfresco.jlan.server.filesys.FileOpenParams) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) MLText(org.alfresco.service.cmr.repository.MLText) TreeConnection(org.alfresco.jlan.server.filesys.TreeConnection) NetworkFile(org.alfresco.jlan.server.filesys.NetworkFile) DiskSharedDevice(org.alfresco.jlan.server.filesys.DiskSharedDevice)

Aggregations

SrvSession (org.alfresco.jlan.server.SrvSession)47 ServerConfiguration (org.alfresco.jlan.server.config.ServerConfiguration)47 DiskSharedDevice (org.alfresco.jlan.server.filesys.DiskSharedDevice)47 TreeConnection (org.alfresco.jlan.server.filesys.TreeConnection)47 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)46 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)45 NodeRef (org.alfresco.service.cmr.repository.NodeRef)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 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)25 FileNotFoundException (java.io.FileNotFoundException)24 DeviceContextException (org.alfresco.jlan.server.core.DeviceContextException)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