KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.Set JavaDoc;
25 import junit.textui.TestRunner;
26 import org.openide.cookies.OpenCookie;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStatusEvent;
29 import org.openide.filesystems.FileSystem;
30 import java.util.Enumeration JavaDoc;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33 import org.openide.cookies.InstanceCookie;
34 import org.openide.filesystems.Repository;
35 import org.netbeans.junit.*;
36 import org.openide.util.Lookup;
37 import org.openide.util.lookup.Lookups;
38
39 /** Test things about node delegates.
40  * Note: if you mess with file status changes in this test, you may effectively
41  * break the testLeakAfterStatusChange test.
42  *
43  * @author Jesse Glick
44  */

45 public class DataNodeTest extends NbTestCase {
46     
47     public DataNodeTest(String JavaDoc name) {
48         super(name);
49     }
50
51     static {
52         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.DataNodeTest$Lkp");
53     }
54     
55     /** Test that for all examples to be found in the system file system,
56      * the node delegate has the same object as a cookie from DataObject.class.
57      * (It is fine to have a different object from a more specific cookie, as
58      * may happen in the case of a data shadow.)
59      * See jglick's message on nbdev as of 22 Jun 2001:
60      * assertTrue(dataObject.getNodeDelegate().getCookie(DataObject.class)==dataObject)
61      */

62     public void testDataNodeHasObjectAsCookie() throws Exception JavaDoc {
63         // First make sure some core is installed. This could be run inside or
64
// outside a running IDE.
65
// TopManager tm = TopManager.getDefault();
66
// Now scan SFS for all DO's and check the assertion.
67
FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
68         DataFolder top = DataFolder.findFolder(sfs.getRoot());
69         Enumeration JavaDoc e = top.children(true);
70         while (e.hasMoreElements()) {
71             DataObject o = (DataObject)e.nextElement();
72             Node n = o.getNodeDelegate();
73             DataObject o2 = (DataObject)n.getCookie(DataObject.class);
74             assertEquals("Correct cookie from node delegate", o, o2);
75         }
76     }
77
78     
79     public void testDataNodeGetHtmlNameDoesNotInitializeAllFiles () throws Exception JavaDoc {
80         org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
81             "F.java", "F.form"
82         });
83         
84         FSWithStatus fs = new FSWithStatus ();
85         fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot()));
86         
87         DataObject obj = DataObject.find (fs.findResource("F.java"));
88         
89         String JavaDoc n = obj.getNodeDelegate ().getHtmlDisplayName ();
90         assertNotNull ("FSWithStatus called", fs.lastFiles);
91      
92         assertEquals ("Primary entry created", 1, TwoPartLoader.get ().primary);
93         if (TwoPartLoader.get ().secondary != 0) {
94             try {
95                 assertEquals ("Secondary entry not", 0, TwoPartLoader.get ().secondary);
96             } catch (Error JavaDoc t1) {
97                 Throwable JavaDoc t2 = TwoPartLoader.get ().whoCreatedSecondary;
98                 if (t2 != null) {
99                     t1.initCause (t2);
100                 }
101                 throw t1;
102             }
103         }
104         assertEquals ("Size is two", 2, fs.lastFiles.size ());
105         assertEquals ("Now the secondary entry had to be created", 1, TwoPartLoader.get ().secondary);
106     }
107
108     
109     public void testWhatIsAddedToNodeLookupIsByDefaultVisibleInDataObjectLookup() throws Exception JavaDoc {
110         org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
111             "F.java", "F.form"
112         });
113         
114         FSWithStatus fs = new FSWithStatus ();
115         fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot()));
116         
117         DataObject obj = DataObject.find (fs.findResource("F.java"));
118
119         OpenCookie fromNode = obj.getNodeDelegate().getLookup().lookup(OpenCookie.class);
120         assertNotNull("There is some cookie in node", fromNode);
121         
122         OpenCookie fromObject = obj.getLookup().lookup(OpenCookie.class);
123         assertNotNull("There is a cookie in data object too", fromObject);
124         
125     }
126     
127     public void testDataNodeGetDataFromLookup() throws Exception JavaDoc {
128
129         FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
130         DataFolder rootFolder = DataFolder.findFolder(sfs.getRoot());
131         
132         class C implements Node.Cookie {
133         }
134         
135         String JavaDoc objectToLookup = "I like my Lookup";
136         C cookieToLookup = new C();
137         Lookup lookup = Lookups.fixed(new Object JavaDoc[] { objectToLookup, cookieToLookup });
138         
139         DataNode node = new DataNode(rootFolder, Children.LEAF, lookup);
140         Object JavaDoc objectLookedUp = node.getLookup().lookup(String JavaDoc.class);
141         assertEquals("object is going to be found", objectToLookup, objectLookedUp);
142         assertEquals("cookie found.", cookieToLookup, node.getLookup().lookup(C.class));
143
144         //assertNull("Cannot return string as cookie", node.getCookie(String.class));
145
assertEquals("But C is found", cookieToLookup, node.getCookie(C.class));
146         
147         assertNull("Data object is not found in lookup", node.getLookup().lookup(DataObject.class));
148         assertNull("DataObject not found in cookies as they delegate to lookup", node.getCookie(DataObject.class));
149     }
150     
151     /**
152      * Verifues that a DataObject/DataNode is not leaked after firing a status
153      * change for one of its files. Note that this test used to fail only
154      * for the very first DataNode changed in the JVM run so if there are tests
155      * running before this one which fire a status change, this test may not
156      * catch a real regression.
157      */

158     public void testLeakAfterStatusChange() throws Exception JavaDoc {
159         org.openide.filesystems.FileSystem lfs = TestUtilHid.createLocalFileSystem(getWorkDir (), new String JavaDoc[] {
160             "F.java", "F.form"
161         });
162         
163         FSWithStatus fs = new FSWithStatus ();
164         fs.setRootDirectory(org.openide.filesystems.FileUtil.toFile(lfs.getRoot()));
165         
166         FileObject fo = fs.findResource("F.java");
167         DataObject obj = DataObject.find (fo);
168         Node n = obj.getNodeDelegate ();
169         fs.fireStatusChange(new FileStatusEvent(fs, fo, true, true));
170         
171         Thread.sleep(100); // let some async processing in DataNode
172
WeakReference JavaDoc refN = new WeakReference JavaDoc(n);
173         WeakReference JavaDoc refD = new WeakReference JavaDoc(obj);
174         n = null;
175         obj = null;
176         
177         assertGC("Node released", refN);
178         assertGC("DataObject released", refD);
179     }
180     
181     private static final class FSWithStatus extends org.openide.filesystems.LocalFileSystem
182     implements FileSystem.HtmlStatus {
183         public Set JavaDoc lastFiles;
184         
185         
186         public FileSystem.Status getStatus () {
187             return this;
188         }
189
190         private void checkFirst (Set JavaDoc files) {
191             lastFiles = files;
192             assertNotNull ("There is first file", files.iterator ().next ());
193         }
194         
195         public java.awt.Image JavaDoc annotateIcon(java.awt.Image JavaDoc icon, int iconType, java.util.Set JavaDoc files) {
196             checkFirst (files);
197             return icon;
198         }
199
200         public String JavaDoc annotateName(String JavaDoc name, java.util.Set JavaDoc files) {
201             checkFirst (files);
202             return name;
203         }
204
205         public String JavaDoc annotateNameHtml(String JavaDoc name, java.util.Set JavaDoc files) {
206             checkFirst (files);
207             return name;
208         }
209         
210         void fireStatusChange(FileStatusEvent fse) {
211             fireFileStatusChanged(fse);
212         }
213     } // end of FSWithStatus
214

215     public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
216         public Lkp () {
217             this (new org.openide.util.lookup.InstanceContent ());
218         }
219         
220         private Lkp (org.openide.util.lookup.InstanceContent ic) {
221             super (ic);
222             
223             ic.add (new Pool ());
224         }
225         
226     } // end of Lkp
227

228     private static final class Pool extends DataLoaderPool {
229         protected Enumeration JavaDoc loaders () {
230             return org.openide.util.Enumerations.singleton(TwoPartLoader.get ());
231         }
232     }
233     
234     public static final class TwoPartLoader extends MultiFileLoader {
235         public int primary;
236         public int secondary;
237         public Throwable JavaDoc whoCreatedSecondary;
238         
239         public static TwoPartLoader get () {
240             return (TwoPartLoader)TwoPartLoader.findObject (TwoPartLoader.class, true);
241         }
242         
243         public TwoPartLoader() {
244             super(TwoPartObject.class.getName ());
245         }
246         protected String JavaDoc displayName() {
247             return "TwoPart";
248         }
249         protected FileObject findPrimaryFile(FileObject fo) {
250             return org.openide.filesystems.FileUtil.findBrother(fo, "java");
251         }
252         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
253             return new TwoPartObject(this, primaryFile);
254         }
255         protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
256             primary++;
257             return new FileEntry.Folder(obj, primaryFile);
258         }
259         protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
260             secondary++;
261             whoCreatedSecondary = new Throwable JavaDoc ("Secondary should not be created");
262             return new FileEntry(obj, secondaryFile);
263         }
264     }
265     public static final class TwoPartObject extends MultiDataObject {
266         public TwoPartObject(TwoPartLoader l, FileObject folder) throws DataObjectExistsException {
267             super(folder, l);
268         }
269
270         protected Node createNodeDelegate() {
271             return new DN(this);
272         }
273     }
274     
275     private static final class DN extends DataNode {
276         public DN(TwoPartObject obj) {
277             super(obj, Children.LEAF);
278         }
279         
280         public <T extends Node.Cookie> T getCookie(Class JavaDoc<T> clazz) {
281             if (clazz.isAssignableFrom(OpenCookie.class)) {
282                 return clazz.cast(new OpenCookie () {
283                     public void open() {
284                     }
285                 });
286             }
287             return super.getCookie(clazz);
288         }
289     }
290     
291 }
292
Popular Tags