alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.masterfs;

import org.openide.filesystems.*;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import org.openide.util.io.NbMarshalledObject;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.ProxyLookup;
import org.openide.util.Lookup;
import org.netbeans.modules.masterfs.providers.FileSystemProvider;
import org.netbeans.modules.masterfs.providers.AutoMountProvider;
import org.netbeans.modules.masterfs.providers.MountSupport;

import javax.swing.filechooser.FileSystemView;

public class MasterFileObjectTestHid extends TestBaseHid{
    private FileObject root;

    public MasterFileObjectTestHid(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        root = testedFS.findResource(getResourcePrefix());
    }

    protected String[] getResources(String testName) {
        return new String[] {"testdir/ignoredir/nextdir/", 
                             "testdir/mountdir/nextdir/",
                             "testdir/mountdir2/nextdir/",
                             "testdir/mountdir2/fname/notestfile",
                             "testdir/mountdir2/fname2/testfile",
                             "testdir/mountdir3/.nbattrs",
                             "testdir/mountdir4/file.ext"
                             
        };
    }

    public void testAttrsFiles () throws Exception {
        assertNotNull(root);
        FileObject attributes = root.getFileObject("testdir/mountdir3/.nbattrs");
        assertNotNull(attributes);
        assertTrue(attributes.isData());
    }
    
    public void testToFileObject () throws Exception {
        assertNotNull(root);

        File rootFile = FileUtil.toFile (root);
        File mnt1 = new File (rootFile, "testdir/mountdir2/fname");
        
        IgnoreDirFileSystem fs1 = new IgnoreDirFileSystem();
        
        fs1.setRootDirectory(mnt1);
        MountTable.getDefault().mount(mnt1.getAbsolutePath(), fs1);
        
        File test = new File (rootFile, "testdir/mountdir2/fname2/testfile");
        assertNotNull (FileUtil.toFileObject(test));                        
    }
    
    public void testFileUtilFromFile () throws Exception {        
        assertNotNull(root);
        
        File f = FileUtil.normalizeFile(getWorkDir());
        IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
        ifs.setRootDirectory(f);
        
        Repository.getDefault().addFileSystem(ifs);
        Repository.getDefault().addFileSystem(testedFS);
        
        FileObject[] fos = FileUtil.fromFile(f);
        assertTrue(fos.length > 0);
        assertEquals(fos[0].getFileSystem(), testedFS );
        
    }

    public void testIssue45485 () {
        assertNotNull(root);        
        final FileObject testdir = root.getFileObject("testdir.");        
        assertNull(testdir);        
    }
    
    public void testMount1 () throws Exception {
        final List events = new ArrayList ();
        assertNotNull(root);
        final FileObject ignore = root.getFileObject("testdir/ignoredir");
        assertNotNull(ignore);
        assertTrue(ignore.isFolder());
        MountTable mt = MountTable.getDefault();
        FileObject fo = root.getFileObject("testdir").getParent();
        File f = FileUtil.normalizeFile(FileUtil.toFile(fo));
        IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
        ifs.setRootDirectory(f);
        
        mt.mount(f.getAbsolutePath(), ifs);            
        
        assertNotNull(root.getFileObject("testdir/ignoredir"));        
        mt.unmount(ifs);
        FileObject file = root.getFileObject("testdir/ignoredir");
        assertNotNull(file);
        assertTrue(file.isFolder());
        assertNotNull(file.getParent());
        assertTrue(FileUtil.isParentOf(root, file));        
    }

    public void testDeleteMountPoint () throws Exception {
        assertNotNull(root);
        FileObject mountDir = root.getFileObject("testdir/mountdir");
        assertNotNull(mountDir);
        File mountFile = FileUtil.normalizeFile(FileUtil.toFile(mountDir));
        IgnoreDirFileSystem ifs = new IgnoreDirFileSystem();
        ifs.setRootDirectory(mountFile);
        
        MountTable mt = MountTable.getDefault();
        mt.mount(mountFile.getAbsolutePath(), ifs);
        
        mountDir.delete();
        assertTrue(!mountDir.isValid());
        assertNull(root.getFileObject("testdir/mountdir"));
    }
    
    public void testFindResourceCaptureExteralChanges () throws Exception {
        String externalName = "newfile.external";        
        assertNotNull(root);
        FileObject fileObject = root.getFileObject("testdir");        
        assertNotNull(fileObject);
        File f = FileUtil.toFile(fileObject);
        
        assertNull(testedFS.findResource(new File (f, externalName).getAbsolutePath().replace('\\',File.separatorChar)));
        assertNull(fileObject.getFileObject(externalName));
        
        assertNotNull(f);
        f = FileUtil.normalizeFile(f);
        assertTrue(f.exists());
        f = new File (f, externalName);
        assertTrue(!f.exists());       
        assertTrue(f.getAbsolutePath(),f.createNewFile());
        assertNotNull(testedFS.findResource(f.getAbsolutePath().replace('\\',File.separatorChar)));
    }

    public void testGetFileObjectCaptureExteralChanges () throws Exception {
        String externalName = "newfile.external2";        
        assertNotNull(root);
        FileObject fileObject = root.getFileObject("testdir");        
        assertNotNull(fileObject);
        File f = FileUtil.toFile(fileObject);
        
        assertNull(testedFS.findResource(new File (f, externalName).getAbsolutePath().replace('\\',File.separatorChar)));
        assertNull(fileObject.getFileObject(externalName));
        
        assertNotNull(f);
        f = FileUtil.normalizeFile(f);
        assertTrue(f.exists());
        f = new File (f, externalName);
        assertTrue(!f.exists());        
        assertTrue(f.getAbsolutePath(),f.createNewFile());
        assertNotNull(fileObject.getFileObject(externalName));        
    }

    public void testGetFileObject47885 () throws Exception {
        assertNotNull(root);
        
        FileObject fileObject = root.getFileObject("testdir/mountdir4/file.ext");        
        assertNotNull(fileObject);
        
        fileObject = root.getFileObject("testdir/mountdir4/file", "ext");        
        assertNull(fileObject);
        
        fileObject = root.getFileObject("testdir\\mountdir4\\file.ext");        
        assertNull(fileObject);
    }
    
    
    public void testValidRoots () throws Exception {
        FileSystemView fsv = FileSystemView.getFileSystemView();
        
        File[] roots = File.listRoots();
        for (int i = 0; i < roots.length; i++) {
            FileObject root = FileUtil.toFileObject(roots[i]);
            if (fsv.isFloppyDrive(roots[i]) || !roots[i].exists()) {
               assertNull(root);
               continue; 
            }
            
            assertNotNull(roots[i].getAbsolutePath (),root);
            assertEquals(testedFS, root.getFileSystem());
            assertTrue(root.isValid());
        }
        assertNotNull(testedFS.getRoot());    
        assertTrue(testedFS.getRoot().isValid());            
    }
    
    public void testDeserializationOfMasterFSLeadsToTheSameFileSystem () throws Exception {
        NbMarshalledObject stream = new NbMarshalledObject (testedFS);
        Object obj = stream.get ();
        assertSame ("After deserialization it is still the same", testedFS, obj);
    }
    
    
    public void testInitialization () throws Exception {
        assertTrue (MasterFileSystemTest.TestFSProvider.DEFAULT.isInitailized());    
    }
    
    
    private class IgnoreDirFileSystem extends LocalFileSystem {
        protected String[] children(String name) {
            String[] strings = super.children(name);
            return strings;
        }
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.