KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import org.openide.filesystems.*;
29 import org.netbeans.junit.*;
30 import org.openide.nodes.Children;
31 import org.openide.nodes.CookieSet;
32 import org.openide.nodes.Node;
33 import org.openide.text.DataEditorSupport;
34 import org.openide.util.Enumerations;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37
38 /*
39  */

40 public class FileObjectInLookupTest extends NbTestCase {
41     FileObject root;
42     
43     public FileObjectInLookupTest(String JavaDoc name) {
44         super(name);
45     }
46
47     protected void setUp() throws Exception JavaDoc {
48         MockServices.setServices(OwnDataLoaderPool.class);
49         clearWorkDir ();
50         FileSystem lfs = TestUtilHid.createLocalFileSystem (getWorkDir (), new String JavaDoc[] {
51             "adir/",
52             "adir/file.txt",
53             "adir/file.own"
54         });
55         
56         root = FileUtil.toFileObject(FileUtil.toFile(lfs.getRoot()));
57         
58         Enumeration JavaDoc<?> en = DataLoaderPool.getDefault().allLoaders();
59         while (en.hasMoreElements()) {
60             if (en.nextElement() instanceof OwnDataLoader) {
61                 return;
62             }
63         }
64         fail("OwnDataLoader shall be registered");
65     }
66     
67     protected void tearDown() throws Exception JavaDoc {
68     }
69     
70     protected boolean runInEQ() {
71         return true;
72     }
73     
74     public void testFOInsideFolder() throws Exception JavaDoc {
75         DataFolder f = DataFolder.findFolder(root.getFileObject("adir"));
76         assertFileObjects(f);
77         f.rename("bdir");
78         assertFileObjects(f);
79     }
80     
81     public void testFOInsideADefaultDataObject() throws Exception JavaDoc {
82         DataObject obj = DataObject.find(root.getFileObject("adir/file.txt"));
83         assertFileObjects(obj);
84         obj.rename("kuk");
85         assertFileObjects(obj);
86         obj.move(obj.getFolder().getFolder());
87         assertFileObjects(obj);
88     }
89
90     public void testOwnLoader() throws Exception JavaDoc {
91         DataObject obj = DataObject.find(root.getFileObject("adir/file.own"));
92         assertEquals(OwnDataLoader.class, obj.getLoader().getClass());
93         assertFileObjects(obj);
94         obj.rename("kuk");
95         assertFileObjects(obj);
96         obj.move(obj.getFolder().getFolder());
97         assertFileObjects(obj);
98     }
99
100     public void testShadow() throws Exception JavaDoc {
101         DataObject obj = DataObject.find(root.getFileObject("adir/file.own"));
102         DataShadow shadow = obj.createShadow(obj.getFolder().getFolder());
103         assertEquals(OwnDataLoader.class, obj.getLoader().getClass());
104         
105         assertEquals("DataObject for the shadow is the shadow", shadow, shadow.getCookie(DataObject.class));
106         
107         assertFileObjects(obj);
108         assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files());
109         obj.rename("kuk");
110         assertFileObjects(obj);
111         assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files());
112         obj.move(obj.getFolder().getFolder());
113         assertFileObjects(obj);
114         assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files());
115         shadow.rename("somenewshadow");
116         assertFileObjects(obj);
117         assertFileObjects("However FileObject of a shadow are delegated to the original", shadow, obj.files());
118         obj.delete();
119         /*
120         DataObject broken = DataObject.find(shadow.getPrimaryFile());
121         if (shadow == broken) {
122             fail("They should be different: " + shadow + " != " + broken);
123         }
124         assertEquals("DataObject for the shadow is now the shadow", broken, broken.getCookie(DataObject.class));
125         assertFileObjects(broken);
126          */

127     }
128     
129     private static void assertFileObjects(DataObject obj) {
130         assertFileObjects("", obj, obj.files());
131     }
132     
133     private static void assertFileObjects(String JavaDoc msg, DataObject obj, Collection JavaDoc<? extends FileObject> expect) {
134         Collection JavaDoc<? extends FileObject> allcol = obj.getNodeDelegate().getLookup().lookupAll(FileObject.class);
135         List JavaDoc<FileObject> all = new ArrayList JavaDoc<FileObject>(allcol);
136         Enumeration JavaDoc<? extends FileObject> files = Collections.enumeration(expect);
137         int i = 0;
138         while (files.hasMoreElements()) {
139             FileObject fo = files.nextElement();
140             if (i >= all.size()) {
141                 fail(msg + "\nThere should be more elements, but there is only " + all.size() + "\nAll: " + all + "\nCurrent: " + fo);
142             }
143             
144             if (fo.equals(all.get(i))) {
145                 i++;
146                 continue;
147             }
148             fail(msg + "\nError at position " + i + " expected: " + fo + " but was: " + all.get(i) + "\nAll: " + all);
149         }
150     }
151     
152     public static final class OwnDataLoaderPool extends DataLoaderPool {
153         protected Enumeration JavaDoc<? extends DataLoader> loaders() {
154             return Enumerations.singleton(OwnDataLoader.getLoader(OwnDataLoader.class));
155         }
156     }
157
158
159     public static class OwnDataLoader extends UniFileLoader {
160         private static final long serialVersionUID = 1L;
161
162         public OwnDataLoader() {
163             super("org.openide.loaders.OwnDataObject");
164         }
165
166         protected String JavaDoc defaultDisplayName() {
167             return NbBundle.getMessage(OwnDataLoader.class, "LBL_Own_loader_name");
168         }
169
170         protected void initialize() {
171             super.initialize();
172             getExtensions().addExtension("own");
173         }
174
175         protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException JavaDoc {
176             return new OwnDataObject(primaryFile, this);
177         }
178     }
179     static class OwnDataObject extends MultiDataObject implements Lookup.Provider {
180
181         public OwnDataObject(FileObject pf, OwnDataLoader loader) throws DataObjectExistsException, IOException JavaDoc {
182             super(pf, loader);
183             CookieSet cookies = getCookieSet();
184             cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
185         }
186
187         protected Node createNodeDelegate() {
188             return new OwnDataNode(this, getLookup());
189         }
190
191         public Lookup getLookup() {
192             return getCookieSet().getLookup();
193         }
194     }
195     
196     static class OwnDataNode extends DataNode {
197         private static final String JavaDoc IMAGE_ICON_BASE = "SET/PATH/TO/ICON/HERE";
198
199         public OwnDataNode(OwnDataObject obj, Lookup lookup) {
200             super(obj, Children.LEAF, lookup);
201             // setIconBaseWithExtension(IMAGE_ICON_BASE);
202
}
203
204     }
205
206 }
207
Popular Tags