Source Code Cross Referenced for FolderInstanceTest.java in  » IDE-Netbeans » openide » org » openide » loaders » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » openide » org.openide.loaders 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 java.io.IOException;
045:        import java.io.Serializable;
046:        import java.util.*;
047:        import java.util.logging.Level;
048:        import java.util.logging.Logger;
049:        import javax.swing.SwingUtilities;
050:
051:        import org.openide.filesystems.*;
052:        import org.openide.loaders.*;
053:        import org.openide.cookies.*;
054:        import org.openide.util.*;
055:
056:        import org.netbeans.junit.*;
057:        import java.util.Enumeration;
058:        import junit.framework.Test;
059:
060:        public class FolderInstanceTest extends NbTestCase {
061:            private Logger err;
062:
063:            public FolderInstanceTest() {
064:                super ("");
065:            }
066:
067:            public FolderInstanceTest(java.lang.String testName) {
068:                super (testName);
069:            }
070:
071:            @Override
072:            protected Level logLevel() {
073:                return Level.FINE;
074:            }
075:
076:            @Override
077:            protected int timeOut() {
078:                return 20000;
079:            }
080:
081:            public static Test suite() {
082:                return new NbTestSuite(FolderInstanceTest.class);
083:                //return new FolderInstanceTest("testListenersCountWithCookie");
084:            }
085:
086:            private static void setSystemProp(String key, String value) {
087:                java.util.Properties prop = System.getProperties();
088:                if (prop.get(key) != null)
089:                    return;
090:                prop.put(key, value);
091:            }
092:
093:            protected void setUp() throws Exception {
094:                MockServices.setServices(Pool.class);
095:
096:                DataLoaderPool pool = DataLoaderPool.getDefault();
097:                assertNotNull(pool);
098:                assertEquals(Pool.class, pool.getClass());
099:
100:                Pool.setExtra(null);
101:
102:                clearWorkDir();
103:
104:                err = Logger.getLogger("TEST-" + getName());
105:            }
106:
107:            /** Checks whether only necessary listeners are attached to the objects.
108:             * Initial object does not have a cookie.
109:             */
110:            public void testListenersCountNoCookie() throws Exception {
111:                doTestListenersCount(false);
112:            }
113:
114:            /** Checks whether only necessary listeners are attached to the objects.
115:             * Initial object has cookie.
116:             */
117:            public void testListenersCountWithCookie() throws Exception {
118:                doTestListenersCount(true);
119:            }
120:
121:            /** Because listeners have different code for objects with cookie and 
122:             * without cookie, we add this utility test and run it twice.
123:             *
124:             * @param cookie add cookie or not
125:             */
126:            private void doTestListenersCount(boolean cookie) throws Exception {
127:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
128:                        getWorkDir(), new String[0]);
129:
130:                FileObject bb = lfs.findResource("/AA");
131:                err.info("Found resource: " + bb);
132:                if (bb != null) {
133:                    bb.delete();
134:                }
135:                err.info("Resource deleted");
136:                FileObject theFile = FileUtil.createData(lfs.getRoot(),
137:                        "/AA/A.simple");
138:                err.info("Found the file: " + theFile);
139:                bb = FileUtil.createFolder(lfs.getRoot(), "/AA");
140:                err.info("Found the folder: " + bb);
141:                assertTrue("Is file", theFile.isData());
142:                err.info("Confirmed, its the data: " + theFile);
143:
144:                DataFolder folder = DataFolder.findFolder(bb);
145:
146:                DataLoader l = DataLoader
147:                        .getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class);
148:                err.info("Add loader: " + l);
149:                Pool.setExtra(l);
150:                err.info("Loader added");
151:                try {
152:                    FileObject aa = lfs.findResource("/AA/A.simple");
153:                    DataObject tmp = DataObject.find(aa);
154:                    assertEquals("Is of the right type",
155:                            DataLoaderOrigTest.SimpleDataObject.class, tmp
156:                                    .getClass());
157:                    DataLoaderOrigTest.SimpleDataObject obj = (DataLoaderOrigTest.SimpleDataObject) tmp;
158:
159:                    err.info("simple object found: " + obj);
160:
161:                    if (cookie) {
162:                        err.info("Adding cookie");
163:                        obj.cookieSet().add(
164:                                new InstanceSupport.Instance(new Integer(100)));
165:                        err.info("Cookie added");
166:                    }
167:
168:                    F instance = new F(folder);
169:                    err.info("Instance for " + folder + " created");
170:                    Object result = instance.instanceCreate();
171:                    err.info("instanceCreate called. Result: " + result);
172:
173:                    Enumeration en = obj.listeners();
174:
175:                    err.info("Asking for listeners of " + obj);
176:
177:                    assertTrue("Folder instance should have add one listener",
178:                            en.hasMoreElements());
179:                    en.nextElement();
180:                    assertTrue("But there should be just one", !en
181:                            .hasMoreElements());
182:
183:                    err
184:                            .info("Successfully tested for one listener, creating B.simple");
185:
186:                    folder.getPrimaryFile().createData("B.simple");
187:                    err.info("B.simple created");
188:                    assertEquals("DO created", folder.getChildren().length, 2);
189:                    err.info("Children obtained correctly");
190:
191:                    // wait to finish processing
192:                    result = instance.instanceCreate();
193:                    err.info("instanceCreate finished, with result: " + result);
194:
195:                    en = obj.listeners();
196:                    err.info("Asking for listeners once again");
197:                    assertTrue(
198:                            "Folder instance should not change the amount of listeners",
199:                            en.hasMoreElements());
200:                    en.nextElement();
201:                    assertTrue("And there still should be just one", !en
202:                            .hasMoreElements());
203:                    err.info("Successfully tested for listeners");
204:                } finally {
205:                    err.info("Clearing data loader");
206:                    Pool.setExtra(null);
207:                    err.info("Loader cleared");
208:                }
209:            }
210:
211:            /** Checks whether folder instance correctly reacts to changes of cookies in data objects.
212:             */
213:            public void testChangeCookie() throws Exception {
214:                String fsstruct[] = new String[] { "AA/A.simple" };
215:
216:                TestUtilHid.destroyLocalFileSystem(getName());
217:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
218:                        getWorkDir(), fsstruct);
219:
220:                FileObject bb = lfs.findResource("/AA");
221:
222:                DataFolder folder = DataFolder.findFolder(bb);
223:
224:                DataLoader l = DataLoader
225:                        .getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class);
226:                Pool.setExtra(l);
227:                try {
228:                    FileObject aa = lfs.findResource("/AA/A.simple");
229:                    DataObject obj = DataObject.find(aa);
230:
231:                    if (!(obj instanceof  DataLoaderOrigTest.SimpleDataObject)) {
232:                        fail("Not instance of desired object");
233:                    }
234:
235:                    F instance = new F(folder);
236:
237:                    org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject) obj)
238:                            .cookieSet();
239:
240:                    List list;
241:                    list = (List) instance.instanceCreate();
242:                    if (!list.isEmpty()) {
243:                        fail("Should be empty with object with no cookies");
244:                    }
245:
246:                    InstanceSupport.Instance is = new InstanceSupport.Instance(
247:                            new Integer(100));
248:                    set.add(is);
249:
250:                    list = (List) instance.instanceCreate();
251:                    if (list.isEmpty()) {
252:                        fail("Cookie added, should return instance");
253:                    }
254:
255:                    set.remove(is);
256:
257:                    list = (List) instance.instanceCreate();
258:                    if (!list.isEmpty()) {
259:                        fail("Cookie removed should be empty");
260:                    }
261:
262:                    set.add(is);
263:                    list = (List) instance.instanceCreate();
264:                    if (list.isEmpty()) {
265:                        fail("Cookie added again, should return instance");
266:                    }
267:                } finally {
268:                    Pool.setExtra(null);
269:                }
270:            }
271:
272:            /** Does FolderInstance react to change of order?
273:             */
274:            public void testChangeOfOrder() throws Exception {
275:                String fsstruct[] = new String[] { "AA/A.simple", "AA/B.simple" };
276:
277:                TestUtilHid.destroyLocalFileSystem(getName());
278:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
279:                        getWorkDir(), fsstruct);
280:
281:                FileObject f = lfs.findResource("/AA");
282:
283:                DataFolder folder = DataFolder.findFolder(f);
284:
285:                DataLoader l = DataLoader
286:                        .getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class);
287:                Pool.setExtra(l);
288:                try {
289:                    FileObject aa = lfs.findResource("/AA/A.simple");
290:                    DataObject objA = DataObject.find(aa);
291:                    FileObject bb = lfs.findResource("/AA/B.simple");
292:                    DataObject objB = DataObject.find(bb);
293:
294:                    if (!(objA instanceof  DataLoaderOrigTest.SimpleDataObject)) {
295:                        fail("Not instance of desired object: " + objA);
296:                    }
297:                    if (!(objB instanceof  DataLoaderOrigTest.SimpleDataObject)) {
298:                        fail("Not instance of desired object: " + objB);
299:                    }
300:
301:                    folder.setOrder(new DataObject[] { objA, objB });
302:
303:                    F instance = new F(folder);
304:
305:                    {
306:                        org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject) objA)
307:                                .cookieSet();
308:                        InstanceSupport.Instance is = new InstanceSupport.Instance(
309:                                new Integer(1));
310:                        set.add(is);
311:                    }
312:                    {
313:                        org.openide.nodes.CookieSet set = ((DataLoaderOrigTest.SimpleDataObject) objB)
314:                                .cookieSet();
315:                        InstanceSupport.Instance is = new InstanceSupport.Instance(
316:                                new Integer(2));
317:                        set.add(is);
318:                    }
319:
320:                    List list;
321:                    list = (List) instance.instanceCreate();
322:                    assertEquals("Two integer", 2, list.size());
323:                    assertEquals("1 is first", new Integer(1), list.get(0));
324:                    assertEquals("2 is next", new Integer(2), list.get(1));
325:
326:                    folder.setOrder(new DataObject[] { objB, objA });
327:
328:                    list = (List) instance.instanceCreate();
329:                    assertEquals("Two integer", 2, list.size());
330:                    assertEquals("2 is first", new Integer(2), list.get(0));
331:                    assertEquals("1 is next", new Integer(1), list.get(1));
332:
333:                } finally {
334:                    Pool.setExtra(null);
335:                }
336:            }
337:
338:            /** Tests whether correct result is returned when an object is added and removed
339:             * from the folder.
340:             */
341:            public void testModification() throws Exception {
342:                String fsstruct[] = new String[] { "AA/" };
343:
344:                TestUtilHid.destroyLocalFileSystem(getName());
345:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
346:                        getWorkDir(), fsstruct);
347:
348:                FileObject bb = lfs.findResource("/AA");
349:
350:                DataFolder folder = DataFolder.findFolder(bb);
351:                DataFolder subfolder = DataFolder.create(folder, "BB");
352:
353:                modification(new F(folder), folder);
354:            }
355:
356:            /** Tests whether correct result is returned when an object is added and removed
357:             * from the folder.
358:             */
359:            public void testModificationOnSubfolder() throws Exception {
360:                String fsstruct[] = new String[] { "AA/BB/" };
361:
362:                TestUtilHid.destroyLocalFileSystem(getName());
363:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
364:                        getWorkDir(), fsstruct);
365:
366:                FileObject bb = lfs.findResource("/AA");
367:
368:                DataFolder folder = DataFolder.findFolder(bb);
369:                DataFolder subfolder = DataFolder.create(folder, "BB");
370:
371:                modification(new F(folder), subfolder);
372:            }
373:
374:            /** Tests whether correct result is returned when an object is added and removed
375:             * from the folder.
376:             */
377:            public void testModificationOnSubSubfolder() throws Exception {
378:                String fsstruct[] = new String[] { "/AA/BB/CC/DD/EE/FF/GG/HH/II/JJ/KK" };
379:
380:                TestUtilHid.destroyLocalFileSystem(getName());
381:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
382:                        getWorkDir(), fsstruct);
383:
384:                FileObject bb = lfs.findResource("/AA");
385:
386:                DataFolder folder = DataFolder.findFolder(bb);
387:
388:                Enumeration en = lfs.getRoot().getChildren(true);
389:                FileObject fo = null;
390:                while (en.hasMoreElements()) {
391:                    FileObject f = (FileObject) en.nextElement();
392:                    if (f.isFolder()) {
393:                        fo = f;
394:                    }
395:                }
396:
397:                DataFolder subfolder = DataFolder.findFolder(fo);
398:
399:                modification(new F(folder), subfolder);
400:            }
401:
402:            public void testWhetherRenameTriggersRevalidationOfTheFolderInstance()
403:                    throws Exception {
404:                String fsstruct[] = new String[] { "/AAXX/OldName.shadow" };
405:
406:                TestUtilHid.destroyLocalFileSystem(getName());
407:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
408:                        getWorkDir(), fsstruct);
409:
410:                FileObject bb = lfs.findResource("/AAXX");
411:                assertEquals("One child", 1, bb.getChildren().length);
412:
413:                class NamedF extends F {
414:                    public NamedF(DataFolder f) {
415:                        super (f);
416:                    }
417:
418:                    protected InstanceCookie acceptDataObject(DataObject obj) {
419:                        return new InstanceSupport.Instance(obj.getName());
420:                    }
421:                }
422:
423:                DataFolder f = DataFolder.findFolder(bb);
424:                NamedF namedf = new NamedF(f);
425:
426:                List result;
427:                result = (List) namedf.instanceCreate();
428:                if (1 != result.size()) {
429:                    fail("One item expected, but: " + result);
430:                }
431:                assertEquals("It is the name of data object", "OldName", result
432:                        .get(0));
433:
434:                FileObject aa = lfs.findResource(fsstruct[0]);
435:                DataObject.find(aa).rename("NewName");
436:
437:                result = (List) namedf.instanceCreate();
438:                assertEquals("One item", 1, result.size());
439:                assertEquals("It is the name of data object", "NewName", result
440:                        .get(0));
441:            }
442:
443:            /** Runs modification test on a given folder with provided folder instance.
444:             */
445:            private static void modification(F instance, DataFolder folder)
446:                    throws Exception {
447:                List list;
448:                int cnt;
449:                list = (List) instance.instanceCreate();
450:
451:                if (list.size() != 0) {
452:                    fail("List should be empty: " + list);
453:                }
454:
455:                cnt = instance.getCount();
456:                if (cnt != 1) {
457:                    fail("Too many calls to createInstance during initialization: "
458:                            + cnt);
459:                }
460:
461:                InstanceDataObject obj = InstanceDataObject.create(folder,
462:                        null, Numb.class);
463:
464:                list = (List) instance.instanceCreate();
465:
466:                assertEquals("One item", 1, list.size());
467:                assertEquals("The item is of the right class", Numb.class, list
468:                        .get(0).getClass());
469:
470:                cnt = instance.getCount();
471:                if (cnt != 1) {
472:                    fail("Too many calls to createInstance after create: "
473:                            + cnt);
474:                }
475:
476:                obj.delete();
477:
478:                list = (List) instance.instanceCreate();
479:
480:                if (list.size() != 0) {
481:                    fail("List should be empty again: " + list);
482:                }
483:
484:                cnt = instance.getCount();
485:                if (cnt != 1) {
486:                    fail("Too many calls to createInstance after delete: "
487:                            + cnt);
488:                }
489:
490:            }
491:
492:            private static class F extends FolderInstance {
493:                /** count number of changes. */
494:                private int count;
495:
496:                public F(DataFolder f) {
497:                    super (f);
498:                }
499:
500:                /** Getter to number of changes of this folder instance.
501:                 */
502:                public synchronized int getCount() {
503:                    int c = count;
504:                    count = 0;
505:                    return c;
506:                }
507:
508:                /** Accepts folder.
509:                 */
510:                protected InstanceCookie acceptFolder(DataFolder f) {
511:                    return new F(f);
512:                }
513:
514:                protected Object createInstance(InstanceCookie[] arr)
515:                        throws java.io.IOException, ClassNotFoundException {
516:                    synchronized (this ) {
517:                        count++;
518:                    }
519:                    LinkedList ll = new LinkedList();
520:                    for (int i = 0; i < arr.length; i++) {
521:                        Object obj = arr[i].instanceCreate();
522:                        if (obj instanceof  Collection) {
523:                            ll.addAll((Collection) obj);
524:                        } else {
525:                            ll.add(obj);
526:                        }
527:                    }
528:                    return ll;
529:                }
530:
531:                protected Task postCreationTask(Runnable run) {
532:                    //super.postCreationTask (run);
533:
534:                    run.run();
535:                    return null;
536:                }
537:            }
538:
539:            /** See #12960.
540:             * Appears that MenuBar.Folder was being passed already-invalidated objects
541:             * on occasion, which of course it was not prepared to deal with.
542:             * @author Jesse Glick
543:             */
544:            public void testFolderInstanceNeverPassesInvObjects()
545:                    throws Exception {
546:                doFolderInstanceNeverPassesInvObjects(100, 1000);
547:            }
548:
549:            private void doFolderInstanceNeverPassesInvObjects(int cnt,
550:                    int sleep) throws Exception {
551:                String[] names = new String[cnt];
552:                for (int i = 0; i < names.length; i++) {
553:                    names[i] = "folder/file" + i + ".simple";
554:                }
555:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
556:                        getWorkDir(), names);
557:                Repository.getDefault().addFileSystem(lfs);
558:                try {
559:                    FileObject folder = lfs.findResource("folder");
560:                    DataLoader l = DataLoader
561:                            .getLoader(DataLoaderOrigTest.SimpleUniFileLoader.class);
562:                    DataFolder f = DataFolder.findFolder(folder);
563:                    InvCheckFolderInstance icfi = new InvCheckFolderInstance(f,
564:                            false);
565:                    assertTrue(icfi.ok);
566:                    assertEquals(new Integer(0), icfi.instanceCreate());
567:                    err.info("sample1: "
568:                            + DataObject.find(lfs.findResource(names[0])));
569:                    Pool.setExtra(l);
570:                    try {
571:                        err.info("sample2: "
572:                                + DataObject.find(lfs.findResource(names[0])));
573:                        assertTrue(icfi.ok);
574:                        /*
575:                        Thread.sleep(100);
576:                        SwingUtilities.invokeAndWait(new Runnable() {
577:                            public void run() {
578:                                // just get here
579:                            }
580:                        });
581:                        Thread.sleep(100);
582:                        System.err.println("sample: " + DataObject.find(lfs.findResource(names[0])));
583:                         */
584:                        Thread.sleep(sleep);
585:                        assertEquals(new Integer(cnt), icfi.instanceCreate());
586:                        //Thread.sleep(sleep);
587:                        assertTrue(icfi.ok);
588:                        //Thread.sleep(sleep);
589:                        //assertTrue(icfi.ok);
590:                    } finally {
591:                        err.info("begining to clear the pool");
592:                        Pool.setExtra(null);
593:                        err.info("clearing pool is finished");
594:                    }
595:                    err.info("sample3: "
596:                            + DataObject.find(lfs.findResource(names[0])));
597:                    err.info("sample4: "
598:                            + DataFolder.findFolder(
599:                                    lfs.findResource(names[0]).getParent())
600:                                    .getChildren()[0]);
601:                    assertTrue(icfi.ok);
602:                    Object instance = null;
603:                    for (int i = 0; i < 1; i++) {
604:                        Thread.sleep(sleep);
605:                        err.info("getting the instance: " + i);
606:                        instance = icfi.instanceCreate();
607:                        err.info("instance is here (" + i + "): " + instance);
608:
609:                        if (new Integer(0).equals(instance)) {
610:                            break;
611:                        }
612:                    }
613:                    assertEquals(new Integer(0), instance);
614:                    err.info("passed the usual failing point");
615:                    //Thread.sleep(sleep);
616:                    assertTrue(icfi.ok);
617:                    Pool.setExtra(l);
618:                    try {
619:                        assertTrue(icfi.ok);
620:                        Thread.sleep(sleep);
621:                        assertTrue(icfi.ok);
622:                    } finally {
623:                        Pool.setExtra(null);
624:                    }
625:                    assertTrue(icfi.ok);
626:                    Pool.setExtra(l);
627:                    try {
628:                        assertTrue(icfi.ok);
629:                    } finally {
630:                        Pool.setExtra(null);
631:                    }
632:                    assertTrue(icfi.ok);
633:                    Thread.sleep(sleep);
634:                    assertTrue(icfi.ok);
635:                } finally {
636:                    Repository.getDefault().removeFileSystem(lfs);
637:                }
638:            }
639:
640:            public void testFolderInstanceNeverPassesInvFolders()
641:                    throws Exception {
642:                String[] names = { "folder/sub/" };
643:                FileSystem lfs = TestUtilHid.createLocalFileSystem(
644:                        getWorkDir(), names);
645:                Repository.getDefault().addFileSystem(lfs);
646:                try {
647:                    FileObject folder = lfs.findResource("folder");
648:                    DataFolder f = DataFolder.findFolder(folder);
649:
650:                    DataObject[] arr = f.getChildren();
651:                    assertEquals("One child", 1, arr.length);
652:                    assertEquals("It is folder", DataFolder.class, arr[0]
653:                            .getClass());
654:
655:                    err.info("Creating InvCheckFolderInstance");
656:                    InvCheckFolderInstance icfi = new InvCheckFolderInstance(f,
657:                            true);
658:                    err.info("Computing result");
659:                    List computed = (List) icfi.instanceCreate();
660:                    err.info("Result is here: " + computed);
661:                    assertEquals("One from folder instance", 1, computed.size());
662:                    assertEquals("The same data object", arr[0], computed
663:                            .get(0));
664:
665:                    arr[0].setValid(false);
666:
667:                    List newComputed = (List) icfi.instanceCreate();
668:                    assertEquals("Still one", 1, newComputed.size());
669:
670:                    DataObject[] arr2 = f.getChildren();
671:                    assertEquals("Still one", 1, arr2.length);
672:                    if (arr[0] == arr2[0]) {
673:                        fail("They should not be the same: " + arr2[0]);
674:                    }
675:
676:                    assertEquals("The same new object", arr2[0], newComputed
677:                            .get(0));
678:
679:                } finally {
680:                    Repository.getDefault().removeFileSystem(lfs);
681:                }
682:            }
683:
684:            private final class InvCheckFolderInstance extends FolderInstance {
685:                public boolean ok = true;
686:                private boolean acceptF;
687:
688:                public InvCheckFolderInstance(DataFolder f, boolean folders) {
689:                    super (f);
690:                    this .acceptF = folders;
691:                }
692:
693:                protected Object createInstance(InstanceCookie[] cookies)
694:                        throws IOException, ClassNotFoundException {
695:                    // Whatever, irrelevant.
696:                    err.info("new createInstance: " + cookies.length);
697:
698:                    if (acceptF) {
699:                        ArrayList list = new ArrayList();
700:                        for (int i = 0; i < cookies.length; i++) {
701:                            list.add(cookies[i].instanceCreate());
702:                        }
703:                        return list;
704:                    }
705:
706:                    return new Integer(cookies.length);
707:                }
708:
709:                protected InstanceCookie acceptDataObject(DataObject o) {
710:                    if (!o.isValid()) {
711:                        ok = false;
712:                        Thread.dumpStack();
713:                        return null;
714:                    }
715:                    if (o instanceof  DataLoaderOrigTest.SimpleDataObject) {
716:                        err.info("got a simpledataobject");
717:                        // Simulate some computation here:
718:                        try {
719:                            Thread.sleep(10);
720:                        } catch (InterruptedException ie) {
721:                        }
722:                        return new InstanceSupport.Instance("ignore");
723:                    } else {
724:                        if (acceptF && o instanceof  DataFolder) {
725:                            err.info("Recognized folder: " + o);
726:                            return new InstanceSupport.Instance(o);
727:                        }
728:                        err.info("got a " + o);
729:                        return null;
730:                    }
731:                }
732:
733:                // For faithfulness to the original:
734:                protected Task postCreationTask(Runnable run) {
735:                    err.info("postCreationTask");
736:                    return new AWTTask(run);
737:                }
738:            }
739:
740:            private final class AWTTask extends Task {
741:                private boolean executed;
742:
743:                public AWTTask(Runnable r) {
744:                    super (r);
745:                    Mutex.EVENT.readAccess(this );
746:                }
747:
748:                public void run() {
749:                    if (!executed) {
750:                        super .run();
751:                        executed = true;
752:                        err.info("AWTTask executed");
753:                    }
754:                }
755:
756:                public void waitFinished() {
757:                    err.info("AWTTask waitFinished");
758:                    if (SwingUtilities.isEventDispatchThread()) {
759:                        err.info("AWTTask waitFinished on AWT thread");
760:                        run();
761:                        err.info("AWTTask waitFinished on AWT thread done");
762:                    } else {
763:                        super .waitFinished();
764:                        err.info("AWTTask waitFinished done");
765:                    }
766:                }
767:            }
768:
769:            public static final class Numb extends Object implements 
770:                    Serializable {
771:                public Numb() {
772:                }
773:            }
774:
775:            public static final class Lkp extends
776:                    org.openide.util.lookup.AbstractLookup {
777:                public Lkp() {
778:                    this (new org.openide.util.lookup.InstanceContent());
779:                }
780:
781:                private Lkp(org.openide.util.lookup.InstanceContent ic) {
782:                    super (ic);
783:                    ic.add(new Pool());
784:                }
785:            }
786:
787:            public static final class Pool extends
788:                    org.openide.loaders.DataLoaderPool {
789:                private static DataLoader extra;
790:
791:                protected Enumeration loaders() {
792:                    if (getExtra() == null) {
793:                        return Enumerations.empty();
794:                    } else {
795:                        return Enumerations.singleton(getExtra());
796:                    }
797:                }
798:
799:                public static DataLoader getExtra() {
800:                    return extra;
801:                }
802:
803:                public static void setExtra(DataLoader aExtra) {
804:                    if (extra != null && aExtra != null) {
805:                        fail("Both are not null: " + extra + " aExtra: "
806:                                + aExtra);
807:                    }
808:                    extra = aExtra;
809:                    Pool p = (Pool) DataLoaderPool.getDefault();
810:                    p.fireChangeEvent(new javax.swing.event.ChangeEvent(p));
811:                }
812:            }
813:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.