1 19 20 package org.netbeans.api.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.fs.FileSystemContextFactory; 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.Repository; 29 import org.openide.filesystems.FileSystem; 30 import org.openide.modules.ModuleInfo; 31 import org.openide.util.Lookup; 32 33 import javax.swing.*; 34 import java.util.ArrayList ; 35 36 40 public class EventsTest extends NbTestCase { 41 public EventsTest (String name) { 42 super (name); 43 } 44 45 public static void main(String [] args) { 46 TestRunner.run(new NbTestSuite(EventsTest.class)); 47 } 48 49 protected void setUp () throws Exception { 50 Lookup.getDefault().lookup(ModuleInfo.class); 51 } 52 53 Context modificationContext; 54 Context eventContext; 55 boolean badMode = false; 56 57 58 public void testFromAtomicAction1 () throws Exception { 59 if (getClass().toString().indexOf("ReusedEventsTest") != -1) return; 61 final Exception e[] = new Exception [1]; 62 final Context ctx = getRootContext().createSubcontext("testFromAtomicAction1"); 63 Listener listener = new Listener(); 64 ctx.addContextListener(listener); 65 Repository.getDefault().getDefaultFileSystem().runAtomicAction( 66 new FileSystem.AtomicAction() { 67 public void run() throws java.io.IOException { 68 try { 69 String subcontextName = "sub1"; 70 Context subcontext = ctx.createSubcontext(subcontextName); 71 assertEquals(subcontext, ctx.getSubcontext(subcontextName)); 72 String bindingName = "binding"; 73 subcontext.putString(bindingName, bindingName); 74 assertEquals(bindingName, subcontext.getString(bindingName, null)); 75 } catch (Exception x) { 76 e[0] = x; 77 } 78 } 79 } 80 ); 81 assertEquals(1,listener.s.size()); 82 assertEquals(1,listener.b.size()); 83 if (e[0] != null) { 84 throw e[0]; 85 } 86 } 87 88 89 public void testEvents() throws Exception { 90 Context ctx = getRootContext().createSubcontext("events"); 91 92 Listener listener = new Listener(); 93 ctx.addContextListener(listener); 94 95 implTestEvents(listener, ctx, ctx); 96 97 ctx.removeContextListener(listener); 98 getRootContext().destroySubcontext("events"); 99 } 100 101 public void testEvents2() throws Exception { 102 Context ctx = getRootContext().createSubcontext("ev/en/ts"); 103 104 Listener listener = new Listener(); 105 getRootContext().addContextListener(listener); 106 107 implTestEvents(listener, ctx, ctx); 108 109 getRootContext().removeContextListener(listener); 110 getRootContext().destroySubcontext("ev"); 111 } 112 113 114 public void _EXCLUDE_testEvents3() throws Exception { 115 116 124 badMode = true; 125 126 BasicContext root = FileSystemContextFactory.createContext(Repository.getDefault().getDefaultFileSystem().getRoot()); 127 Context rootContext_ = SpiUtils.createContext(root); 128 Context ctx = rootContext_.createSubcontext("ev1/en3/ts3"); 129 130 Listener listener = new Listener(); 131 getRootContext().addContextListener(listener); 132 Context eventCtx = getRootContext().createSubcontext("ev1/en3/ts3"); 133 134 implTestEvents(listener, ctx, eventCtx); 135 136 getRootContext().removeContextListener(listener); 137 rootContext_.destroySubcontext("ev1"); 138 } 139 140 public void implTestEvents(Listener listener, Context modificationCtx, Context eventCtx) throws Exception { 141 modificationContext = modificationCtx; 142 eventContext = eventCtx; 143 144 modificationContext.createSubcontext("abcd"); 146 assertTrue("Number of events does not match", listener.s.size() == 1 && listener.all.size() == 1); 147 assertTrue("Event type does not match", ((SubcontextEvent)listener.s.get(0)).getType() == SubcontextEvent.SUBCONTEXT_ADDED); 148 assertTrue("Event name does not match", ((SubcontextEvent)listener.s.get(0)).getSubcontextName().equals("abcd")); 149 assertTrue("Event context does not match", ((SubcontextEvent)listener.s.get(0)).getContext().equals(eventContext)); 150 assertNull(listener.error, listener.error); 151 listener.reset(); 152 153 modificationContext.destroySubcontext("abcd"); 154 assertTrue("Number of events does not match", listener.s.size() == 1 && listener.all.size() == 1); 155 assertTrue("Event type does not match", ((SubcontextEvent)listener.s.get(0)).getType() == SubcontextEvent.SUBCONTEXT_REMOVED); 156 assertTrue("Event name does not match", ((SubcontextEvent)listener.s.get(0)).getSubcontextName().equals("abcd")); 157 assertTrue("Event context does not match", ((SubcontextEvent)listener.s.get(0)).getContext().equals(eventContext)); 158 assertNull(listener.error, listener.error); 159 listener.reset(); 160 161 163 modificationContext.putObject("dadada", new JLabel("456")); 166 assertTrue("Number of events does not match", listener.b.size() == 2 && listener.all.size() == 2); 167 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_ADDED); 168 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("dadada")); 169 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 170 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(1)).getType() == BindingEvent.BINDING_MODIFIED); 171 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(1)).getBindingName().equals("dadada")); 172 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(1)).getContext().equals(eventContext)); 173 assertNull(listener.error, listener.error); 174 listener.reset(); 175 176 modificationContext.putObject("dadada", new JLabel("4565")); 177 assertTrue("Number of events does not match", listener.b.size() == 1 && listener.all.size() == 1); 178 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_MODIFIED); 179 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("dadada")); 180 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 181 assertNull(listener.error, listener.error); 182 listener.reset(); 183 185 187 modificationContext.putInt("bind", 789); 188 assertTrue("Number of events does not match", listener.b.size() == 1 && listener.all.size() == 1); 189 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_ADDED); 190 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("bind")); 191 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 192 assertNull(listener.error, listener.error); 193 listener.reset(); 194 195 modificationContext.putInt("bind", 123); 196 assertTrue("Number of events does not match", listener.b.size() == 1 && listener.all.size() == 1); 197 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_MODIFIED); 198 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("bind")); 199 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 200 assertNull(listener.error, listener.error); 201 listener.reset(); 202 203 204 206 modificationContext.setAttribute("dadada", "aaatt", "value789"); 207 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 208 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_ADDED); 209 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("dadada")); 210 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 211 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 212 assertNull(listener.error, listener.error); 213 listener.reset(); 214 215 modificationContext.setAttribute("dadada", "aaatt", "value123"); 216 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 217 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_MODIFIED); 218 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("dadada")); 219 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 220 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 221 assertNull(listener.error, listener.error); 222 listener.reset(); 223 224 modificationContext.setAttribute("dadada", "aaatt", null); 225 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 226 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_REMOVED); 227 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("dadada")); 228 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 229 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 230 assertNull(listener.error, listener.error); 231 listener.reset(); 232 233 modificationContext.setAttribute("bind", "aaatt", "value789"); 234 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 235 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_ADDED); 236 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("bind")); 237 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 238 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 239 assertNull(listener.error, listener.error); 240 listener.reset(); 241 242 modificationContext.setAttribute("bind", "aaatt", "value123"); 243 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 244 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_MODIFIED); 245 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("bind")); 246 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 247 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 248 assertNull(listener.error, listener.error); 249 listener.reset(); 250 251 modificationContext.setAttribute("bind", "aaatt", null); 252 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 253 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_REMOVED); 254 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName().equals("bind")); 255 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 256 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 257 assertNull(listener.error, listener.error); 258 listener.reset(); 259 260 262 modificationContext.putObject("dadada", null); 263 assertTrue("Number of events does not match", listener.b.size() == 1 && listener.all.size() == 1); 264 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_REMOVED); 265 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("dadada")); 266 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 267 assertNull(listener.error, listener.error); 268 listener.reset(); 269 270 modificationContext.putObject("bind", null); 271 assertTrue("Number of events does not match", listener.b.size() == 1); 273 assertTrue("Event type does not match", ((BindingEvent)listener.b.get(0)).getType() == BindingEvent.BINDING_REMOVED); 274 assertTrue("Event name does not match", ((BindingEvent)listener.b.get(0)).getBindingName().equals("bind")); 275 assertTrue("Event context does not match", ((BindingEvent)listener.b.get(0)).getContext().equals(eventContext)); 276 assertNull(listener.error, listener.error); 277 listener.reset(); 278 279 281 modificationContext.setAttribute(null, "aaatt", "value789"); 282 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 283 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_ADDED); 284 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName() == null); 285 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 286 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 287 assertNull(listener.error, listener.error); 288 listener.reset(); 289 290 modificationContext.setAttribute(null, "aaatt", "value123"); 291 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 292 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_MODIFIED); 293 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName() == null); 294 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 295 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 296 assertNull(listener.error, listener.error); 297 listener.reset(); 298 299 modificationContext.setAttribute(null, "aaatt", null); 300 assertTrue("Number of events does not match", listener.a.size() == 1 && listener.all.size() == 1); 301 assertTrue("Event type does not match", ((AttributeEvent)listener.a.get(0)).getType() == AttributeEvent.ATTRIBUTE_REMOVED); 302 assertTrue("Event bndname does not match", ((AttributeEvent)listener.a.get(0)).getBindingName() == null); 303 assertTrue("Event name does not match", ((AttributeEvent)listener.a.get(0)).getAttributeName().equals("aaatt")); 304 assertTrue("Event context does not match", ((AttributeEvent)listener.a.get(0)).getContext().equals(eventContext)); 305 assertNull(listener.error, listener.error); 306 listener.reset(); 307 } 308 309 protected Context getRootContext () { 310 return Context.getDefault(); 311 } 312 313 314 public static class Listener implements ContextListener { 315 316 ArrayList s; 317 ArrayList a; 318 ArrayList b; 319 ArrayList all; 320 String error = null; 321 322 public Listener() { 323 reset(); 324 } 325 326 public void reset() { 327 s = new ArrayList (); 328 b = new ArrayList (); 329 a = new ArrayList (); 330 all = new ArrayList (); 331 error = null; 332 } 333 334 public void subcontextChanged(SubcontextEvent evt) { 335 s.add(evt); 336 all.add(evt); 337 if (evt.getType() == SubcontextEvent.SUBCONTEXT_ADDED) { 338 if (evt.getContext().getSubcontext(evt.getSubcontextName()) == null) { 339 error = "Added subcontext must already exist: "+evt; 340 } 341 } 342 if (evt.getType() == SubcontextEvent.SUBCONTEXT_REMOVED) { 343 if (evt.getContext().getSubcontext(evt.getSubcontextName()) != null) { 344 error = "Removed subcontext must be already deleted: "+evt; 345 } 346 } 347 } 348 349 public void bindingChanged(BindingEvent evt) { 350 b.add(evt); 351 all.add(evt); 352 if (evt.getType() == BindingEvent.BINDING_ADDED || evt.getType() == BindingEvent.BINDING_MODIFIED) { 353 if (evt.getContext().getObject(evt.getBindingName(), null) == null) { 354 error = "Added or modified binding cannot have null value: "+evt; 355 } 356 } 357 if (evt.getType() == BindingEvent.BINDING_REMOVED) { 358 if (!evt.getContext().getObject(evt.getBindingName(), "abcd").equals("abcd")) { 359 error = "Removed binding must have null value: "+evt; 360 } 361 } 362 } 363 364 public void attributeChanged(AttributeEvent evt) { 365 a.add(evt); 366 all.add(evt); 367 if (evt.getType() == AttributeEvent.ATTRIBUTE_ADDED || evt.getType() == AttributeEvent.ATTRIBUTE_MODIFIED) { 368 if (evt.getContext().getAttribute(evt.getBindingName(), evt.getAttributeName(), null) == null) { 369 error = "Added or modified attribute cannot have null value: "+evt; 370 } 371 } 372 if (evt.getType() == AttributeEvent.ATTRIBUTE_REMOVED) { 373 if (!evt.getContext().getAttribute(evt.getBindingName(), evt.getAttributeName(), "abcd").equals("abcd")) { 374 error = "Removed attribute must have null value: "+evt; 375 } 376 } 377 } 378 379 } 380 381 } 382 | Popular Tags |