KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > lookup > InstanceDataObjectModuleTest7


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.lookup;
21
22 import javax.swing.Action JavaDoc;
23 import org.openide.cookies.InstanceCookie;
24 import org.openide.filesystems.FileAttributeEvent;
25 import org.openide.filesystems.FileChangeListener;
26 import org.openide.filesystems.FileEvent;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileRenameEvent;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.filesystems.Repository;
31 import org.openide.loaders.DataObject;
32
33 /** A test.
34  * @author Jesse Glick
35  * @see InstanceDataObjectModuleTestHid
36  */

37 public class InstanceDataObjectModuleTest7 extends InstanceDataObjectModuleTestHid
38 implements FileChangeListener {
39     private boolean instanceSaved;
40
41     public InstanceDataObjectModuleTest7(String JavaDoc name) {
42         super(name);
43     }
44     
45     protected void setUp() throws Exception JavaDoc {
46         Repository.getDefault ().getDefaultFileSystem ().addFileChangeListener (this);
47         assertNotNull("have org-netbeans-modules-settings.jar in CP", InstanceDataObjectModuleTest7.class.getResource("/org/netbeans/modules/settings/resources/Bundle.properties"));
48         super.setUp();
49     }
50     
51     protected void tearDown() throws Exception JavaDoc {
52         Repository.getDefault ().getDefaultFileSystem ().removeFileChangeListener (this);
53         super.tearDown ();
54     }
55     
56     public void testFixedSettingsChangeInstanceAfterSlowReload() throws Exception JavaDoc {
57         twiddle(m2, TWIDDLE_ENABLE);
58         assertTrue ("m2 is enabled", m2.isEnabled ());
59         DataObject obj1;
60             obj1 = findIt("Services/Misc/inst-2.settings");
61             assertEquals("No saved state for inst-2.settings", null, FileUtil.toFile(obj1.getPrimaryFile()));
62             org.openide.ErrorManager.getDefault ().log ("BEFORE THE COOKIE QUERY");
63             InstanceCookie inst1 = (InstanceCookie)obj1.getCookie(InstanceCookie.class);
64             org.openide.ErrorManager.getDefault ().log ("AFTER THE COOKIE QUERY");
65             {
66                 int debug = 5;
67                 while (inst1 == null && debug-- > 0) {
68                     Thread.sleep (300);
69                     inst1 = (InstanceCookie)obj1.getCookie(InstanceCookie.class);
70                     org.openide.ErrorManager.getDefault ().log (" SLEEP[300ms]: " + inst1);
71                 }
72             }
73             assertNotNull("Had an instance from " + obj1, inst1);
74             Action JavaDoc a1 = (Action JavaDoc)inst1.instanceCreate();
75             assertTrue("Old version of action", a1.isEnabled());
76             // Make some change which should cause it to be written to disk:
77
synchronized (this) {
78                 a1.setEnabled(false);
79                 // Cf. InstanceDataObject.SettingsInstance.SAVE_DELAY = 2000:
80
wait (60000);
81                 assertTrue ("Really was saved", instanceSaved);
82             }
83             twiddle(m2, TWIDDLE_DISABLE);
84             // Just in case it is needed:
85
Thread.sleep(1000);
86             
87             assertTrue ("Data object is still valid", obj1.isValid ());
88
89             // Yarda's patch:
90
InstanceCookie.Of notExists = (InstanceCookie.Of)obj1.getCookie (InstanceCookie.class);
91             if (notExists != null && notExists.instanceOf(Action JavaDoc.class)) {
92                 fail ("Module is disabled, so " + obj1 + " should have no instance cookie " + notExists + " with " + notExists.instanceClass());
93             }
94             // it is OK for there to be an instance of BrokenSettings...
95

96             twiddle(m2, TWIDDLE_ENABLE);
97             // Make sure there is time for changes to take effect:
98
Thread.sleep(2000);
99             DataObject obj2 = findIt("Services/Misc/inst-2.settings");
100             assertSameDataObject ("same data object", obj1, obj2);
101             InstanceCookie inst2 = (InstanceCookie)obj2.getCookie(InstanceCookie.class);
102             assertNotNull("Had an instance", inst2);
103             assertTrue("InstanceCookie changed", inst1 != inst2);
104             Action JavaDoc a2 = (Action JavaDoc)inst2.instanceCreate();
105             assertTrue("Action changed", a1 != a2);
106             assertTrue("Correct action", "SomeAction".equals(a2.getValue(Action.NAME)));
107             assertTrue("New version of action", !a2.isEnabled());
108             assertTrue("module still enabled", m2.isEnabled());
109             twiddle(m2, TWIDDLE_DISABLE);
110         // Now make sure it has no cookie.
111
Thread.sleep(1000);
112         DataObject obj3 = findIt("Services/Misc/inst-2.settings");
113         assertSameDataObject ("same data object3", obj1, obj3);
114         InstanceCookie inst3 = (InstanceCookie)obj3.getCookie(InstanceCookie.class);
115         assertNull("Had instance", inst3);
116     }
117     
118     public void fileAttributeChanged(FileAttributeEvent fe) {}
119     
120     public synchronized void fileChanged(FileEvent fe) {
121         if ("inst-2.settings".equals (fe.getFile ().getNameExt ())) {
122             instanceSaved = true;
123             notifyAll ();
124         }
125     }
126     
127     public void fileDataCreated(FileEvent fe) {}
128     
129     public void fileDeleted(FileEvent fe) {
130         if ("inst-2.settings".equals (fe.getFile ().getNameExt ())) {
131             FileObject isThere = Repository.getDefault ().getDefaultFileSystem ().findResource (fe.getFile ().getPath ());
132             
133             fail ("File " + fe.getFile () + " should not be deleted as this will discard the data object. Moreover it is expected that similar file is still there: " + isThere);
134         }
135     }
136     
137     public void fileFolderCreated(FileEvent fe) {}
138     
139     public void fileRenamed(FileRenameEvent fe) {}
140     
141 }
142
Popular Tags