1 19 20 package org.netbeans.core.lookup; 21 22 import javax.swing.Action ; 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 37 public class InstanceDataObjectModuleTest7 extends InstanceDataObjectModuleTestHid 38 implements FileChangeListener { 39 private boolean instanceSaved; 40 41 public InstanceDataObjectModuleTest7(String name) { 42 super(name); 43 } 44 45 protected void setUp() throws Exception { 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 { 52 Repository.getDefault ().getDefaultFileSystem ().removeFileChangeListener (this); 53 super.tearDown (); 54 } 55 56 public void testFixedSettingsChangeInstanceAfterSlowReload() throws Exception { 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 a1 = (Action )inst1.instanceCreate(); 75 assertTrue("Old version of action", a1.isEnabled()); 76 synchronized (this) { 78 a1.setEnabled(false); 79 wait (60000); 81 assertTrue ("Really was saved", instanceSaved); 82 } 83 twiddle(m2, TWIDDLE_DISABLE); 84 Thread.sleep(1000); 86 87 assertTrue ("Data object is still valid", obj1.isValid ()); 88 89 InstanceCookie.Of notExists = (InstanceCookie.Of)obj1.getCookie (InstanceCookie.class); 91 if (notExists != null && notExists.instanceOf(Action .class)) { 92 fail ("Module is disabled, so " + obj1 + " should have no instance cookie " + notExists + " with " + notExists.instanceClass()); 93 } 94 96 twiddle(m2, TWIDDLE_ENABLE); 97 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 a2 = (Action )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 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 |