1 19 20 package org.netbeans.upgrade; 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.net.URL ; 27 import java.util.*; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 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 43 public final class CopyTest extends org.netbeans.junit.NbTestCase { 44 public CopyTest (String name) { 45 super (name); 46 } 47 48 protected void setUp() throws java.lang.Exception { 49 super.setUp(); 50 51 clearWorkDir(); 52 } 53 54 public void testAppendSelectedLines() throws Exception { 55 List expectedLines = new ArrayList(); 57 File wDir = getWorkDir(); 58 File sFile = new File (wDir,this.getName()+".file"); 59 assertTrue(sFile.createNewFile()); 60 File tFolder = new File (wDir,this.getName()); 61 assertTrue(tFolder.mkdir()); 62 File tFile = new File (tFolder,this.getName()+".file"); 63 assertTrue(tFile.createNewFile()); 64 FileOutputStream fos = new FileOutputStream (tFile); 65 try { 66 String 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 (sFile); 74 try { 75 String 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 } finally { 87 fos.close(); 88 } 89 String [] regexForSelection = new String [] { 90 "^nbplatform[.](?![dD]efault).+[.](netbeans[.]dest[.]dir|label|harness[.]dir)=.+$" }; 92 93 Copy.appendSelectedLines(sFile, tFolder, regexForSelection); 94 String line = null; 95 List resultLines = new ArrayList(); 96 BufferedReader reader = new BufferedReader (new FileReader (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 { 108 FileSystem fs = createLocalFileSystem (new String [] { 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 set = new java.util.HashSet (); 118 set.add ("X.txt"); 119 Copy.copyDeep (fo, tg, set); 120 121 assertEquals ("One file copied", 1, tg.getChildren().length); 122 String n = tg.getChildren ()[0].getNameExt(); 123 assertEquals ("Name is X.txt", "X.txt", n); 124 125 } 126 127 public void testDoesDeepCopy () throws Exception { 128 FileSystem fs = createLocalFileSystem (new String [] { 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 set = new java.util.HashSet (); 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 { 149 FileSystem fs = createLocalFileSystem (new String [] { 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 set = new java.util.HashSet (); 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 { 170 FileSystem fs = createLocalFileSystem (new String [] { 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 set = new java.util.HashSet (); 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 { 193 FileSystem fs = createLocalFileSystem (new String [] { 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 set = new java.util.HashSet (); 204 Copy.copyDeep (fo, tg, set); 205 206 assertEquals ("Nothing copied", 0, tg.getChildren().length); 207 } 208 209 public void testDoNotOverwriteFiles () throws Exception { 210 java.util.HashSet set = new java.util.HashSet (); 211 set.add ("X.txt"); 212 213 FileSystem fs = createLocalFileSystem (new String [] { 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 content = new String (arr, 0, len); 239 240 assertEquals ("The content is kept from project", content, "content-project"); 241 } 242 243 public void testDoesCopyHiddenFiles () throws Exception { 244 String [] res = { 245 "root/Yes.txt", 246 "root/X.txt_hidden", 247 }; 248 LocalFileSystem fs = createLocalFileSystem (res); 249 URL 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 res, String content) throws java.io.IOException { 277 FileObject fo = org.openide.filesystems.FileUtil.createData (fs.getRoot (), res); 278 org.openide.filesystems.FileLock lock = fo.lock (); 279 java.io.OutputStream os = fo.getOutputStream (lock); 280 os.write (content.getBytes ()); 281 os.close (); 282 lock.releaseLock (); 283 } 284 285 public LocalFileSystem createLocalFileSystem(String [] resources) throws IOException { 286 File mountPoint = new File (getWorkDir(), "tmpfs"); 287 mountPoint.mkdir(); 288 289 for (int i = 0; i < resources.length; i++) { 290 File f = new File (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 iex) { 299 throw new IOException ("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 ex) {} 308 309 return lfs; 310 } 311 } 312 | Popular Tags |