KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > OperationListenerTest


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.openide.loaders;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import junit.framework.AssertionFailedError;
27 import org.openide.filesystems.*;
28 import org.openide.loaders.*;
29 import java.beans.*;
30 import java.io.IOException JavaDoc;
31 import java.util.*;
32 import junit.textui.TestRunner;
33 import org.netbeans.junit.*;
34 import org.openide.util.Enumerations;
35 import org.openide.util.RequestProcessor;
36
37 /*
38  * Checks whether a during a modify operation (copy, move) some
39  * other thread can get a grip on unfinished and uncostructed
40  * content on filesystem.
41  *
42  * @author Jaroslav Tulach
43  */

44 public class OperationListenerTest extends LoggingTestCaseHid
45 implements OperationListener {
46     private ArrayList events = new ArrayList ();
47     private FileSystem fs;
48     private DataLoaderPool pool;
49     private Logger JavaDoc err;
50     
51     /** Creates the test */
52     public OperationListenerTest(String JavaDoc name) {
53         super(name);
54     }
55     
56     // For each test setup a FileSystem and DataObjects
57
protected void setUp() throws Exception JavaDoc {
58         registerIntoLookup(new Pool());
59         
60         String JavaDoc fsstruct [] = new String JavaDoc [] {
61             "source/A.attr",
62             "B.attr",
63             "dir/",
64             "fake/A.instance"
65         };
66         TestUtilHid.destroyLocalFileSystem (getName());
67         fs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
68         
69         err = Logger.getLogger("TEST-" + getName());
70         
71         pool = DataLoaderPool.getDefault ();
72         assertNotNull (pool);
73         assertEquals (Pool.class, pool.getClass ());
74         
75         Pool.setExtra(null);
76         
77         err.info("setUp is over");
78     }
79     
80     //Clear all stuff when the test finish
81
protected void tearDown() throws Exception JavaDoc {
82         err.info("entering tearDown");
83         
84         pool.removeOperationListener(this);
85         
86         err.info("Making sure everything is cleaned");
87         WeakReference JavaDoc ref = new WeakReference JavaDoc(fs);
88         fs = null;
89         events = null;
90         assertGC("GC the filesystem", ref);
91         err.info("Ok, tearDown finished");
92         
93 // AddLoaderManuallyHid.addRemoveLoader (ALoader.getLoader (ALoader.class), false);
94
// AddLoaderManuallyHid.addRemoveLoader (BLoader.getLoader (BLoader.class), false);
95
}
96     
97     //
98
// Tests
99
//
100

101     public void testRecognizeFolder () {
102         pool.addOperationListener(this);
103         DataFolder df = DataFolder.findFolder (fs.findResource ("fake"));
104         DataObject[] arr = df.getChildren ();
105         
106         assertEquals ("One child", 1, arr.length);
107         
108         assertEvents ("Recognized well", new OperationEvent[] {
109             new OperationEvent (df),
110             new OperationEvent (arr[0])
111         });
112     }
113
114     public void testCopyFile() throws Exception JavaDoc {
115         err.info("Before add listener");
116         pool.addOperationListener(this);
117         err.info("after add listener");
118         DataObject obj = DataObject.find (fs.findResource ("fake/A.instance"));
119         err.info("object found: " + obj);
120         DataFolder df = DataFolder.findFolder (fs.findResource ("dir"));
121         err.info("folder found: " + df);
122         DataObject n = obj.copy (df);
123         err.info("copy done: " + n);
124         assertEquals ("Copy successfull", n.getFolder(), df);
125         
126         err.info("Comparing events");
127         assertEvents ("All well", new OperationEvent[] {
128             new OperationEvent (obj),
129             new OperationEvent (df),
130             new OperationEvent (n),
131             new OperationEvent.Copy (n, obj)
132         });
133     }
134     
135     public void testBrokenLoader () throws Exception JavaDoc {
136         BrokenLoader loader = (BrokenLoader)DataLoader.getLoader(BrokenLoader.class);
137         
138         try {
139             err.info("before setExtra: " + loader);
140             Pool.setExtra(loader);
141             
142             err.info("before addOperationListener");
143             pool.addOperationListener(this);
144             
145             loader.acceptableFO = fs.findResource ("source/A.attr");
146             err.info("File object found: " + loader.acceptableFO);
147             try {
148                 DataObject obj = DataObject.find (fs.findResource ("source/A.attr"));
149                 fail ("The broken loader throws exception and cannot be created");
150             } catch (IOException JavaDoc ex) {
151                 // ok
152
err.info("Exception thrown correctly:");
153                 err.log(Level.INFO, null, ex);
154             }
155             assertEquals ("Loader created an object", loader, loader.obj.getLoader());
156             
157             err.info("brefore waitFinished");
158             // and the task can be finished
159
loader.recognize.waitFinished ();
160             
161             err.info("waitFinished done");
162             
163             assertEvents ("One creation notified even if the object is broken", new OperationEvent[] {
164                 new OperationEvent (loader.obj),
165             });
166         } finally {
167             Pool.setExtra(null);
168         }
169     }
170     
171     //
172
// helper methods
173
//
174

175     private void assertEvents (String JavaDoc txt, OperationEvent[] expected) {
176         boolean failure = false;
177         if (expected.length != events.size ()) {
178             failure = true;
179         } else {
180             for (int i = 0; i < expected.length; i++) {
181                 OperationEvent e = expected[i];
182                 OperationEvent r = (OperationEvent)events.get (i);
183                 if (e.getClass () != r.getClass ()) {
184                     failure = true;
185                     break;
186                 }
187                 if (e.getObject () != r.getObject()) {
188                     failure = true;
189                     break;
190                 }
191             }
192         }
193         
194         
195         if (failure) {
196             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
197             
198             int till = Math.max (expected.length, events.size ());
199             sb.append ("Expected events: " + expected.length + " was: " + events.size () + "\n");
200             for (int i = 0; i < till; i++) {
201                 sb.append (" Expected: ");
202                 if (i < expected.length) {
203                     sb.append (expected[i].getClass () + " source: " + expected[i].getObject ());
204                 }
205                 sb.append ('\n');
206                 sb.append (" Was : ");
207                 if (i < events.size ()) {
208                     OperationEvent ev = (OperationEvent)events.get (i);
209                     sb.append (ev.getClass () + " source: " + ev.getObject ());
210                 }
211                 sb.append ('\n');
212             }
213             
214             fail (sb.toString ());
215         }
216         
217         events.clear();
218     }
219     
220     //
221
// Listener implementation
222
//
223

224     public void operationCopy(org.openide.loaders.OperationEvent.Copy ev) {
225         events.add (ev);
226         err.info(" operationCopy: " + ev);
227     }
228     
229     public void operationCreateFromTemplate(org.openide.loaders.OperationEvent.Copy ev) {
230         events.add (ev);
231         err.info(" operationCreateFromTemplate: " + ev);
232     }
233     
234     public void operationCreateShadow(org.openide.loaders.OperationEvent.Copy ev) {
235         events.add (ev);
236         err.info(" operationCreateShadow: " + ev);
237     }
238     
239     public void operationDelete(OperationEvent ev) {
240         events.add (ev);
241         err.info(" operationDelete: " + ev);
242     }
243     
244     public void operationMove(org.openide.loaders.OperationEvent.Move ev) {
245         events.add (ev);
246         err.info(" operationMove: " + ev);
247     }
248     
249     public void operationPostCreate(OperationEvent ev) {
250         events.add (ev);
251         err.info(" operationPostCreate: " + ev);
252     }
253     
254     public void operationRename(org.openide.loaders.OperationEvent.Rename ev) {
255         events.add (ev);
256         err.info(" operationRename: " + ev);
257     }
258
259     
260     //
261
// Own loader
262
//
263
public static final class BrokenLoader extends UniFileLoader {
264         public FileObject acceptableFO;
265         public RequestProcessor.Task recognize;
266         public MultiDataObject obj;
267         
268         public BrokenLoader() {
269             super(MultiDataObject.class.getName ());
270         }
271         protected String JavaDoc displayName() {
272             return "BrokenLoader";
273         }
274         protected FileObject findPrimaryFile(FileObject fo) {
275             if (acceptableFO != null && acceptableFO.equals(fo)) {
276                 return fo;
277             }
278             return null;
279         }
280         protected MultiDataObject createMultiObject(final FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
281             obj = new MultiDataObject (primaryFile, this);
282             
283             assertNull ("Only one invocation of this code allowed", recognize);
284             
285             class R implements Runnable JavaDoc {
286                 public DataObject found;
287                 public void run () {
288                     synchronized (this) {
289                         notify ();
290                     }
291                     // this basicly means another call to createMultiObject method
292
// of this loader again, but the new MultiDataObject will throw
293
// DataObjectExistsException and will block in its
294
// getDataObject method
295
try {
296                         found = DataObject.find (primaryFile);
297                     } catch (IOException JavaDoc ex) {
298                         fail ("Unexepcted exception: " + ex);
299                     }
300                     
301                     assertEquals ("DataObjects are the same", found, obj);
302                 }
303             }
304             R run = new R ();
305             synchronized (run) {
306                 recognize = RequestProcessor.getDefault ().post (run);
307                 try {
308                     run.wait ();
309                 } catch (InterruptedException JavaDoc ex) {
310                     fail ("Unexepcted ex: " + ex);
311                 }
312             }
313                 
314             
315             throw new IOException JavaDoc ("I am broken!");
316         }
317         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
318             return new FileEntry(obj, primaryFile);
319         }
320         
321         public void run() {
322         }
323         
324     }
325     
326     private static final class Pool extends DataLoaderPool {
327         private static DataLoader extra;
328         
329         
330         protected Enumeration loaders () {
331             if (extra == null) {
332                 return Enumerations.empty ();
333             } else {
334                 return Enumerations.singleton (extra);
335             }
336         }
337
338         public static void setExtra(DataLoader aExtra) {
339             extra = aExtra;
340             Pool p = (Pool)DataLoaderPool.getDefault();
341             p.fireChangeEvent(new ChangeEvent JavaDoc(p));
342         }
343     }
344 }
345
Popular Tags