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: /*
043: * DataObjectTest.java
044: *
045: * Here are implemented separated tests methos, testing manipulating operations with DataObejcts.
046: * Other classes in this packages use them in the logic chain to create meaningfull testcases.
047: *
048: * Created on June 13, 2001, 1:19 PM
049: */
050:
051: package DataLoaderTests.DataObjectTest;
052:
053: import java.io.FileNotFoundException;
054: import java.io.StringWriter;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.loaders.DataObject;
058: import org.openide.loaders.DataShadow;
059: import org.openide.loaders.DataFolder;
060: import org.openide.loaders.DataLoader;
061: import org.openide.loaders.DataNode;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.Lookup;
064:
065: import java.io.*;
066: import java.util.Enumeration;
067: import java.util.Properties;
068: import junit.framework.*;
069: import org.netbeans.junit.*;
070: import org.openide.loaders.DataObjectNotFoundException;
071:
072: public class DataObjectTest extends NbTestCase {
073:
074: DataFolder resources = null;
075: /**
076: * teporary data folder
077: */
078: DataFolder temp = null;
079:
080: /** Creates new DataObjectTest */
081: public DataObjectTest(java.lang.String testName) {
082: super (testName);
083: }
084:
085: boolean successful = true;
086:
087: /**This methods write an output to log stream*/
088: public void writeLog(String text) {
089: log(text);
090: System.out.println(text);
091: if (text.equals(FAILED))
092: successful = false;
093: }
094:
095: /**This methods write an output to reference stream*/
096: public void writeRef(String text) {
097: ref(text);
098: System.out.println(text);
099: if (text.equals(FAILED))
100: successful = false;
101: }
102:
103: /**This methods write an output to reference stream and asserts success of the test fragment*/
104: public void writeRef(String text, String inf) {
105: ref(text);
106: System.out.println(text);
107: if (inf.equals(FAILED))
108: successful = false;
109: assertTrue(text, successful);
110: }
111:
112: /**If enabled, prints exception to the output and to the ref stream*/
113: void printException(Exception e) {
114: // if(PRINT_EXCEPTIONS) {
115: e.printStackTrace();
116: e.printStackTrace(getRef());
117: // }
118: }
119:
120: String exceptionToString(Exception e) {
121: StringWriter writer = new StringWriter();
122: PrintWriter pw = new PrintWriter(writer);
123: e.printStackTrace(pw);
124: pw.close();
125: return writer.toString();
126: }
127:
128: /**overrides parent definition of this methot,
129: *so this new works in this way - returns work filed
130: *that should have been set by user of this utility class
131: */
132: public String getWorkDirPath() {
133: if (work == null)
134: fail("Working directory not set!");
135: //always return what a someone else has set
136: return work;
137: }
138:
139: /**
140: *Performs initializing before own tests starts
141: */
142: void prepare() throws Exception {
143: //initializing ide
144:
145: //when not in XTest harness -> woring directory will be under actual userdir
146: successful = true;
147:
148: String xtestData = System.getProperty("xtest.data");
149: File dataDir = new File(xtestData, "DataObjectTest");
150: assertTrue(dataDir.exists());
151: if (dataDir.exists() == false) {
152: throw new FileNotFoundException(dataDir.getPath());
153: }
154: FileObject fo = FileUtil.toFileObject(dataDir);
155: assertNotNull(fo);
156: resources = DataFolder.findFolder(fo);
157: System.err.println("resources:" + resources);
158:
159: temp = DataFolder.findFolder(FileUtil.toFileObject(new File(
160: System.getProperty("xtest.tmpdir"))));
161: assertNotNull("No temporary folder found.", temp);
162: }
163:
164: /**
165: *Performs clean up
166: */
167: void clean() {
168: //Put some cleaning stuff here ...
169: }
170:
171: /**
172: *Performs waiting of current thread for time in millis
173: *@param millist integer number - time in millis to wait
174: */
175: void dummyWait(int millis) {
176: try {
177: Thread.sleep(millis);
178: } catch (Exception ex) {
179: printException(ex);
180: }
181: }
182:
183: DataObject getDataObject(FileObject fo) {
184: DataObject dob = null;
185: try {
186: dob = DataObject.find(fo);
187: } catch (Exception ex) {
188: printException(ex);
189: writeRef("DataObject.find has failed!", FAILED);
190: return null;
191: }
192: DataLoader loader = dob.getLoader();
193:
194: return getDataObject(fo, loader.getDisplayName());
195: }
196:
197: DataObject getDataObject(FileObject fo, String moduleName) {
198: writeRef("\nGetting DataObject started ...");
199: DataLoader loader = null;
200: try {
201: loader = new DataLoaderTests.LoaderPoolTest.LoaderPoolTest(
202: "x").getDataLoader(moduleName);
203: } catch (Exception ex) {
204: //simple do nothing
205: }
206: DataObject do1 = null;
207: try {
208: do1 = DataObject.find(fo);
209: } catch (Exception ex) {
210: printException(ex);
211: writeRef("DataObject.find has failed", FAILED);
212: return null;
213: }
214:
215: DataLoader loader2 = do1.getLoader();
216: if ((loader != null) && (loader2 != null)) {
217: if (!loader.equals(loader2)) {
218: writeRef("DLs do not equals!", FAILED);
219: return null;
220: } else {
221: writeRef(PASSED);
222: return do1;
223: }
224: } else {
225: if (loader2 != null) {
226: writeRef(PASSED);
227: return do1;
228: }
229: }
230: writeRef("Both DLs null!?!", FAILED);
231: return null;
232: }
233:
234: boolean containsFO(DataObject dob, FileObject fob) {
235: writeRef("\nDoes files contain FO ...");
236: java.util.Iterator it = dob.files().iterator();
237: while (it.hasNext()) {
238: if (fob.equals(it.next())) {
239: writeRef(PASSED);
240: return true;
241: }
242: }
243: writeRef("Files() doesn't contain the FO!", FAILED);
244: return false;
245: }
246:
247: boolean isInFolder(DataObject dob, DataFolder df) {
248: writeRef("\nIs this DO in that DF ...");
249: if (dob.getFolder().equals(df)) {
250: writeRef(PASSED);
251: return true;
252: } else {
253: writeRef("This DO isn't in the DF!", FAILED);
254: return false;
255: }
256: }
257:
258: boolean testHelpCtx(DataObject dob) {
259: writeRef("\nAsking for HelpCtx ...");
260: HelpCtx hc = dob.getHelpCtx();
261: assertTrue(dob.isValid());
262: System.out.println(dob.getClass());
263:
264: if (((dob instanceof DataFolder) && hc == null)
265: || ((hc.getHelpID() == null) && (hc.getHelp() != null))
266: || ((hc.getHelpID() != null) && (hc.getHelp() == null))) {
267: writeRef(PASSED);
268: return true;
269: } else {
270: writeRef("HelpCtx error!", FAILED);
271: return false;
272: }
273: }
274:
275: void getName(DataObject dob) {
276: writeRef("\nGetting name ...");
277: writeRef("\n" + dob.getName());
278: writeRef(PASSED);
279: }
280:
281: void testDelegate(DataObject dob) {
282: //very simple test this test tests DO not Nodes ;-)
283: writeRef("\nTesting DataObject's node delegate ...");
284: DataNode dn = (DataNode) dob.getNodeDelegate();
285:
286: DataObjectTest.PChL l = new DataObjectTest.PChL();
287:
288: dob.addPropertyChangeListener(l);
289:
290: writeRef("\nDisplayName: " + dn.getDisplayName());
291: writeRef("Name: " + dn.getName());
292: writeRef("ShortDescription: " + dn.getShortDescription());
293: writeRef("ShowFileExtensions: " + dn.getShowFileExtensions());
294: writeRef("Preferred: " + dn.isPreferred());
295: //writeRef("ParentNode name: " + dn.getParentNode().getName());
296: writeRef("Expert: " + dn.isExpert());
297: writeRef("Hidden: " + dn.isHidden());
298: writeRef("Leaf: " + dn.isLeaf());
299:
300: String dsn = dn.getDisplayName();
301: String n = dn.getName();
302: String sd = dn.getShortDescription();
303: boolean sfe = dn.getShowFileExtensions();
304: boolean p = dn.isPreferred();
305:
306: //new settings
307: dn.setDisplayName("Oleee");
308: dn.setName("Hmmm", true);
309: dn.setShortDescription("A short description.");
310: dn.setShowFileExtensions(true);
311: dn.setPreferred(true);
312:
313: writeRef("\nnew DisplayName: " + dn.getDisplayName());
314: writeRef("new Name: " + dn.getName());
315: writeRef("new ShortDescription: " + dn.getShortDescription());
316: writeRef("new ShowFileExtensions: "
317: + dn.getShowFileExtensions());
318: writeRef("new Preferred: " + dn.isPreferred());
319:
320: dummyWait(1000);
321:
322: dob.removePropertyChangeListener(l);
323:
324: //restoring old settings
325: dn.setDisplayName(dsn);
326: dn.setName(n, true);
327: dn.setShortDescription(sd);
328: dn.setShowFileExtensions(sfe);
329: dn.setPreferred(p);
330:
331: writeRef(PASSED);
332: }
333:
334: class PChL implements java.beans.PropertyChangeListener {
335: public void propertyChange(
336: java.beans.PropertyChangeEvent propertyChangeEvent) {
337: writeRef("\nFrom DO's property listener: "
338: + propertyChangeEvent.getPropertyName() + "("
339: + propertyChangeEvent.getOldValue() + " -> "
340: + propertyChangeEvent.getNewValue() + ")");
341: }
342: }
343:
344: class VChL implements java.beans.VetoableChangeListener {
345: public void vetoableChange(
346: java.beans.PropertyChangeEvent propertyChangeEvent)
347: throws java.beans.PropertyVetoException {
348: if (DataObject.PROP_VALID.equals(propertyChangeEvent
349: .getPropertyName())
350: && (propertyChangeEvent.getNewValue() == Boolean.FALSE)) {
351: writeRef("\nGoing to veto this change ...");
352: throw new java.beans.PropertyVetoException(
353: "This change is not allowed ;-)",
354: propertyChangeEvent);
355: } else {
356: writeRef("\nNot vetoing this change.");
357: }
358: }
359: }
360:
361: boolean inModifiedContainer(DataObject dob) {
362: writeRef("\nShould be in modified container ...");
363: java.util.Iterator it = dob.getRegistry().getModifiedSet()
364: .iterator();
365: while (it.hasNext()) {
366: if (dob.equals(it.next())) {
367: writeRef(PASSED);
368: return true;
369: }
370: }
371: writeRef("This DO isn't in modified container!", FAILED);
372: return false;
373: }
374:
375: boolean notInModifiedContainer(DataObject dob) {
376: writeRef("\nShould not be in modified container ...");
377: java.util.Iterator it = dob.getRegistry().getModifiedSet()
378: .iterator();
379: while (it.hasNext()) {
380: if (dob.equals(it.next())) {
381: writeRef("This DO is in modified container!", FAILED);
382: return false;
383: }
384: }
385: writeRef(PASSED);
386: return true;
387: }
388:
389: class ChL implements javax.swing.event.ChangeListener {
390: public void stateChanged(javax.swing.event.ChangeEvent e) {
391: writeRef("\nSome change over set of modified DOs!");
392: writeRef("Registry: " + e.getSource().toString());
393: }
394: }
395:
396: public boolean checkModifyAbility(DataObject dob) throws Exception {
397: writeRef("\nChecking modify marking facility ...");
398: javax.swing.event.ChangeListener l = new DataObjectTest.ChL();
399: dob.getRegistry().addChangeListener(l);
400: performDelete(dob);
401: dummyWait(1000);
402: if (!dob.isModified()) {
403: if (notInModifiedContainer(dob)) {
404: writeRef("\nMarking as modified ...");
405: dob.setModified(true);
406: dummyWait(1000);
407: if (inModifiedContainer(dob)) {
408: writeRef(PASSED);
409: writeRef("\nMarking as not modified ...");
410: dob.setModified(false);
411: } else {
412: writeRef(
413: "Now should be in modified container but isn't!",
414: FAILED);
415: return false;
416: }
417: } else {
418: writeRef("Modified but not in the modified registry!",
419: FAILED);
420: return false;
421: }
422: } else {
423: writeRef(
424: "I have thought that it shouldn't be in modified registry.!",
425: FAILED);
426: return false;
427: }
428: dummyWait(1000);
429: if (!notInModifiedContainer(dob)) {
430: writeRef("Shouldn't be in modified registry!!", FAILED);
431: return false;
432: } else
433: writeRef(PASSED);
434: dummyWait(1000);
435: dob.getRegistry().removeChangeListener(l);
436: writeRef(PASSED);
437: return true;
438: }
439:
440: public void checkValidity(DataObject dob) throws Exception {
441: writeRef("\nTesting validity ...");
442: if (!dob.isValid()) {
443: writeRef("DO have to be valid for this test!!", FAILED);
444: return;
445: }
446: DataObject newDO = null;
447: if (dob.isCopyAllowed()) {
448: newDO = dob.copy(temp);
449: } else {
450: writeRef("\nCopy not allowed!");
451: return;
452: }
453: if (!newDO.isValid()) {
454: writeRef(
455: "Newly created working DO have to be valid for this test!",
456: FAILED);
457: return;
458: }
459: DataObjectTest.VChL v = new DataObjectTest.VChL();
460: newDO.addVetoableChangeListener(v);
461: writeRef("\nChecking vetoableChangeListener ...");
462: try {
463: newDO.setValid(false);
464: writeRef("This change should have been vetoed!", FAILED);
465: } catch (Exception ex) {
466: writeRef(ex.getMessage());
467: writeRef(PASSED);
468: }
469: dummyWait(2000);
470: newDO.removeVetoableChangeListener(v);
471: if (!newDO.isValid()) {
472: writeRef(
473: "setValid(false) should have been vetoed, but isn't, now is newDO invalid, cannot continue!",
474: FAILED);
475: return;
476: }
477: if (newDO.isDeleteAllowed()) {
478: try {
479: newDO.delete();
480: } catch (Exception ex) {
481: printException(ex);
482: writeRef("Deleting of copied object failed!", FAILED);
483: return;
484: }
485: } else {
486: writeRef("\nDelete not allowed!");
487: return;
488: }
489: if (newDO.isValid()) {
490: writeRef(
491: "newDO should not be valid at the end of this test!",
492: FAILED);
493: return;
494: }
495: writeRef(PASSED);
496: }
497:
498: public boolean performCopy(DataObject dob) throws Exception {
499: writeRef("\nNow copying ...");
500: DataObject newDO = null;
501: if (dob.isCopyAllowed()) {
502: newDO = dob.copy(temp);
503: } else {
504: writeRef("\nCopy not allowed!");
505: return false;
506: }
507: if (!newDO.getName().equals(dob.getName())) {
508: writeRef("Old and new name differ!", FAILED);
509: return false;
510: }
511: if (newDO.isDeleteAllowed()) {
512: newDO.delete();
513: } else {
514: writeRef("\nDelete not allowed!");
515: return false;
516: }
517: writeRef(PASSED);
518: return true;
519: }
520:
521: public boolean performMove(DataObject dob) throws Exception {
522: writeRef("\nNow moving ...");
523: if (dob.isMoveAllowed()) {
524:
525: for (int i = 0; i < 10; i++) {
526: try {
527: dob.move(temp);
528: break;
529: } catch (IOException ioe) {
530: // on windows the file is locked
531: Thread.currentThread().sleep(500);
532: if (i == 9) {
533: throw new Exception(ioe);
534: }
535: }
536: }
537:
538: // }catch(Exception ex){
539: // printException(ex);
540: // writeRef("Moving failed!",FAILED);
541: // return false;
542: // }
543: } else {
544: writeRef("\nMove not allowed!");
545: return false;
546: }
547: if (dob.isMoveAllowed()) {
548: try {
549: dob.move(resources);
550: } catch (Exception ex) {
551: String str = exceptionToString(ex);
552: writeRef("Moving back failed! + str", FAILED);
553: return false;
554: }
555: } else {
556: writeRef("\nMove not allowed!");
557: return false;
558: }
559: writeRef(PASSED);
560: return true;
561: }
562:
563: public boolean performRename(DataObject dob) {
564: writeRef("\nNow renaming ...");
565: final String newName = "NewName";
566: String oldName = dob.getName();
567: if (dob.isRenameAllowed()) {
568: try {
569: dob.rename(newName);
570: } catch (Exception ex) {
571: printException(ex);
572: writeRef("Renaming failed!", FAILED);
573: return false;
574: }
575: } else {
576: writeRef("\nRename not allowed!");
577: return false;
578: }
579: if (!newName.equals(dob.getName())) {
580: writeRef("New name not set!", FAILED);
581: return false;
582: }
583: if (dob.isRenameAllowed()) {
584: try {
585: dob.rename(oldName);
586: } catch (Exception ex) {
587: printException(ex);
588: writeRef("Renaming back failed!", FAILED);
589: return false;
590: }
591: } else {
592: writeRef("\nRename not allowed!");
593: return false;
594: }
595: writeRef(PASSED);
596: return true;
597: }
598:
599: public boolean performDelete(DataObject dob) throws Exception {
600: writeRef("\nNow deleting ...");
601: String oldName = dob.getName();
602: DataObject backup = null;
603: if (dob.isCopyAllowed()) {
604: backup = dob.copy(temp);
605: } else {
606: writeRef("\nCopy not allowed!");
607: return false;
608: }
609: if (dob.isDeleteAllowed()) {
610: dob.delete();
611: } else {
612: writeRef("\nDelete not allowed!");
613: return false;
614: }
615: if (backup.isMoveAllowed()) {
616: backup.move(resources);
617: } else {
618: writeRef("\nMove not allowed!");
619: return false;
620: }
621: writeRef(PASSED);
622: return true;
623: }
624:
625: boolean checkManipulationOperations(DataObject dob)
626: throws Exception {
627: writeRef("\nChecking manipulating operations for "
628: + dob.getName() + " ...");
629: return performCopy(dob) && performMove(dob)
630: && performRename(dob) && performDelete(dob);
631: }
632:
633: public boolean checkTemplate(DataObject dob) {
634: writeRef("\nNow checking template ...");
635: if (!dob.isTemplate()) {
636: try {
637: dob.setTemplate(true);
638: } catch (Exception ex) {
639: printException(ex);
640: writeRef("Setting template failed!", FAILED);
641: return false;
642: }
643: if (!dob.isTemplate()) {
644: writeRef("DO should be template but isn't!", FAILED);
645: return false;
646: }
647: try {
648: dob.setTemplate(false);
649: } catch (Exception ex) {
650: printException(ex);
651: writeRef("Unsetting template failed!", FAILED);
652: return false;
653: }
654: } else
655: writeRef("\nIs a template.");
656: writeRef(PASSED);
657: return true;
658: }
659:
660: public DataShadow shadowMe(DataObject dob) throws Exception {
661: return shadowMe(dob, null);
662: }
663:
664: public DataShadow shadowMe(DataObject dob, String name)
665: throws Exception {
666: writeRef("\nCreating shadow from " + dob.getName() + " ...");
667: if (dob.isShadowAllowed()) {
668: // try{
669: DataShadow ds = null;
670: ds = DataShadow.create(temp, name, dob);
671: writeRef(PASSED);
672: return ds;
673: // }catch(Exception ex){
674: // printException(ex);
675: // writeRef("Creating of shadow failed!",FAILED);
676: // return null;
677: // }
678: } else {
679: writeRef("\nThis DO cannot be shadowed!");
680: writeRef(PASSED);
681: return null;
682: }
683: }
684:
685: public static void showFilesRecursively(File f, PrintStream ps) {
686: ps.println(f);
687: if (f.isDirectory()) {
688: File files[] = f.listFiles();
689: for (int i = 0; i < files.length; i++) {
690: showFilesRecursively(files[i], ps);
691: }
692: }
693: }
694:
695: public static DataObject findResource(String name) throws Exception {
696: String xtestData = System.getProperty("xtest.data");
697: if (!xtestData.endsWith(File.separator)) {
698: xtestData = xtestData + "/";
699: }
700: String fileName = xtestData
701: + name.substring(1).replace('/', File.separatorChar);
702: System.out.println("fileName:" + fileName);
703: if (new File(fileName).exists() == false) {
704: throw new FileNotFoundException(fileName);
705: }
706: FileObject fo = org.openide.filesystems.FileUtil
707: .toFileObject(new File(fileName));
708: if (fo == null) {
709: throw new NullPointerException("No resource found for "
710: + fileName);
711: }
712: try {
713: return DataObject.find(fo);
714: } catch (Exception e) {
715: e.printStackTrace();
716: throw new NullPointerException("No resource found for "
717: + fileName);
718: }
719: }
720:
721: /**ONLY FOR TESTING PURPOSES
722: *REAL TESTS ARE IN SEPARATED CLASSES
723: */
724:
725: //if you want print exceptions into log file, put here true.
726: public final boolean PRINT_EXCEPTIONS = true;
727:
728: public final String PASSED = "passed.\n";
729: public final String FAILED = "failed.\n";
730: //workdir, this hould be set by an user of this class
731: //user of this class shouldn't use their own ref and log
732: public static String work = null;
733: }
|