Please use the Apache issue tracking system for new NetBeans issues (https://issues.apache.org/jira/projects/NETBEANS0/issues) !!
View | Details | Raw Unified | Return to bug 91534 | Differences between
and this patch

Collapse All | Expand All

(-)openide/fs/src/org/openide/filesystems/FileUtil.java (+53 lines)
 Lines 1668-1671    Link Here 
1668
            return wrapFileNoCanonicalize(delegate.createFileObject(path));
1668
            return wrapFileNoCanonicalize(delegate.createFileObject(path));
1669
        }
1669
        }
1670
    }
1670
    }
1671
    
1672
    /**
1673
     * Get a data file from the System (configuration) Filesystem with the
1674
     * specified file path, creating it if necessary.
1675
     * 
1676
     * @param The path from the root of the System Filesystem to the file,
1677
     *        including its name and extension
1678
     * @return A FileObject representing a new file created with the specified
1679
     *        path, or one representing the existing file if the file already
1680
     *        was present, or null if an I/O error occurred
1681
     */ 
1682
    public static FileObject getConfigurationFile (String path) {
1683
        if (path == null) {
1684
            throw new NullPointerException ("null path to file");
1685
        }
1686
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
1687
        FileObject root = sfs.getRoot();
1688
        FileObject result = sfs.findResource(path);
1689
        if (result == null) {
1690
            try {
1691
                result = FileUtil.createData(root, path);
1692
            } catch (IOException ioe) {
1693
                Exceptions.printStackTrace(ioe);
1694
            }
1695
        }
1696
        return result;
1697
    }
1698
    
1699
    /**
1700
     * Get a folder in the System (configuration) Filesystem with the
1701
     * specified file path, creating it if necessary.
1702
     * 
1703
     * @param The path from the root of the System Filesystem to the file,
1704
     *        including its name and extension.  If the path is "/", null
1705
     *        or the empty string, returns the root of the system filesystem
1706
     * @return A FileObject representing a new file created with the specified
1707
     *        path, or one representing the existing file if the file already
1708
     *        was present, or null if an I/O error occurred
1709
     */ 
1710
    public static FileObject getConfigurationFolder (String path) {
1711
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
1712
        FileObject root = sfs.getRoot();
1713
        FileObject result = path == null || "/".equals(path) || "".equals(path) ?
1714
            root : sfs.findResource(path);
1715
        if (result == null) {
1716
            try {
1717
                result = FileUtil.createFolder(root, path);
1718
            } catch (IOException ioe) {
1719
                Exceptions.printStackTrace(ioe);
1720
            }
1721
        }
1722
        return result;
1723
    }
1671
}
1724
}
(-)openide/fs/src/org/openide/filesystems/Repository.java (+3 lines)
 Lines 145-151    Link Here 
145
    /**
145
    /**
146
     * Gets the NetBeans default (system, configuration) filesystem.
146
     * Gets the NetBeans default (system, configuration) filesystem.
147
     * @return the default filesystem
147
     * @return the default filesystem
148
     * @deprecated Use FileUtil.getConfigurationFile() or 
149
     *             FileUtil.getConfigurationFolder() instead
148
     */
150
     */
151
    @Deprecated
149
    public final FileSystem getDefaultFileSystem() {
152
    public final FileSystem getDefaultFileSystem() {
150
        return system;
153
        return system;
151
    }
154
    }
(-)openide/fs/test/unit/src/org/openide/filesystems/FileUtilSfsMethodsTest.java (+118 lines)
Added Link Here 
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.openide.filesystems;
21
import java.io.IOException;
22
import junit.framework.TestCase;
23
import org.openide.util.lookup.Lookups;
24
import org.openide.util.lookup.ProxyLookup;
25
/**
26
 * Tests FileUtil.getConfigurationData(), etc.
27
 *
28
 * @author  Tim Boudreau
29
 */
30
public class FileUtilSfsMethodsTest extends TestCase {
31
    
32
    public FileUtilSfsMethodsTest (String name) {
33
        super (name);
34
    }
35
    
36
    protected void setUp() throws java.lang.Exception {
37
        System.setProperty ("org.openide.util.Lookup", 
38
                "org.openide.filesystems.FileUtilSfsMethodsTest$MyLkp");
39
    }
40
    
41
    protected String[] getResources(String testName) {
42
        return new String[] { "somefolder/somefile.txt" };
43
    }
44
45
    public void testGetConfigurationData() throws Exception {
46
        System.out.println("testGetConfigurationData");
47
        FileObject f = FileUtil.getConfigurationFolder("folder");
48
        assertNotNull (f);
49
        FileObject f1 = FileUtil.getConfigurationFile ("folder/file.txt");
50
        assertNotNull (f1);
51
        assertEquals (f1, f.getFileObject ("file.txt"));
52
    }
53
    
54
    public void testCreateConfigurationFile() throws Exception {
55
        System.out.println("testCreateConfigurationFile");
56
        FileObject f = FileUtil.getConfigurationFile("somewhere/file2.txt");
57
        assertNotNull (f);
58
        Exception ioe = null;
59
        FileObject f1 = null;
60
        try {
61
            f1 = FileUtil.getConfigurationFile ("somewhere/file2.txt");
62
        } catch (Exception e) {
63
            ioe = e;
64
        }
65
        assertNull (ioe);
66
        assertNotNull (f1);
67
        assertEquals (f, f1);
68
        ioe = null;
69
        try {
70
            f = FileUtil.getConfigurationFolder ("somewhere");
71
        } catch (Exception e) {
72
            ioe = e;
73
        }
74
        assertNull (ioe);
75
        assertEquals (f, f1.getParent());
76
        
77
        f = FileUtil.getConfigurationFolder("folder");
78
        FileObject nue = f.createData ("hello.txt");
79
        assertEquals (nue, FileUtil.getConfigurationFile("folder/hello.txt"));
80
        assertNotNull (f.getFileObject ("hello.txt"));
81
        assertTrue (f.getFileObject ("hello.txt").isData());
82
        assertFalse (f.getFileObject ("hello.txt").isFolder());
83
    }
84
    
85
    public void testCreateConfigurationFolder() throws Exception {
86
        System.out.println("testCreateConfigurationFolder");
87
        FileObject f = FileUtil.getConfigurationFolder ("other");
88
        assertNotNull (f);
89
        Exception ioe = null;
90
        FileObject f1 = null;
91
        try {
92
            f1 = FileUtil.getConfigurationFolder("other");
93
        } catch (Exception e) {
94
            ioe = e;
95
        }
96
        assertNull (ioe);
97
        assertEquals (f, f1);
98
    }
99
    
100
    private static class MyRepo extends Repository {
101
        static FileSystem sysfs;
102
        public MyRepo () {
103
            super(sysfs = FileUtil.createMemoryFileSystem());
104
            try {
105
                FileObject fld = sysfs.getRoot().createFolder("folder");
106
                fld.createData("file.txt");
107
            } catch (IOException ioe) {
108
                throw new Error (ioe);
109
            }
110
        }
111
    }
112
    
113
    public static final class MyLkp extends ProxyLookup {
114
        public MyLkp () {
115
            super (Lookups.singleton (new MyRepo()));
116
        }
117
    }
118
}

Return to bug 91534
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo