KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > EventsTest


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.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 JavaDoc;
35
36 /**
37  *
38  * @author David Konecny
39  */

40 public class EventsTest extends NbTestCase {
41     public EventsTest (String JavaDoc name) {
42         super (name);
43     }
44     
45     public static void main(String JavaDoc[] args) {
46         TestRunner.run(new NbTestSuite(EventsTest.class));
47     }
48     
49     protected void setUp () throws Exception JavaDoc {
50         Lookup.getDefault().lookup(ModuleInfo.class);
51     }
52
53     Context modificationContext;
54     Context eventContext;
55     boolean badMode = false;
56     
57     /** Test for issue #40498 */
58     public void testFromAtomicAction1 () throws Exception JavaDoc {
59         //TODO: #40578 - this test fails for MergedContexts
60
if (getClass().toString().indexOf("ReusedEventsTest") != -1) return;
61         final Exception JavaDoc e[] = new Exception JavaDoc[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 JavaDoc {
68                     try {
69                         String JavaDoc subcontextName = "sub1";
70                         Context subcontext = ctx.createSubcontext(subcontextName);
71                         assertEquals(subcontext, ctx.getSubcontext(subcontextName));
72                         String JavaDoc bindingName = "binding";
73                         subcontext.putString(bindingName, bindingName);
74                         assertEquals(bindingName, subcontext.getString(bindingName, null));
75                     } catch (Exception JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
115
116         /* This testcase is not very well supported at the moment.
117          * Setting badMode to true which will ignore some inaccurate
118          * values in events. This test is a bit hacky, but it does following:
119          * it simulates separate registry based on the same root as the default one.
120          * Any changes in this registry appear to the default registry as
121          * external modifications. Anyway, some events (although inaacurate)
122          * should be fired.
123          */

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 JavaDoc {
141         modificationContext = modificationCtx;
142         eventContext = eventCtx;
143
144         // subcontext events
145
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         // binding events. objects
162

163         // creating binding fires two events: BINDING_ADDED and BINDING_MODIFIED
164
// is that OK?
165
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         // do not unbind. it will be used below for testing binding attributes
184

185         // binding events. primitive data
186

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         // attribute events
205

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         // binding event test - unbind the object
261

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         // does not test number of alll attribs, because there can be some ATTRIBUTE_REMOVED events
272
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         // attribute events for context
280

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 JavaDoc s;
317         ArrayList JavaDoc a;
318         ArrayList JavaDoc b;
319         ArrayList JavaDoc all;
320         String JavaDoc error = null;
321         
322         public Listener() {
323             reset();
324         }
325         
326         public void reset() {
327             s = new ArrayList JavaDoc();
328             b = new ArrayList JavaDoc();
329             a = new ArrayList JavaDoc();
330             all = new ArrayList JavaDoc();
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