KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.SwingUtilities JavaDoc;
23 import org.openide.filesystems.*;
24 import org.openide.loaders.*;
25 import java.beans.*;
26 import org.netbeans.junit.*;
27 import org.openide.nodes.Node;
28
29 /*
30  * This class tests the basic functionality of standard DataObjects - Instance,
31  * Settings, Default, Folder and Shadow.
32  * @author js104452
33  */

34 public class BasicDataObjectTest extends NbTestCase {
35
36     /** Creates new BasicDataObjectTest */
37     public BasicDataObjectTest(String JavaDoc name) {
38         super(name);
39     }
40
41     // For each test setup a FileSystem and DataObjects
42
protected void setUp() throws Exception JavaDoc {
43         clearWorkDir ();
44         lfs = TestUtilHid.createLocalFileSystem (getWorkDir (), fsstruct);
45         Repository.getDefault ().addFileSystem (lfs);
46         subDir = lfs.findResource("/Dir/SubDir");
47         dir = lfs.findResource("/Dir");
48         do1 = DataObject.find(subDir);
49         do2 = DataObject.find(dir);
50         df1 = DataFolder.findFolder(subDir);
51         df2 = DataFolder.findFolder(dir);
52     }
53     
54     //Clear all stuff when the test finish
55
protected void tearDown() throws Exception JavaDoc {
56         TestUtilHid.destroyLocalFileSystem (getName());
57     }
58     
59     public void testDirectCallToDataObjectContructorIsNotAllowed () throws Exception JavaDoc {
60         try {
61             LocalFileSystem lfs = new LocalFileSystem ();
62             new MultiDataObject (lfs.getRoot(), DataLoaderPool.getFolderLoader ());
63             fail ("Constructor succeeded, it should not");
64         } catch (IllegalStateException JavaDoc ex) {
65             // ok, we expect IllegalStateException
66
}
67     }
68     
69     public void testRenameShouldNotMove () throws Exception JavaDoc {
70         FileObject fo = dir.createData ("file.txt");
71         DataObject obj = DataObject.find (fo);
72         try {
73             obj.getNodeDelegate ().setName ("SubDir/x.txt");
74             fail ("Rename should not be allowed to move: " + obj);
75         } catch (IllegalArgumentException JavaDoc ex) {
76             // this is what should be thrown
77
}
78         try {
79             obj.getNodeDelegate ().setName ("../x.txt");
80             fail ("Move to root should not be allowed to move: " + obj);
81         } catch (IllegalArgumentException JavaDoc ex) {
82             // this is what should be thrown
83
}
84     }
85
86     public void testRenameFiresProperly () throws Exception JavaDoc {
87         
88         class L implements PropertyChangeListener{
89             boolean name;
90             boolean files;
91             boolean dname;
92             
93             public void propertyChange(PropertyChangeEvent evt) {
94                 if(evt.getPropertyName().equals(DataObject.PROP_NAME)) name = true;
95                 if(evt.getPropertyName().equals(DataObject.PROP_FILES)) files = true;
96                 if(evt.getPropertyName().equals(Node.PROP_DISPLAY_NAME)) dname = true;
97             }
98         }
99         
100         FileObject fo = dir.createData ("file.txt");
101         DataObject data = DataObject.find(fo);
102         L dol = new L();
103         L dnl = new L();
104         data.addPropertyChangeListener(dol);
105         data.getNodeDelegate().addPropertyChangeListener(dnl);
106
107         data.getNodeDelegate ().setName ("file2.txt");
108         
109         // wait for DataNode firing in AWT
110
SwingUtilities.invokeAndWait(new Runnable JavaDoc() { public void run() {} });
111         
112         assertTrue("DataObject fired PROP_NAME on rename.", dol.name);
113         assertTrue("DataObject fired PROP_FILES on rename.", dol.files);
114         assertTrue("DataNode fired PROP_NAME on rename.", dnl.name);
115         assertTrue("DataNode fired PROP_FILES on rename.", dnl.files);
116         // assertTrue("DataNode fired PROP_DISPLAY_NAME on rename.", dnl.dname);
117
}
118
119     
120     // Test basic functionality of a DataFolder
121
public void testBasicDataFolder() throws Exception JavaDoc {
122         
123         assertTrue("The DataObject.getFolder() method hasn't returned the same Folder as Folder which was created directly from the FileObject.",do1.equals(df1));
124
125         assertTrue("The DataObject.getClass() method hasn't returned class org.openide.loaders.DataFolder but "+do1.getClass().toString()+".",do1.getClass().toString().equalsIgnoreCase("class org.openide.loaders.DataFolder"));
126
127         assertTrue("The DataObject.getNodeDelegate().getCookie(DataObject.getClass()) method hasn't returned the DataFolder itself.",df1.equals(do1.getNodeDelegate().getCookie(do1.getClass())));
128         assertTrue("The DataObject.getNodeDelegate().getCookie(DataObject.getClass()) method hasn't returned the DataObject itself.",do1.equals(do1.getNodeDelegate().getCookie(do1.getClass())));
129
130         assertTrue("The DataObject.getFolder().getPrimaryFile() hasn't returned the same value as the DataObject.getPrimaryFile().getParent() method.",do1.getFolder().getPrimaryFile().equals(do1.getPrimaryFile().getParent()));
131
132         assertTrue("The DataObject.getName() hasn't returned the proper name but "+do1.getName()+".",do1.getName().equalsIgnoreCase("SubDir"));
133         
134         assertTrue("The DataObject.getLoader() hasn't returned the proper loader class but "+do1.getLoader().getClass().toString()+".",do1.getLoader().getClass().toString().equalsIgnoreCase("class org.openide.loaders.DataLoaderPool$FolderLoader"));
135         assertTrue("Two different DataObjects created on folder have not the same loader.",do1.getLoader().equals(do2.getLoader()));
136         assertTrue("DataObject created on folder has not the same loader as the DataFolder.",do2.getLoader().equals(df1.getLoader()) || df1.getLoader().equals(df2.getLoader()));
137
138         DataObject[] arr = df1.getChildren();
139
140         assertTrue("The DataFolder.getChildren() hasn't returned proper value.",arr.length == 0);
141         
142         boolean hlpBool_1 = do1.isValid();
143         do1.setValid(!hlpBool_1);
144         boolean hlpBool_2 = (do1.isValid() == !hlpBool_1);
145
146         assertTrue("The DataObject.setValid() method has not changed the Valid state.", hlpBool_2);
147
148         hlpBool_2 = (do1.isValid() == df1.isValid());
149
150         assertTrue("The DataObject.setValid() method has not changed the Valid state on a DataFolder object created directly from the Folder.", hlpBool_2);
151     }
152
153     // Test basic functionality of a DataInstance
154
public void testBasicDataInstance() throws Exception JavaDoc {
155         FileObject fo1 = do1.getPrimaryFile().createData("file.instance");
156         FileObject fo2 = do1.getPrimaryFile().createData("file.settings");
157         Object JavaDoc nc = do1.getCookie(DataFolder.class);
158         if (nc == null) {
159             fail("FAILED - can't recieave the DataFolder.class Coockie.");
160         }
161         DataFolder df1 = (DataFolder)nc;
162         DataObject[] arr = df1.getChildren();
163         
164         if (arr.length <= 0 && arr.length > 2) {
165             fail("The DataFolder.getChildren() hasn't returned proper value.");
166         }
167
168         for (int i=0; i<arr.length; i++) {
169             assertTrue("The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is." ,arr[i].getFolder().equals(df1));
170             
171             assertTrue("The DataObject.getLoader() hasn't returned the proper loader class but "+arr[i].getLoader().getClass().toString()+".",arr[i].getLoader().getClass().toString().equalsIgnoreCase("class org.openide.loaders.DataLoaderPool$InstanceLoader"));
172             
173             assertTrue("The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",arr[i].getPrimaryFile().getParent().getName().equalsIgnoreCase(df1.getName()));
174
175             assertTrue("The DataObject.getClass() method hasn't returned class org.openide.loaders.InstanceDataObject but "+arr[i].getClass().toString()+".",arr[i].getClass().toString().equalsIgnoreCase("class org.openide.loaders.InstanceDataObject"));
176
177             assertTrue("The DataObject.getPrimaryFile().getName() hasn't returned the same value as DataObject.getName().",arr[i].getPrimaryFile().getName().equalsIgnoreCase(arr[i].getName()));
178         }
179     }
180
181     // Test basic functionality of a DataDefault
182
public void testBasicDataDefault() throws Exception JavaDoc {
183         FileObject fo = do1.getPrimaryFile().createData("file.default");
184         Object JavaDoc nc = do1.getCookie(DataFolder.class);
185         if (nc == null) {
186             fail("FAILED - can't recieave the DataFolder.class Coockie.");
187         }
188         DataFolder df1 = (DataFolder)nc;
189         DataObject[] arr = df1.getChildren();
190         
191         if (arr.length <= 0 && arr.length > 1) {
192             fail("The DataFolder.getChildren() hasn't returned proper value.");
193         }
194
195         int i = 0;
196         
197         assertTrue("The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is." ,arr[i].getFolder().equals(df1));
198
199         assertTrue("The DataObject.getLoader() hasn't returned the proper loader class but "+arr[i].getLoader().getClass().toString()+".",arr[i].getLoader().getClass().toString().equalsIgnoreCase("class org.openide.loaders.DataLoaderPool$DefaultLoader"));
200
201         assertTrue("The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",arr[i].getPrimaryFile().getParent().getName().equalsIgnoreCase(df1.getName()));
202
203         assertTrue("The DataObject.getClass() method hasn't returned class org.openide.loaders.DefaultDataObject but "+arr[i].getClass().toString()+".",arr[i].getClass().toString().equalsIgnoreCase("class org.openide.loaders.DefaultDataObject"));
204
205         assertTrue("The DataObject.getPrimaryFile().getNameExt() hasn't returned the same value as DataObject.getName().",arr[i].getPrimaryFile().getNameExt().equalsIgnoreCase(arr[i].getName()));
206     }
207
208     // Test basic functionality of a DataShadow
209
public void testBasicDataShadow() throws Exception JavaDoc {
210         FileObject fo = do1.getPrimaryFile().createData("file.shadow");
211         Object JavaDoc nc = do1.getCookie(DataFolder.class);
212         if (nc == null) {
213             fail("FAILED - can't recieave the DataFolder.class Coockie.");
214         }
215         DataFolder df1 = (DataFolder)nc;
216         DataObject[] arr = df1.getChildren();
217         
218         if (arr.length <= 0 && arr.length > 1) {
219             fail("The DataFolder.getChildren() hasn't returned proper value.");
220         }
221
222         int i = 0;
223         
224         assertTrue("The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is." ,arr[i].getFolder().equals(df1));
225
226         assertTrue("The DataObject.getLoader() hasn't returned the proper loader class but "+arr[i].getLoader().getClass().toString()+".",arr[i].getLoader().getClass().toString().equalsIgnoreCase("class org.openide.loaders.DataLoaderPool$ShadowLoader"));
227
228         assertTrue("The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",arr[i].getPrimaryFile().getParent().getName().equalsIgnoreCase(df1.getName()));
229
230         assertTrue("The DataObject.getClass() method hasn't returned class org.openide.loaders.BrokenDataShadow but "+arr[i].getClass().toString()+".",arr[i].getClass().toString().equalsIgnoreCase("class org.openide.loaders.BrokenDataShadow"));
231         
232         assertTrue("The DataObject.getPrimaryFile().getName() hasn't returned the same value as DataObject.getName().",arr[i].getPrimaryFile().getName().equalsIgnoreCase(arr[i].getName()));
233     }
234     
235     
236     private String JavaDoc fsstruct [] = new String JavaDoc [] {"Dir/SubDir/"};
237     private FileSystem lfs;
238     private FileObject subDir;
239     private FileObject dir;
240     private DataObject do1;
241     private DataObject do2;
242     private DataFolder df1;
243     private DataFolder df2;
244     
245     public void testPropValidAfeterFileDeletion() throws Exception JavaDoc{
246         class PropListener implements PropertyChangeListener{
247             PropertyChangeEvent validEvent;
248             public void propertyChange(PropertyChangeEvent evt) {
249                 if(evt.getPropertyName().equals(DataObject.PROP_VALID)){
250                 validEvent = evt;
251                 }
252             }
253             public PropertyChangeEvent getEvent(){
254                 return validEvent;
255             }
256         }
257         
258         FileObject fo = dir.createData("test.test");
259         DataObject data = DataObject.find(fo);
260         PropListener l = new PropListener();
261         data.addPropertyChangeListener(l);
262         assertTrue(data.isValid());
263         assertNull(l.getEvent());
264         fo.delete();
265         // now DO becomes invalid
266
assertFalse(data.isValid());
267         assertNotNull("Data object schould recive DataObject.PROP_VALID event.",l.getEvent());
268     }
269     
270     public void testDeleteAndTryToDeserializeIssue47446 () throws Exception JavaDoc {
271         doSerTest (this.do1);
272     }
273     
274     public void testDeleteAndTryToDeserializeOnFolderIssue47446 () throws Exception JavaDoc {
275         doSerTest (DataObject.find (
276             FileUtil.createData (subDir, "somedata.txt")
277         ));
278     }
279     
280     public void testDataObjectIsInItLookup() throws Exception JavaDoc {
281         DataObject obj = DataObject.find (
282             FileUtil.createData (subDir, "somedata.txt")
283         );
284         
285         DataObject query = obj.getLookup().lookup(DataObject.class);
286         assertSame("Object is in its own lookup", obj, query);
287     }
288     
289     private void doSerTest (DataObject obj) throws Exception JavaDoc {
290         org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (obj);
291         
292         assertSame ("If my object exists, deserialization returns the same", obj, mar.get ());
293         obj.delete ();
294         
295         try {
296             mar.get ();
297             fail ("Deserialization is supposed to fire an exception");
298         } catch (java.io.FileNotFoundException JavaDoc ex) {
299             // ok
300
}
301     }
302 }
303
Popular Tags