Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.masterfs.filebasedfs.fileobjects; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.nio.file.Files; |
47 |
import java.nio.file.Path; |
48 |
import java.nio.file.Paths; |
49 |
import org.netbeans.junit.NbTestCase; |
50 |
import org.openide.filesystems.FileObject; |
51 |
import org.openide.filesystems.FileUtil; |
52 |
|
53 |
/** |
54 |
* Test support for detection and reading of symbolic links. |
55 |
* |
56 |
* @author jhavlin |
57 |
*/ |
58 |
public class FileObjSymlinkTest extends NbTestCase { |
59 |
|
60 |
public FileObjSymlinkTest(String name) { |
61 |
super(name); |
62 |
} |
63 |
|
64 |
@Override |
65 |
protected void setUp() throws Exception { |
66 |
deleteSymlinks(getWorkDir()); |
67 |
clearWorkDir(); |
68 |
} |
69 |
|
70 |
public void testIsSymbolicLink() throws IOException { |
71 |
if (!checkSymlinksSupported()) { |
72 |
return; |
73 |
} |
74 |
File dir = getWorkDir(); |
75 |
Path p = dir.toPath(); |
76 |
Path data = p.resolve("data.dat"); |
77 |
Path link = p.resolve("link.lk"); |
78 |
data.toFile().createNewFile(); |
79 |
Files.createSymbolicLink(link, data); |
80 |
FileObject dirFO = FileUtil.toFileObject(dir); |
81 |
dirFO.refresh(); |
82 |
FileObject dataFO = dirFO.getFileObject("data.dat"); |
83 |
FileObject linkFO = dirFO.getFileObject("link.lk"); |
84 |
assertFalse(dataFO.isSymbolicLink()); |
85 |
assertTrue(linkFO.isSymbolicLink()); |
86 |
} |
87 |
|
88 |
/** |
89 |
* Test isRecursiveSymbolicLink method. Use this folder tree: |
90 |
* <pre> |
91 |
* - workdir |
92 |
* - a |
93 |
* - b |
94 |
* - c (symlink to a) |
95 |
* - d (symlink to e) |
96 |
* - e |
97 |
* </pre> |
98 |
* |
99 |
* @throws java.io.IOException |
100 |
*/ |
101 |
public void testIsRecursiveSymbolicLink() throws IOException { |
102 |
if (!checkSymlinksSupported()) { |
103 |
return; |
104 |
} |
105 |
File dir = getWorkDir(); |
106 |
File a = new File(dir, "a"); |
107 |
File b = new File(a, "b"); |
108 |
File c = new File(b, "c"); |
109 |
File d = new File(b, "d"); |
110 |
File e = new File(dir, "e"); |
111 |
b.mkdirs(); |
112 |
e.mkdirs(); |
113 |
Files.createSymbolicLink(c.toPath(), a.toPath()); |
114 |
Files.createSymbolicLink(d.toPath(), e.toPath()); |
115 |
FileObject dirFO = FileUtil.toFileObject(dir); |
116 |
dirFO.refresh(); |
117 |
FileObject cFO = dirFO.getFileObject("a/b/c"); |
118 |
FileObject dFO = dirFO.getFileObject("a/b/d"); |
119 |
assertTrue(cFO.isRecursiveSymbolicLink()); |
120 |
assertFalse(dFO.isRecursiveSymbolicLink()); |
121 |
assertFalse(dirFO.isRecursiveSymbolicLink()); |
122 |
} |
123 |
|
124 |
/** |
125 |
* Test isRecursiveSymbolicLink method. Use this folder tree: |
126 |
* <pre> |
127 |
* - workdir |
128 |
* - a |
129 |
* - b |
130 |
* - c (symlink to d) |
131 |
* - d (symlink to a) |
132 |
* </pre> |
133 |
* |
134 |
* @throws java.io.IOException |
135 |
*/ |
136 |
public void testIsRecursiveSymbolicLinkIndirect() throws IOException { |
137 |
if (!checkSymlinksSupported()) { |
138 |
return; |
139 |
} |
140 |
File dir = getWorkDir(); |
141 |
File a = new File(dir, "a"); |
142 |
File b = new File(a, "b"); |
143 |
File c = new File(b, "c"); |
144 |
File d = new File(dir, "d"); |
145 |
b.mkdirs(); |
146 |
Files.createSymbolicLink(d.toPath(), a.toPath()); |
147 |
Files.createSymbolicLink(c.toPath(), d.toPath()); |
148 |
FileObject dirFO = FileUtil.toFileObject(dir); |
149 |
FileObject cFO = dirFO.getFileObject("a/b/c"); |
150 |
assertTrue(cFO.isRecursiveSymbolicLink()); |
151 |
} |
152 |
|
153 |
public void testReadSymbolicLinkAbsolute() throws IOException { |
154 |
if (!checkSymlinksSupported()) { |
155 |
return; |
156 |
} |
157 |
File dir = getWorkDir(); |
158 |
File data = new File(dir, "data.dat"); |
159 |
File link = new File(dir, "link.lnk"); |
160 |
data.createNewFile(); |
161 |
Files.createSymbolicLink(link.toPath(), data.toPath()); |
162 |
FileObject linkFO = FileUtil.toFileObject(link); |
163 |
FileObject dataFO = linkFO.readSymbolicLink(); |
164 |
assertNotSame(linkFO, dataFO); |
165 |
assertNotNull(dataFO); |
166 |
assertEquals("data.dat", dataFO.getNameExt()); |
167 |
} |
168 |
|
169 |
public void testReadSymbolicLinkRelative() throws IOException { |
170 |
if (!checkSymlinksSupported()) { |
171 |
return; |
172 |
} |
173 |
File dir = getWorkDir(); |
174 |
File folder = new File(dir, "folder"); |
175 |
File link = new File(dir, "link"); |
176 |
folder.mkdir(); |
177 |
Path lp = Files.createSymbolicLink(link.toPath(), Paths.get("folder")); |
178 |
assertNotNull(lp); |
179 |
FileObject linkFO = FileUtil.toFileObject(link); |
180 |
assertNotNull(linkFO); |
181 |
FileObject dataFO = linkFO.readSymbolicLink(); |
182 |
assertNotSame(linkFO, dataFO); |
183 |
assertNotNull(dataFO); |
184 |
assertEquals("folder", dataFO.getNameExt()); |
185 |
} |
186 |
|
187 |
public void testReadSymbolicLinkPath() throws IOException { |
188 |
if (!checkSymlinksSupported()) { |
189 |
return; |
190 |
} |
191 |
File dir = getWorkDir(); |
192 |
File data = new File(dir, "data.dat"); |
193 |
File link = new File(dir, "link.lnk"); |
194 |
data.createNewFile(); |
195 |
Files.createSymbolicLink(link.toPath(), data.toPath()); |
196 |
FileObject linkFO = FileUtil.toFileObject(link); |
197 |
assertTrue(linkFO.readSymbolicLinkPath().endsWith("data.dat")); |
198 |
} |
199 |
|
200 |
/** |
201 |
* Test getRealFileObject method. |
202 |
* |
203 |
* Use this directory tree: |
204 |
* <pre> |
205 |
* - workdir |
206 |
* - a |
207 |
* - data.dat |
208 |
* - b |
209 |
* - link.lnk (symlink to data.dat) |
210 |
* - c |
211 |
* - link2.lnk (symlink to link.link) |
212 |
* - d |
213 |
* - folderLink (symlink to c) |
214 |
* </pre> |
215 |
* @throws java.io.IOException |
216 |
*/ |
217 |
public void testGetRealFileObject() throws IOException { |
218 |
if (!checkSymlinksSupported()) { |
219 |
return; |
220 |
} |
221 |
File dir = getWorkDir(); |
222 |
File a = new File(dir, "a"); |
223 |
File dataDat = new File(a, "data.dat"); |
224 |
File b = new File(dir, "b"); |
225 |
File linkLnk = new File(b, "link.lnk"); |
226 |
File c = new File(dir, "c"); |
227 |
File link2Lnk = new File(c, "link2.lnk"); |
228 |
File d = new File(dir, "d"); |
229 |
File folderLink = new File(d, "folderLink"); |
230 |
a.mkdir(); |
231 |
dataDat.createNewFile(); |
232 |
b.mkdir(); |
233 |
Files.createSymbolicLink(linkLnk.toPath(), dataDat.toPath()); |
234 |
c.mkdir(); |
235 |
Files.createSymbolicLink(link2Lnk.toPath(), linkLnk.toPath()); |
236 |
d.mkdir(); |
237 |
Files.createSymbolicLink(folderLink.toPath(), c.toPath()); |
238 |
FileObject dirFO = FileUtil.toFileObject(dir); |
239 |
dirFO.refresh(); |
240 |
FileObject fo = dirFO.getFileObject("d/folderLink/link2.lnk"); |
241 |
assertNotNull(fo); |
242 |
FileObject realFO = fo.getRealFileObject(); |
243 |
assertNotNull(realFO); |
244 |
assertTrue(realFO.getPath().endsWith("a/data.dat")); |
245 |
} |
246 |
|
247 |
/** |
248 |
* Recursively delete all symlinks in a directory. |
249 |
*/ |
250 |
private void deleteSymlinks(File root) { |
251 |
for (File f : root.listFiles()) { |
252 |
if (Files.isSymbolicLink(f.toPath())) { |
253 |
f.delete(); |
254 |
} else if (f.isDirectory()) { |
255 |
deleteSymlinks(f); |
256 |
} |
257 |
} |
258 |
} |
259 |
|
260 |
private boolean checkSymlinksSupported() { |
261 |
File dir; |
262 |
try { |
263 |
dir = getWorkDir(); |
264 |
} catch (IOException ex) { |
265 |
printSkipping(); |
266 |
return false; |
267 |
} |
268 |
File a = new File(dir, "a"); |
269 |
try { |
270 |
File lk = new File(dir, "lk"); |
271 |
Files.createSymbolicLink(lk.toPath(), a.toPath()); |
272 |
lk.delete(); |
273 |
return true; |
274 |
} catch (RuntimeException e) { |
275 |
} catch (IOException ex) { |
276 |
} |
277 |
printSkipping(); |
278 |
return false; |
279 |
} |
280 |
|
281 |
private void printSkipping() { |
282 |
System.out.println( |
283 |
"Symbolic links are not supported, skipping test " + getName()); |
284 |
} |
285 |
} |