1 19 20 package org.netbeans.core.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.*; 24 import org.netbeans.junit.NbTestCase; 25 import org.netbeans.junit.NbTestSuite; 26 import org.netbeans.spi.registry.BasicContext; 27 import org.netbeans.spi.registry.SpiUtils; 28 import org.openide.filesystems.FileLock; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.LocalFileSystem; 31 import org.openide.modules.ModuleInfo; 32 import org.openide.util.Lookup; 33 34 import java.io.OutputStream ; 35 import java.util.ArrayList ; 36 37 41 public class ExternalModificationTest extends NbTestCase { 42 43 public ExternalModificationTest(String name) { 44 super (name); 45 } 46 47 public static void main(String [] args) { 48 TestRunner.run(new NbTestSuite(ExternalModificationTest.class)); 49 } 50 51 protected void setUp () throws Exception { 52 clearWorkDir(); 53 Lookup.getDefault().lookup(ModuleInfo.class); 54 } 55 56 public void testExternalModification() throws Exception { 57 LocalFileSystem lfs = new LocalFileSystem(); 58 lfs.setRootDirectory(getWorkDir()); 59 FileObject exte = lfs.getRoot().createFolder("exte"); 60 61 BasicContext root = new ResettableContextImpl(lfs.getRoot()); 62 Context rootContext = SpiUtils.createContext(root); 63 64 Context ctx = rootContext.getSubcontext("exte"); 65 66 FileSystemListener l = new FileSystemListener(); 67 rootContext.addContextListener(l); 68 FileSystemListener l2 = new FileSystemListener(); 69 ctx.addContextListener(l2); 70 71 assertTrue("There cannot be any binding yet", ctx.getBindingNames().size() == 0); 72 73 FileObject fo2 = exte.createData("smth", "instance"); 74 assertEquals("There must be one event fired", 1, l.b.size()); 76 assertEquals("There must be one event fired", 1, l.all.size()); 77 assertEquals("There must be one event fired", 1, l2.b.size()); 78 assertEquals("There must be one event fired", 1, l2.all.size()); 79 assertEquals("The event must be BindingEvent.BINDING_ADDED", BindingEvent.BINDING_ADDED, ((BindingEvent)l.b.get(0)).getType()); 80 assertEquals("There must be one binding", 1, ctx.getBindingNames().size()); 81 l.reset(); 82 l2.reset(); 83 84 FileLock lock = fo2.lock(); 85 OutputStream os = fo2.getOutputStream(lock); 86 os.write(74); 88 os.close(); 89 lock.releaseLock(); 90 assertEquals("There must be one event fired", 1, l.b.size()); 92 assertEquals("There must be one event fired", 1, l.all.size()); 93 assertEquals("There must be one event fired", 1, l2.b.size()); 94 assertEquals("There must be one event fired", 1, l2.all.size()); 95 assertEquals("The event must be BindingEvent.BINDING_MODIFIED", BindingEvent.BINDING_MODIFIED, ((BindingEvent)l.b.get(0)).getType()); 96 assertEquals("There must be one binding", 1, ctx.getBindingNames().size()); 97 l.reset(); 98 l2.reset(); 99 100 fo2.delete(); 101 assertEquals("There must be one event fired", 1, l.b.size()); 103 assertEquals("There must be one event fired", 1, l.all.size()); 104 assertEquals("There must be one event fired", 1, l2.b.size()); 105 assertEquals("There must be one event fired", 1, l2.all.size()); 106 assertEquals("The event must be BindingEvent.BINDING_REMOVED", BindingEvent.BINDING_REMOVED, ((BindingEvent)l.b.get(0)).getType()); 107 assertEquals("There must be zero binding", 0, ctx.getBindingNames().size()); 108 l.reset(); 109 l2.reset(); 110 111 112 fo2 = exte.createData("thrash", "odpad"); 114 assertEquals("There must be zero event fired", 0, l.b.size()); 116 assertEquals("There must be zero event fired", 0, l.all.size()); 117 assertEquals("There must be zero event fired", 0, l2.b.size()); 118 assertEquals("There must be zero event fired", 0, l2.all.size()); 119 assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size()); 120 l.reset(); 121 l2.reset(); 122 123 lock = fo2.lock(); 124 os = fo2.getOutputStream(lock); 125 os.write(74); 127 os.close(); 128 lock.releaseLock(); 129 assertEquals("There must be zero event fired", 0, l.b.size()); 131 assertEquals("There must be zero event fired", 0, l.all.size()); 132 assertEquals("There must be zero event fired", 0, l2.b.size()); 133 assertEquals("There must be zero event fired", 0, l2.all.size()); 134 assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size()); 135 l.reset(); 136 l2.reset(); 137 138 fo2.delete(); 139 assertEquals("There must be zero event fired", 0, l.b.size()); 141 assertEquals("There must be zero event fired", 0, l.all.size()); 142 assertEquals("There must be zero event fired", 0, l2.b.size()); 143 assertEquals("There must be zero event fired", 0, l2.all.size()); 144 assertEquals("There must be zero binding only", 0, ctx.getBindingNames().size()); 145 l.reset(); 146 l2.reset(); 147 148 } 149 150 public static class FileSystemListener implements ContextListener { 151 152 ArrayList s; 153 ArrayList a; 154 ArrayList b; 155 ArrayList all; 156 String error = null; 157 158 public FileSystemListener() { 159 reset(); 160 } 161 162 public void reset() { 163 s = new ArrayList (); 164 b = new ArrayList (); 165 a = new ArrayList (); 166 all = new ArrayList (); 167 error = null; 168 } 169 170 public void subcontextChanged(SubcontextEvent evt) { 171 s.add(evt); 172 all.add(evt); 173 } 174 175 public void bindingChanged(BindingEvent evt) { 176 b.add(evt); 177 all.add(evt); 178 } 179 180 public void attributeChanged(AttributeEvent evt) { 181 a.add(evt); 182 all.add(evt); 183 } 184 185 } 186 } 187 | Popular Tags |