Search in sources :

Example 41 with NetworkFile

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

the class ContentDiskDriverTest method testMetadataExtractionForMac.

// testScenarioShuffleMetadataExtraction
/**
 * ALF-12812
 *
 * This test tries to simulate the shuffling that is done by MS Word 2011 for Mac
 * with regard to metadata extraction.  In particular the temporary file names are
 * different.
 * <p>
 * 1: Setup an update rule for ContentMetadataExtractor.
 * Simulate a WORD 2011 for Mac Create
 * 2: Write "Word Work File D_1725484373.tmp"
 * 3: Close file
 * 4: Rename "Word Work File D_1725484373.tmp" to ContentDiskDriver.docx
 * 5: Check metadata extraction
 */
public void testMetadataExtractionForMac() throws Exception {
    logger.debug("testMetadataExtractionForMac");
    final String FILE_NAME = "ContentDiskDriver.docx";
    // final String FILE_OLD_TEMP = "._Word Work File D_1725484373.tmp";
    final String FILE_NEW_TEMP = "Word Work File D_1725484373.tmp";
    class TestContext {

        NodeRef testDirNodeRef;

        NodeRef testNodeRef;

        NetworkFile firstFileHandle;
        // NetworkFile secondFileHandle;
    }
    ;
    final TestContext testContext = new TestContext();
    final String TEST_DIR = TEST_ROOT_DOS_PATH + "\\testMetadataExtractionForMac";
    ServerConfiguration scfg = new ServerConfiguration("testServer");
    TestServer testServer = new TestServer("testServer", scfg);
    final SrvSession testSession = new TestSrvSession(666, testServer, "cifs", "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.UPDATE);
            rule.applyToChildren(true);
            rule.setRuleDisabled(false);
            rule.setTitle("Extract Metadata from update 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_NEW_TEMP, 0, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
            testContext.firstFileHandle = driver.createFile(testSession, testConnection, createFileParams);
            assertNotNull("first file Handle is null", 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_NEW_TEMP);
            assertNotNull("testContext.testNodeRef is null", testContext.testNodeRef);
            // test non CM namespace property
            nodeService.setProperty(testContext.testNodeRef, TransferModel.PROP_ENABLED, true);
            // Check that the temporary aspect has been applied.
            assertTrue("temporary aspect not applied", nodeService.hasAspect(testContext.testNodeRef, ContentModel.ASPECT_TEMPORARY));
            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);
            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);
    logger.debug("Step b: rename the test file.");
    /**
     * 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("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));
            // Check that the temporary aspect has been applied.
            assertTrue("temporary aspect has not been removed", !nodeService.hasAspect(testContext.testNodeRef, ContentModel.ASPECT_TEMPORARY));
            assertTrue("hidden aspect has not been removed", !nodeService.hasAspect(testContext.testNodeRef, ContentModel.ASPECT_HIDDEN));
            // 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));
            ContentData data = (ContentData) props.get(ContentModel.PROP_CONTENT);
            assertEquals("mimeType is wrong", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", data.getMimetype());
            assertEquals("size is wrong", 11302, data.getSize());
            return null;
        }
    };
    tran.doInTransaction(validateFirstExtractionCB, false, 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) 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) 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 42 with NetworkFile

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

the class ContentDiskDriverTest method testOpenFile.

// test set file info
/**
 * Test Open File
 */
public void testOpenFile() throws Exception {
    logger.debug("testOpenFile");
    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();
    class TestContext {

        NodeRef testDirNodeRef;
    }
    ;
    final TestContext testContext = new TestContext();
    final String FILE_NAME = "testOpenFile.txt";
    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);
    /**
     * Step 1 : Negative test - try to open a file that does not exist
     */
    final String FILE_PATH = TEST_ROOT_DOS_PATH + "\\" + FILE_NAME;
    FileOpenParams params = new FileOpenParams(FILE_PATH, FileAction.CreateNotExist, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    try {
        NetworkFile file = driver.openFile(testSession, testConnection, params);
        fail("managed to open non existant file!");
    } catch (IOException ie) {
    // expect to go here
    }
    /**
     * Step 2: Now create the file through the node service and open it.
     */
    logger.debug("Step 2) Open file created by node service");
    RetryingTransactionCallback<Void> createFileCB = new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            ChildAssociationRef ref = nodeService.createNode(testContext.testDirNodeRef, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, FILE_NAME), ContentModel.TYPE_CONTENT);
            nodeService.setProperty(ref.getChildRef(), ContentModel.PROP_NAME, FILE_NAME);
            return null;
        }
    };
    tran.doInTransaction(createFileCB, false, true);
    NetworkFile file = driver.openFile(testSession, testConnection, params);
    assertNotNull(file);
    assertFalse("file is closed", file.isClosed());
    /**
     * Step 3: Open the root directory.
     */
    logger.debug("Step 3) Open the root directory");
    FileOpenParams rootParams = new FileOpenParams("\\", FileAction.CreateNotExist, AccessMode.ReadWrite, FileAttribute.NTNormal, 0);
    NetworkFile file3 = driver.openFile(testSession, testConnection, rootParams);
    assertNotNull(file3);
    assertFalse("file is closed", file3.isClosed());
}
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) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) 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 43 with NetworkFile

use of org.alfresco.jlan.server.filesys.NetworkFile 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 44 with NetworkFile

use of org.alfresco.jlan.server.filesys.NetworkFile 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 45 with NetworkFile

use of org.alfresco.jlan.server.filesys.NetworkFile 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)

Aggregations

NetworkFile (org.alfresco.jlan.server.filesys.NetworkFile)54 NodeRef (org.alfresco.service.cmr.repository.NodeRef)48 SrvSession (org.alfresco.jlan.server.SrvSession)44 ServerConfiguration (org.alfresco.jlan.server.config.ServerConfiguration)44 DiskSharedDevice (org.alfresco.jlan.server.filesys.DiskSharedDevice)44 FileOpenParams (org.alfresco.jlan.server.filesys.FileOpenParams)44 TreeConnection (org.alfresco.jlan.server.filesys.TreeConnection)44 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)44 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)44 IOException (java.io.IOException)36 Serializable (java.io.Serializable)33 AccessDeniedException (org.alfresco.jlan.server.filesys.AccessDeniedException)29 QName (org.alfresco.service.namespace.QName)27 FileNotFoundException (java.io.FileNotFoundException)24 FileExistsException (org.alfresco.jlan.server.filesys.FileExistsException)24 PermissionDeniedException (org.alfresco.jlan.server.filesys.PermissionDeniedException)24 DeviceContextException (org.alfresco.jlan.server.core.DeviceContextException)23 ContentData (org.alfresco.service.cmr.repository.ContentData)14 ClassPathResource (org.springframework.core.io.ClassPathResource)12 InputStream (java.io.InputStream)7