Java Code Examples for org.openide.filesystems.FileUtil#createMemoryFileSystem()

The following examples show how to use org.openide.filesystems.FileUtil#createMemoryFileSystem() . 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: 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 2
Source File: DefaultDataObjectTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private DataObject createDataObjectWithContent(byte singleByteContent)
        throws IOException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fob = fs.getRoot().createData("test.txt");
    OutputStream os = fob.getOutputStream();
    try {
        os.write(new byte[]{singleByteContent});
    } finally {
        os.close();
    }
    DataObject dob = DataObject.find(fob);
    if (!(dob instanceof DefaultDataObject)) {
        fail("Expected an instance of DefaultDataObject");
    }
    return dob;
}
 
Example 3
Source File: JavaI18nFinderTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fo = fs.getRoot().createData("JFrame.java");
    
    source = convertStreamToString(getClass().getResourceAsStream("resources/JFrame.txt"));
    
    writeFile(source, fo);
    DataObject sourceDataObject = DataObject.find(fo);
    
    EditorCookie editorCookie = sourceDataObject.getCookie(EditorCookie.class);        
    if (editorCookie == null) {
        throw new IllegalArgumentException("I18N: Illegal data object type"+ sourceDataObject); // NOI18N
    }        
    StyledDocument document = editorCookie.getDocument();
    while(document == null) {
        document = editorCookie.openDocument();
    }        
                    
    instance = new JavaI18nFinder(document);
}
 
Example 4
Source File: GsfDataObjectTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testSetModifiedNestedChange() throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject f = fs.getRoot().createData("index.test");
    DataObject dob = DataObject.find(f);
    assertEquals("The right object", GsfDataObject.class, dob.getClass());
    dob.getLookup().lookup(EditorCookie.class).openDocument().insertString(0,
            "modified", null);
    assertTrue("Should be modified.", dob.isModified());
    dob.addPropertyChangeListener(new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            String s = evt.getPropertyName();
            if (DataObject.PROP_MODIFIED.equals(s) && !dob.isModified()) {
                dob.setModified(true);
            }
        }
    });
    dob.setModified(false);
    assertTrue("Should be still modified.", dob.isModified());
    assertNotNull("Still should have save cookie.",
            dob.getLookup().lookup(SaveCookie.class));
}
 
Example 5
Source File: ProjectOperations.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void rememberConfigurations () {
    FileObject fo = project.getProjectDirectory().getFileObject(ProjectConfigurations.CONFIG_PROPS_PATH);
    if (fo != null) {
        //Has configurations
        try {
            FileSystem fs = FileUtil.createMemoryFileSystem();
            FileUtil.copyFile(fo, fs.getRoot(),fo.getName());
            fo = project.getProjectDirectory().getFileObject("nbproject/private/configs");      //NOI18N
            if (fo != null && fo.isFolder()) {
                FileObject cfgs = fs.getRoot().createFolder("configs");                         //NOI18N
                for (FileObject child : fo.getChildren()) {
                    FileUtil.copyFile(child, cfgs, child.getName());
                }
            }
            configs = fs;
        } catch (IOException ioe) {
            Exceptions.printStackTrace(ioe);
        }
    }
}
 
Example 6
Source File: HudsonInstanceImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static FileSystem getFileSystemFromCache(Map<String,Reference<RemoteFileSystem>> cache, String key, Callable<RemoteFileSystem> create) {
    synchronized (cache) {
        RemoteFileSystem fs = cache.containsKey(key) ? cache.get(key).get() : null;
        if (fs == null) {
            try {
                fs = create.call();
                if (fs == null) {
                    return null;
                }
                cache.put(key, new WeakReference<RemoteFileSystem>(fs));
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
                return FileUtil.createMemoryFileSystem();
            }
        }
        return fs;
    }
}
 
Example 7
Source File: DataFolderIndexTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testIndexCookieOfferedOnlyWhenAppropriate() throws Exception {
    Node n = df.getNodeDelegate();
    assertNotNull("have an index cookie on SFS", n.getLookup().lookup(Index.class));
    LocalFileSystem lfs = new LocalFileSystem();
    lfs.setRootDirectory(getWorkDir());
    n = DataFolder.findFolder(lfs.getRoot()).getNodeDelegate();
    assertNull("have no index cookie on a local folder", n.getLookup().lookup(Index.class));
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject x = fs.getRoot().createFolder("x");
    FileObject y = fs.getRoot().createFolder("y");
    y.setAttribute("DataFolder.Index.reorderable", Boolean.TRUE);
    n = DataFolder.findFolder(x).getNodeDelegate();
    assertNull("have no index cookie on a random folder in a random FS", n.getLookup().lookup(Index.class));
    n = DataFolder.findFolder(y).getNodeDelegate();
    assertNotNull("do have index cookie if magic attr is set", n.getLookup().lookup(Index.class));
}
 
Example 8
Source File: DesignSupportTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws IOException {
    FileSystem ms = FileUtil.createMemoryFileSystem();
    fo = ms.getRoot().createData("my.wsmode");
    OutputStream os = fo.getOutputStream();
    FileUtil.copy(DesignSupportTest.class.getResourceAsStream("testWsmode.xml"), os);
    os.close();
}
 
Example 9
Source File: RepositoryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static FileSystem createDefFs() {
    try
    {
        FileSystem writeFs = FileUtil.createMemoryFileSystem();
        FileSystem layerFs = new XMLFileSystem(RepositoryImpl.class.getClassLoader().getResource("org/netbeans/modules/db/resources/mf-layer.xml"));
        return new MultiFileSystem(new FileSystem[] { writeFs, layerFs });
    } catch (SAXException e) {
        return null;
    }
}
 
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<>();
        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: SystemFileSystemTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testContentOfFileSystemIsInfluencedByLookup () throws Exception {
    // XXX move to org.netbeans.core.startup.layers.SystemFileSystemTest
    FileSystem mem = FileUtil.createMemoryFileSystem();
    String dir = "/yarda/own/file";
    org.openide.filesystems.FileUtil.createFolder (mem.getRoot (), dir);
    
    assertNull ("File is not there yet", FileUtil.getConfigFile (dir));
    MainLookup.register (mem);
    try {
        assertNotNull ("The file is there now", FileUtil.getConfigFile (dir));
    } finally {
        MainLookup.unregister (mem);
    }
    assertNull ("File is no longer there", FileUtil.getConfigFile (dir));
}
 
Example 12
Source File: GsfDataObjectTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testSetModifiedRemovesSaveCookie() throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject f = fs.getRoot().createData("index.test");
    DataObject dob = DataObject.find(f);
    assertEquals("The right object", GsfDataObject.class, dob.getClass());

    dob.getLookup().lookup(EditorCookie.class).openDocument().insertString(0,
            "modified", null);
    assertTrue("Should be modified.", dob.isModified());
    dob.setModified(false);
    assertFalse("Should not be modified.", dob.isModified());
    assertNull("Should not have SaveCookie.",
            dob.getLookup().lookup(SaveCookie.class));
}
 
Example 13
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 14
Source File: ArchetypeTemplateHandlerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Before
public void setUpMethod() throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileUtil.createData(fs.getRoot(), "root/wizard/src/main/java/com/yourorg/somepkg/MyClass.java");
    FileUtil.createData(fs.getRoot(), "root/wizard/src/main/java/com/yourorg/somepkg/Main.java");
    FileUtil.createData(fs.getRoot(), "root/wizard/src/main/webapp/index.html");
    root = fs.findResource("root");
    assertNotNull(root);
}
 
Example 15
Source File: DataShadowTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that findOriginal throws Exception on a malformed 
 * shadow
 * @throws Exception 
 */
public void testFindBrokenOriginal() throws Exception {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject orig = FileUtil.createData(fs.getRoot(), "path/to/orig");
    FileObject shadow = FileUtil.createData(fs.getRoot(), "link.shadow");
    try {
        DataShadow.findOriginal(shadow);
        fail("IOException should be thrown on malformed shadow");
    } catch (IOException ex) {
        // this is oK
    }
}
 
Example 16
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 17
Source File: RepositoryImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static FileSystem createDefFs() {
    try
    {
        FileSystem writeFs = FileUtil.createMemoryFileSystem();
        FileSystem layerFs = new XMLFileSystem(RepositoryImpl.class.getClassLoader().getResource("org/netbeans/modules/css/resources/layer.xml"));
        return new MultiFileSystem(new FileSystem[] { writeFs, layerFs });
    } catch (SAXException e) {
        return null;
    }
}
 
Example 18
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 19
Source File: BinaryForSourceQuery2Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testQuery2() throws Exception {
    MockServices.setServices(SampleQuery.class);
    SampleQuery sampleQuery = Lookup.getDefault().lookup(SampleQuery.class);
    assertNotNull("Instance of SampleQuery is registered", sampleQuery);

    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject data = FileUtil.createData(fs.getRoot(), "fldr/data.txt");
    URL url = data.toURL();


    BinaryForSourceQuery.Result2 result = BinaryForSourceQuery.findBinaryRoots2(url);

    assertNotNull("Queried now", sampleQuery);
    assertTrue("prefers binaries", result.preferBinaries());

    URL[] roots = result.getRoots();
    assertEquals("One", 1, roots.length);
    assertEquals("Same", roots[0], url);

    SampleQuery.PrivateData privateData = (SampleQuery.PrivateData) BinaryForSourceQuery.CACHE.findRegistered(new SampleQuery.PrivateData(url, null));
    assertNotNull("Internal data found", privateData);

    ChangeListener listener = (ChangeEvent e) -> {};
    result.addChangeListener(listener);
    assertEquals(privateData.listener, listener);
    result.removeChangeListener(listener);
    assertNull(privateData.listener);

    BinaryForSourceQuery.Result2 result2 = BinaryForSourceQuery.findBinaryRoots2(url);
    assertSame("The result should be cached", result, result2);
}
 
Example 20
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;
}