Package org.apache.commons.vfs

Examples of org.apache.commons.vfs.FileObject


     * Tests that folders have no content.
     */
    public void testFolderContent() throws Exception
    {
        // Try getting the content of a folder
        FileObject folder = getReadFolder().resolveFile("dir1");
        try
        {
            folder.getContent().getInputStream();
            fail();
        }
        catch (FileSystemException e)
        {
            assertSameMessage("vfs.provider/read-not-file.error", folder, e);
View Full Code Here


    /**
     * Tests can perform operations on a folder while reading from a different files.
     */
    public void testConcurrentReadFolder() throws Exception
    {
        final FileObject file = getReadFolder().resolveFile("file1.txt");
        assertTrue(file.exists());
        final FileObject folder = getReadFolder().resolveFile("dir1");
        assertTrue(folder.exists());

        // Start reading from the file
        final InputStream instr = file.getContent().getInputStream();
        try
        {
            // Do some operations
            folder.exists();
            folder.getType();
            folder.getChildren();
        }
        finally
        {
            instr.close();
        }
View Full Code Here

        final List expectedFiles = selector.finish();
        assertEquals(expectedFiles.size(), actualFiles.length);
        final int count = expectedFiles.size();
        for (int i = 0; i < count; i++)
        {
            final FileObject expected = (FileObject) expectedFiles.get(i);
            final FileObject actual = actualFiles[i];
            assertEquals(expected, actual);
        }
    }
View Full Code Here

      if (ref == null)
      {
        return null;
      }

      FileObject fo = (FileObject) ref.get();
      if (fo == null)
      {
        removeFile(filesystem, name);
      }
      return fo;
View Full Code Here

     * Determines if a file or folder should be selected.
     */
    public boolean includeFile(final FileSelectInfo fileInfo)
        throws FileSystemException
    {
        final FileObject file = fileInfo.getFile();
        if (file == currentFolder)
        {
            // Pop current folder
            assertEquals(0, children.size());
            currentFolder = currentFolder.getParent();
            currentFolderInfo = currentFolderInfo.getParent();
            children = (Set) stack.remove(0);
        }

        final String baseName = file.getName().getBaseName();

        final FileInfo childInfo = getChild(baseName);
        assertSame(childInfo.type, file.getType());

        final boolean isChild = children.remove(baseName);
        assertTrue(isChild);

        files.add(file);
View Full Code Here

     */
    public boolean traverseDescendents(final FileSelectInfo fileInfo)
        throws FileSystemException
    {
        // Check that the given file is a folder
        final FileObject folder = fileInfo.getFile();
        assertSame(FileType.FOLDER, folder.getType());

        // Locate the info for the folder
        final String baseName = folder.getName().getBaseName();
        if (currentFolder == null)
        {
            assertEquals(rootFile.baseName, baseName);
            currentFolderInfo = rootFile;
        }
        else
        {
            assertSame(currentFolder, folder.getParent());

            // Locate the info for the child, and make sure it is folder
            currentFolderInfo = getChild(baseName);
            assertSame(FileType.FOLDER, currentFolderInfo.type);
        }
View Full Code Here

     * Locates a file by URI.
     */
    public FileObject resolveFile(final File baseFile, final String uri)
        throws FileSystemException
    {
        final FileObject baseFileObj =
            getLocalFileProvider().findLocalFile(baseFile);
        return resolveFile(baseFileObj, uri);
    }
View Full Code Here

     * Resolves a URI, realtive to a base file with specified FileSystem configuration
     */
    public FileObject resolveFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
        throws FileSystemException
    {
        final FileObject realBaseFile;
        if (baseFile != null && VFS.isUriStyle() && baseFile.getName().getType() == FileType.FILE)
        {
            realBaseFile = baseFile.getParent();
        }
        else
        {
            realBaseFile = baseFile;
        }
        // TODO: use resolveName and use this name to resolve the fileObject


        UriParser.checkUriEncoding(uri);

        if (uri == null)
        {
            throw new IllegalArgumentException();
        }

// Extract the scheme
        final String scheme = UriParser.extractScheme(uri);
        if (scheme != null)
        {
// An absolute URI - locate the provider
            final FileProvider provider = (FileProvider) providers.get(scheme);
            if (provider != null)
            {
                return provider.findFile(realBaseFile, uri, fileSystemOptions);
            }
// Otherwise, assume a local file
        }

// Handle absolute file names
        if (localFileProvider != null
            && localFileProvider.isAbsoluteLocalName(uri))
        {
            return localFileProvider.findLocalFile(uri);
        }

        if (scheme != null)
        {
// An unknown scheme - hand it to the default provider
            if (defaultProvider == null)
            {
                throw new FileSystemException("vfs.impl/unknown-scheme.error", new Object[]{scheme, uri});
            }
            return defaultProvider.findFile(realBaseFile, uri, fileSystemOptions);
        }

// Assume a relative name - use the supplied base file
        if (realBaseFile == null)
        {
            throw new FileSystemException("vfs.impl/find-rel-file.error", uri);
        }

        return realBaseFile.resolveFile(uri);
    }
View Full Code Here

    /**
     * Sets up a scratch folder for the test to use.
     */
    protected FileObject createScratchFolder() throws Exception
    {
        FileObject scratchFolder = getWriteFolder();

        // Make sure the test folder is empty
        scratchFolder.delete(Selectors.EXCLUDE_SELF);
        scratchFolder.createFolder();

        return scratchFolder;
    }
View Full Code Here

    /**
     * Tests create-delete-create-a-file sequence on the same file system.
     */
    public void testRenameFile() throws Exception
    {
        final FileObject scratchFolder = createScratchFolder();

        // Create direct child of the test folder
        final FileObject file = scratchFolder.resolveFile("file1.txt");
        assertTrue(!file.exists());

        // Create the source file
        final String content = "Here is some sample content for the file.  Blah Blah Blah.";

        final OutputStream os = file.getContent().getOutputStream();
        try
        {
            os.write(content.getBytes("utf-8"));
        }
        finally
        {
            os.close();
        }
        assertSameContent(content, file);


        // Make sure we can move the new file to another file on the same filesystem

        FileObject fileMove = scratchFolder.resolveFile("file1move.txt");
        assertTrue(!fileMove.exists());

        file.moveTo(fileMove);

        assertTrue(!file.exists());
        assertTrue(fileMove.exists());

        assertSameContent(content, fileMove);

        // Delete the file.
        assertTrue(fileMove.exists());
        assertTrue(fileMove.delete());
    }
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.