Java Code Examples for org.openide.filesystems.FileSystem#getRoot()

The following examples show how to use org.openide.filesystems.FileSystem#getRoot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of getInputStream method, of class org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj.
 */
public void testGetInputStream() {
    File f = testFile;
    FileSystem fs = FileBasedFileSystem.getInstance();
    
    FileObject root = fs.getRoot();
    assertNotNull(root);
    
    Enumeration<? extends FileObject> en = root.getFolders(true);
    for (int i = 0; i < 10 && en.hasMoreElements(); i++) {
        FileObject fo = (FileObject) en.nextElement();
        assertTrue(fo.isFolder());
        assertFalse(fo.isData());
        try {
            fo.getInputStream();
            fail ();
        } catch (FileNotFoundException e) {
            
        }

    }
}
 
Example 2
Source File: FolderObjTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of getOutputStream method, of class org.netbeans.modules.masterfs.filebasedfs.fileobjects.FolderObj.
 */
public void testGetOutputStream() {
    File f = testFile;
    FileSystem fs = FileBasedFileSystem.getInstance();
    
    FileObject root = fs.getRoot();
    assertNotNull(root);
    
    Enumeration<? extends FileObject> en = root.getFolders(true);
    for (int i = 0; i < 10 && en.hasMoreElements(); i++) {
        FileObject fo = (FileObject) en.nextElement();
        assertTrue(fo.isFolder());
        assertFalse(fo.isData());
        try {
            fo.getOutputStream(fo.lock());
            fail ();
        } catch (IOException e) {
            
        } finally {
            
        }

    }
}
 
Example 3
Source File: FolderComparatorTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void testNaturalComparator(String[] fileNames,
        String[] expectedOrder) throws IOException {
    FolderComparator c = new FolderComparator(FolderComparator.NATURAL);
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject root = fs.getRoot();
    List<DataObject> list = new ArrayList<DataObject>();
    for (String n : fileNames) {
        FileObject fo = root.createData(n);
        assertNotNull(fo);
        list.add(DataObject.find(fo));
    }

    Collections.sort(list, c);
    for (int i = 0; i < expectedOrder.length; i++) {
        assertEquals(expectedOrder[i], list.get(i).getName());
    }
}
 
Example 4
Source File: ModuleUtilsTest.java    From cakephp3-netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of isChild method, of class ModuleUtils.
 *
 * @throws java.io.IOException
 */
@Test
public void testIsChild_FileObject_FileObject() throws IOException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject root = fs.getRoot();
    FileObject parent1 = root.createFolder("parent1");
    FileObject child1 = parent1.createData("child1.txt");
    FileObject parent2 = root.createFolder("parent2");
    assertTrue(ModuleUtils.isChild(parent1, child1));

    assertFalse(ModuleUtils.isChild(parent2, child1));
    assertFalse(ModuleUtils.isChild(null, child1));
    assertFalse(ModuleUtils.isChild(parent2, (FileObject) null));
    assertFalse(ModuleUtils.isChild(child1, parent1));
    child1.delete();
    parent1.delete();
    parent2.delete();
}
 
Example 5
Source File: DotcakeTest.java    From cakephp3-netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Test of isDotcake method, of class Dotcake.
 *
 * @throws java.io.IOException
 */
@Test
public void testIsDotcake() throws IOException {
    FileSystem fileSystem = FileUtil.createMemoryFileSystem();
    FileObject root = fileSystem.getRoot();
    FileObject dotcakeFile = root.createData(".cake");
    assertTrue(Dotcake.isDotcake(dotcakeFile));
    if (dotcakeFile != null) {
        dotcakeFile.delete();
        dotcakeFile = null;
    }

    dotcakeFile = root.createData(".cake2");
    assertFalse(Dotcake.isDotcake(dotcakeFile));
    if (dotcakeFile != null) {
        dotcakeFile.delete();
        dotcakeFile = null;
    }

    assertFalse(Dotcake.isDotcake(null));

}
 
Example 6
Source File: FileObjectCrawlerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testDuplicateRandomTest() throws IOException {
    if (!RUN_RANDOM_TESTS) {
        return;
    }
    final FileSystem fs = FileUtil.createMemoryFileSystem();
    String[] paths = generateRandomTree(TREE_DEPTH,TREE_CHILD_COUNT);
    FileObject root = fs.getRoot();
    populateFolderStructure(root, paths);
    Random r = new Random(System.currentTimeMillis());
    for (int runCount=0; runCount<TEST_COUNT; runCount++) {
        System.out.println("Run: " + runCount);
        final Set<FileObject> testSet = new HashSet<FileObject>();
        while (testSet.size() < SINGLE_TEST_SET_SIZE) {
            final String s = paths[r.nextInt(paths.length)];
            FileObject fo = root.getFileObject(s);
            final int shift = r.nextInt(TREE_DEPTH);
            for (int i = 0; i<shift; i++) {
                fo = fo.getParent();
            }
            testSet.add(fo);
        }
        FileObjectCrawler crawler = new FileObjectCrawler(
            root,
            testSet.toArray(new FileObject[testSet.size()]),
            EnumSet.<Crawler.TimeStampAction>of(Crawler.TimeStampAction.UPDATE),
            null,
            CR,
            SuspendSupport.NOP);
        assertCollectedFiles(
                "Wrong files collected for: " + testSet,
                crawler.getResources(),
                crawl(testSet, root));
    }
}
 
Example 7
Source File: DotcakeTest.java    From cakephp3-netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test of fromJson method, of class Dotcake.
 *
 * @throws java.io.IOException
 */
@Test
public void testFromJson() throws IOException {
    FileSystem fileSystem = FileUtil.createMemoryFileSystem();
    FileObject root = fileSystem.getRoot();
    FileObject dotcakeFile = dotcakeDirectory.getFileObject(".cake");

    // normal .cake file
    Dotcake dotcake = Dotcake.fromJson(dotcakeFile);
    assertNotNull(dotcake);
    if (dotcakeFile != null) {
        dotcakeFile = null;
    }

    // empty file
    dotcakeFile = root.createData(".cake");
    dotcake = Dotcake.fromJson(dotcakeFile);
    assertNull(dotcake);
    if (dotcakeFile != null) {
        dotcakeFile.delete();
        dotcakeFile = null;
    }

    dotcakeFile = root.createData(".dotcake");
    dotcake = Dotcake.fromJson(dotcakeFile);
    assertNull(dotcake);
    if (dotcakeFile != null) {
        dotcakeFile.delete();
        dotcakeFile = null;
    }

    dotcake = Dotcake.fromJson(null);
    assertNull(dotcake);
    if (dotcakeFile != null) {
        dotcakeFile.delete();
        dotcakeFile = null;
    }
}
 
Example 8
Source File: CompoundSearchInfoTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testOneItemFilesList() throws IOException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fsRoot = fs.getRoot();

    FileObject dir = fsRoot.createFolder("dir");
    dir.createData("a", DummyDataLoader.dummyExt);
    dir.createData("b", DummyDataLoader.dummyExt);
    dir.createData("c", DummyDataLoader.dummyExt);
    DataFolder folder = DataFolder.findFolder(dir);

    SearchInfo.Files refSearchInfo;
    SearchInfo.Files testSearchInfo;
    Iterator refIt;
    Iterator testIt;
    Set testSet = new HashSet();

    refSearchInfo = new SimpleSearchInfo(folder, false, null);
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo});
    assertTrue(testSearchInfo.canSearch());

    for(testIt = testSearchInfo.filesToSearch(); testIt.hasNext();){
        testSet.add(testIt.next());
    }
    refIt = refSearchInfo.filesToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    assertTrue(testSet.isEmpty());

    refSearchInfo = new SimpleSearchInfo(folder, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo});
    assertEquals(refSearchInfo.canSearch(), testSearchInfo.canSearch());
}
 
Example 9
Source File: CompoundSearchInfoTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testOneItemList() throws IOException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fsRoot = fs.getRoot();
    
    FileObject dir = fsRoot.createFolder("dir");
    dir.createData("a", DummyDataLoader.dummyExt);
    dir.createData("b", DummyDataLoader.dummyExt);
    dir.createData("c", DummyDataLoader.dummyExt);
    DataFolder folder = DataFolder.findFolder(dir);
    
    
    SearchInfo refSearchInfo;
    SearchInfo testSearchInfo;
    Iterator refIt;
    Iterator testIt;

    Set testSet = new HashSet();
    
    refSearchInfo = new SimpleSearchInfo(folder, false, null);
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo});
    assertTrue(testSearchInfo.canSearch());
    
    for(testIt = testSearchInfo.objectsToSearch(); testIt.hasNext();){
        testSet.add(testIt.next());
    }
    refIt = refSearchInfo.objectsToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    assertTrue(testSet.isEmpty());

    refSearchInfo = new SimpleSearchInfo(folder, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo});
    assertEquals(refSearchInfo.canSearch(), testSearchInfo.canSearch());
}
 
Example 10
Source File: TestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected Document[] createDocuments(String... fileName) {
    try {
        List<Document> docs = new ArrayList<Document>();
        FileSystem memFS = FileUtil.createMemoryFileSystem();
        for (String fName : fileName) {

            //we may also create folders
            StringTokenizer items = new StringTokenizer(fName, "/");
            FileObject fo = memFS.getRoot();
            while (items.hasMoreTokens()) {
                String item = items.nextToken();
                if (items.hasMoreTokens()) {
                    //folder
                    fo = fo.createFolder(item);
                } else {
                    //last, create file
                    fo = fo.createData(item);
                }
                assertNotNull(fo);
            }

            DataObject dobj = DataObject.find(fo);
            assertNotNull(dobj);

            EditorCookie cookie = dobj.getCookie(EditorCookie.class);
            assertNotNull(cookie);

            Document document = (Document) cookie.openDocument();
            assertEquals(0, document.getLength());

            docs.add(document);

        }
        return docs.toArray(new Document[]{});
    } catch (Exception ex) {
        throw new IllegalStateException("Error setting up tests", ex);
    }
}
 
Example 11
Source File: ELTestBase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected Document[] createDocuments(String... fileName) {
    try {
        List<Document> docs = new ArrayList<Document>();
        FileSystem memFS = FileUtil.createMemoryFileSystem();
        for (String fName : fileName) {

            //we may also create folders
            StringTokenizer items = new StringTokenizer(fName, "/");
            FileObject fo = memFS.getRoot();
            while (items.hasMoreTokens()) {
                String item = items.nextToken();
                if (items.hasMoreTokens()) {
                    //folder
                    fo = fo.createFolder(item);
                } else {
                    //last, create file
                    fo = fo.createData(item);
                }
                assertNotNull(fo);
            }

            DataObject dobj = DataObject.find(fo);
            assertNotNull(dobj);

            EditorCookie cookie = dobj.getCookie(EditorCookie.class);
            assertNotNull(cookie);

            Document document = (Document) cookie.openDocument();
            assertEquals(0, document.getLength());

            docs.add(document);

        }
        return docs.toArray(new Document[]{});
    } catch (Exception ex) {
        throw new IllegalStateException("Error setting up tests", ex);
    }
}
 
Example 12
Source File: WritableXMLFileSystemTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRemoveAttr() throws Exception { // #199189
    Layer l = new Layer("");
    FileSystem fs = l.read();
    FileObject r = fs.getRoot();
    FileObject f = r.createData("f");
    f.setAttribute("a", "v");
    assertEquals("    <file name=\"f\">\n        <attr name=\"a\" stringvalue=\"v\"/>\n    </file>\n", l.write());
    FileSystem mfs = new MultiFileSystem(fs);
    f = mfs.findResource("f");
    f.setAttribute("a", null);
    assertEquals("    <file name=\"f\"/>\n", l.write());
}
 
Example 13
Source File: ModuleSystem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
final void init(FileSystem systemFileSystem) throws IOException {
    if (Boolean.getBoolean("org.netbeans.core.startup.ModuleSystem.CULPRIT")) Thread.dumpStack(); // NOI18N
    PropertyChangeListener l = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent ev) {
            if (ModuleManager.PROP_CLASS_LOADER.equals(ev.getPropertyName())) {
                org.netbeans.core.startup.MainLookup.systemClassLoaderChanged(mgr.getClassLoader());
            }
        }
    };
    mgr.addPropertyChangeListener(l);
    
    // now initialize to core/* classloader, later we reassign to all modules
    org.netbeans.core.startup.MainLookup.systemClassLoaderChanged(installer.getClass ().getClassLoader ());
    // #28465: initialize module lookup early
    org.netbeans.core.startup.MainLookup.moduleLookupReady(mgr.getModuleLookup());
    if (systemFileSystem.isReadOnly()) {
        list = null;
    } else {
        FileObject root = systemFileSystem.getRoot();
        FileObject modulesFolder = root.getFileObject("Modules"); // NOI18N
        if (modulesFolder == null) {
            modulesFolder = root.createFolder("Modules"); // NOI18N
        }
        list = new ModuleList(mgr, modulesFolder, ev);
        installer.registerList(list);
        installer.registerManager(mgr);
    }
    ev.log(Events.CREATED_MODULE_SYSTEM);
}
 
Example 14
Source File: ModificationResultTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void prepareTest(String code) throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject root = fs.getRoot();
    testFile = FileUtil.createData(root, "test/test.java");
    
    TestUtilities.copyStringToFile(testFile, code);
    
    DataObject od = DataObject.find(testFile);
    
    ces = (CloneableEditorSupport) od.getCookie(EditorCookie.class);
}
 
Example 15
Source File: DataShadowUpdatesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * The test simulates a situation in which a .shadow file in the user
 * configuration is reverted, and an original contents/atributes,
 * with a different target, are revealed.
 * <p/>
 * The DataShadow should at least produce the appropriate cookies.
 * 
 * @throws Exception T
 */
public void testShadowFileReverted() throws Exception {

    clearWorkDir();
    
    File wd = getWorkDir();
    File homeDir = new File(wd, "home");
    File localDir = new File(wd, "data");
    localDir.mkdirs();
    homeDir.mkdirs();
    
    LocalFileSystem lfs1 = new LocalFileSystem();
    lfs1.setRootDirectory(homeDir);
    FileObject r = lfs1.getRoot();

    Class c = SystemFileSystem.class;
    Method m = c.getDeclaredMethod("create", File.class, File.class, File[].class);
    m.setAccessible(true);
    
    FileSystem mfs = (FileSystem)m.invoke(null,
            localDir,
            homeDir,
            new File[] {} 
    );
    
    FileObject file1 = r.createData("org-openide-actions-NewTemplateAction.instance");
    FileObject file2 = r.createData("org-openide-actions-SaveAsTemplateAction.instance");
    FileObject shadow = r.createData("file.shadow");
    shadow.setAttribute("originalFile", "org-openide-actions-NewTemplateAction.instance");
    
    FileObject mfsRoot = mfs.getRoot();
    FileObject mfsShadow = mfsRoot.getFileObject("file.shadow");
    // must create the file contents, otherwise canRevert does not work either
    mfsShadow.getOutputStream().close();
    mfsShadow.setAttribute("originalFile", "org-openide-actions-SaveAsTemplateAction.instance");
    
    DataShadow s = (DataShadow)DataObject.find(mfsShadow);
    assertEquals(
            file2.getPath(),
            s.getOriginal().getPrimaryFile().getPath());
    
    assertTrue(mfsShadow.canRevert());
    
    InstanceCookie ic = s.getCookie(InstanceCookie.class);
    assertSame(SaveAsTemplateAction.class, ic.instanceClass());
    
    mfsShadow.revert();
    
    s = (DataShadow)DataObject.find(mfsShadow);
    ic = s.getCookie(InstanceCookie.class);
    assertSame(NewTemplateAction.class, ic.instanceClass());
    assertEquals(
            file1.getPath(),
            s.getOriginal().getPrimaryFile().getPath());
    
}
 
Example 16
Source File: BootClassPathUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static ClassPath getModuleBootOnJDK8() throws Exception {
    if (moduleBootOnJDK8 == null) {
        List<FileSystem> roots = new ArrayList<>();

        FileSystem output = FileUtil.createMemoryFileSystem();
        FileObject sink = output.getRoot();

        roots.add(output);

        Set<String> packages = new HashSet<>();
        for (FileObject r : getBootClassPath().getRoots()) {
            FileObject javaDir = r.getFileObject("java");

            if (javaDir == null)
                continue;

            roots.add(r.getFileSystem());

            Enumeration<? extends FileObject> c = javaDir.getChildren(true);

            while (c.hasMoreElements()) {
                FileObject current = c.nextElement();

                if (!current.isData() || !current.hasExt("class")) continue;

                String rel = FileUtil.getRelativePath(r, current.getParent());

                packages.add(rel.replace('/', '.'));
            }
        }

        FileSystem outS = new MultiFileSystem(roots.toArray(new FileSystem[0])) {
            {
                setSystemName("module-boot");
            }
        };

        Repository.getDefault().addFileSystem(outS);

        StringBuilder moduleInfo = new StringBuilder();

        moduleInfo.append("module java.base {\n");

        for (String pack : packages) {
            moduleInfo.append("    exports " + pack + ";\n");
        }

        moduleInfo.append("}\n");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        FileObject javaBase = outS.getRoot();

        try (JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
             JFMImpl fmImpl = new JFMImpl(fm, javaBase, sink)) {
            compiler.getTask(null, fmImpl, null, Arrays.asList("-proc:none"), null,
                             Arrays.asList(new SimpleJavaFileObject(new URI("mem:///module-info.java"), javax.tools.JavaFileObject.Kind.SOURCE) {
                @Override
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                    return moduleInfo.toString();
                }
            })).call();
        }

        javaBase.refresh();
        moduleBootOnJDK8 = ClassPathSupport.createClassPath(javaBase);
    }

    return moduleBootOnJDK8;
}
 
Example 17
Source File: CompoundSearchInfoTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testMultipleItemsList() throws IOException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fsRoot = fs.getRoot();
    
    FileObject dir1 = fsRoot.createFolder("dir1");
    dir1.createData("1a", DummyDataLoader.dummyExt);
    dir1.createData("1b", DummyDataLoader.dummyExt);
    dir1.createData("1c", DummyDataLoader.dummyExt);
    DataFolder folder1 = DataFolder.findFolder(dir1);
    
    FileObject dir2 = fsRoot.createFolder("dir2");
    dir2.createData("2a", DummyDataLoader.dummyExt);
    dir2.createData("2b", DummyDataLoader.dummyExt);
    DataFolder folder2 = DataFolder.findFolder(dir2);
    
    
    SearchInfo refSearchInfo1, refSearchInfo2;
    SearchInfo testSearchInfo;
    Iterator refIt;
    Iterator testIt;
    
    refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
    refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                              refSearchInfo2});
    assertTrue(testSearchInfo.canSearch());
    

    Set testSet = new HashSet();
    for(testIt = testSearchInfo.objectsToSearch(); testIt.hasNext();){
        testSet.add(testIt.next());
    }
    refIt = refSearchInfo1.objectsToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    refIt = refSearchInfo2.objectsToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    
    assertTrue(testSet.isEmpty());
    
    refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
    refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                              refSearchInfo2});
    assertTrue(testSearchInfo.canSearch());
    
    testSet.clear();
    for(testIt = testSearchInfo.objectsToSearch(); testIt.hasNext();){
        testSet.add(testIt.next());
    }
    refIt = refSearchInfo1.objectsToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    assertTrue(testSet.isEmpty());
    
    
    refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                              refSearchInfo2});
    assertTrue(testSearchInfo.canSearch());
    
    testSet.clear();
    for(testIt = testSearchInfo.objectsToSearch(); testIt.hasNext();){
        testSet.add(testIt.next());
    }
    refIt = refSearchInfo2.objectsToSearch();
    while (refIt.hasNext()) {
        assertTrue(testSet.remove(refIt.next()));
    }
    assertTrue(testSet.isEmpty());
    
    
    refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
        public boolean canSearch() {
            return false;
        }
    };
    testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                              refSearchInfo2});
    assertFalse(testSearchInfo.canSearch());
}
 
Example 18
Source File: CompoundSearchInfoTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testMultipleItemsFilesList() throws IOException {
     FileSystem fs = FileUtil.createMemoryFileSystem();
     FileObject fsRoot = fs.getRoot();

     FileObject dir1 = fsRoot.createFolder("dir1");
     dir1.createData("1a", DummyDataLoader.dummyExt);
     dir1.createData("1b", DummyDataLoader.dummyExt);
     dir1.createData("1c", DummyDataLoader.dummyExt);
     DataFolder folder1 = DataFolder.findFolder(dir1);

     FileObject dir2 = fsRoot.createFolder("dir2");
     dir2.createData("2a", DummyDataLoader.dummyExt);
     dir2.createData("2b", DummyDataLoader.dummyExt);
     DataFolder folder2 = DataFolder.findFolder(dir2);


     SearchInfo.Files refSearchInfo1, refSearchInfo2;
     SearchInfo.Files testSearchInfo;
     Iterator refIt;
     Iterator testIt;

     refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
     refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
     testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                               refSearchInfo2});
     assertTrue(testSearchInfo.canSearch());

     Set testSet = new HashSet();
     for(testIt = testSearchInfo.filesToSearch(); testIt.hasNext();){
         testSet.add(testIt.next());
     }

     refIt = refSearchInfo1.filesToSearch();
     while (refIt.hasNext()) {
         assertTrue(testSet.remove(refIt.next()));
     }
     refIt = refSearchInfo2.filesToSearch();
     while (refIt.hasNext()) {
         assertTrue(testSet.remove(refIt.next()));
     }
     assertTrue(testSet.isEmpty());


     refSearchInfo1 = new SimpleSearchInfo(folder1, false, null);
     refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
         public boolean canSearch() {
             return false;
         }
     };
     testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                               refSearchInfo2});
     assertTrue(testSearchInfo.canSearch());

     testSet.clear();
     for(testIt = testSearchInfo.filesToSearch(); testIt.hasNext();){
         testSet.add(testIt.next());
     }

     refIt = refSearchInfo1.filesToSearch();
     while (refIt.hasNext()) {
         assertTrue(testSet.remove(refIt.next()));
     }
     assertTrue(testSet.isEmpty());


     refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
         public boolean canSearch() {
             return false;
         }
     };
     refSearchInfo2 = new SimpleSearchInfo(folder2, false, null);
     testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                               refSearchInfo2});
     assertTrue(testSearchInfo.canSearch());

     testSet.clear();
     for(testIt = testSearchInfo.filesToSearch(); testIt.hasNext();){
         testSet.add(testIt.next());
     }
     refIt = refSearchInfo2.filesToSearch();
     while (refIt.hasNext()) {
         assertTrue(testSet.remove(refIt.next()));
     }
     assertTrue(testSet.isEmpty());


     refSearchInfo1 = new SimpleSearchInfo(folder1, false, null) {
         public boolean canSearch() {
             return false;
         }
     };
     refSearchInfo2 = new SimpleSearchInfo(folder2, false, null) {
         public boolean canSearch() {
             return false;
         }
     };
     testSearchInfo = new CompoundSearchInfo(new SearchInfo[] {refSearchInfo1,
                                                               refSearchInfo2});
     assertFalse(testSearchInfo.canSearch());
}
 
Example 19
Source File: BootClassPathUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static ClassPath getModuleBootOnJDK8() throws Exception {
    if (moduleBootOnJDK8 == null) {
        List<FileSystem> roots = new ArrayList<>();

        FileSystem output = FileUtil.createMemoryFileSystem();
        FileObject sink = output.getRoot();

        roots.add(output);

        Set<String> packages = new HashSet<>();
        for (FileObject r : getBootClassPath().getRoots()) {
            FileObject javaDir = r.getFileObject("java");

            if (javaDir == null)
                continue;

            roots.add(r.getFileSystem());

            Enumeration<? extends FileObject> c = javaDir.getChildren(true);

            while (c.hasMoreElements()) {
                FileObject current = c.nextElement();

                if (!current.isData() || !current.hasExt("class")) continue;

                String rel = FileUtil.getRelativePath(r, current.getParent());

                packages.add(rel.replace('/', '.'));
            }
        }

        moduleBootOnJDK8FileSystem = new MultiFileSystem(roots.toArray(new FileSystem[0])) {
            {
                setSystemName("module-boot");
            }
        };

        StringBuilder moduleInfo = new StringBuilder();

        moduleInfo.append("module java.base {\n");

        for (String pack : packages) {
            moduleInfo.append("    exports " + pack + ";\n");
        }

        moduleInfo.append("}\n");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        FileObject javaBase = moduleBootOnJDK8FileSystem.getRoot();

        try (JavaFileManager fm = compiler.getStandardFileManager(null, null, null);
             JFMImpl fmImpl = new JFMImpl(fm, javaBase, sink)) {
            compiler.getTask(null, fmImpl, null, Arrays.asList("-proc:none"), null,
                             Arrays.asList(new SimpleJavaFileObject(new URI("mem:///module-info.java"), javax.tools.JavaFileObject.Kind.SOURCE) {
                @Override
                public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                    return moduleInfo.toString();
                }
            })).call();
        }

        javaBase.refresh();
        moduleBootOnJDK8 = ClassPathSupport.createClassPath(javaBase);
    }

    //Ensure the filesystem is registered in the current Repository:
    Repository.getDefault().removeFileSystem(moduleBootOnJDK8FileSystem);
    Repository.getDefault().addFileSystem(moduleBootOnJDK8FileSystem);

    return moduleBootOnJDK8;
}
 
Example 20
Source File: BinaryUsagesTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private FileObject[] findFileObjectsInRepository(File f) {
    if (!f.equals(FileUtil.normalizeFile(f))) {
        throw new IllegalArgumentException(
            "Parameter file was not " + // NOI18N
            "normalized. Was " + f + " instead of " + FileUtil.normalizeFile(f)
        ); // NOI18N
    }

    @SuppressWarnings("deprecation") // keep for backward compatibility w/ NB 3.x
    Enumeration<? extends FileSystem> en = Repository.getDefault().getFileSystems();
    List<FileObject> list = new LinkedList<FileObject>();
    String fileName = f.getAbsolutePath();

    while (en.hasMoreElements()) {
        FileSystem fs = en.nextElement();
        String rootName = null;
        FileObject fsRoot = fs.getRoot();
        File root = findFileInRepository(fsRoot);

        if (root == null) {
            Object rootPath = fsRoot.getAttribute("FileSystem.rootPath"); //NOI18N

            if ((rootPath != null) && (rootPath instanceof String)) {
                rootName = (String) rootPath;
            } else {
                continue;
            }
        }

        if (rootName == null) {
            rootName = root.getAbsolutePath();
        }

        /**root is parent of file*/
        if (fileName.indexOf(rootName) == 0) {
            String res = fileName.substring(rootName.length()).replace(File.separatorChar, '/');
            FileObject fo = fs.findResource(res);
            File file2Fo = (fo != null) ? findFileInRepository(fo) : null;
            if ((fo != null) && (file2Fo != null) && f.equals(file2Fo)) {
                list.add(fo);
            }
        }
    }
    FileObject[] results = new FileObject[list.size()];
    list.toArray(results);
    return results;
}