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.upgrade; |
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Arrays; |
48 |
import org.junit.Before; |
49 |
import org.junit.Test; |
50 |
import org.openide.filesystems.FileObject; |
51 |
import org.openide.filesystems.FileSystem; |
52 |
import org.openide.filesystems.FileUtil; |
53 |
import org.openide.filesystems.LocalFileSystem; |
54 |
|
55 |
public class CopyFilesTest extends org.netbeans.junit.NbTestCase { |
56 |
|
57 |
public CopyFilesTest(String name) { |
58 |
super(name); |
59 |
} |
60 |
|
61 |
@Before |
62 |
@Override |
63 |
public void setUp() throws Exception { |
64 |
super.setUp(); |
65 |
clearWorkDir(); |
66 |
} |
67 |
|
68 |
@Test |
69 |
public void testCopyDeep() throws Exception { |
70 |
ArrayList<String> fileList = new ArrayList<String>(); |
71 |
fileList.addAll(Arrays.asList(new java.lang.String[]{"source/foo/X.txt", |
72 |
"source/foo/A.txt", "source/foo/B.txt", "source/foo/foo2/C.txt"})); |
73 |
|
74 |
FileSystem fs = createLocalFileSystem(fileList.toArray(new String[fileList.size()])); |
75 |
|
76 |
FileObject path = fs.findResource("source"); |
77 |
assertNotNull(path); |
78 |
FileObject tg = fs.getRoot().createFolder("target"); |
79 |
assertNotNull(tg); |
80 |
FileObject patterns = FileUtil.createData(fs.getRoot(), "source/foo/etc/patterns.import"); |
81 |
assertNotNull(patterns); |
82 |
String pattern = "# ignore comment\n" |
83 |
+ "include foo/.*\n" |
84 |
+ "translate foo=>bar\n"; |
85 |
writeTo(fs, "source/foo/etc/patterns.import", pattern); |
86 |
|
87 |
org.netbeans.upgrade.CopyFiles.copyDeep(FileUtil.toFile(path), FileUtil.toFile(tg), FileUtil.toFile(patterns)); |
88 |
|
89 |
assertNotNull("file not copied: " + "foo/X.txt", tg.getFileObject("bar/X.txt")); |
90 |
assertNotNull("file not copied: " + "foo/A.txt", tg.getFileObject("bar/A.txt")); |
91 |
assertNotNull("file not copied: " + "foo/B.txt", tg.getFileObject("bar/B.txt")); |
92 |
assertNotNull("file not copied: " + "foo/foo2/C.txt", tg.getFileObject("bar/foo2/C.txt")); |
93 |
} |
94 |
|
95 |
private static void writeTo (FileSystem fs, String res, String content) throws java.io.IOException { |
96 |
FileObject fo = org.openide.filesystems.FileUtil.createData (fs.getRoot (), res); |
97 |
org.openide.filesystems.FileLock lock = fo.lock (); |
98 |
java.io.OutputStream os = fo.getOutputStream (lock); |
99 |
os.write (content.getBytes ()); |
100 |
os.close (); |
101 |
lock.releaseLock (); |
102 |
} |
103 |
|
104 |
public LocalFileSystem createLocalFileSystem(String[] resources) throws IOException { |
105 |
File mountPoint = new File(getWorkDir(), "tmpfs"); |
106 |
mountPoint.mkdir(); |
107 |
|
108 |
for (int i = 0; i < resources.length; i++) { |
109 |
File f = new File (mountPoint,resources[i]); |
110 |
if (f.isDirectory() || resources[i].endsWith("/")) { |
111 |
FileUtil.createFolder(f); |
112 |
} else { |
113 |
FileUtil.createData(f); |
114 |
} |
115 |
} |
116 |
|
117 |
LocalFileSystem lfs = new LocalFileSystem(); |
118 |
try { |
119 |
lfs.setRootDirectory(mountPoint); |
120 |
} catch (Exception ex) {} |
121 |
|
122 |
return lfs; |
123 |
} |
124 |
} |