This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 91534
Collapse All | Expand All

(-)openide/fs/src/org/openide/filesystems/FileUtil.java (+59 lines)
Lines 1709-1712 Link Here
1709
            return wrapFileNoCanonicalize(delegate.createFileObject(path));
1709
            return wrapFileNoCanonicalize(delegate.createFileObject(path));
1710
        }
1710
        }
1711
    }
1711
    }
1712
    
1713
    /**
1714
     * Get a data file or folder from the System (configuration) Filesystem 
1715
     * with the specified file path, creating it if necessary.
1716
     * 
1717
     * @param The path from the root of the System Filesystem to the file,
1718
     *        including its name and extension.  
1719
     * @return A FileObject representing a new file created with the specified
1720
     *        path, or one representing the existing file if the file already
1721
     *        was present, or null when trying to create a non-existent folder
1722
     */ 
1723
    public static FileObject getConfigurationFile (String path, boolean create) {
1724
        if (path == null) {
1725
            throw new NullPointerException ("Null path to file"); //NOI18N
1726
        }
1727
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
1728
        FileObject root = sfs.getRoot();
1729
        FileObject result = root.getFileObject(path);
1730
        assert result == null || result.isData();
1731
        if (result == null && create) {
1732
            try {
1733
                result = FileUtil.createData(root, path);
1734
            } catch (IOException ioe) {
1735
                Exceptions.printStackTrace(ioe);
1736
            }
1737
        }
1738
        return result;
1739
    }
1740
    
1741
    /**
1742
     * Get a folder in the System (configuration) Filesystem with the
1743
     * specified file path, creating it if necessary.
1744
     * 
1745
     * @param The path from the root of the System Filesystem to the file,
1746
     *        including its name and extension.  If the path is the
1747
     *        empty string, returns the root of the system filesystem.
1748
     * @return A FileObject representing a new file created with the specified
1749
     *        path, or one representing the existing file if the file already
1750
     *        was present, or null if an I/O error occurred when trying to
1751
     *        create a non-existent folder
1752
     */ 
1753
    public static FileObject getConfigurationFolder (String path, boolean create) {
1754
        if (path == null) {
1755
            throw new NullPointerException ("Null path");
1756
        }
1757
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
1758
        FileObject root = sfs.getRoot();
1759
        FileObject result = "".equals(path) ?
1760
            root : root.getFileObject(path);
1761
        assert result == null || result.isFolder();
1762
        if (result == null && create) {
1763
            try {
1764
                result = FileUtil.createFolder(root, path);
1765
            } catch (IOException ioe) {
1766
                Exceptions.printStackTrace(ioe);
1767
            }
1768
        }
1769
        return result;
1770
    }
1712
}
1771
}
(-)openide/fs/src/org/openide/filesystems/Repository.java (-1 / +11 lines)
Lines 144-150 Link Here
144
144
145
    /**
145
    /**
146
     * Gets the NetBeans default (system, configuration) filesystem.
146
     * Gets the NetBeans default (system, configuration) filesystem.
147
     * @return the default filesystem
147
     * The System Filesystem contains runtime data that is a merge of
148
     * all of the XML layer files provided by enabled modules, and the
149
     * <code>config/</code> subfolder of the userdir on the user's disk.
150
     * 
151
     * If you want to simply get a file or folder from the system,
152
     * it is simpler to call <a href="FileUtil.html#getConfigurationFile(java.lang.String)">
153
     * FileUtil.getConfigurationFile(path)</a> or 
154
     * <a href="FileUtil.html#getConfigurationFolder(java.lang.String)">
155
     * FileUtil.getConfigurationFolder(path)</a>.
156
     * 
157
     * @return the system filesystem
148
     */
158
     */
149
    public final FileSystem getDefaultFileSystem() {
159
    public final FileSystem getDefaultFileSystem() {
150
        return system;
160
        return system;
(-)openide/fs/test/unit/src/org/openide/filesystems/FileUtilTest.java (-2 / +79 lines)
Lines 20-39 Link Here
20
package org.openide.filesystems;
20
package org.openide.filesystems;
21
21
22
import java.io.File;
22
import java.io.File;
23
import java.io.IOException;
23
import java.net.URL;
24
import java.net.URL;
24
import org.netbeans.junit.MockServices;
25
import org.netbeans.junit.MockServices;
25
import org.netbeans.junit.NbTestCase;
26
import org.netbeans.junit.NbTestCase;
26
import org.openide.util.Utilities;
27
import org.openide.util.Utilities;
28
import org.netbeans.junit.MockServices;
27
29
28
/**
30
/**
29
 * @author Jesse Glick
31
 * @author Jesse Glick, Tim Boudreau
30
 */
32
 */
31
public class FileUtilTest extends NbTestCase {
33
public class FileUtilTest extends NbTestCase {
32
34
33
    public FileUtilTest(String n) {
35
    public FileUtilTest(String n) {
34
        super(n);
36
        super(n);
35
    }
37
    }
36
38
    
39
    protected void setUp() throws java.lang.Exception {
40
        MockServices.setServices(MyRepo.class);
41
    }
42
    
37
    public void testToFileObjectSlash() throws Exception { // #98388
43
    public void testToFileObjectSlash() throws Exception { // #98388
38
        if (!Utilities.isUnix()) {
44
        if (!Utilities.isUnix()) {
39
            return;
45
            return;
Lines 60-64 Link Here
60
            }
66
            }
61
        }
67
        }
62
    }
68
    }
69
    
70
    protected String[] getResources(String testName) {
71
        return new String[] { "somefolder/somefile.txt" };
72
    }
63
73
74
    public void testGetConfigurationData() throws Exception {
75
        System.out.println("testGetConfigurationData");
76
        FileObject f = FileUtil.getConfigurationFolder("folder", true);
77
        assertNotNull (f);
78
        FileObject f1 = FileUtil.getConfigurationFile ("folder/file.txt", true);
79
        assertNotNull (f1);
80
        assertEquals (f1, f.getFileObject ("file.txt"));
81
    }
82
    
83
    public void testCreateConfigurationFile() throws Exception {
84
        System.out.println("testCreateConfigurationFile");
85
        FileObject f = FileUtil.getConfigurationFile("somewhere/file2.txt", true);
86
        assertNotNull (f);
87
        Exception ioe = null;
88
        FileObject f1 = null;
89
        try {
90
            f1 = FileUtil.getConfigurationFile ("somewhere/file2.txt", true);
91
        } catch (Exception e) {
92
            ioe = e;
93
        }
94
        assertNull (ioe);
95
        assertNotNull (f1);
96
        assertEquals (f, f1);
97
        ioe = null;
98
        try {
99
            f = FileUtil.getConfigurationFolder ("somewhere", true);
100
        } catch (Exception e) {
101
            ioe = e;
102
        }
103
        assertNull (ioe);
104
        assertEquals (f, f1.getParent());
105
        
106
        f = FileUtil.getConfigurationFolder("folder", true);
107
        FileObject nue = f.createData ("hello.txt");
108
        assertEquals (nue, FileUtil.getConfigurationFile("folder/hello.txt", true));
109
        assertNotNull (f.getFileObject ("hello.txt"));
110
        assertTrue (f.getFileObject ("hello.txt").isData());
111
        assertFalse (f.getFileObject ("hello.txt").isFolder());
112
    }
113
    
114
    public void testCreateConfigurationFolder() throws Exception {
115
        System.out.println("testCreateConfigurationFolder");
116
        FileObject f = FileUtil.getConfigurationFolder ("other", true);
117
        assertNotNull (f);
118
        Exception ioe = null;
119
        FileObject f1 = null;
120
        try {
121
            f1 = FileUtil.getConfigurationFolder("other", true);
122
        } catch (Exception e) {
123
            ioe = e;
124
        }
125
        assertNull (ioe);
126
        assertEquals (f, f1);
127
    }
128
    
129
    public static class MyRepo extends Repository {
130
        static FileSystem sysfs;
131
        public MyRepo () {
132
            super(sysfs = FileUtil.createMemoryFileSystem());
133
            try {
134
                FileObject fld = sysfs.getRoot().createFolder("folder");
135
                fld.createData("file.txt");
136
            } catch (IOException ioe) {
137
                throw new Error (ioe);
138
            }
139
        }
140
    }
64
}
141
}

Return to bug 91534