Package org.apache.commons.vfs

Examples of org.apache.commons.vfs.FileObject


    /**
     * Read a file
     */
    public void testRandomWrite() throws Exception
    {
        FileObject file = null;
        try
        {
            file = createScratchFolder().resolveFile("random_write.txt");
            file.createFile();
            RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);

            // write first byte
            ra.writeByte(TEST_DATA.charAt(0));

            // start at pos 4
            ra.seek(3);
            ra.writeByte(TEST_DATA.charAt(3));
            ra.writeByte(TEST_DATA.charAt(4));

            // restart at pos 4 (but overwrite with different content)
            ra.seek(3);
            ra.writeByte(TEST_DATA.charAt(7));
            ra.writeByte(TEST_DATA.charAt(8));

            // advance to pos 11
            ra.seek(10);
            ra.writeByte(TEST_DATA.charAt(10));
            ra.writeByte(TEST_DATA.charAt(11));

            // now read
            ra.seek(0);
            assertEquals(ra.readByte(), TEST_DATA.charAt(0));

            ra.seek(3);
            assertEquals(ra.readByte(), TEST_DATA.charAt(7));
            assertEquals(ra.readByte(), TEST_DATA.charAt(8));

            ra.seek(10);
            assertEquals(ra.readByte(), TEST_DATA.charAt(10));
            assertEquals(ra.readByte(), TEST_DATA.charAt(11));
        }
        finally
        {
            if (file != null)
            {
                file.close();
            }
        }
    }
View Full Code Here


     * Asserts that every expected file exists, and has the expected content.
     */
    public void testAllContent() throws Exception
    {
        final FileInfo baseInfo = buildExpectedStructure();
        final FileObject baseFolder = getReadFolder();

        assertSameContent(baseInfo, baseFolder);
    }
View Full Code Here

                                   final FileObject folder) throws Exception
    {
        for (Iterator iterator = expected.children.values().iterator(); iterator.hasNext();)
        {
            final FileInfo fileInfo = (FileInfo) iterator.next();
            final FileObject child = folder.resolveFile(fileInfo.baseName, NameScope.CHILD);

            assertTrue(child.getName().toString(), child.exists());
            if (fileInfo.type == FileType.FILE)
            {
                assertSameContent(fileInfo.content, child);
            }
            else
View Full Code Here

     * Tests existence determination.
     */
    public void testExists() throws Exception
    {
        // Test a file
        FileObject file = getReadFolder().resolveFile("file1.txt");
        assertTrue("file exists", file.exists());
        assertTrue("file exists", file.getType() != FileType.IMAGINARY);

        // Test a folder
        file = getReadFolder().resolveFile("dir1");
        assertTrue("folder exists", file.exists());
        assertTrue("folder exists", file.getType() != FileType.IMAGINARY);

        // Test an unknown file
        file = getReadFolder().resolveFile("unknown-child");
        assertTrue("unknown file does not exist", !file.exists());
        assertTrue("unknown file does not exist",
            file.getType() == FileType.IMAGINARY);

        // Test an unknown file in an unknown folder
        file = getReadFolder().resolveFile("unknown-folder/unknown-child");
        assertTrue("unknown file does not exist", !file.exists());
        assertTrue("unknown file does not exist",
            file.getType() == FileType.IMAGINARY);
    }
View Full Code Here

    /**
     * Tests root of file system exists.
     */
    public void testRoot() throws FileSystemException
    {
        final FileObject file = getReadFolder().getFileSystem().getRoot();
        assertTrue(file.exists());
        assertTrue(file.getType() != FileType.IMAGINARY);
    }
View Full Code Here

     * Tests parent identity
     */
    public void testParent() throws FileSystemException
    {
        // Test when both exist
        FileObject folder = getReadFolder().resolveFile("dir1");
        FileObject child = folder.resolveFile("file3.txt");
        assertTrue("folder exists", folder.exists());
        assertTrue("child exists", child.exists());
        assertSame(folder, child.getParent());

        // Test when file does not exist
        child = folder.resolveFile("unknown-file");
        assertTrue("folder exists", folder.exists());
        assertTrue("child does not exist", !child.exists());
        assertSame(folder, child.getParent());

        // Test when neither exists
        folder = getReadFolder().resolveFile("unknown-folder");
        child = folder.resolveFile("unknown-file");
        assertTrue("folder does not exist", !folder.exists());
        assertTrue("child does not exist", !child.exists());
        assertSame(folder, child.getParent());

        // Test the parent of the root of the file system
        // TODO - refactor out test cases for layered vs originating fs
        final FileSystem fileSystem = getReadFolder().getFileSystem();
        FileObject root = fileSystem.getRoot();
        if (fileSystem.getParentLayer() == null)
        {
            // No parent layer, so parent should be null
            assertNull("root has null parent", root.getParent());
        }
        else
        {
            // Parent should be parent of parent layer.
            assertSame(fileSystem.getParentLayer().getParent(), root.getParent());
        }
    }
View Full Code Here

     * Tests that children cannot be listed for non-folders.
     */
    public void testChildren() throws FileSystemException
    {
        // Check for file
        FileObject file = getReadFolder().resolveFile("file1.txt");
        assertSame(FileType.FILE, file.getType());
        try
        {
            file.getChildren();
            fail();
        }
        catch (FileSystemException e)
        {
            assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
        }

        // Should be able to get child by name
        file = file.resolveFile("some-child");
        assertNotNull(file);

        // Check for unknown file
        file = getReadFolder().resolveFile("unknown-file");
        assertTrue(!file.exists());
        try
        {
            file.getChildren();
            fail();
        }
        catch (final FileSystemException e)
        {
            assertSameMessage("vfs.provider/list-children-not-folder.error", file, e);
        }

        // Should be able to get child by name
        FileObject child = file.resolveFile("some-child");
        assertNotNull(child);
    }
View Full Code Here

     * Tests content.
     */
    public void testContent() throws Exception
    {
        // Test non-empty file
        FileObject file = getReadFolder().resolveFile("file1.txt");
        assertSameContent(FILE1_CONTENT, file);

        // Test empty file
        file = getReadFolder().resolveFile("empty.txt");
        assertSameContent("", file);
View Full Code Here

     */
    public void testUnknownContent() throws Exception
    {

        // Try getting the content of an unknown file
        FileObject unknownFile = getReadFolder().resolveFile("unknown-file");
        FileContent content = unknownFile.getContent();
        try
        {
            content.getInputStream();
            fail();
        }
View Full Code Here

    /**
     * Tests concurrent reads on a file.
     */
    public void testConcurrentRead() throws Exception
    {
        final FileObject file = getReadFolder().resolveFile("file1.txt");
        assertTrue(file.exists());

        // Start reading from the file
        final InputStream instr = file.getContent().getInputStream();
        try
        {
            // Start reading again
            file.getContent().getInputStream().close();
        }
        finally
        {
            instr.close();
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.vfs.FileObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.