1 19 20 package org.netbeans.modules.masterfs.providers; 21 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import javax.swing.Action ; 27 import junit.framework.AssertionFailedError; 28 import org.netbeans.junit.NbTestCase; 29 import org.openide.filesystems.FileChangeAdapter; 30 import org.openide.filesystems.FileEvent; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.util.Lookup; 34 import org.openide.util.lookup.Lookups; 35 36 40 public class InterceptionListenerTest extends NbTestCase { 41 private InterceptionListenerImpl iListener; 42 protected void setUp() throws Exception { 43 super.setUp(); 44 iListener = lookupImpl(); 45 assertNotNull(iListener); 46 iListener.clear(); 47 clearWorkDir(); 48 } 49 50 private InterceptionListenerImpl lookupImpl() { 51 Lookup.Result result = Lookups.metaInfServices(Thread.currentThread().getContextClassLoader()). 52 lookup(new Lookup.Template(AnnotationProvider.class)); 53 Collection all = result.allInstances(); 54 for (Iterator it = all.iterator(); it.hasNext();) { 55 AnnotationProvider ap = (AnnotationProvider) it.next(); 56 InterceptionListener iil = ap.getInterceptionListener(); 57 if (iil != null && !(iil instanceof ProvidedExtensions)) { 58 return (InterceptionListenerImpl)iil; 59 } 60 } 61 return null; 62 } 63 64 public InterceptionListenerTest(String testName) { 65 super(testName); 66 } 67 68 69 public void testBeforeCreate() throws IOException { 70 FileObject fo = FileUtil.toFileObject(getWorkDir()); 71 assertNotNull(fo); 72 assertNotNull(iListener); 73 assertEquals(0,iListener.beforeCreateCalls); 74 assertEquals(0,iListener.createSuccessCalls); 75 76 assertNotNull(fo.createData("aa")); 77 assertEquals(1,iListener.beforeCreateCalls); 78 assertEquals(1,iListener.createSuccessCalls); 79 80 iListener.clear(); 81 try { 82 assertEquals(0,iListener.createSuccessCalls); 83 assertEquals(0,iListener.createFailureCalls); 84 assertNotNull(fo.createData("aa")); 85 fail(); 86 } catch (IOException ex) { 87 assertEquals(0,iListener.createSuccessCalls); 88 assertEquals(1,iListener.createFailureCalls); 89 } 90 } 91 92 public void testBeforeDelete() throws IOException { 93 FileObject fo = FileUtil.toFileObject(getWorkDir()); 94 assertNotNull(fo); 95 FileObject toDel = fo.createData("aa"); 96 assertNotNull(toDel); 97 iListener.clear(); 98 99 assertNotNull(iListener); 100 assertEquals(0,iListener.beforeDeleteCalls); 101 assertEquals(0,iListener.deleteSuccessCalls); 102 toDel.delete(); 103 assertFalse(toDel.isValid()); 104 assertEquals(1,iListener.beforeDeleteCalls); 105 assertEquals(1,iListener.deleteSuccessCalls); 106 107 iListener.clear(); 108 try { 109 assertEquals(0,iListener.deleteSuccessCalls); 110 assertEquals(0,iListener.deleteFailureCalls); 111 toDel.delete(); 112 fail(); 113 } catch (IOException ex) { 114 assertEquals(0,iListener.deleteSuccessCalls); 115 assertEquals(1,iListener.deleteFailureCalls); 116 } 117 } 118 119 public static class AnnotationProviderImpl extends AnnotationProvider { 120 private InterceptionListenerImpl impl = new InterceptionListenerImpl(); 121 public String annotateName(String name, java.util.Set files) { 122 java.lang.StringBuffer sb = new StringBuffer (name); 123 Iterator it = files.iterator(); 124 while (it.hasNext()) { 125 FileObject fo = (FileObject)it.next(); 126 try { 127 sb.append("," +fo.getNameExt()); } catch (Exception ex) { 129 fail(); 130 } 131 } 132 133 return sb.toString() ; 134 } 135 136 public java.awt.Image annotateIcon(java.awt.Image icon, int iconType, java.util.Set files) { 137 return icon; 138 } 139 140 public String annotateNameHtml(String name, Set files) { 141 return annotateName(name, files); 142 } 143 144 public Action [] actions(Set files) { 145 return new Action []{}; 146 } 147 148 public InterceptionListener getInterceptionListener() { 149 return impl; 150 } 151 } 152 153 public static class InterceptionListenerImpl implements InterceptionListener { 154 private int beforeCreateCalls = 0; 155 private int createFailureCalls = 0; 156 private int createSuccessCalls = 0; 157 private int beforeDeleteCalls = 0; 158 private int deleteSuccessCalls = 0; 159 private int deleteFailureCalls = 0; 160 161 public void clear() { 162 beforeCreateCalls = 0; 163 createFailureCalls = 0; 164 createSuccessCalls = 0; 165 beforeDeleteCalls = 0; 166 deleteSuccessCalls = 0; 167 deleteFailureCalls = 0; 168 } 169 170 public void beforeCreate(org.openide.filesystems.FileObject parent, java.lang.String name, boolean isFolder) { 171 beforeCreateCalls++; 172 } 173 174 public void createSuccess(org.openide.filesystems.FileObject fo) { 175 assertNotNull(fo); 176 createSuccessCalls++; 177 } 178 179 public void createFailure(org.openide.filesystems.FileObject parent, java.lang.String name, boolean isFolder) { 180 createFailureCalls++; 181 } 182 183 public void beforeDelete(org.openide.filesystems.FileObject fo) { 184 beforeDeleteCalls++; 185 } 186 187 public void deleteSuccess(org.openide.filesystems.FileObject fo) { 188 deleteSuccessCalls++; 189 } 190 191 public void deleteFailure(org.openide.filesystems.FileObject fo) { 192 deleteFailureCalls++; 193 } 194 } 195 } 196 | Popular Tags |