KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > CopyTest


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.netbeans.upgrade;
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.*;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31 import org.openide.filesystems.FileObject;
32
33 import org.openide.filesystems.FileSystem;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.LocalFileSystem;
36 import org.openide.filesystems.MultiFileSystem;
37 import org.openide.filesystems.XMLFileSystem;
38
39 /** Tests to check that copy of files works.
40  *
41  * @author Jaroslav Tulach
42  */

43 public final class CopyTest extends org.netbeans.junit.NbTestCase {
44     public CopyTest (String JavaDoc name) {
45         super (name);
46     }
47     
48     protected void setUp() throws java.lang.Exception JavaDoc {
49         super.setUp();
50         
51         clearWorkDir();
52     }
53    
54     public void testAppendSelectedLines() throws Exception JavaDoc {
55         //setup
56
List expectedLines = new ArrayList();
57         File JavaDoc wDir = getWorkDir();
58         File JavaDoc sFile = new File JavaDoc(wDir,this.getName()+".file");
59         assertTrue(sFile.createNewFile());
60         File JavaDoc tFolder = new File JavaDoc(wDir,this.getName());
61         assertTrue(tFolder.mkdir());
62         File JavaDoc tFile = new File JavaDoc(tFolder,this.getName()+".file");
63         assertTrue(tFile.createNewFile());
64         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tFile);
65         try {
66             String JavaDoc line = "nbplatform.default.harness.dir=${nbplatform.default.netbeans.dest.dir}/harness \n";
67             fos.write(line.getBytes());
68             expectedLines.add(line);
69         } finally {
70             fos.close();
71         }
72         
73         fos = new FileOutputStream JavaDoc(sFile);
74         try {
75             String JavaDoc line = "nbplatform.id.netbeans.dest.dir=/work/nball/nbbuild/netbeans \n";
76             fos.write(line.getBytes());
77             expectedLines.add(line);
78             
79             line = "nbplatform.id.netbeans.dest.dir=/work/nbide/netbeans \n";
80             fos.write(line.getBytes());
81             expectedLines.add(line);
82             
83             line = "nbplatform.default.netbeans.dest.dir=/work/nbide/netbeans \n";
84             fos.write(line.getBytes());
85             //lines.add(line); -- should be excluded
86
} finally {
87             fos.close();
88         }
89         String JavaDoc[] regexForSelection = new String JavaDoc[] {
90             "^nbplatform[.](?![dD]efault).+[.](netbeans[.]dest[.]dir|label|harness[.]dir)=.+$"//NOI18N
91
};
92         
93         Copy.appendSelectedLines(sFile, tFolder, regexForSelection);
94         String JavaDoc line = null;
95         List resultLines = new ArrayList();
96         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new FileReader JavaDoc(tFile));
97         try {
98             while ((line = reader.readLine()) != null) {
99                 resultLines.add(line+"\n");
100             }
101         } finally {
102             reader.close();
103         }
104         assertEquals(expectedLines,resultLines);
105     }
106     
107     public void testDoesSomeCopy () throws Exception JavaDoc {
108         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
109             "root/X.txt",
110             "root/Y.txt",
111             "nonroot/Z.txt"
112         });
113         
114         FileObject fo = fs.findResource ("root");
115         FileObject tg = fs.getRoot().createFolder ("target");
116         
117         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
118         set.add ("X.txt");
119           Copy.copyDeep (fo, tg, set);
120
121         assertEquals ("One file copied", 1, tg.getChildren().length);
122         String JavaDoc n = tg.getChildren ()[0].getNameExt();
123         assertEquals ("Name is X.txt", "X.txt", n);
124         
125     }
126     
127     public void testDoesDeepCopy () throws Exception JavaDoc {
128         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
129             "root/subdir/X.txt",
130             "root/Y.txt",
131             "nonroot/Z.txt"
132         });
133         
134         FileObject fo = fs.findResource ("root");
135         FileObject tg = fs.getRoot().createFolder ("target");
136         
137         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
138         set.add ("subdir/X.txt");
139         Copy.copyDeep (fo, tg, set);
140         
141         assertEquals ("One file copied", 1, tg.getChildren().length);
142         assertEquals ("Name is X.txt", "subdir", tg.getChildren ()[0].getNameExt());
143         assertEquals ("One children of one child", 1, tg.getChildren()[0].getChildren().length);
144         assertEquals ("X.txt", "X.txt", tg.getChildren()[0].getChildren()[0].getNameExt());
145         
146     }
147     
148     public void testCopyAttributes () throws Exception JavaDoc {
149         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
150             "root/X.txt",
151             "root/Y.txt",
152             "nonroot/Z.txt"
153         });
154         FileObject x = fs.findResource ("root/X.txt");
155         x.setAttribute ("ahoj", "yarda");
156         
157         FileObject fo = fs.findResource ("root");
158         FileObject tg = fs.getRoot().createFolder ("target");
159         
160         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
161         set.add ("X.txt");
162         Copy.copyDeep (fo, tg, set);
163         
164         assertEquals ("One file copied", 1, tg.getChildren().length);
165         assertEquals ("Name is X.txt", "X.txt", tg.getChildren ()[0].getNameExt());
166         assertEquals ("attribute copied", "yarda", tg.getChildren()[0].getAttribute("ahoj"));
167     }
168     
169     public void testCopyFolderAttributes () throws Exception JavaDoc {
170         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
171             "root/sub/X.txt",
172             "root/Y.txt",
173             "nonroot/Z.txt"
174         });
175         FileObject x = fs.findResource ("root/sub");
176         x.setAttribute ("ahoj", "yarda");
177         
178         FileObject fo = fs.findResource ("root");
179         FileObject tg = fs.getRoot().createFolder ("target");
180         
181         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
182         set.add ("sub");
183         set.add ("sub/X.txt");
184         Copy.copyDeep (fo, tg, set);
185         
186         assertEquals ("One file copied", 1, tg.getChildren().length);
187         assertEquals ("Name of the dir is sub", "sub", tg.getChildren ()[0].getNameExt());
188         assertEquals ("attribute copied", "yarda", tg.getChildren()[0].getAttribute("ahoj"));
189         assertEquals ("X.txt", "X.txt", tg.getChildren()[0].getChildren()[0].getNameExt());
190     }
191     
192     public void testDoNotCopyEmptyDirs () throws Exception JavaDoc {
193         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
194             "root/sub/X.txt",
195             "root/Y.txt",
196             "nonroot/Z.txt"
197         });
198         FileObject x = fs.findResource ("root/sub");
199         
200         FileObject fo = fs.findResource ("root");
201         FileObject tg = fs.getRoot().createFolder ("target");
202         
203         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
204         Copy.copyDeep (fo, tg, set);
205         
206         assertEquals ("Nothing copied", 0, tg.getChildren().length);
207     }
208     
209     public void testDoNotOverwriteFiles () throws Exception JavaDoc {
210         java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc ();
211         set.add ("X.txt");
212         
213         FileSystem fs = createLocalFileSystem (new String JavaDoc[] {
214             "root/project/X.txt",
215             "root/X.txt",
216             "nonroot/Z.txt"
217         });
218         
219         writeTo (fs, "root/project/X.txt", "content-project");
220         writeTo (fs, "root/X.txt", "content-global");
221
222         FileObject tg = fs.getRoot().createFolder ("target");
223         
224         FileObject project = fs.findResource ("root/project");
225         Copy.copyDeep (project, tg, set);
226         
227         
228         
229         FileObject root = fs.findResource ("root");
230         Copy.copyDeep (root, tg, set);
231
232         
233         FileObject x = tg.getFileObject ("X.txt");
234         assertNotNull ("File copied", x);
235         
236         byte[] arr = new byte[300];
237         int len = x.getInputStream ().read (arr);
238         String JavaDoc content = new String JavaDoc (arr, 0, len);
239         
240         assertEquals ("The content is kept from project", content, "content-project");
241     }
242     
243     public void testDoesCopyHiddenFiles () throws Exception JavaDoc {
244         String JavaDoc[] res = {
245             "root/Yes.txt",
246             "root/X.txt_hidden",
247         };
248         LocalFileSystem fs = createLocalFileSystem (res);
249         URL JavaDoc url = getClass().getResource("layer5.5.xml");
250         assertNotNull("found sample layer", url);
251         XMLFileSystem xfs = new XMLFileSystem(url);
252         
253         MultiFileSystem mfs = AutoUpgrade.createLayeredSystem(fs, xfs);
254         
255         FileObject fo = mfs.findResource ("root");
256         
257         FileSystem original = FileUtil.createMemoryFileSystem();
258         
259         MultiFileSystem tgfs = new MultiFileSystem(new FileSystem[] { fs, original });
260         FileObject tg = tgfs.getRoot().createFolder ("target");
261         FileObject toBeHidden = FileUtil.createData(original.getRoot(), "target/X.txt");
262         
263         assertEquals ("One file is there", 1, tg.getChildren().length);
264         assertEquals ("X.txt", tg.getChildren()[0].getNameExt());
265         
266         
267         HashSet set = new HashSet ();
268         set.add ("Yes.txt");
269         set.add ("X.txt_hidden");
270         Copy.copyDeep (fo, tg, set);
271         
272         assertEquals ("After the copy there is still one file", 1, tg.getChildren().length);
273         assertEquals ("but the file is Yes.txt, as X.txt is hidden by txt_hidden", "Yes.txt", tg.getChildren()[0].getNameExt());
274     }
275     
276     private static void writeTo (FileSystem fs, String JavaDoc res, String JavaDoc content) throws java.io.IOException JavaDoc {
277         FileObject fo = org.openide.filesystems.FileUtil.createData (fs.getRoot (), res);
278         org.openide.filesystems.FileLock lock = fo.lock ();
279         java.io.OutputStream JavaDoc os = fo.getOutputStream (lock);
280         os.write (content.getBytes ());
281         os.close ();
282         lock.releaseLock ();
283     }
284     
285     public LocalFileSystem createLocalFileSystem(String JavaDoc[] resources) throws IOException JavaDoc {
286         File JavaDoc mountPoint = new File JavaDoc(getWorkDir(), "tmpfs");
287         mountPoint.mkdir();
288         
289         for (int i = 0; i < resources.length; i++) {
290             File JavaDoc f = new File JavaDoc (mountPoint,resources[i]);
291             if (f.isDirectory() || resources[i].endsWith("/")) {
292                 f.mkdirs();
293             }
294             else {
295                 f.getParentFile().mkdirs();
296                 try {
297                     f.createNewFile();
298                 } catch (IOException JavaDoc iex) {
299                     throw new IOException JavaDoc ("While creating " + resources[i] + " in " + mountPoint.getAbsolutePath() + ": " + iex.toString() + ": " + f.getAbsolutePath() + " with resource list: " + Arrays.asList(resources));
300                 }
301             }
302         }
303         
304         LocalFileSystem lfs = new LocalFileSystem();
305         try {
306             lfs.setRootDirectory(mountPoint);
307         } catch (Exception JavaDoc ex) {}
308         
309         return lfs;
310     }
311  }
312
Popular Tags