KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.lang.ref.WeakReference JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import org.openide.ErrorManager;
32
33 import org.openide.filesystems.*;
34 import org.openide.nodes.*;
35
36 // XXX to do:
37
// - loaders are never asked to recognize an invalid file object (#13926)
38

39 /** Test invalidation of objects: getting of node delegate,
40  * whether folder instances include them, whether loaders are
41  * asked to recognize invalid objects, etc.
42  * @author Jesse Glick
43  */

44 public class DataObjectInvalidationOneFileTest extends LoggingTestCaseHid {
45     Logger JavaDoc log;
46     
47     public DataObjectInvalidationOneFileTest(String JavaDoc name) {
48         super(name);
49     }
50
51     protected Level JavaDoc logLevel() {
52         return Level.FINE;
53     }
54     
55     protected void setUp() throws IOException JavaDoc {
56         clearWorkDir();
57
58         log = Logger.getLogger("TEST-" + getName());
59         registerIntoLookup(new Pool());
60     }
61     
62     protected void tearDown() throws Exception JavaDoc {
63         WeakReference JavaDoc ref = new WeakReference JavaDoc(DataLoader.getLoader(SlowDataLoader.class));
64         Pool.setExtra(null);
65         assertGC("Let's cleanup all nodes, data objects created in previous test", ref);
66     }
67     
68     /** Tests that the loader pool does not
69      * try to create a DataObject for a given file object more
70      * than once.
71      * Refer to #15898.
72      */

73     public void testDataObjectsCreatedOncePerFile() throws Exception JavaDoc {
74         FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
75             "folder/file.slow",
76         });
77         FileObject folder = lfs.findResource("folder");
78         DataLoader l = DataLoader.getLoader(SlowDataLoader.class);
79         log.info("Registering the slow loader");
80         Pool.setExtra(l);
81         
82         log.info("Clearing the counts");
83         SlowDataLoader.createCount = 0;
84         SlowDataObject.createCount = 0;
85         log.info("Counts cleared");
86         
87         DataFolder f = DataFolder.findFolder(folder);
88         
89         log.info("Folder created: " + f);
90         
91         Node foldernode = f.getNodeDelegate();
92         
93         log.info("Node created: " + foldernode);
94         
95         Children folderkids = foldernode.getChildren();
96         
97         log.info("Children are here");
98         assertEquals("Getting a folder node does not start automatically scanning children", 0, SlowDataLoader.createCount);
99         assertEquals("Getting a folder node does not finish automatically scanning children", 0, SlowDataObject.createCount);
100         
101         Node[] keep = folderkids.getNodes(true);
102         
103         log.info("Nodes for children are computed: " + keep.length);
104         
105         assertEquals("After getting folder node children, a data object is not started to be created >1 time", 1, SlowDataLoader.createCount);
106         assertEquals("After getting folder node children, a data object is not successfully created >1 time", 1, SlowDataObject.createCount);
107     }
108
109     private static final class ExpectingListener implements PropertyChangeListener JavaDoc {
110         private final Set JavaDoc changes = new HashSet JavaDoc(); // Set<String>
111
public synchronized void propertyChange(PropertyChangeEvent JavaDoc ev) {
112             changes.add(ev.getPropertyName());
113             //System.err.println("got: " + ev.getSource() + " " + ev.getPropertyName() + " " + ev.getOldValue() + " " + ev.getNewValue());//XXX
114
notifyAll();
115         }
116         public synchronized boolean gotSomething(String JavaDoc prop) throws InterruptedException JavaDoc {
117             if (changes.contains(prop)) return true;
118             wait(3000);
119             return changes.contains(prop);
120         }
121     }
122     
123     public static final class SlowDataLoader extends UniFileLoader {
124         public static int createCount = 0;
125         private static Logger JavaDoc ERR = Logger.getLogger("SlowDataLoader");
126         public SlowDataLoader() {
127             super(SlowDataObject.class.getName());
128         }
129         protected void initialize() {
130             super.initialize();
131             getExtensions().addExtension("slow");
132         }
133         protected String JavaDoc displayName() {
134             return "Slow";
135         }
136         protected MultiDataObject createMultiObject(FileObject pf) throws IOException JavaDoc {
137             ERR.info("in createMultiObject for: " + pf);
138             SlowDataObject o = new SlowDataObject(pf, this);
139             ERR.info("created object : " + o);
140             //new Exception("creating for: " + pf + " count=" + createCount).printStackTrace();
141
return o;
142         }
143     }
144     public static final class SlowDataObject extends MultiDataObject {
145         public Thread JavaDoc ok;
146         public static int createCount = 0;
147         public SlowDataObject(FileObject pf, MultiFileLoader loader) throws IOException JavaDoc {
148             super(pf, loader);
149             synchronized (loader) {
150                 SlowDataLoader.ERR.info("Incrementing SlowDataObject count to " + ++createCount);
151                 SlowDataLoader.ERR.info("Incrementing SlowDataLoader count to " + ++SlowDataLoader.createCount);
152                 
153                 // in case somebody is listening on the loader for our creation
154
// let him wake up
155
SlowDataLoader.ERR.info("Wake up sleepers");
156                 loader.notifyAll ();
157             }
158             
159             int cnt = 1;
160             
161             while (cnt-- > 0) {
162                 try {
163                     Thread.sleep(2000);
164                 } catch (InterruptedException JavaDoc ie) {
165                     throw new IOException JavaDoc(ie.toString());
166                 }
167             }
168             
169             
170             ok = Thread.currentThread();
171             SlowDataLoader.ERR.info("End of constructor");
172         }
173         protected Node createNodeDelegate() {
174             return new SlowDataNode(this);
175         }
176         
177         protected DataObject handleCopy (DataFolder df) throws java.io.IOException JavaDoc {
178             FileObject fo = this.getPrimaryEntry().copy (df.getPrimaryFile(), "slow");
179             return new SlowDataObject (fo, (MultiFileLoader)this.getLoader());
180         }
181         protected DataObject handleCreateFromTemplate (DataFolder df, String JavaDoc s) throws java.io.IOException JavaDoc {
182             FileObject fo = this.getPrimaryEntry().createFromTemplate (df.getPrimaryFile(), null);
183             return new SlowDataObject (fo, (MultiFileLoader)this.getLoader());
184         }
185     }
186     public static final class SlowDataNode extends DataNode {
187         public SlowDataNode(SlowDataObject o) {
188             super(o, Children.LEAF);
189             if (o.ok == null) throw new IllegalStateException JavaDoc("getDataNode called too early");
190             // Serve as a marker that this is the correct data node kind
191
// (instanceof will not work because of filter nodes):
192
setShortDescription("slownode");
193         }
194     }
195
196     private static final class Pool extends DataLoaderPool {
197         private static DataLoader extra;
198         
199         
200         protected java.util.Enumeration JavaDoc loaders () {
201             if (extra == null) {
202                 return org.openide.util.Enumerations.empty ();
203             } else {
204                 return org.openide.util.Enumerations.singleton (extra);
205             }
206         }
207
208         public static void setExtra(DataLoader aExtra) {
209             if (aExtra != null && extra != null) {
210                 fail("Cannot set extra loader while one is already there. 1: " + extra + " 2: " + aExtra);
211             }
212             extra = aExtra;
213             Pool p = (Pool)DataLoaderPool.getDefault();
214             p.fireChangeEvent(new javax.swing.event.ChangeEvent JavaDoc(p));
215         }
216     }
217 }
218
Popular Tags