KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.Action JavaDoc;
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 /**
37  *
38  * @author Radek Matous
39  */

40 public class InterceptionListenerTest extends NbTestCase {
41     private InterceptionListenerImpl iListener;
42     protected void setUp() throws Exception JavaDoc {
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 JavaDoc all = result.allInstances();
54         for (Iterator JavaDoc 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 JavaDoc testName) {
65         super(testName);
66     }
67     
68     
69     public void testBeforeCreate() throws IOException JavaDoc {
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 JavaDoc ex) {
87             assertEquals(0,iListener.createSuccessCalls);
88             assertEquals(1,iListener.createFailureCalls);
89         }
90     }
91     
92     public void testBeforeDelete() throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc annotateName(String JavaDoc name, java.util.Set JavaDoc files) {
122             java.lang.StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
123             Iterator JavaDoc it = files.iterator();
124             while (it.hasNext()) {
125                 FileObject fo = (FileObject)it.next();
126                 try {
127                     sb.append("," +fo.getNameExt());//NOI18N
128
} catch (Exception JavaDoc ex) {
129                     fail();
130                 }
131             }
132             
133             return sb.toString() ;
134         }
135         
136         public java.awt.Image JavaDoc annotateIcon(java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc files) {
137             return icon;
138         }
139         
140         public String JavaDoc annotateNameHtml(String JavaDoc name, Set JavaDoc files) {
141             return annotateName(name, files);
142         }
143         
144         public Action JavaDoc[] actions(Set JavaDoc files) {
145             return new Action JavaDoc[]{};
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 JavaDoc 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 JavaDoc 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