Source Code Cross Referenced for SerialDataConvertorTest.java in  » IDE-Netbeans » settings » org » netbeans » modules » settings » convertors » 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 » settings » org.netbeans.modules.settings.convertors 
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 2002-2003 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.netbeans.modules.settings.convertors;
043:
044:        import org.netbeans.core.startup.layers.SystemFileSystem;
045:        import org.openide.filesystems.*;
046:        import org.openide.filesystems.FileSystem; // override java.io.FileSystem
047:        import org.openide.loaders.*;
048:        import org.openide.cookies.*;
049:        import org.openide.modules.ModuleInfo;
050:        import org.openide.util.*;
051:        import java.io.*;
052:        import java.lang.ref.Reference;
053:        import java.lang.ref.WeakReference;
054:        import java.util.*;
055:
056:        import org.netbeans.junit.*;
057:        import org.openide.nodes.Node;
058:
059:        /**
060:         * @author Jan Pokorsky
061:         */
062:        public class SerialDataConvertorTest extends NbTestCase {
063:            /** folder to create instances in */
064:            private DataFolder folder;
065:            /** filesystem containing created instances */
066:            private FileSystem lfs;
067:
068:            /** Creates new DataFolderTest */
069:            public SerialDataConvertorTest(String name) {
070:                super (name);
071:            }
072:
073:            /** Setups variables.
074:             */
075:            protected void setUp() throws Exception {
076:                Lookup.getDefault().lookup(ModuleInfo.class);
077:                lfs = Repository.getDefault().getDefaultFileSystem();
078:                org.openide.filesystems.FileUtil.createFolder(lfs.getRoot(),
079:                        "BB/AAA");
080:                org.openide.filesystems.FileUtil.createFolder(lfs.getRoot(),
081:                        "system/Services/lookupTest");
082:                org.openide.filesystems.FileUtil.createFolder(lfs.getRoot(),
083:                        "testCreateInstance");
084:
085:                String fsstruct[] = new String[] { "BB/AAA/",
086:                        "system/Services/lookupTest/", "testCreateInstance/", };
087:
088:                FileObject bb = lfs.findResource("/BB");
089:                FileObject bb_aaa = lfs.findResource("/BB/AAA");
090:
091:                DataObject dest = DataObject.find(bb_aaa);
092:
093:                assertTrue("Destination folder doesn't exist.", dest != null);
094:                assertTrue("Destination folder is not valid.", dest.isValid());
095:
096:                folder = DataFolder.findFolder(bb);
097:            }
098:
099:            public void testSaveCookieChanges() throws Exception {
100:                class MyListener implements  LookupListener {
101:                    int i = 0;
102:
103:                    public void resultChanged(LookupEvent ev) {
104:                        i++;
105:                    }
106:                }
107:                MyListener ml = new MyListener();
108:                LocalFileSystem lfs = new LocalFileSystem();
109:                InstanceDataObject i = InstanceDataObject.create(folder, null,
110:                        lfs, null);
111:                assertNull(i.getCookie(SaveCookie.class));
112:                Lookup.Result<SaveCookie> scr = i.getLookup().lookup(
113:                        new Lookup.Template(SaveCookie.class));
114:                scr.addLookupListener(ml);
115:                Collection<? extends SaveCookie> saveCookies = scr
116:                        .allInstances();
117:                assertFalse(saveCookies.contains(lfs));
118:
119:                lfs.setRootDirectory(getWorkDir());
120:                SaveCookie sv = i.getCookie(SaveCookie.class);
121:                assertNotNull(sv);
122:                saveCookies = scr.allInstances();
123:                assertTrue("Cookie " + sv + "is there: " + saveCookies,
124:                        saveCookies.contains(sv));
125:                assertEquals(1, ml.i);
126:                sv.save();
127:                sv = i.getCookie(SaveCookie.class);
128:                assertNull(sv);
129:                saveCookies = scr.allInstances();
130:                assertTrue(saveCookies.isEmpty());
131:                assertEquals(2, ml.i);
132:                i.getPrimaryFile().delete();
133:            }
134:
135:            public void test50177ProblemSimulation() throws Exception {
136:                FileObject testFolder = FileUtil.createFolder(Repository
137:                        .getDefault().getDefaultFileSystem().getRoot(),
138:                        "Services");
139:                assertNotNull(testFolder);
140:
141:                InstanceDataObject ido = InstanceDataObject.create(DataFolder
142:                        .findFolder(testFolder), "test50177ProblemSimulation",
143:                        new Ex50177(), null);
144:
145:                Lookup.Item item = Lookup.getDefault().lookupItem(
146:                        new Lookup.Template(Ex50177.class, null, null));
147:                assertNotNull(item);
148:                String id = item.getId();
149:
150:                Ex50177 exObj = (Ex50177) item.getInstance();
151:                assertNotNull(exObj);
152:
153:                exObj.setSomething("set any value shouldn't cause #50177");
154:                SerialDataConvertorTest.waitUntilIsSaved(ido);
155:
156:                //!!! this is the failing line causing #50177
157:                assertNotNull(Lookup.getDefault().lookupItem(
158:                        new Lookup.Template(null, id, null)));
159:            }
160:
161:            public void test50177Cause() throws Exception {
162:                FileObject testFolder = FileUtil.createFolder(Repository
163:                        .getDefault().getDefaultFileSystem().getRoot(),
164:                        "Services");
165:                assertNotNull(testFolder);
166:
167:                InstanceDataObject ido = InstanceDataObject.create(DataFolder
168:                        .findFolder(testFolder), "test50177Cause",
169:                        new Ex50177(), null);
170:                String idoName = ido.getName();
171:
172:                Ex50177 exObj = (Ex50177) ido.instanceCreate();
173:                assertNotNull(exObj);
174:                assertEquals(idoName, ido.getName());
175:
176:                exObj.setSomething("any value");//set any value shouldn't cause #50177
177:                SerialDataConvertorTest.waitUntilIsSaved(ido);
178:                //!!! this is the failing line causing #50177
179:                assertEquals(idoName, ido.getName());
180:            }
181:
182:            private static void waitUntilIsSaved(InstanceDataObject ido)
183:                    throws InterruptedException {
184:                SaveCookie sc = (SaveCookie) ido.getCookie(SaveCookie.class);
185:                for (int i = 0; i < 5 && sc != null; i++) {
186:                    Thread.sleep(3000);
187:                    sc = (SaveCookie) ido.getCookie(SaveCookie.class);
188:                }
189:                assertNull(sc);
190:            }
191:
192:            public void test50177SideEffectsAfterRename() throws Exception {
193:                FileObject testFolder = FileUtil.createFolder(Repository
194:                        .getDefault().getDefaultFileSystem().getRoot(),
195:                        "Services");
196:                assertNotNull(testFolder);
197:
198:                InstanceDataObject ido = InstanceDataObject.create(DataFolder
199:                        .findFolder(testFolder),
200:                        "test50177SideEffectsAfterRename", new Ex50177(), null);
201:                FileObject fo = ido.getPrimaryFile();
202:                fo.setAttribute("SystemFileSystem.localizingBundle",
203:                        "org.netbeans.modules.settings.convertors.data.Bundle");
204:
205:                String newName = "newName";
206:                ido.getNodeDelegate().setName(newName);
207:                SerialDataConvertorTest.waitUntilIsSaved(ido);
208:                assertEquals(newName, ido.getNodeDelegate().getDisplayName());
209:
210:                /// simulates recretaion of instance e.g. after IDE restart
211:                fo = FileUtil.copyFile(fo, fo.getParent(), "copiedPeer", fo
212:                        .getExt());
213:                fo.setAttribute("SystemFileSystem.localizingBundle",
214:                        "org.netbeans.modules.settings.convertors.data.Bundle");
215:                ido = (InstanceDataObject) DataObject.find(fo);
216:
217:                assertEquals(newName, ido.getNodeDelegate().getDisplayName());
218:            }
219:
220:            public static final class Ex50177 extends org.openide.ServiceType {
221:                /** generated Serialized Version UID */
222:                private static final long serialVersionUID = -7572487174423654252L;
223:                private String name = "My Own Ex";
224:
225:                protected String displayName() {
226:                    return name;
227:                }
228:
229:                public HelpCtx getHelpCtx() {
230:                    return HelpCtx.DEFAULT_HELP;
231:                }
232:
233:                private String something;
234:
235:                public String getSomething() {
236:                    return something;
237:                }
238:
239:                public void setSomething(String s) {
240:                    String old = something;
241:                    something = s;
242:                    firePropertyChange("Something", old, s);
243:                }
244:
245:                public void setName(String name) {
246:                    this .name = name;
247:                    firePropertyChange(name, null, name);
248:                }
249:            }
250:
251:            /** Checks whether the instance is the same.
252:             */
253:            public void testSame() throws Exception {
254:
255:                Ser ser = new Ser("1");
256:
257:                InstanceDataObject i = InstanceDataObject.create(folder, null,
258:                        ser, null);
259:
260:                Object n = i.instanceCreate();
261:                if (n != ser) {
262:                    fail("instanceCreate is not the same: " + ser + " != " + n);
263:                }
264:
265:                i.delete();
266:            }
267:
268:            /** Test whether instances survive garbage collection.
269:             */
270:            public void testSameWithGC() throws Exception {
271:                Object ser = new java.awt.Button();
272:
273:                FileObject prim = InstanceDataObject.create(folder, "MyName",
274:                        ser, null).getPrimaryFile();
275:                String name = prim.getName();
276:                String ext = prim.getExt();
277:                prim = null;
278:
279:                System.gc();
280:                System.gc();
281:                System.gc();
282:                System.gc();
283:                System.gc();
284:                System.gc();
285:                System.gc();
286:                System.gc();
287:                System.gc();
288:
289:                FileObject fo = folder.getPrimaryFile()
290:                        .getFileObject(name, ext);
291:                assertTrue("MyName.settings not found", fo != null);
292:
293:                DataObject obj = DataObject.find(fo);
294:
295:                InstanceCookie ic = (InstanceCookie) obj
296:                        .getCookie(InstanceCookie.class);
297:                assertTrue("Object: " + obj + " does not have instance cookie",
298:                        ic != null);
299:
300:                Object value = ic.instanceCreate();
301:                if (value != ser) {
302:                    fail("Value is different than serialized: "
303:                            + System.identityHashCode(ser) + " value: "
304:                            + System.identityHashCode(value));
305:                }
306:
307:                obj.delete();
308:            }
309:
310:            /** Tests the creation in atomic section.
311:             */
312:            public void testSameInAtomicSection() throws Exception {
313:                class AtomAct extends FileChangeAdapter implements 
314:                        FileSystem.AtomicAction {
315:
316:                    private java.awt.Button testSer = new java.awt.Button();
317:
318:                    private FileObject data;
319:                    private InstanceDataObject obj;
320:
321:                    public void run() throws IOException {
322:                        folder.getPrimaryFile().addFileChangeListener(this );
323:                        data = folder.getPrimaryFile().createData("SomeData");
324:
325:                        obj = InstanceDataObject.create(folder, null, testSer,
326:                                null);
327:                    }
328:
329:                    public void doTest() throws Exception {
330:                        Object now = obj.instanceCreate();
331:                        if (now != testSer) {
332:                            fail("Different values. Original: " + testSer
333:                                    + " now: " + now);
334:                        }
335:                    }
336:
337:                    public void cleanUp() throws Exception {
338:                        data.delete();
339:                        obj.delete();
340:                    }
341:
342:                    public void fileDataCreated(FileEvent ev) {
343:                        try {
344:                            Thread.sleep(500);
345:                        } catch (Exception ex) {
346:                        }
347:                    }
348:                }
349:
350:                AtomAct t = new AtomAct();
351:                try {
352:                    folder.getPrimaryFile().getFileSystem().runAtomicAction(t);
353:
354:                    t.doTest();
355:                } finally {
356:                    t.cleanUp();
357:                }
358:            }
359:
360:            /** Tests whether createFromTemplate works correctly.
361:             */
362:            public void testCreateFromTemplateForSettingsFile()
363:                    throws Exception {
364:                Object ser = new java.awt.Button();
365:
366:                InstanceDataObject obj = InstanceDataObject.create(folder,
367:                        "SomeName", ser, null);
368:                obj.setTemplate(true);
369:
370:                DataObject newObj = obj.createFromTemplate(folder, "NewName");
371:
372:                if (!newObj.getName().equals("NewName")) {
373:                    fail("Wrong name of new data object: " + newObj.getName());
374:                }
375:
376:                InstanceCookie ic = (InstanceCookie) newObj
377:                        .getCookie(InstanceCookie.class);
378:
379:                if (ic == null) {
380:                    fail("No instance cookie for " + newObj);
381:                }
382:
383:                if (ic.instanceCreate() != ser) {
384:                    fail("created instance is different than the original in template");
385:                }
386:
387:                if (ic.instanceCreate() == obj.instanceCreate()) {
388:                    fail("Instance of the new object is same as the current of the template");
389:                }
390:            }
391:
392:            /** Test if the Lookup reflects IDO' cokie changes. */
393:            public void testLookupRefreshOfInstanceCookieChanges()
394:                    throws Exception {
395:                //        Object ser = new java.awt.Button ();
396:                Object ser = new java.beans.beancontext.BeanContextChildSupport();
397:
398:                FileObject lookupFO = lfs
399:                        .findResource("/system/Services/lookupTest");
400:                FileObject systemFO = lfs.findResource("/system");
401:
402:                FolderLookup lookup = new FolderLookup(DataFolder
403:                        .findFolder(systemFO));
404:                Lookup l = lookup.getLookup();
405:                DataFolder folderTest = DataFolder.findFolder(lookupFO);
406:
407:                InstanceDataObject ido = InstanceDataObject.create(folderTest,
408:                        "testLookupRefresh", ser, null);
409:                Lookup.Result res = l
410:                        .lookup(new Lookup.Template(ser.getClass()));
411:                Collection col = res.allInstances();
412:                InstanceCookie ic = (InstanceCookie) ido
413:                        .getCookie(InstanceCookie.class);
414:                assertEquals("IDO did not create new InstanceCookie", ser, ic
415:                        .instanceCreate());
416:
417:                Set origSet = new HashSet(Arrays.asList(new Object[] { ser }));
418:                assertEquals("wrong lookup result", origSet, new HashSet(col));
419:
420:                assertTrue(
421:                        "Lookup is not finished and surprisingly returned a result",
422:                        lookup.isFinished());
423:
424:                Object found = col.iterator().next();
425:                assertEquals("found wrong object instance", ser, found);
426:
427:                // due to #14795 workaround
428:                Thread.sleep(1000);
429:
430:                // external file change forcing IDO to create new InstanceCookie
431:                final FileObject fo = ido.getPrimaryFile();
432:                lfs.runAtomicAction(new FileSystem.AtomicAction() {
433:                    public void run() throws IOException {
434:                        FileLock lock = null;
435:                        try {
436:                            InputStream in = fo.getInputStream();
437:                            byte[] buf = new byte[(int) fo.getSize()];
438:                            in.read(buf);
439:                            in.close();
440:
441:                            lock = fo.lock();
442:                            OutputStream out = fo.getOutputStream(lock);
443:                            out.write(buf);
444:                            out.write(32);
445:                            out.flush();
446:                            out.close();
447:
448:                        } finally {
449:                            if (lock != null)
450:                                lock.releaseLock();
451:                        }
452:                    }
453:                });
454:
455:                col = res.allInstances();
456:                ic = (InstanceCookie) ido.getCookie(InstanceCookie.class);
457:                origSet = new HashSet(Arrays.asList(new Object[] { ic
458:                        .instanceCreate() }));
459:
460:                assertEquals("wrong lookup result", origSet, new HashSet(col));
461:
462:                found = col.iterator().next();
463:                assertTrue("IDO did not create new InstanceCookie", ser != ic
464:                        .instanceCreate());
465:                assertTrue("Lookup did not refresh changed InstanceCookie",
466:                        ser != found);
467:            }
468:
469:            /*
470:            private void assertEquals(boolean b1, boolean b2) {
471:                assertEquals(b1  ? Boolean.TRUE : Boolean.FALSE, b2  ? Boolean.TRUE : Boolean.FALSE);
472:            }
473:             */
474:            /** Checks whether the instance is not saved multiple times.
475:             *
476:            public void testMultiSave () throws Exception {
477:                Ser ser1 = new Ser ("1");
478:                Ser ser2 = new Ser ("2");
479:                
480:                InstanceDataObject i = InstanceDataObject.create (folder, null, ser1, null);
481:                
482:                Thread.sleep (3000);
483:                
484:                InstanceDataObject j = InstanceDataObject.create (folder, null, ser2, null);
485:                Thread.sleep (3000);
486:                
487:                Object n = i.instanceCreate ();
488:                if (n != ser1) {
489:                    fail ("instanceCreate is not the same: ");
490:                }
491:                i.instanceCreate ();
492:                j.instanceCreate ();
493:                j.instanceCreate ();
494:                
495:            } */
496:
497:            public static final class Ser extends Object implements 
498:                    Externalizable {
499:                static final long serialVersionUID = -123456;
500:                public int deserialized;
501:                public int serialized;
502:                private String name;
503:
504:                private int property;
505:
506:                private java.beans.PropertyChangeSupport propertyChangeSupport = new java.beans.PropertyChangeSupport(
507:                        this );
508:
509:                public Ser(String name) {
510:                    this .name = name;
511:                }
512:
513:                public synchronized void readExternal(
514:                        java.io.ObjectInput objectInput)
515:                        throws java.io.IOException,
516:                        java.lang.ClassNotFoundException {
517:                    //            System.err.println(name + " deserialized");
518:                    deserialized++;
519:                }
520:
521:                public synchronized void writeExternal(
522:                        java.io.ObjectOutput objectOutput)
523:                        throws java.io.IOException {
524:                    //            System.err.println(name + " serialized");
525:                    serialized++;
526:                }
527:
528:                public void addPropertyChangeListener(
529:                        java.beans.PropertyChangeListener l) {
530:                    propertyChangeSupport.addPropertyChangeListener(l);
531:                }
532:
533:                public void removePropertyChangeListener(
534:                        java.beans.PropertyChangeListener l) {
535:                    propertyChangeSupport.removePropertyChangeListener(l);
536:                }
537:
538:                public int getProperty() {
539:                    return this .property;
540:                }
541:
542:                public void setProperty(int property) {
543:                    int oldProperty = this .property;
544:                    this .property = property;
545:                    propertyChangeSupport.firePropertyChange("property",
546:                            new Integer(oldProperty), new Integer(property));
547:                }
548:
549:            }
550:
551:            /** Tests creating .settings file (<code>IDO.create</code>) using parameter
552:             * <code>create</code>
553:             */
554:            public void testCreateSettings() throws Exception {
555:                FileObject fo = lfs.findResource("/testCreateInstance");
556:                assertNotNull("missing folder /testCreateInstance", fo);
557:                DataFolder folder = DataFolder.findFolder(fo);
558:                assertNotNull("cannot find DataFolder /testCreateInstance",
559:                        folder);
560:
561:                // test non null filename
562:                String filename = "testCreateSettings";
563:                Object obj = new javax.swing.JButton();
564:                InstanceDataObject ido = InstanceDataObject.create(folder,
565:                        filename, obj, null, false);
566:                assertNotNull("InstanceDataObject.create cannot return null!",
567:                        ido);
568:
569:                InstanceDataObject ido2 = InstanceDataObject.create(folder,
570:                        filename, obj, null, false);
571:                assertNotNull("InstanceDataObject.create cannot return null!",
572:                        ido2);
573:                assertEquals(
574:                        "InstanceDataObject.create(..., false) must reuse existing file: ",
575:                        ido.getPrimaryFile(), ido2.getPrimaryFile());
576:
577:                for (int i = 0; i < 3; i++) {
578:                    ido2 = InstanceDataObject.create(folder, filename, obj,
579:                            null, true);
580:                    assertNotNull(
581:                            "InstanceDataObject.create cannot return null!",
582:                            ido2);
583:                    assertTrue(
584:                            "InstanceDataObject.create(..., true) must create new file: "
585:                                    + "step: " + i + ", "
586:                                    + ido2.getPrimaryFile(), ido
587:                                    .getPrimaryFile() != ido2.getPrimaryFile());
588:                }
589:
590:                // test null filename
591:                ido = InstanceDataObject.create(folder, null, obj, null, false);
592:                assertNotNull("InstanceDataObject.create cannot return null!",
593:                        ido);
594:
595:                ido2 = InstanceDataObject
596:                        .create(folder, null, obj, null, false);
597:                assertNotNull("InstanceDataObject.create cannot return null!",
598:                        ido2);
599:                // filename == null => always create new file (ignore create parameter) => backward compatibility
600:                assertTrue(
601:                        "InstanceDataObject.create(..., false) must create new file: "
602:                                + ido2.getPrimaryFile(),
603:                        ido.getPrimaryFile() != ido2.getPrimaryFile());
604:
605:                for (int i = 0; i < 3; i++) {
606:                    ido2 = InstanceDataObject.create(folder, null, obj, null,
607:                            true);
608:                    assertNotNull(
609:                            "InstanceDataObject.create cannot return null!",
610:                            ido2);
611:                    assertTrue(
612:                            "InstanceDataObject.create(..., true) must create new file: "
613:                                    + ido2.getPrimaryFile(), ido
614:                                    .getPrimaryFile() != ido2.getPrimaryFile());
615:                }
616:            }
617:
618:            public void testDeleteSettings() throws Exception {
619:                FileObject root = lfs.getRoot();
620:                DataFolder folder = DataFolder.findFolder(root);
621:
622:                String filename = "testDeleteSettings";
623:                javax.swing.JButton obj = new javax.swing.JButton();
624:                InstanceDataObject ido = InstanceDataObject.create(folder,
625:                        filename, obj, null, false);
626:                assertNotNull("InstanceDataObject.create cannot return null!",
627:                        ido);
628:
629:                // test if file object does not remain locked when ido is deleted and
630:                // the storing is not rescheduled in consequence of the serialization 
631:                obj.setForeground(java.awt.Color.black);
632:                Thread.sleep(500);
633:                ido.delete();
634:                assertNull(filename + ".settings was not deleted!", root
635:                        .getFileObject(filename));
636:                Thread.sleep(3000);
637:                assertNull(filename + ".settings was not deleted!", root
638:                        .getFileObject(filename));
639:
640:                filename = "testDeleteSettings2";
641:                Ser ser = new Ser("bla");
642:                ido = InstanceDataObject.create(folder, filename, ser, null,
643:                        false);
644:                assertNotNull("InstanceDataObject.create cannot return null!",
645:                        ido);
646:
647:                ser.setProperty(10);
648:                ido.delete();
649:                assertNull(filename + ".settings was not deleted!", root
650:                        .getFileObject(filename));
651:                Thread.sleep(3000);
652:                assertNull(filename + ".settings was not deleted!", root
653:                        .getFileObject(filename));
654:            }
655:
656:            public void testDisabledOrUnknownModule() throws Exception {
657:                final FileObject valid = lfs
658:                        .findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingDisabledOrUnknownModule.settings");
659:                assertNotNull(valid);
660:                DataObject ido = DataObject.find(valid);
661:                InstanceCookie ic = (InstanceCookie) ido
662:                        .getCookie(InstanceCookie.class);
663:                assertNull(
664:                        "There shouldn't be provided InstanceCookie for disabled module",
665:                        ic);
666:            }
667:
668:            public void testDeleteOfUnrecognizedSettingsFile() throws Exception {
669:                final FileObject corrupted = lfs
670:                        .findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingUnrecognizedSettingsFile.settings");
671:                assertNotNull(corrupted);
672:
673:                DataObject ido = DataObject.find(corrupted);
674:                org.openide.nodes.Node node = ido.getNodeDelegate();
675:                node.destroy();
676:                FileObject corrupted2 = lfs
677:                        .findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingUnrecognizedSettingsFile.settings");
678:                assertNull(corrupted2);
679:            }
680:
681:            public void testCorruptedSettingsFile() throws Exception {
682:                final FileObject corrupted = lfs
683:                        .findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingSerialDataCorruptedTest.settings");
684:                assertNotNull(corrupted);
685:
686:                DataObject ido = DataObject.find(corrupted);
687:                InstanceCookie ic = (InstanceCookie) ido
688:                        .getCookie(InstanceCookie.class);
689:                assertNotNull("Missing InstanceCookie", ic);
690:
691:                Object obj = null;
692:                try {
693:                    obj = ic.instanceCreate();
694:                } catch (IOException ex) {
695:                }
696:                assertNull("corrupted .settings file cannot provide an object",
697:                        obj);
698:
699:                final FileObject valid = lfs
700:                        .findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingSerialDataCorruptedTest2.settings");
701:                assertNotNull(valid);
702:
703:                // simulate revert to default of a corrupted setting object
704:                corrupted.getFileSystem().runAtomicAction(
705:                        new FileSystem.AtomicAction() {
706:                            public void run() throws IOException {
707:                                FileLock l = null;
708:                                OutputStream os = null;
709:                                try {
710:                                    l = corrupted.lock();
711:                                    os = corrupted.getOutputStream(l);
712:                                    FileUtil.copy(valid.getInputStream(), os);
713:                                    os.flush();
714:                                } finally {
715:                                    if (os != null)
716:                                        try {
717:                                            os.close();
718:                                        } catch (IOException ex) {
719:                                        }
720:                                    if (l != null)
721:                                        l.releaseLock();
722:                                }
723:                            }
724:                        });
725:
726:                ic = (InstanceCookie) ido.getCookie(InstanceCookie.class);
727:                assertNotNull("Missing InstanceCookie", ic);
728:                assertNotNull("the persisted object cannot be read", ic
729:                        .instanceCreate());
730:            }
731:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.