KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > providers > ProvidedExtensionsTest


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.modules.masterfs.providers;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35 import junit.framework.AssertionFailedError;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.modules.masterfs.filebasedfs.FileBasedFileSystem;
38 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo;
39 import org.openide.filesystems.FileChangeAdapter;
40 import org.openide.filesystems.FileChangeListener;
41 import org.openide.filesystems.FileEvent;
42 import org.openide.filesystems.FileLock;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileRenameEvent;
45 import org.openide.filesystems.FileStateInvalidException;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.Lookup;
48 import org.openide.util.lookup.Lookups;
49
50 /**
51  *
52  * @author Radek Matous
53  */

54 public class ProvidedExtensionsTest extends NbTestCase {
55     private ProvidedExtensionsImpl iListener;
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         AnnotationProvider provider = (AnnotationProvider)Lookups.metaInfServices(
59                 Thread.currentThread().getContextClassLoader()).lookup(AnnotationProvider.class);
60         assertNotNull(provider);
61         iListener = lookupImpl();
62         assertNotNull(iListener);
63         clearWorkDir();
64     }
65     
66     private ProvidedExtensionsImpl lookupImpl() {
67         Lookup.Result result = Lookups.metaInfServices(Thread.currentThread().getContextClassLoader()).
68                 lookup(new Lookup.Template(AnnotationProvider.class));
69         Collection JavaDoc all = result.allInstances();
70         for (Iterator JavaDoc it = all.iterator(); it.hasNext();) {
71             AnnotationProvider ap = (AnnotationProvider) it.next();
72             InterceptionListener iil = ap.getInterceptionListener();
73             if (iil instanceof ProvidedExtensions) {
74                 return (ProvidedExtensionsImpl)iil;
75             }
76         }
77         return null;
78     }
79     
80     public ProvidedExtensionsTest(String JavaDoc testName) {
81         super(testName);
82     }
83     
84
85     public void testImplsBeforeChange() throws IOException JavaDoc {
86         FileObject fo = FileUtil.toFileObject(getWorkDir());
87         assertNotNull(fo);
88         assertNotNull(iListener);
89         FileObject toChange = fo.createData("aa");
90         assertNotNull(toChange);
91         OutputStream JavaDoc os = toChange.getOutputStream();
92         try {
93             assertEquals(1, iListener.implsBeforeChangeCalls);
94         } finally {
95             os.close();
96         }
97         
98     }
99     
100     public void testImplsMove() throws IOException JavaDoc {
101         FileObject fo = FileUtil.toFileObject(getWorkDir());
102         assertNotNull(fo);
103         assertNotNull(iListener);
104         FileObject toMove = fo.createData("aa");
105         assertNotNull(toMove);
106         FileObject whereToMove = fo.createFolder("aafolder");
107         assertNotNull(whereToMove);
108         
109         iListener.clear();
110         FileLock lock = toMove.lock();
111         iListener.setLock(lock);
112         try {
113             assertEquals(0,iListener.implsMoveCalls);
114             assertEquals(0,iListener.moveImplCalls);
115             iListener.setImplsMoveRetVal(true);
116             assertNotNull(toMove.move(lock, whereToMove, toMove.getName(), toMove.getExt()));
117             assertEquals(1,iListener.implsMoveCalls);
118             assertEquals(1,iListener.moveImplCalls);
119         } finally {
120             if (lock != null) {
121                 iListener.setLock(null);
122                 lock.releaseLock();
123             }
124         }
125     }
126     
127     public void testImplsMove2() throws IOException JavaDoc {
128         FileObject fo = FileUtil.toFileObject(getWorkDir());
129         assertNotNull(fo);
130         assertNotNull(iListener);
131         FileObject toMove = fo.createData("aa");
132         assertNotNull(toMove);
133         FileObject whereToMove = fo.createFolder("aafolder");
134         assertNotNull(whereToMove);
135         
136         iListener.clear();
137         FileLock lock = toMove.lock();
138         iListener.setLock(lock);
139         try {
140             assertEquals(0,iListener.implsMoveCalls);
141             assertEquals(0,iListener.moveImplCalls);
142             iListener.setImplsMoveRetVal(false);
143             assertNotNull(toMove.move(lock, whereToMove, toMove.getName(), toMove.getExt()));
144             assertEquals(1,iListener.implsMoveCalls);
145             assertEquals(0,iListener.moveImplCalls);
146         } finally {
147             if (lock != null) {
148                 iListener.setLock(null);
149                 lock.releaseLock();
150             }
151         }
152     }
153     
154     public void testImplsRename() throws IOException JavaDoc {
155         FileObject fo = FileUtil.toFileObject(getWorkDir());
156         assertNotNull(fo);
157         assertNotNull(iListener);
158         FileObject toRename = fo.createData("aa");
159         assertNotNull(toRename);
160         
161         iListener.clear();
162         FileLock lock = toRename.lock();
163         iListener.setLock(lock);
164         try {
165             assertEquals(0,iListener.implsRenameCalls);
166             assertEquals(0,iListener.renameImplCalls);
167             iListener.setImplsRenameRetVal(true);
168             assertTrue(toRename.isValid());
169             assertNull(toRename.getParent().getFileObject(toRename.getExt(), toRename.getName()));
170             toRename.rename(lock,toRename.getExt(), toRename.getName());
171             assertEquals(1,iListener.implsRenameCalls);
172             assertEquals(1,iListener.renameImplCalls);
173         } finally {
174             if (lock != null) {
175                 iListener.setLock(null);
176                 lock.releaseLock();
177             }
178         }
179     }
180     
181     public void testImplsRename2() throws IOException JavaDoc {
182         final List JavaDoc events = new ArrayList JavaDoc();
183         FileObject fo = FileUtil.toFileObject(getWorkDir());
184         assertNotNull(fo);
185         final FileObject toRename = fo.createData(getName());
186         assertNotNull(toRename);
187         
188         iListener.clear();
189         FileLock lock = toRename.lock();
190         iListener.setLock(lock);
191         FileChangeListener fcl = null;
192         try {
193             final String JavaDoc origNameExt = toRename.getNameExt();
194             final String JavaDoc origPath = toRename.getPath();
195             final File JavaDoc origFile = FileUtil.toFile(toRename);
196
197             iListener.setImplsRenameRetVal(true);
198             fcl = new FileChangeAdapter() {
199                 public void fileRenamed(FileRenameEvent fe) {
200                     events.add(fe);
201                     assertFalse(fe.getFile().getNameExt().equals(origNameExt));
202                     assertNull(toRename.getParent().getFileObject(origNameExt));
203                     File JavaDoc f = FileUtil.toFile(toRename);
204                     assertNotNull(f);
205                     assertNotNull(FileUtil.toFileObject(f));
206                     assertSame(toRename, FileUtil.toFileObject(f));
207
208                     assertTrue(f.exists());
209                     FileObject delegate = FileBasedFileSystem.getFileObject(f);
210                     assertNotNull(delegate);
211                     assertTrue(delegate.isValid());
212                     assertNull(FileBasedFileSystem.getFileObject(origFile));
213                     assertTrue(toRename.isValid());
214
215                     assertSame(toRename, fe.getFile());
216                     assertSame(toRename.getParent(), fe.getFile().getParent());
217                     try {
218                         assertNotNull(toRename.getFileSystem().findResource(toRename.getPath()));
219                         assertNull(toRename.getFileSystem().findResource(origPath));
220
221                     } catch (FileStateInvalidException ex) {
222                         fail();
223                     }
224
225                     assertEquals("bb",toRename.getName());
226                     assertEquals("ext",toRename.getExt());
227                     assertFalse(origNameExt.equals(toRename.getNameExt()));
228
229                     assertEquals("bb",fe.getFile().getName());
230                     assertEquals("ext",fe.getFile().getExt());
231                     assertFalse(origNameExt.equals(fe.getFile().getNameExt()));
232
233                     assertFalse(fe.getName().equals(fe.getFile().getName()));
234                     assertFalse(fe.getExt().equals(fe.getFile().getName()));
235
236                     //refreshes shouldn't generate any additional events
237
toRename.refresh();
238                     toRename.getParent().refresh();
239                 }
240
241                 public void fileChanged(FileEvent fe) {
242                     fail();
243                 }
244
245                 public void fileDeleted(FileEvent fe) {
246                     fail();
247                 }
248
249                 public void fileDataCreated(FileEvent fe) {
250                     fail();
251                 }
252
253                 public void fileFolderCreated(FileEvent fe) {
254                     fail();
255                 }
256             };
257             toRename.getParent().addFileChangeListener(fcl);
258             toRename.addFileChangeListener(fcl);
259             toRename.getFileSystem().addFileChangeListener(fcl);
260             toRename.rename(lock,"bb", "ext");
261             assertNull(toRename.getParent().getFileObject(origNameExt));
262             
263         } finally {
264             toRename.getParent().removeFileChangeListener(fcl);
265             toRename.removeFileChangeListener(fcl);
266             toRename.getFileSystem().removeFileChangeListener(fcl);
267
268             if (lock != null) {
269                 iListener.setLock(null);
270                 lock.releaseLock();
271             }
272         }
273         assertEquals(3,events.size());
274     }
275
276     public void testImplsRename3() throws IOException JavaDoc {
277         FileObject fo = FileUtil.toFileObject(getWorkDir());
278         assertNotNull(fo);
279         final FileObject toRename = fo.createData("aa");
280         assertNotNull(toRename);
281         
282         iListener.clear();
283         FileLock lock = toRename.lock();
284         iListener.setLock(lock);
285         try {
286             final String JavaDoc origNameExt = toRename.getNameExt();
287             final String JavaDoc origPath = toRename.getPath();
288             final File JavaDoc origFile = FileUtil.toFile(toRename);
289             final File JavaDoc newFile = new File JavaDoc(origFile.getParentFile(),FileInfo.composeName("bb", "ext"));
290
291
292             iListener.setImplsRenameRetVal(true);
293             FileChangeListener fcl = new FileChangeAdapter() {
294                 public void fileRenamed(FileRenameEvent fe) {
295                     try {
296                         File JavaDoc f = FileUtil.toFile(toRename);
297                         assertNotNull(f);
298                         assertNotNull(FileUtil.toFileObject(f));
299                         assertSame(toRename, FileUtil.toFileObject(f));
300
301                         assertTrue(f.exists());
302                         FileObject delegate = FileBasedFileSystem.getFileObject(f);
303                         assertNotNull(delegate);
304                         assertTrue(delegate.isValid());
305                         if (fe.getFile().getNameExt().equals(newFile.getName())) {
306                             assertNull(FileBasedFileSystem.getFileObject(origFile));
307                             assertNotNull(FileBasedFileSystem.getFileObject(newFile));
308                             assertNotNull(FileUtil.toFileObject(newFile));
309                             assertNull(FileUtil.toFileObject(origFile));
310                         } else {
311                             assertNotNull(FileBasedFileSystem.getFileObject(origFile));
312                             assertNull(FileBasedFileSystem.getFileObject(newFile));
313                             assertNull(FileUtil.toFileObject(newFile));
314                             assertNotNull(FileUtil.toFileObject(origFile));
315                         }
316                         assertTrue(toRename.isValid());
317
318                         assertSame(toRename, fe.getFile());
319                         assertSame(toRename.getParent(), fe.getFile().getParent());
320                         try {
321                             assertNotNull(toRename.getFileSystem().findResource(toRename.getPath()));
322
323                         } catch (FileStateInvalidException ex) {
324                             fail();
325                         }
326
327                         assertFalse(fe.getName().equals(fe.getFile().getName()));
328                         assertFalse(fe.getExt().equals(fe.getFile().getName()));
329
330                         //refreshes shouldn't generate any additional events
331
toRename.refresh();
332                         toRename.getParent().refresh();
333                     } catch(AssertionFailedError afe) {
334                         afe.printStackTrace();
335                         throw afe;
336                     }
337                 }
338
339                 public void fileChanged(FileEvent fe) {
340                     fail();
341                 }
342
343                 public void fileDeleted(FileEvent fe) {
344                     fail();
345                 }
346
347                 public void fileDataCreated(FileEvent fe) {
348                     fail();
349                 }
350
351                 public void fileFolderCreated(FileEvent fe) {
352                     fail();
353                 }
354             };
355             toRename.getParent().addFileChangeListener(fcl);
356             toRename.addFileChangeListener(fcl);
357             toRename.getFileSystem().addFileChangeListener(fcl);
358             toRename.rename(lock,"bb", "ext");
359             assertNull(toRename.getParent().getFileObject(origNameExt));
360             toRename.rename(lock,"aa", "");
361             toRename.rename(lock,"bb", "ext");
362             toRename.rename(lock,"aa", "");
363             toRename.rename(lock,"bb", "ext");
364             toRename.rename(lock,"aa", "");
365         } finally {
366             if (lock != null) {
367                 iListener.setLock(null);
368                 lock.releaseLock();
369             }
370         }
371     }
372
373     
374     public static class AnnotationProviderImpl extends InterceptionListenerTest.AnnotationProviderImpl {
375         private ProvidedExtensionsImpl impl = new ProvidedExtensionsImpl();
376         public InterceptionListener getInterceptionListener() {
377             return impl;
378         }
379     }
380     
381     public static class ProvidedExtensionsImpl extends ProvidedExtensions {
382         private int implsMoveCalls;
383         private int moveImplCalls;
384         private int implsRenameCalls;
385         private int renameImplCalls;
386         private int implsBeforeChangeCalls;
387         
388         private static boolean implsMoveRetVal = true;
389         private static boolean implsRenameRetVal = true;
390         private static boolean implsDeleteRetVal = true;
391         
392         public static FileLock lock;
393         
394         public void clear() {
395             implsMoveCalls = 0;
396             moveImplCalls = 0;
397             implsRenameCalls = 0;
398             renameImplCalls = 0;
399             implsBeforeChangeCalls = 0;
400         }
401
402         public void beforeChange(FileObject f) {
403             assertNotNull(FileUtil.toFile(f));
404             implsBeforeChangeCalls++;
405         }
406         
407         public ProvidedExtensions.DeleteHandler getDeleteHandler(File JavaDoc f) {
408             return (!isImplsDeleteRetVal()) ? null : new ProvidedExtensions.DeleteHandler(){
409                 final Set JavaDoc s = new HashSet JavaDoc();
410                 public boolean delete(File JavaDoc file) {
411                     if (file.isDirectory()) {
412                         File JavaDoc[] childs = file.listFiles(new FileFilter JavaDoc() {
413                             public boolean accept(File JavaDoc pathname) {
414                                 boolean accepted = pathname.isFile();
415                                 if (!accepted && pathname.isDirectory()) {
416                                     accepted = !s.contains(pathname);
417                                     if (!s.contains(pathname)) {
418                                         s.add(pathname);
419                                     }
420                                 }
421                                 return accepted;
422                             }
423                         });
424                         return childs.length == 0;
425                     }
426                     return file.delete();
427                 }
428             };
429         }
430                 
431         public ProvidedExtensions.IOHandler getRenameHandler(final File JavaDoc from, final String JavaDoc newName) {
432             implsRenameCalls++;
433             final File JavaDoc f = new File JavaDoc(from.getParentFile(),newName);
434             
435             return (!isImplsRenameRetVal()) ? null : new ProvidedExtensions.IOHandler(){
436                 public void handle() throws IOException JavaDoc {
437                     renameImplCalls++;
438                     assertTrue(from.renameTo(f));
439                 }
440             };
441         }
442         
443         public ProvidedExtensions.IOHandler getMoveHandler(final File JavaDoc from, final File JavaDoc to) {
444             implsMoveCalls++;
445             return (!isImplsMoveRetVal()) ? null : new ProvidedExtensions.IOHandler(){
446                 public void handle() throws IOException JavaDoc {
447                     moveImplCalls++;
448                     if (to.exists()) {
449                         throw new IOException JavaDoc();
450                     }
451                     assertTrue(from.exists());
452                     assertFalse(to.exists());
453                     
454                     assertFalse(from.equals(to));
455                     InputStream JavaDoc inputStream = new FileInputStream JavaDoc(from);
456                     OutputStream JavaDoc outputStream = new FileOutputStream JavaDoc(to);
457                     try {
458                         FileUtil.copy(inputStream, outputStream);
459                     } finally {
460                         if (inputStream != null) inputStream.close();
461                         if (outputStream != null) outputStream.close();
462                     }
463                     assertTrue(from.delete());
464                     
465                     assertFalse(from.exists());
466                     assertTrue(to.exists());
467                 }
468             };
469         }
470         
471         public static FileLock getLock() {
472             return lock;
473         }
474         
475         public static void setLock(FileLock lock) {
476             ProvidedExtensionsImpl.lock = lock;
477         }
478         
479         public static boolean isImplsMoveRetVal() {
480             return implsMoveRetVal;
481         }
482         
483         public static void setImplsMoveRetVal(boolean implsMoveRetVal) {
484             ProvidedExtensionsImpl.implsMoveRetVal = implsMoveRetVal;
485         }
486         
487         public static boolean isImplsRenameRetVal() {
488             return implsRenameRetVal;
489         }
490         
491         public static void setImplsRenameRetVal(boolean implsRenameRetVal) {
492             ProvidedExtensionsImpl.implsRenameRetVal = implsRenameRetVal;
493         }
494                
495         public static boolean isImplsDeleteRetVal() {
496             return implsDeleteRetVal;
497         }
498         
499         public static void setImplsDeleteRetVal(boolean implsDeleteRetVal) {
500             ProvidedExtensionsImpl.implsDeleteRetVal = implsDeleteRetVal;
501         }
502     }
503 }
504
Popular Tags