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 39415
Collapse All | Expand All

(-)src/org/openide/filesystems/FileUtil.java (+23 lines)
Lines 272-277 Link Here
272
     * @param file File whose coresponding FileObjects will be looked for
272
     * @param file File whose coresponding FileObjects will be looked for
273
     * @return corresponding FileObjects or empty array  if no 
273
     * @return corresponding FileObjects or empty array  if no 
274
     * corresponding FileObject exists.
274
     * corresponding FileObject exists.
275
     * @see #findBestFileObject
275
     * @since 1.29
276
     * @since 1.29
276
     */        
277
     */        
277
    public static FileObject[] fromFile (File file) {
278
    public static FileObject[] fromFile (File file) {
Lines 316-321 Link Here
316
        return results;            
317
        return results;            
317
    }
318
    }
318
319
320
    /**
321
     * Finds the best FileObject for the given File. If there is multiple
322
     * FileObjects available for the File (use {@link #fromFile} method
323
     * to list them) the FileObject with minimal number of path components
324
     * will be choosen.
325
     *
326
     * @param file File for which the FileObject will be searched
327
     * @return the most suitable FileObject or null if there is not any
328
     * @since X.XX
329
     */
330
    public static FileObject findBestFileObject(File file) {
331
        // XXX: consider replacing fromFile() call
332
        // with more efficient algorithm
333
        FileObject fos[] = fromFile(file);
334
        FileObject shortest = fos.length > 0 ? fos[0] : null;
335
        for (int i=1; i<fos.length; i++) {
336
            if (fos[i].getPath().length() <  shortest.getPath().length()) {
337
                shortest = fos[i];
338
            }
339
        }
340
        return shortest;
341
    }
319
342
320
    
343
    
321
    
344
    
(-)src/org/openide/filesystems/URLMapper.java (+24 lines)
Lines 101-106 Link Here
101
     * <code> findURL(FileObject fo, int type) </code>.
101
     * <code> findURL(FileObject fo, int type) </code>.
102
     * @param url to wanted FileObjects
102
     * @param url to wanted FileObjects
103
     * @return a suitable arry of FileObjects, or empty array if not successful
103
     * @return a suitable arry of FileObjects, or empty array if not successful
104
     * @see #findBestFileObject
104
     * @since  2.22*/
105
     * @since  2.22*/
105
    public static FileObject[] findFileObjects (URL url) {
106
    public static FileObject[] findFileObjects (URL url) {
106
        /** first basic implementation */
107
        /** first basic implementation */
Lines 121-126 Link Here
121
        return retVal;
122
        return retVal;
122
    }
123
    }
123
124
125
    /**
126
     * Finds the best FileObject for the given URL. If there is multiple
127
     * FileObjects available for the URL (use {@link #findFileObjects} method
128
     * to list them) the FileObject with minimal number of path components
129
     * will be choosen.
130
     *
131
     * @param url URL for which the FileObject will be searched
132
     * @return the most suitable FileObject or null if there is not any
133
     * @since X.XX
134
     */
135
    public static FileObject findBestFileObject(URL url) {
136
        // XXX: consider replacing findFileObjects() call
137
        // with more efficient algorithm
138
        FileObject fos[] = findFileObjects(url);
139
        FileObject shortest = fos.length > 0 ? fos[0] : null;
140
        for (int i=1; i<fos.length; i++) {
141
            if (fos[i].getPath().length() <  shortest.getPath().length()) {
142
                shortest = fos[i];
143
            }
144
        }
145
        return shortest;
146
    }
147
    
124
    /** Get an array of FileObjects for this url
148
    /** Get an array of FileObjects for this url
125
     * @param url to wanted FileObjects
149
     * @param url to wanted FileObjects
126
     * @return a suitable arry of FileObjects, or null
150
     * @return a suitable arry of FileObjects, or null
(-)test/unit/src/org/openide/filesystems/FileUtilTest.java (+90 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.filesystems;
15
16
import java.io.File;
17
import java.util.ArrayList;
18
import org.netbeans.junit.NbTestCase;
19
20
/**
21
 * Test functionality of FileUtils methods.
22
 *
23
 * @author David Konecny
24
 */
25
public class FileUtilTest extends NbTestCase {
26
    
27
    public FileUtilTest(String name) {
28
        super(name);
29
    }
30
    
31
    public void testFindBestFileObject() throws Exception {
32
        // This tests creates three subfolders in working directory and a file
33
        // in each subfolder. Then it mounts all subfolders (including the working 
34
        // dir) into repository as individual (overlapping) filesystems and 
35
        // tests that "best" fileobject is returned for misc io.Files.
36
        File workdir = getWorkDir();
37
        String path = workdir.getAbsolutePath() + "/folder1/folder2/folder3";
38
        File f = new File(path);
39
        f.mkdirs();
40
        
41
        path = workdir.getAbsolutePath() + "/folder1/file1.txt";
42
        f = new File(path);
43
        f.createNewFile();
44
        path = workdir.getAbsolutePath() + "/folder1/folder2/file2.txt";
45
        f = new File(path);
46
        f.createNewFile();
47
        path = workdir.getAbsolutePath() + "/folder1/folder2/folder3/file3.txt";
48
        f = new File(path);
49
        f.createNewFile();
50
        
51
        LocalFileSystem lfs = new LocalFileSystem();
52
        File folder = workdir;
53
        lfs.setRootDirectory(folder);
54
        Repository.getDefault().addFileSystem(lfs);
55
        lfs = new LocalFileSystem();
56
        folder = new File(folder, "folder1");
57
        lfs.setRootDirectory(folder);
58
        Repository.getDefault().addFileSystem(lfs);
59
        lfs = new LocalFileSystem();
60
        folder = new File(folder, "folder2");
61
        lfs.setRootDirectory(folder);
62
        Repository.getDefault().addFileSystem(lfs);
63
        lfs = new LocalFileSystem();
64
        folder = new File(folder, "folder3");
65
        lfs.setRootDirectory(folder);
66
        Repository.getDefault().addFileSystem(lfs);
67
        
68
        String base = workdir.getAbsolutePath();
69
        FileObject fos[] = FileUtil.fromFile(new File(base+"/folder1/folder2/folder3/file3.txt"));
70
        ArrayList list = new ArrayList();
71
        for (int i=0; i<fos.length; i++) {
72
            list.add(fos[i].getPath());
73
        }
74
        assertTrue("list="+list, list.remove("folder1/folder2/folder3/file3.txt"));
75
        assertTrue(list.remove("folder2/folder3/file3.txt"));
76
        assertTrue(list.remove("folder3/file3.txt"));
77
        assertTrue(list.remove("file3.txt"));
78
        assertEquals(0, list.size());
79
        
80
        assertEquals("file3.txt", FileUtil.findBestFileObject(new File(base+"/folder1/folder2/folder3/file3.txt")).getPath());
81
        assertEquals("", FileUtil.findBestFileObject(new File(base+"/folder1/folder2/folder3")).getPath());
82
        assertEquals("file2.txt", FileUtil.findBestFileObject(new File(base+"/folder1/folder2/file2.txt")).getPath());
83
        assertEquals("", FileUtil.findBestFileObject(new File(base+"/folder1/folder2")).getPath());
84
        assertEquals("file1.txt", FileUtil.findBestFileObject(new File(base+"/folder1/file1.txt")).getPath());
85
        assertEquals("", FileUtil.findBestFileObject(new File(base+"/folder1")).getPath());
86
        assertEquals("", FileUtil.findBestFileObject(new File(base)).getPath());
87
        
88
    }
89
    
90
}
(-)test/unit/src/org/openide/filesystems/URLMapperTest.java (+60 lines)
Lines 17-22 Link Here
17
import java.io.FileOutputStream;
17
import java.io.FileOutputStream;
18
import java.io.OutputStream;
18
import java.io.OutputStream;
19
import java.net.URL;
19
import java.net.URL;
20
import java.util.ArrayList;
20
import java.util.Arrays;
21
import java.util.Arrays;
21
import java.util.Collections;
22
import java.util.Collections;
22
import java.util.jar.JarEntry;
23
import java.util.jar.JarEntry;
Lines 72-77 Link Here
72
        assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL));
73
        assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL));
73
        assertEquals("correct URL -> FO for root", Collections.singletonList(rootFO), Arrays.asList(URLMapper.findFileObjects(rootU)));
74
        assertEquals("correct URL -> FO for root", Collections.singletonList(rootFO), Arrays.asList(URLMapper.findFileObjects(rootU)));
74
        assertEquals("correct URL -> FO for " + textPath, Collections.singletonList(textFO), Arrays.asList(URLMapper.findFileObjects(textU)));
75
        assertEquals("correct URL -> FO for " + textPath, Collections.singletonList(textFO), Arrays.asList(URLMapper.findFileObjects(textU)));
76
    }
77
    
78
    public void testFindBestFileObject() throws Exception {
79
        // This tests creates three subfolders in working directory and a file
80
        // in each subfolder. Then it mounts all subfolders (including the working 
81
        // dir) into repository as individual (overlapping) filesystems and 
82
        // tests that "best" fileobject is returned for misc URLs.
83
        File workdir = getWorkDir();
84
        String path = workdir.getAbsolutePath() + "/folder1/folder2/folder3";
85
        File f = new File(path);
86
        f.mkdirs();
87
        
88
        path = workdir.getAbsolutePath() + "/folder1/file1.txt";
89
        f = new File(path);
90
        f.createNewFile();
91
        path = workdir.getAbsolutePath() + "/folder1/folder2/file2.txt";
92
        f = new File(path);
93
        f.createNewFile();
94
        path = workdir.getAbsolutePath() + "/folder1/folder2/folder3/file3.txt";
95
        f = new File(path);
96
        f.createNewFile();
97
        
98
        LocalFileSystem lfs = new LocalFileSystem();
99
        File folder = workdir;
100
        lfs.setRootDirectory(folder);
101
        Repository.getDefault().addFileSystem(lfs);
102
        lfs = new LocalFileSystem();
103
        folder = new File(folder, "folder1");
104
        lfs.setRootDirectory(folder);
105
        Repository.getDefault().addFileSystem(lfs);
106
        lfs = new LocalFileSystem();
107
        folder = new File(folder, "folder2");
108
        lfs.setRootDirectory(folder);
109
        Repository.getDefault().addFileSystem(lfs);
110
        lfs = new LocalFileSystem();
111
        folder = new File(folder, "folder3");
112
        lfs.setRootDirectory(folder);
113
        Repository.getDefault().addFileSystem(lfs);
114
        
115
        String base = workdir.toURI().toURL().toExternalForm();
116
        FileObject fos[] = URLMapper.findFileObjects(new URL(base+"/folder1/folder2/folder3/file3.txt"));
117
        ArrayList list = new ArrayList();
118
        for (int i=0; i<fos.length; i++) {
119
            list.add(fos[i].getPath());
120
        }
121
        assertTrue("list="+list, list.remove("folder1/folder2/folder3/file3.txt"));
122
        assertTrue(list.remove("folder2/folder3/file3.txt"));
123
        assertTrue(list.remove("folder3/file3.txt"));
124
        assertTrue(list.remove("file3.txt"));
125
        assertEquals(0, list.size());
126
        
127
        assertEquals("file3.txt", URLMapper.findBestFileObject(new URL(base+"/folder1/folder2/folder3/file3.txt")).getPath());
128
        assertEquals("", URLMapper.findBestFileObject(new URL(base+"/folder1/folder2/folder3")).getPath());
129
        assertEquals("file2.txt", URLMapper.findBestFileObject(new URL(base+"/folder1/folder2/file2.txt")).getPath());
130
        assertEquals("", URLMapper.findBestFileObject(new URL(base+"/folder1/folder2")).getPath());
131
        assertEquals("file1.txt", URLMapper.findBestFileObject(new URL(base+"/folder1/file1.txt")).getPath());
132
        assertEquals("", URLMapper.findBestFileObject(new URL(base+"/folder1")).getPath());
133
        assertEquals("", URLMapper.findBestFileObject(new URL(base)).getPath());
134
        
75
    }
135
    }
76
    
136
    
77
}
137
}

Return to bug 39415