001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.loaders;
043:
044: import javax.swing.SwingUtilities;
045: import org.openide.filesystems.*;
046: import org.openide.loaders.*;
047: import java.beans.*;
048: import java.util.logging.Level;
049: import org.netbeans.junit.*;
050: import org.openide.nodes.Node;
051:
052: /*
053: * This class tests the basic functionality of standard DataObjects - Instance,
054: * Settings, Default, Folder and Shadow.
055: * @author js104452
056: */
057: import org.openide.util.Lookup;
058:
059: public class BasicDataObjectTest extends NbTestCase {
060:
061: /** Creates new BasicDataObjectTest */
062: public BasicDataObjectTest(String name) {
063: super (name);
064: }
065:
066: // For each test setup a FileSystem and DataObjects
067: protected void setUp() throws Exception {
068: clearWorkDir();
069: lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct);
070: Repository.getDefault().addFileSystem(lfs);
071: subDir = lfs.findResource("/Dir/SubDir");
072: dir = lfs.findResource("/Dir");
073: do1 = DataObject.find(subDir);
074: do2 = DataObject.find(dir);
075: df1 = DataFolder.findFolder(subDir);
076: df2 = DataFolder.findFolder(dir);
077: }
078:
079: //Clear all stuff when the test finish
080: protected void tearDown() throws Exception {
081: TestUtilHid.destroyLocalFileSystem(getName());
082: }
083:
084: public void testLookupIsReturnedEvenIfDataObjectIsDeleted()
085: throws Exception {
086: CharSequence log = Log.enable("org.openide.loaders",
087: Level.WARNING);
088:
089: DataObject obj = DataObject.find(FileUtil.createData(subDir,
090: "somedata.txt"));
091:
092: Lookup l = obj.getLookup();
093:
094: obj.delete();
095:
096: assertFalse("Does not exist", obj.isValid());
097:
098: Lookup ln = obj.getLookup();
099:
100: assertEquals("Lookups are the same type", l.getClass(), ln
101: .getClass());
102:
103: // assertEquals("No warnings", "", log.toString());
104: }
105:
106: public void testDirectCallToDataObjectContructorIsNotAllowed()
107: throws Exception {
108: try {
109: LocalFileSystem lfs = new LocalFileSystem();
110: new MultiDataObject(lfs.getRoot(), DataLoaderPool
111: .getFolderLoader());
112: fail("Constructor succeeded, it should not");
113: } catch (IllegalStateException ex) {
114: // ok, we expect IllegalStateException
115: }
116: }
117:
118: public void testRenameShouldNotMove() throws Exception {
119: FileObject fo = dir.createData("file.txt");
120: DataObject obj = DataObject.find(fo);
121: try {
122: obj.getNodeDelegate().setName("SubDir/x.txt");
123: fail("Rename should not be allowed to move: " + obj);
124: } catch (IllegalArgumentException ex) {
125: // this is what should be thrown
126: }
127: try {
128: obj.getNodeDelegate().setName("../x.txt");
129: fail("Move to root should not be allowed to move: " + obj);
130: } catch (IllegalArgumentException ex) {
131: // this is what should be thrown
132: }
133: }
134:
135: public void testRenameFiresProperly() throws Exception {
136:
137: class L implements PropertyChangeListener {
138: boolean name;
139: boolean files;
140: boolean dname;
141:
142: public void propertyChange(PropertyChangeEvent evt) {
143: if (evt.getPropertyName().equals(DataObject.PROP_NAME))
144: name = true;
145: if (evt.getPropertyName().equals(DataObject.PROP_FILES))
146: files = true;
147: if (evt.getPropertyName()
148: .equals(Node.PROP_DISPLAY_NAME))
149: dname = true;
150: }
151: }
152:
153: FileObject fo = dir.createData("file.txt");
154: DataObject data = DataObject.find(fo);
155: L dol = new L();
156: L dnl = new L();
157: data.addPropertyChangeListener(dol);
158: data.getNodeDelegate().addPropertyChangeListener(dnl);
159:
160: data.getNodeDelegate().setName("file2.txt");
161:
162: // wait for DataNode firing in AWT
163: SwingUtilities.invokeAndWait(new Runnable() {
164: public void run() {
165: }
166: });
167:
168: assertTrue("DataObject fired PROP_NAME on rename.", dol.name);
169: assertTrue("DataObject fired PROP_FILES on rename.", dol.files);
170: assertTrue("DataNode fired PROP_NAME on rename.", dnl.name);
171: assertTrue("DataNode fired PROP_FILES on rename.", dnl.files);
172: // assertTrue("DataNode fired PROP_DISPLAY_NAME on rename.", dnl.dname);
173: }
174:
175: // Test basic functionality of a DataFolder
176: public void testBasicDataFolder() throws Exception {
177:
178: assertTrue(
179: "The DataObject.getFolder() method hasn't returned the same Folder as Folder which was created directly from the FileObject.",
180: do1.equals(df1));
181:
182: assertTrue(
183: "The DataObject.getClass() method hasn't returned class org.openide.loaders.DataFolder but "
184: + do1.getClass().toString() + ".", do1
185: .getClass().toString().equalsIgnoreCase(
186: "class org.openide.loaders.DataFolder"));
187:
188: assertTrue(
189: "The DataObject.getNodeDelegate().getCookie(DataObject.getClass()) method hasn't returned the DataFolder itself.",
190: df1.equals(do1.getNodeDelegate().getCookie(
191: do1.getClass())));
192: assertTrue(
193: "The DataObject.getNodeDelegate().getCookie(DataObject.getClass()) method hasn't returned the DataObject itself.",
194: do1.equals(do1.getNodeDelegate().getCookie(
195: do1.getClass())));
196:
197: assertTrue(
198: "The DataObject.getFolder().getPrimaryFile() hasn't returned the same value as the DataObject.getPrimaryFile().getParent() method.",
199: do1.getFolder().getPrimaryFile().equals(
200: do1.getPrimaryFile().getParent()));
201:
202: assertTrue(
203: "The DataObject.getName() hasn't returned the proper name but "
204: + do1.getName() + ".", do1.getName()
205: .equalsIgnoreCase("SubDir"));
206:
207: assertTrue(
208: "The DataObject.getLoader() hasn't returned the proper loader class but "
209: + do1.getLoader().getClass().toString() + ".",
210: do1
211: .getLoader()
212: .getClass()
213: .toString()
214: .equalsIgnoreCase(
215: "class org.openide.loaders.DataLoaderPool$FolderLoader"));
216: assertTrue(
217: "Two different DataObjects created on folder have not the same loader.",
218: do1.getLoader().equals(do2.getLoader()));
219: assertTrue(
220: "DataObject created on folder has not the same loader as the DataFolder.",
221: do2.getLoader().equals(df1.getLoader())
222: || df1.getLoader().equals(df2.getLoader()));
223:
224: DataObject[] arr = df1.getChildren();
225:
226: assertTrue(
227: "The DataFolder.getChildren() hasn't returned proper value.",
228: arr.length == 0);
229:
230: boolean hlpBool_1 = do1.isValid();
231: do1.setValid(!hlpBool_1);
232: boolean hlpBool_2 = (do1.isValid() == !hlpBool_1);
233:
234: assertTrue(
235: "The DataObject.setValid() method has not changed the Valid state.",
236: hlpBool_2);
237:
238: hlpBool_2 = (do1.isValid() == df1.isValid());
239:
240: assertTrue(
241: "The DataObject.setValid() method has not changed the Valid state on a DataFolder object created directly from the Folder.",
242: hlpBool_2);
243: }
244:
245: // Test basic functionality of a DataInstance
246: public void testBasicDataInstance() throws Exception {
247: FileObject fo1 = do1.getPrimaryFile().createData(
248: "file.instance");
249: FileObject fo2 = do1.getPrimaryFile().createData(
250: "file.settings");
251: Object nc = do1.getCookie(DataFolder.class);
252: if (nc == null) {
253: fail("FAILED - can't recieave the DataFolder.class Coockie.");
254: }
255: DataFolder df1 = (DataFolder) nc;
256: DataObject[] arr = df1.getChildren();
257:
258: if (arr.length <= 0 && arr.length > 2) {
259: fail("The DataFolder.getChildren() hasn't returned proper value.");
260: }
261:
262: for (int i = 0; i < arr.length; i++) {
263: assertTrue(
264: "The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is.",
265: arr[i].getFolder().equals(df1));
266:
267: assertTrue(
268: "The DataObject.getLoader() hasn't returned the proper loader class but "
269: + arr[i].getLoader().getClass().toString()
270: + ".",
271: arr[i]
272: .getLoader()
273: .getClass()
274: .toString()
275: .equalsIgnoreCase(
276: "class org.openide.loaders.DataLoaderPool$InstanceLoader"));
277:
278: assertTrue(
279: "The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",
280: arr[i].getPrimaryFile().getParent().getName()
281: .equalsIgnoreCase(df1.getName()));
282:
283: assertTrue(
284: "The DataObject.getClass() method hasn't returned class org.openide.loaders.InstanceDataObject but "
285: + arr[i].getClass().toString() + ".",
286: arr[i]
287: .getClass()
288: .toString()
289: .equalsIgnoreCase(
290: "class org.openide.loaders.InstanceDataObject"));
291:
292: assertTrue(
293: "The DataObject.getPrimaryFile().getName() hasn't returned the same value as DataObject.getName().",
294: arr[i].getPrimaryFile().getName().equalsIgnoreCase(
295: arr[i].getName()));
296: }
297: }
298:
299: // Test basic functionality of a DataDefault
300: public void testBasicDataDefault() throws Exception {
301: FileObject fo = do1.getPrimaryFile().createData("file.default");
302: Object nc = do1.getCookie(DataFolder.class);
303: if (nc == null) {
304: fail("FAILED - can't recieave the DataFolder.class Coockie.");
305: }
306: DataFolder df1 = (DataFolder) nc;
307: DataObject[] arr = df1.getChildren();
308:
309: if (arr.length <= 0 && arr.length > 1) {
310: fail("The DataFolder.getChildren() hasn't returned proper value.");
311: }
312:
313: int i = 0;
314:
315: assertTrue(
316: "The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is.",
317: arr[i].getFolder().equals(df1));
318:
319: assertTrue(
320: "The DataObject.getLoader() hasn't returned the proper loader class but "
321: + arr[i].getLoader().getClass().toString()
322: + ".",
323: arr[i]
324: .getLoader()
325: .getClass()
326: .toString()
327: .equalsIgnoreCase(
328: "class org.openide.loaders.DataLoaderPool$DefaultLoader"));
329:
330: assertTrue(
331: "The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",
332: arr[i].getPrimaryFile().getParent().getName()
333: .equalsIgnoreCase(df1.getName()));
334:
335: assertTrue(
336: "The DataObject.getClass() method hasn't returned class org.openide.loaders.DefaultDataObject but "
337: + arr[i].getClass().toString() + ".",
338: arr[i].getClass().toString().equalsIgnoreCase(
339: "class org.openide.loaders.DefaultDataObject"));
340:
341: assertTrue(
342: "The DataObject.getPrimaryFile().getNameExt() hasn't returned the same value as DataObject.getName().",
343: arr[i].getPrimaryFile().getNameExt().equalsIgnoreCase(
344: arr[i].getName()));
345: }
346:
347: // Test basic functionality of a DataShadow
348: public void testBasicDataShadow() throws Exception {
349: FileObject fo = do1.getPrimaryFile().createData("file.shadow");
350: Object nc = do1.getCookie(DataFolder.class);
351: if (nc == null) {
352: fail("FAILED - can't recieave the DataFolder.class Coockie.");
353: }
354: DataFolder df1 = (DataFolder) nc;
355: DataObject[] arr = df1.getChildren();
356:
357: if (arr.length <= 0 && arr.length > 1) {
358: fail("The DataFolder.getChildren() hasn't returned proper value.");
359: }
360:
361: int i = 0;
362:
363: assertTrue(
364: "The DataObject.getFolder() hasn't returned the same DataFolder as the existing DataFolder is.",
365: arr[i].getFolder().equals(df1));
366:
367: assertTrue(
368: "The DataObject.getLoader() hasn't returned the proper loader class but "
369: + arr[i].getLoader().getClass().toString()
370: + ".",
371: arr[i]
372: .getLoader()
373: .getClass()
374: .toString()
375: .equalsIgnoreCase(
376: "class org.openide.loaders.DataLoaderPool$ShadowLoader"));
377:
378: assertTrue(
379: "The DataObject.getPrimaryFile().getParent().getName() hasn't returned the same value as the existing DataFolder.getName()",
380: arr[i].getPrimaryFile().getParent().getName()
381: .equalsIgnoreCase(df1.getName()));
382:
383: assertTrue(
384: "The DataObject.getClass() method hasn't returned class org.openide.loaders.BrokenDataShadow but "
385: + arr[i].getClass().toString() + ".",
386: arr[i].getClass().toString().equalsIgnoreCase(
387: "class org.openide.loaders.BrokenDataShadow"));
388:
389: assertTrue(
390: "The DataObject.getPrimaryFile().getName() hasn't returned the same value as DataObject.getName().",
391: arr[i].getPrimaryFile().getName().equalsIgnoreCase(
392: arr[i].getName()));
393: }
394:
395: private String fsstruct[] = new String[] { "Dir/SubDir/" };
396: private FileSystem lfs;
397: private FileObject subDir;
398: private FileObject dir;
399: private DataObject do1;
400: private DataObject do2;
401: private DataFolder df1;
402: private DataFolder df2;
403:
404: public void testPropValidAfeterFileDeletion() throws Exception {
405: class PropListener implements PropertyChangeListener {
406: PropertyChangeEvent validEvent;
407:
408: public void propertyChange(PropertyChangeEvent evt) {
409: if (evt.getPropertyName().equals(DataObject.PROP_VALID)) {
410: validEvent = evt;
411: }
412: }
413:
414: public PropertyChangeEvent getEvent() {
415: return validEvent;
416: }
417: }
418:
419: FileObject fo = dir.createData("test.test");
420: DataObject data = DataObject.find(fo);
421: PropListener l = new PropListener();
422: data.addPropertyChangeListener(l);
423: assertTrue(data.isValid());
424: assertNull(l.getEvent());
425: fo.delete();
426: // now DO becomes invalid
427: assertFalse(data.isValid());
428: assertNotNull(
429: "Data object schould recive DataObject.PROP_VALID event.",
430: l.getEvent());
431: }
432:
433: public void testDeleteAndTryToDeserializeIssue47446()
434: throws Exception {
435: doSerTest(this .do1);
436: }
437:
438: public void testDeleteAndTryToDeserializeOnFolderIssue47446()
439: throws Exception {
440: doSerTest(DataObject.find(FileUtil.createData(subDir,
441: "somedata.txt")));
442: }
443:
444: public void testDataObjectIsInItLookup() throws Exception {
445: DataObject obj = DataObject.find(FileUtil.createData(subDir,
446: "somedata.txt"));
447:
448: DataObject query = obj.getLookup().lookup(DataObject.class);
449: assertSame("Object is in its own lookup", obj, query);
450: }
451:
452: private void doSerTest(DataObject obj) throws Exception {
453: org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject(
454: obj);
455:
456: assertSame(
457: "If my object exists, deserialization returns the same",
458: obj, mar.get());
459: obj.delete();
460:
461: try {
462: mar.get();
463: fail("Deserialization is supposed to fire an exception");
464: } catch (java.io.FileNotFoundException ex) {
465: // ok
466: }
467: }
468: }
|