KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbeans.core.LoaderPoolNode;
24 import org.openide.cookies.InstanceCookie;
25 import org.openide.filesystems.FileAttributeEvent;
26 import org.openide.filesystems.FileChangeListener;
27 import org.openide.filesystems.FileEvent;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileRenameEvent;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.filesystems.Repository;
32 import org.openide.loaders.DataObject;
33
34 /** A test.
35  * @author Jesse Glick
36  * @see InstanceDataObjectModuleTestHid
37  */

38 public class InstanceDataObjectModuleTest8 extends InstanceDataObjectModuleTestHid
39 implements FileChangeListener {
40     
41     /*
42     static {
43         // Turn on verbose logging while developing tests:
44         System.setProperty("org.netbeans.core.modules", "0");
45     }
46      */

47     
48     private boolean instanceSaved;
49
50     public InstanceDataObjectModuleTest8(String JavaDoc name) {
51         super(name);
52     }
53     
54     protected void setUp() throws Exception JavaDoc {
55         Repository.getDefault ().getDefaultFileSystem ().addFileChangeListener (this);
56         assertNotNull("have org-netbeans-modules-settings.jar in CP", InstanceDataObjectModuleTest7.class.getResource("/org/netbeans/modules/settings/resources/Bundle.properties"));
57         super.setUp();
58     }
59      
60     protected void tearDown () throws java.lang.Exception JavaDoc {
61         Repository.getDefault ().getDefaultFileSystem ().removeFileChangeListener (this);
62         super.tearDown ();
63     }
64     
65     /** Currently fails.
66      * Same as #7, but reloading is done quickly (one write mutex, no pause).
67      */

68     public void testFixedSettingsChangeInstanceAfterFastReload() throws Exception JavaDoc {
69         twiddle(m2, TWIDDLE_ENABLE);
70         DataObject obj1;
71         try {
72             obj1 = findIt("Services/Misc/inst-8.settings");
73             ERR.log("Found obj1: " + obj1);
74             assertEquals("No saved state for inst-8.settings", null, FileUtil.toFile(obj1.getPrimaryFile()));
75             InstanceCookie inst1 = (InstanceCookie)obj1.getCookie(InstanceCookie.class);
76             ERR.log("There is a cookie: " + inst1);
77             assertNotNull("Had an instance", inst1);
78             Action JavaDoc a1 = (Action JavaDoc)inst1.instanceCreate();
79             assertEquals("Correct action class", "test2.SomeAction", a1.getClass().getName());
80             assertTrue("Old version of action", a1.isEnabled());
81             
82             ERR.log("Action created" + a1);
83             
84             // Make some change which should cause it to be written to disk:
85
synchronized (this) {
86                 ERR.log("In sync block");
87                 a1.setEnabled(false);
88                 ERR.log("setEnabled(false)");
89                 // Cf. InstanceDataObject.SettingsInstance.SAVE_DELAY = 2000:
90
ERR.log("Waiting");
91                 wait (60000);
92                 ERR.log("Waiting done");
93                 assertTrue ("Really was saved", instanceSaved);
94             }
95             /*
96             File saved = new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings");
97             assertTrue("Wrote to disk: " + saved, saved.isFile());
98              */

99             /*
100             File saved = FileUtil.toFile(obj1.getPrimaryFile());
101             assertNotNull("Wrote to disk; expecting: " + new File(new File(new File(systemDir, "Services"), "Misc"), "inst-8.settings"),
102                 saved);
103              */

104             ERR.log("Twidle reload");
105             twiddle(m2, TWIDDLE_RELOAD);
106             ERR.log("TWIDDLE_RELOAD done");
107             LoaderPoolNode.waitFinished();
108             ERR.log("pool refeshed");
109             DataObject obj2 = findIt("Services/Misc/inst-8.settings");
110             ERR.log("Data object for inst-8: " + obj2);
111             assertSameDataObject ("same data object", obj1, obj2);
112             InstanceCookie inst2 = (InstanceCookie)obj2.getCookie(InstanceCookie.class);
113             ERR.log("Cookie from the object: " + inst2);
114             assertNotNull("Had an instance", inst2);
115             assertTrue("InstanceCookie changed", inst1 != inst2);
116             Action JavaDoc a2 = (Action JavaDoc)inst2.instanceCreate();
117             ERR.log("Action2 created: " + a2);
118             assertTrue("Action changed", a1 != a2);
119             assertTrue("Correct action", "SomeAction".equals(a2.getValue(Action.NAME)));
120             assertTrue("New version of action", !a2.isEnabled());
121         } finally {
122             ERR.log("Final disable");
123             twiddle(m2, TWIDDLE_DISABLE);
124             ERR.log("Final disable done");
125         }
126         // Now make sure it has no cookie.
127
LoaderPoolNode.waitFinished();
128         ERR.log("loader pool node refreshed");
129         DataObject obj3 = findIt("Services/Misc/inst-8.settings");
130         ERR.log("Third data object: " + obj3);
131         assertSameDataObject ("same data object2", obj1, obj3);
132         InstanceCookie inst3 = (InstanceCookie)obj3.getCookie(InstanceCookie.class);
133         ERR.log("Cookie is here: " + inst3);
134         assertNull("Had instance", inst3);
135     }
136     
137     
138     public void fileAttributeChanged(FileAttributeEvent fe) {}
139     
140     public synchronized void fileChanged(FileEvent fe) {
141         if ("inst-8.settings".equals (fe.getFile ().getNameExt ())) {
142             instanceSaved = true;
143             notifyAll ();
144         }
145     }
146     
147     public void fileDataCreated(FileEvent fe) {}
148     
149     public void fileDeleted(FileEvent fe) {
150         if ("inst-8.settings".equals (fe.getFile ().getNameExt ())) {
151             FileObject isThere = Repository.getDefault ().getDefaultFileSystem ().findResource (fe.getFile ().getPath ());
152             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);
153         }
154     }
155     
156     public void fileFolderCreated(FileEvent fe) {}
157     
158     public void fileRenamed(FileRenameEvent fe) {}
159     
160 }
161
Popular Tags