KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > mergedctx > ImplReferenceTest


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 package org.netbeans.api.registry.mergedctx;
20
21 import junit.textui.TestRunner;
22 import org.netbeans.api.registry.*;
23 import org.netbeans.junit.NbTestCase;
24 import org.netbeans.junit.NbTestSuite;
25 import org.openide.modules.ModuleInfo;
26 import org.openide.util.Lookup;
27
28 import java.util.*;
29
30 public class ImplReferenceTest extends NbTestCase {
31     public ImplReferenceTest (String JavaDoc name) {
32         super (name);
33     }
34
35     public static void main(String JavaDoc[] args) {
36         TestRunner.run(new NbTestSuite(ImplReferenceTest.class));
37     }
38
39     protected void setUp () throws Exception JavaDoc {
40         Lookup.getDefault().lookup(ModuleInfo.class);
41         getMergeContext();
42     }
43
44     Context getMergeContext() {return SetUpUtils.getSimpleContext(Context.getDefault());}
45     Context getActiveDelegate() {return SetUpUtils.getSubctx1();}
46     Context getReadOnlyDelegate1() {return SetUpUtils.getSubctx2();}
47     Context getReadOnlyDelegate2() {return SetUpUtils.getSubctx3();}
48
49     public void testInstances () throws Exception JavaDoc {
50         // init
51
Context rootCtx = getMergeContext();
52
53         //test
54
List list = new ArrayList();
55         Context test = rootCtx;
56         list.add(test);
57         Enumeration en = new StringTokenizer("/testRootOfMergeContext/A/B/C/D/E/F", "/");
58         while (en.hasMoreElements()) {
59             String JavaDoc s = (String JavaDoc)en.nextElement();
60             test = test.createSubcontext(s);
61             assertNotNull(test);
62             assertEquals(-1, list.indexOf(test));
63             list.add(test);
64         }
65
66         en = new StringTokenizer(test.getAbsoluteContextName(), "/");
67         while (en.hasMoreElements()) {
68             en.nextElement();
69             assertNotNull(test);
70             assertTrue(list.indexOf(test) != -1);
71             assertEquals(System.identityHashCode(test), System.identityHashCode(list.get(list.indexOf(test))));
72             test = test.getParentContext();
73         }
74
75         // we reached root
76
assertTrue(list.indexOf(test) != -1);
77         assertNull(test.getParentContext());
78
79         rootCtx.destroySubcontext("testRootOfMergeContext");
80         en = new StringTokenizer("/testRootOfMergeContext/A/B/C/D/E/F", "/");
81         while (en.hasMoreElements()) {
82             String JavaDoc s = (String JavaDoc)en.nextElement();
83             test = test.createSubcontext(s);
84             assertNotNull(test);
85             assertEquals(-1, list.indexOf(test));
86         }
87
88         en = new StringTokenizer(test.getAbsoluteContextName(), "/");
89         while (en.hasMoreElements()) {
90             en.nextElement();
91             assertNotNull(test);
92             assertTrue(list.indexOf(test) == -1);
93             test = test.getParentContext();
94         }
95
96         // we reached root
97
assertTrue(list.indexOf(test) != -1);
98         assertNull(test.getParentContext());
99
100     }
101
102     /**
103      * Destroy or creation of subcontext on delegates must affect merged context
104      *
105      */

106     public void testMergeFunctionality () throws Exception JavaDoc {
107         // init
108
Context rootCtx = getMergeContext();
109         Context activeDelegate = getActiveDelegate ();
110         Context roDelegate1 = getReadOnlyDelegate1 ();
111         Context roDelegate2 = getReadOnlyDelegate2();
112
113         String JavaDoc subcontextName = "testMergeFunctionality";
114         String JavaDoc activeDelegateName = "A";
115         String JavaDoc roDelegate1Name = "B";
116         String JavaDoc roDelegate2Name = "C";
117         TestContextListener l = new TestContextListener();
118         roDelegate1.createSubcontext(subcontextName).createSubcontext(roDelegate1Name);
119         roDelegate2.createSubcontext(subcontextName).createSubcontext(roDelegate2Name);
120         rootCtx.addContextListener(l);
121
122         // test
123
Context test = rootCtx.getSubcontext(subcontextName);
124         assertNotNull(test);
125         assertNull(test.getSubcontext(activeDelegateName));
126         assertNotNull(test.getSubcontext(roDelegate1Name));
127         assertNotNull(test.getSubcontext(roDelegate2Name));
128
129
130         activeDelegate = activeDelegate.createSubcontext(subcontextName);
131         assertEquals(0, l.s.size());// subcontextName already exist on r/o delegates
132
assertEquals(0, l.all.size());
133
134         activeDelegate.createSubcontext(activeDelegateName);
135         assertEquals(1, l.s.size()); //activeDelegateName doesn't exist yet
136
assertEquals(1, l.all.size());
137         assertNotNull(test.getSubcontext(activeDelegateName)); //already exist
138
assertNotNull(test.getSubcontext(roDelegate1Name));
139         assertNotNull(test.getSubcontext(roDelegate2Name) );
140
141         l = new TestContextListener();
142         rootCtx.addContextListener(l);
143         activeDelegate.destroySubcontext(activeDelegateName);
144         assertEquals(1, l.s.size());
145         assertEquals(1, l.all.size());
146         assertNull(test.getSubcontext(activeDelegateName));//doesn't exist
147
assertNotNull(test.getSubcontext(roDelegate1Name));
148         assertNotNull(test.getSubcontext(roDelegate2Name));
149
150         l = new TestContextListener();
151         rootCtx.addContextListener(l);
152         activeDelegate = activeDelegate.getParentContext();
153         activeDelegate.destroySubcontext(subcontextName);
154         assertEquals(0, l.s.size());
155         assertEquals(0, l.all.size());
156
157         assertNotNull(rootCtx.getSubcontext(subcontextName));
158         assertNotNull(roDelegate1.getSubcontext(subcontextName));
159         assertNotNull(roDelegate2.getSubcontext(subcontextName));
160     }
161
162     /**
163      * Simply tests binding events
164      */

165     public void testSimpleBindingEvents () throws Exception JavaDoc {
166         Context rootCtx = getMergeContext();
167
168         Context activeDelegate = getActiveDelegate ();
169         Context roDelegate1 = getReadOnlyDelegate1 ();
170         Context roDelegate2 = getReadOnlyDelegate2 ();
171
172         String JavaDoc subcontextName = "testSimpleBindingEvents";
173         String JavaDoc subSubcontextName1 = "TMA";
174         String JavaDoc subSubcontextName2 = "TMB";
175         String JavaDoc subSubcontextName3 = "TMC";
176
177         activeDelegate = activeDelegate.createSubcontext(subcontextName).createSubcontext(subSubcontextName1);
178         roDelegate1 = roDelegate1.createSubcontext(subcontextName).createSubcontext(subSubcontextName2);
179         roDelegate2 = roDelegate2.createSubcontext(subcontextName).createSubcontext(subSubcontextName3);
180
181         TestContextListener l = new TestContextListener();
182         rootCtx.addContextListener(l);
183         Context hard = rootCtx.getSubcontext("/"+subcontextName+"/"+subSubcontextName1);
184
185         String JavaDoc bindingName = "tmBinding";
186         activeDelegate.putString(bindingName, bindingName);
187
188         assertEquals(1, l.b.size());
189         assertEquals(1, l.all.size());
190         assertEquals(BindingEvent.BINDING_ADDED,((BindingEvent)l.b.get(0)).getType());
191         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
192
193         System.setProperty("tst","tst");
194         activeDelegate.putString(bindingName, null);
195         System.setProperty("tst","");
196
197         assertEquals(2, l.b.size());
198         assertEquals(2, l.all.size());
199         assertEquals(BindingEvent.BINDING_REMOVED,((BindingEvent)l.b.get(1)).getType());
200         assertEquals("defVal",hard.getString(bindingName,"defVal"));
201     }
202
203     /**
204      * No instance of merged context doesn't fire events
205      */

206     public void testNoInstanceNoBindingEvent () throws Exception JavaDoc {
207         Context rootCtx = getMergeContext();
208         Context activeDelegate = getActiveDelegate ();
209         Context roDelegate1 = getReadOnlyDelegate1 ();
210         Context roDelegate2 = getReadOnlyDelegate2 ();
211
212         String JavaDoc subcontextName = "testNoInstanceNoBindingEvent";
213         String JavaDoc activeDelegateName = "TMA2";
214         String JavaDoc roDelegate1Name = "TMB2";
215         String JavaDoc roDelegate2Name = "TMC2";
216         String JavaDoc bindingName = "tmBinding2";
217
218         activeDelegate = activeDelegate.createSubcontext(subcontextName).createSubcontext(activeDelegateName);
219         roDelegate1 = roDelegate1.createSubcontext(subcontextName).createSubcontext(roDelegate1Name);
220         roDelegate2 = roDelegate2.createSubcontext(subcontextName).createSubcontext(roDelegate2Name);
221
222         TestContextListener l = new TestContextListener();
223         rootCtx.addContextListener(l);
224
225         // there doesn't exist merged context with activeDelegate as its delegate
226
activeDelegate.putString(bindingName, bindingName);
227         activeDelegate.putString(bindingName, null);
228         assertEquals(0, l.b.size());
229         assertEquals(0, l.all.size());
230
231         Context test = rootCtx.getSubcontext(subcontextName).getSubcontext(activeDelegateName);
232         activeDelegate.putString(bindingName, bindingName);
233         activeDelegate.putString(bindingName, null);
234         assertEquals(2, l.b.size());
235         assertEquals(2, l.all.size());
236     }
237
238     public void testBindingAttributesAreNotMerged () throws Exception JavaDoc {
239         Context rootCtx = getMergeContext();
240
241         Context ctx1 = getActiveDelegate ();
242         Context ctx2 = getReadOnlyDelegate1 ();
243         Context ctx3 = getReadOnlyDelegate2 ();
244
245         String JavaDoc subcontextName = "testBindingAttributesAreNotMerged";
246         ctx1 = ctx1.createSubcontext(subcontextName);
247         ctx2 = ctx2.createSubcontext(subcontextName);
248         ctx3 = ctx3.createSubcontext(subcontextName);
249
250         String JavaDoc subSubcontextName = "TMA3";
251
252         ctx1 = ctx1.createSubcontext(subSubcontextName);
253         ctx2 = ctx2.createSubcontext(subSubcontextName);
254
255         TestContextListener l = new TestContextListener();
256         rootCtx.addContextListener(l);
257
258         Context hard = rootCtx.getSubcontext("/" + subcontextName + "/" + subSubcontextName);
259
260         String JavaDoc bindingName = "tmBinding";
261         String JavaDoc attrName = "tmAttr";
262
263         ctx2.putString(bindingName, bindingName);
264         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
265         ctx2.setAttribute(bindingName, attrName, attrName);
266
267         assertEquals(attrName,hard.getAttribute(bindingName, attrName, "defValue"));
268
269         // new binding means no previous attributes are overtaken
270
ctx1.putString(bindingName, bindingName);
271         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
272         assertEquals("defValue",hard.getAttribute(bindingName, attrName, "defValue"));
273     }
274
275     /**
276      * Once deleted binding masks read-only delegates forever.
277      *
278      */

279     public void testMaskedBindings () throws Exception JavaDoc {
280         Context rootCtx = getMergeContext();
281
282         Context ctx1 = getActiveDelegate ();
283         Context ctx2 = getReadOnlyDelegate1 ();
284         Context ctx3 = getReadOnlyDelegate2 ();
285
286         String JavaDoc subcontextName = "testMaskedBindings";
287         ctx1 = ctx1.createSubcontext(subcontextName);
288         ctx2 = ctx2.createSubcontext(subcontextName);
289         ctx3 = ctx3.createSubcontext(subcontextName);
290
291         String JavaDoc subSubcontextName1 = "TMA4";
292
293         ctx1 = ctx1.createSubcontext(subSubcontextName1);
294         ctx2 = ctx2.createSubcontext(subSubcontextName1);
295
296         Context hard = rootCtx.getSubcontext("/"+ subcontextName + "/"+ subSubcontextName1);
297
298         String JavaDoc bindingName = "tmBinding";
299         String JavaDoc attrName = "tmAttr";
300
301         ctx2.putString(bindingName, bindingName);
302         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
303         ctx2.setAttribute(bindingName, attrName, attrName);
304         assertEquals(attrName,hard.getAttribute(bindingName, attrName, "defValue"));
305
306         hard.putString(bindingName, null);
307         assertEquals("defVal",hard.getString(bindingName,"defVal"));
308         //ctx2.setAttribute(bindingName, attrName, attrName);
309
assertEquals("defValue",hard.getAttribute(bindingName, attrName, "defValue"));
310
311         ctx2.putString(bindingName, bindingName);
312         assertEquals("defVal",hard.getString(bindingName,"defVal"));
313         ctx2.setAttribute(bindingName, attrName, attrName);
314         assertEquals("defValue",hard.getAttribute(bindingName, attrName, "defValue"));
315
316
317         ctx1.putString(bindingName, bindingName);
318         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
319         ctx1.setAttribute(bindingName, attrName, attrName);
320         assertEquals(attrName,hard.getAttribute(bindingName, attrName, "defValue"));
321     }
322
323     public void testBindingChangeFollowedByAttributeChange () throws Exception JavaDoc {
324         Context rootCtx = getMergeContext();
325
326         Context ctx1 = getActiveDelegate ();
327         Context ctx2 = getReadOnlyDelegate1 ();
328
329         String JavaDoc subcontextName = "testBindingChangeFollowedByAttributeChange";
330         ctx1 = ctx1.createSubcontext(subcontextName);
331         ctx2 = ctx2.createSubcontext(subcontextName);
332
333         String JavaDoc subSubcontextName1 = "TMA5";
334
335         ctx1 = ctx1.createSubcontext(subSubcontextName1);
336         ctx2 = ctx2.createSubcontext(subSubcontextName1);
337
338         Context hard = rootCtx.getSubcontext("/"+subcontextName+"/"+subSubcontextName1);
339
340         String JavaDoc bindingName = "tmBinding";
341         String JavaDoc attrName = "tmAttr";
342
343         TestContextListener l1 = new TestContextListener();
344         rootCtx.addContextListener(l1);
345
346         ctx2.putString(bindingName, bindingName);
347         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
348         assertEquals(1, l1.b.size());
349         assertEquals(BindingEvent.BINDING_ADDED, ((BindingEvent)l1.b.get(0)).getType());
350         assertEquals(1, l1.all.size());
351
352         ctx2.setAttribute(bindingName, attrName, attrName);
353         assertEquals(attrName,hard.getAttribute(bindingName, attrName, "defValue"));
354
355         TestContextListener l = new TestContextListener();
356         rootCtx.addContextListener(l);
357
358         ctx1.putString(bindingName, bindingName);
359
360         assertEquals(bindingName,hard.getString(bindingName,"defVal"));
361         assertEquals("defValue",hard.getAttribute(bindingName, attrName, "defValue"));
362         assertEquals(1, l.b.size());
363         assertEquals(BindingEvent.BINDING_MODIFIED, ((BindingEvent)l.b.get(0)).getType());
364
365         assertEquals(1, l.a.size());
366         assertEquals(AttributeEvent.ATTRIBUTE_REMOVED, ((AttributeEvent)l.a.get(0)).getType());
367     }
368
369     public void testBindingChangeNotFollowedByAttributeChange () throws Exception JavaDoc {
370         Context rootCtx = getMergeContext();
371
372         Context ctx1 = getActiveDelegate ();
373         Context ctx2 = getReadOnlyDelegate1 ();
374
375         String JavaDoc subcontextName = "testBindingChangeNotFollowedByAttributeChange";
376         ctx1 = ctx1.createSubcontext(subcontextName);
377         ctx2 = ctx2.createSubcontext(subcontextName);
378
379         String JavaDoc subSubcontextName1 = "TMA";
380
381         ctx1 = ctx1.createSubcontext(subSubcontextName1);
382         ctx2 = ctx2.createSubcontext(subSubcontextName1);
383
384         Context hard = rootCtx.getSubcontext("/"+subcontextName+"/"+subSubcontextName1);
385
386         String JavaDoc bindingName = "tmBinding";
387
388         TestContextListener l1 = new TestContextListener();
389         rootCtx.addContextListener(l1);
390
391         hard.putString(bindingName, bindingName);
392         assertEquals(1, l1.b.size());
393         assertEquals(BindingEvent.BINDING_ADDED, ((BindingEvent)l1.b.get(0)).getType());
394         assertEquals(1, l1.all.size());
395
396         hard.putString(bindingName, "newBinding");
397
398         assertEquals(2, l1.b.size());
399         assertEquals(BindingEvent.BINDING_MODIFIED, ((BindingEvent)l1.b.get(1)).getType());
400         assertEquals(2, l1.all.size());
401
402         hard.putString(bindingName, null);
403         assertEquals(3, l1.b.size());
404         assertEquals(BindingEvent.BINDING_REMOVED, ((BindingEvent)l1.b.get(2)).getType());
405         assertEquals(3, l1.all.size());
406     }
407
408     public void testEventAfterSubCtxDestroy () throws Exception JavaDoc {
409         Context rootCtx = getMergeContext();
410
411         Context ctx1 = getActiveDelegate ();
412         Context ctx2 = getReadOnlyDelegate1 ();
413         Context ctx3 = getReadOnlyDelegate2 ();
414
415
416         String JavaDoc subcontextName = "testEventAfterSubCtxDestroy";
417         ctx1 = ctx1.createSubcontext(subcontextName);
418         ctx2 = ctx2.createSubcontext(subcontextName);
419         ctx3 = ctx3.createSubcontext(subcontextName);
420
421         String JavaDoc subSubcontextName1 = "TMA6";
422
423         ctx1 = ctx1.createSubcontext(subSubcontextName1);
424         ctx2 = ctx2.createSubcontext(subSubcontextName1);
425
426         Context hard = rootCtx.getSubcontext("/" + subcontextName + "/" + subSubcontextName1);
427
428         ctx1.createSubcontext(subSubcontextName1);
429         TestContextListener l = new TestContextListener();
430         rootCtx.addContextListener(l);
431         ctx1.destroySubcontext(subSubcontextName1);
432
433         assertNull(hard.getSubcontext(subSubcontextName1));
434         assertEquals(1, l.s.size());
435         assertEquals(1, l.all.size());
436         assertEquals(SubcontextEvent.SUBCONTEXT_REMOVED, ((SubcontextEvent)l.s.get(0)).getType());
437     }
438
439     public void testEventsAfterRecursiveDelete () throws Exception JavaDoc {
440         Context rootCtx = getMergeContext();
441
442         Context ctx1 = getActiveDelegate ();
443         Context ctx2 = getReadOnlyDelegate1 ();
444         Context ctx3 = getReadOnlyDelegate2 ();
445
446
447         String JavaDoc subcontextName = "testEventsAfterRecursiveDelete";
448         ctx1 = ctx1.createSubcontext(subcontextName);
449         ctx2 = ctx2.createSubcontext(subcontextName);
450         ctx3 = ctx3.createSubcontext(subcontextName);
451
452         String JavaDoc subSubcontextName1 = "TMA7";
453
454         Context hard = rootCtx.getSubcontext(subcontextName);
455         assertNotNull(hard);
456         List list = new ArrayList();
457         Context temp = hard;
458
459         for (int i = 0; i < 10; i++) {
460             Context temp2 = temp.createSubcontext(subSubcontextName1);
461             ctx1 = ctx1.getSubcontext(subSubcontextName1);
462             assertNotNull(ctx1);
463             assertNotNull(temp2);
464             assertNotNull(temp.getSubcontext(subSubcontextName1));
465             temp = temp2;
466             list.add(temp2);
467         }
468
469         TestContextListener l = new TestContextListener();
470         rootCtx.addContextListener(l);
471         assertNotNull(hard.getSubcontext(subSubcontextName1));
472         hard.destroySubcontext(subSubcontextName1);
473         assertEquals(10, l.s.size());
474     }
475
476     public void testRecreatedContextCantContainBindings () throws Exception JavaDoc {
477         Context rootCtx = getMergeContext();
478
479         Context ctx1 = getActiveDelegate ();
480         Context ctx2 = getReadOnlyDelegate1 ();
481         Context ctx3 = getReadOnlyDelegate2 ();
482
483
484         String JavaDoc subcontextName = "testRecreatedContextCantContainBindings";
485         ctx1 = ctx1.createSubcontext(subcontextName);
486         ctx2 = ctx2.createSubcontext(subcontextName);
487         ctx3 = ctx3.createSubcontext(subcontextName);
488
489         String JavaDoc subSubcontextName1 = "TMA8";
490
491         Context hard = rootCtx.getSubcontext(subcontextName);
492         assertNotNull(hard);
493         Context temp = hard;
494         temp.putString(subSubcontextName1,subSubcontextName1);
495
496         for (int i = 0; i < 10; i++) {
497             temp = temp.createSubcontext(subSubcontextName1);
498             temp.putString(subSubcontextName1,subSubcontextName1);
499             ctx1 = ctx1.getSubcontext(subSubcontextName1);
500             assertNotNull(ctx1);
501             assertNotNull(temp);
502         }
503
504         assertNotNull(hard.getSubcontext(subSubcontextName1));
505         hard.destroySubcontext(subSubcontextName1);
506
507         temp = hard;
508         for (int i = 0; i < 10; i++) {
509             temp = temp.createSubcontext(subSubcontextName1);
510             assertNull(temp.getObject(subSubcontextName1,null));
511             assertNotNull(temp);
512         }
513
514     }
515
516     public void testCtxChangeFollowedByBindingChange() throws Exception JavaDoc {
517         Context rootCtx = getMergeContext();
518
519         Context ctx1 = getActiveDelegate ();
520         Context ctx2 = getReadOnlyDelegate1 ();
521         Context ctx3 = getReadOnlyDelegate2 ();
522
523
524         String JavaDoc subcontextName = "testCtxChangeFollowedByBindingChange";
525         ctx1 = ctx1.createSubcontext(subcontextName);
526         ctx1.putString("b1","b1");
527
528         ctx2 = ctx2.createSubcontext(subcontextName);
529         ctx2.putString("b2","b2");
530
531         ctx3 = ctx3.createSubcontext(subcontextName);
532         ctx3.putString("b3","b3");
533
534         Context hard = rootCtx.getSubcontext(subcontextName);
535         assertNotNull(hard);
536
537         TestContextListener l = new TestContextListener();
538         rootCtx.addContextListener(l);
539
540         ctx1 = ctx1.getParentContext();
541         assertNotNull(ctx1);
542
543         System.setProperty("test1", "test1");
544         ctx1.destroySubcontext(subcontextName);
545         System.setProperty("test1", "");
546
547         assertEquals(1,l.b.size());
548         assertEquals(1,l.all.size());
549     }
550
551     public void testCtxChangeNotFollowedByBindingChange() throws Exception JavaDoc {
552         Context rootCtx = getMergeContext();
553
554         Context ctx1 = getActiveDelegate ();
555         Context ctx2 = getReadOnlyDelegate1 ();
556         Context ctx3 = getReadOnlyDelegate2 ();
557
558
559         String JavaDoc subcontextName = "testCtxChangeNotFollowedByBindingChange";
560         ctx1 = ctx1.createSubcontext(subcontextName);
561         ctx1.putString("b1","b1");
562
563         ctx2 = ctx2.createSubcontext(subcontextName);
564         ctx2.putString("b2","b2");
565
566         ctx3 = ctx3.createSubcontext(subcontextName);
567         ctx3.putString("b3","b3");
568
569         Context hard = rootCtx.getSubcontext(subcontextName);
570         assertNotNull(hard);
571
572         TestContextListener l = new TestContextListener();
573         rootCtx.addContextListener(l);
574
575         hard = hard.getParentContext();
576         assertNotNull(hard);
577
578         System.setProperty("test1", "test1");
579         hard.destroySubcontext(subcontextName);
580         System.setProperty("test1", "");
581
582         assertEquals(0,l.b.size()); //OK
583
assertEquals(1,l.all.size());
584     }
585
586     public void testAttributes () throws Exception JavaDoc {
587         String JavaDoc bindingName = "b";
588         String JavaDoc attributeName = "a";
589
590         Context rootCtx = getMergeContext();
591
592         Context ctx1 = getActiveDelegate ();
593         Context ctx2 = getReadOnlyDelegate1 ();
594         Context ctx3 = getReadOnlyDelegate2 ();
595
596         String JavaDoc subcontextName = "testAttributes";
597         ctx1 = ctx1.createSubcontext(subcontextName);
598         ctx1.putString(bindingName, bindingName);
599         ctx2 = ctx2.createSubcontext(subcontextName);
600         ctx2.putString(bindingName, bindingName);
601         ctx3 = ctx3.createSubcontext(subcontextName);
602
603         String JavaDoc subcontextName1 = "subcontextName1";
604         ctx1 = ctx1.createSubcontext(subcontextName1);
605         ctx1.putString(bindingName, bindingName);
606         ctx2 = ctx2.createSubcontext(subcontextName1);
607         ctx2.putString(bindingName, bindingName);
608         ctx3 = ctx3.createSubcontext(subcontextName1);
609
610         Context test = rootCtx.getSubcontext("/"+subcontextName + "/" + subcontextName1);
611
612         TestContextListener testL = new TestContextListener();
613         TestContextListener rootL = new TestContextListener();
614         test.addContextListener(testL);
615         rootCtx.addContextListener(rootL);
616
617
618         ctx1.setAttribute(null, attributeName, attributeName);
619         ctx1.setAttribute(bindingName, attributeName, attributeName);
620         assertEquals(2, testL.a.size());
621         assertEquals(2, testL.all.size());
622         assertEquals(2, rootL.a.size());
623         assertEquals(2, rootL.all.size());
624
625         ctx2.setAttribute(null, attributeName, attributeName);
626         ctx2.setAttribute(bindingName, attributeName, attributeName);
627         assertEquals(2, testL.a.size());
628         assertEquals(2, testL.all.size());
629         assertEquals(2, rootL.a.size());
630         assertEquals(2, rootL.all.size());
631
632
633         ctx1 = ctx1.getParentContext();
634         ctx1.setAttribute(null, attributeName, attributeName);
635         ctx1.setAttribute(bindingName, attributeName, attributeName);
636         assertEquals(2, testL.a.size());
637         assertEquals(2, testL.all.size());
638         assertEquals(4, rootL.a.size());
639         assertEquals(4, rootL.all.size());
640
641         ctx2 = ctx2.getParentContext();
642         ctx2.setAttribute(null, attributeName, attributeName);
643         ctx2.setAttribute(bindingName, attributeName, attributeName);
644         assertEquals(2, testL.a.size());
645         assertEquals(2, testL.all.size());
646         assertEquals(4, rootL.a.size());
647         assertEquals(4, rootL.all.size());
648     }
649
650     public void testAttributes2 () throws Exception JavaDoc {
651         String JavaDoc bindingName = "b";
652         String JavaDoc attributeName = "a";
653
654         Context rootCtx = getMergeContext();
655
656         Context ctx1 = getActiveDelegate ();
657         Context ctx2 = getReadOnlyDelegate1 ();
658         Context ctx3 = getReadOnlyDelegate2 ();
659
660         String JavaDoc subcontextName = "testAttributes2";
661         ctx1 = ctx1.createSubcontext(subcontextName);
662         ctx1.putString(bindingName, bindingName);
663         ctx2 = ctx2.createSubcontext(subcontextName);
664         ctx2.putString(bindingName, bindingName);
665         ctx3 = ctx3.createSubcontext(subcontextName);
666
667         String JavaDoc subcontextName1 = "subcontextName1";
668         ctx1 = ctx1.createSubcontext(subcontextName1);
669         ctx1.putString(bindingName, bindingName);
670         ctx2 = ctx2.createSubcontext(subcontextName1);
671         ctx2.putString(bindingName, bindingName);
672         ctx3 = ctx3.createSubcontext(subcontextName1);
673
674         Context test = rootCtx.getSubcontext("/"+subcontextName + "/" + subcontextName1);
675
676         TestContextListener testL = new TestContextListener();
677         TestContextListener rootL = new TestContextListener();
678         test.addContextListener(testL);
679         rootCtx.addContextListener(rootL);
680
681         ctx2.setAttribute(null, attributeName, attributeName);
682         ctx2.setAttribute(bindingName, attributeName, attributeName);
683         ctx3.setAttribute(null, attributeName, attributeName);
684         ctx3.setAttribute(bindingName, attributeName, attributeName);
685         assertEquals("defVal",test.getAttribute(bindingName, attributeName, "defVal"));
686         assertEquals("defVal",ctx3.getAttribute(bindingName, attributeName, "defVal"));
687
688         assertEquals(1, testL.a.size());
689         assertEquals(1, testL.all.size());
690         assertEquals(1, rootL.a.size());
691         assertEquals(1, rootL.all.size());
692
693         ctx1.setAttribute(null, attributeName, attributeName);
694         ctx1.setAttribute(bindingName, attributeName, attributeName);
695         assertEquals(3, testL.a.size());
696         assertEquals(3, testL.all.size());
697         assertEquals(3, rootL.a.size());
698         assertEquals(3, rootL.all.size());
699
700
701         ctx2 = ctx2.getParentContext();
702         ctx2.setAttribute(null, attributeName, attributeName);
703         ctx2.setAttribute(bindingName, attributeName, attributeName);
704         ctx3.setAttribute(null, attributeName, attributeName);
705         ctx3.setAttribute(bindingName, attributeName, attributeName);
706         assertEquals("defVal",test.getParentContext().getAttribute(bindingName, attributeName, "defVal"));
707         assertEquals("defVal",ctx3.getParentContext().getAttribute(bindingName, attributeName, "defVal"));
708         assertEquals(3, testL.a.size());
709         assertEquals(3, testL.all.size());
710         assertEquals(4, rootL.a.size());
711         assertEquals(4, rootL.all.size());
712
713         ctx1 = ctx1.getParentContext();
714         ctx1.setAttribute(null, attributeName, attributeName);
715         ctx1.setAttribute(bindingName, attributeName, attributeName);
716         assertEquals(3, testL.a.size());
717         assertEquals(3, testL.all.size());
718         assertEquals(6, rootL.a.size());
719         assertEquals(6, rootL.all.size());
720     }
721
722     public void testAttributes3 () throws Exception JavaDoc {
723         String JavaDoc bindingName = "b";
724         String JavaDoc attributeName = "a";
725
726         Context rootCtx = getMergeContext();
727
728         Context ctx1 = getActiveDelegate ();
729         Context ctx2 = getReadOnlyDelegate1 ();
730         Context ctx3 = getReadOnlyDelegate2 ();
731
732         String JavaDoc subcontextName = "testAttributes3";
733         ctx1 = ctx1.createSubcontext(subcontextName);
734         ctx1.putString(bindingName, bindingName);
735         ctx2 = ctx2.createSubcontext(subcontextName);
736         ctx2.putString(bindingName, bindingName);
737         ctx3 = ctx3.createSubcontext(subcontextName);
738
739         String JavaDoc subcontextName1 = "subcontextName1";
740         ctx1 = ctx1.createSubcontext(subcontextName1);
741         ctx1.putString(bindingName, bindingName);
742         ctx2 = ctx2.createSubcontext(subcontextName1);
743         ctx2.putString(bindingName, bindingName);
744         ctx3 = ctx3.createSubcontext(subcontextName1);
745
746         Context test = rootCtx.getSubcontext("/"+subcontextName + "/" + subcontextName1);
747
748         TestContextListener testL = new TestContextListener();
749         TestContextListener rootL = new TestContextListener();
750         test.addContextListener(testL);
751         rootCtx.addContextListener(rootL);
752
753         ctx1.setAttribute(bindingName, attributeName, attributeName);
754         assertEquals(1, testL.a.size());
755         assertEquals(1, testL.all.size());
756         assertEquals(1, rootL.a.size());
757         assertEquals(1, rootL.all.size());
758
759         ctx1.getParentContext() .setAttribute(bindingName, attributeName, attributeName);
760         assertEquals(1, testL.a.size());
761         assertEquals(1, testL.all.size());
762         assertEquals(2, rootL.a.size());
763         assertEquals(2, rootL.all.size());
764
765     }
766
767     public void testCopyAttributes () throws Exception JavaDoc {
768         String JavaDoc bindingName = "b";
769         String JavaDoc attributeName = "a";
770
771         Context rootCtx = getMergeContext();
772
773         Context ctx1 = getActiveDelegate ();
774         Context ctx2 = getReadOnlyDelegate1 ();
775         Context ctx3 = getReadOnlyDelegate2 ();
776
777         String JavaDoc subcontextName = "testCopyAttributes";
778         ctx1 = ctx1.createSubcontext(subcontextName);
779         ctx2 = ctx2.createSubcontext(subcontextName);
780         ctx2.putString(bindingName, bindingName);
781         ctx2.setAttribute(bindingName, attributeName, attributeName);
782         ctx3 = ctx3.createSubcontext(subcontextName);
783
784         Context test = rootCtx.getSubcontext("/"+subcontextName);
785         assertEquals(attributeName, test.getAttribute(bindingName, attributeName, "defVal"));
786
787         test.putString(bindingName, bindingName);
788         assertEquals(attributeName, test.getAttribute(bindingName, attributeName, "defVal"));
789
790         test.putString(bindingName, null);
791         assertEquals("defVal", test.getObject(bindingName, "defVal"));
792         assertEquals("defVal", test.getAttribute(bindingName, attributeName, "defVal"));
793
794         test.putString(bindingName, bindingName);
795         assertEquals(bindingName, test.getObject(bindingName, "defVal"));
796         assertEquals("defVal", test.getAttribute(bindingName, attributeName, "defVal"));
797     }
798
799     public void testNotCopyAttributes () throws Exception JavaDoc {
800         String JavaDoc bindingName = "b";
801         String JavaDoc attributeName = "a";
802
803         Context rootCtx = getMergeContext();
804
805         Context ctx1 = getActiveDelegate ();
806         Context ctx2 = getReadOnlyDelegate1 ();
807         Context ctx3 = getReadOnlyDelegate2 ();
808
809         String JavaDoc subcontextName = "testNotCopyAttributes";
810         ctx1 = ctx1.createSubcontext(subcontextName);
811         ctx2 = ctx2.createSubcontext(subcontextName);
812         ctx2.putString(bindingName, bindingName);
813         ctx2.setAttribute(bindingName, attributeName, attributeName);
814         ctx3 = ctx3.createSubcontext(subcontextName);
815
816         Context test = rootCtx.getSubcontext("/"+subcontextName);
817         assertEquals(attributeName, test.getAttribute(bindingName, attributeName, "defVal"));
818
819         ctx1.putString(bindingName, bindingName);
820         assertEquals("defVal", test.getAttribute(bindingName, attributeName, "defVal"));
821
822         ctx1.putString(bindingName, null);
823         assertEquals(bindingName, test.getObject(bindingName, "defVal"));
824         assertEquals(attributeName, test.getAttribute(bindingName, attributeName, "defVal"));
825
826         ctx1.putString(bindingName, bindingName);
827         assertEquals("defVal", test.getAttribute(bindingName, attributeName, "defVal"));
828     }
829
830     public void testNotChangedAttributeNoEvent () throws Exception JavaDoc {
831         Context rootCtx = getMergeContext();
832
833         Context ctx1 = getActiveDelegate ();
834         Context ctx2 = getReadOnlyDelegate1 ();
835         Context ctx3 = getReadOnlyDelegate2 ();
836
837         String JavaDoc subcontextName = "testNotChangedAttributeNoEvent";
838         ctx1 = ctx1.createSubcontext(subcontextName);
839         ctx2 = ctx2.createSubcontext(subcontextName);
840         ctx3 = ctx3.createSubcontext(subcontextName);
841
842         String JavaDoc subcontextName1 = "subcontextName1";
843         ctx1 = ctx1.createSubcontext(subcontextName1);
844         ctx2 = ctx2.createSubcontext(subcontextName1);
845         ctx3 = ctx3.createSubcontext(subcontextName1);
846
847         Context test = rootCtx.getSubcontext("/"+subcontextName + "/" + subcontextName1);
848         String JavaDoc bindingName = "b";
849         String JavaDoc attributeName = "b";
850
851         ctx1 = ctx1.getParentContext();
852         ctx1.putString(bindingName, bindingName);
853         ctx2.putString(bindingName, bindingName);
854
855         TestContextListener testL = new TestContextListener();
856         TestContextListener rootL = new TestContextListener();
857         test.addContextListener(testL);
858         rootCtx.addContextListener(rootL);
859
860
861
862         System.setProperty("kst","kst");
863         ctx2.setAttribute(null, attributeName, attributeName);
864         ctx2.setAttribute(bindingName, attributeName, attributeName);
865         System.setProperty("kst","");
866         assertEquals(2, testL.a.size());
867         assertEquals(2, testL.all.size());
868         assertEquals(2, rootL.a.size());
869         assertEquals(2, rootL.all.size());
870
871         ctx2.setAttribute(null, attributeName, attributeName);
872         ctx2.setAttribute(bindingName, attributeName, attributeName);
873
874         assertEquals(2, testL.a.size());
875         assertEquals(2, testL.all.size());
876         assertEquals(2, rootL.a.size());
877         assertEquals(2, rootL.all.size());
878
879
880         ctx1.setAttribute(null, attributeName, attributeName);
881         ctx1.setAttribute(bindingName, attributeName, attributeName);
882         assertEquals(2, testL.a.size());
883         assertEquals(2, testL.all.size());
884         assertEquals(4, rootL.a.size());
885         assertEquals(4, rootL.all.size());
886
887         ctx1.setAttribute(null, attributeName, attributeName);
888         ctx1.setAttribute(bindingName, attributeName, attributeName);
889         assertEquals(2, testL.a.size());
890         assertEquals(2, testL.all.size());
891         assertEquals(4, rootL.a.size());
892         assertEquals(4, rootL.all.size());
893     }
894
895     public void testNotChangedAttributeNoEvent2 () throws Exception JavaDoc {
896         Context rootCtx = getMergeContext();
897
898         Context ctx1 = getActiveDelegate ();
899         Context ctx2 = getReadOnlyDelegate1 ();
900         Context ctx3 = getReadOnlyDelegate2 ();
901
902         String JavaDoc subcontextName = "testNotChangedAttributeNoEvent2";
903         String JavaDoc attributeName = "a";
904         String JavaDoc bindingName = "a";
905
906         ctx1 = ctx1.createSubcontext(subcontextName);
907
908         ctx2.putString(bindingName, bindingName);
909         ctx2.setAttribute(null, attributeName, attributeName);
910         ctx2.setAttribute(bindingName, attributeName, attributeName);
911
912         ctx2 = ctx2.createSubcontext(subcontextName);
913         ctx3 = ctx3.createSubcontext(subcontextName);
914
915         String JavaDoc subcontextName1 = "subcontextName1";
916         ctx1 = ctx1.createSubcontext(subcontextName1);
917         ctx1.putString(bindingName, bindingName);
918         ctx1.setAttribute(null, attributeName, attributeName);
919         ctx1.setAttribute(bindingName, attributeName, attributeName);
920
921         ctx2 = ctx2.createSubcontext(subcontextName1);
922         ctx3 = ctx3.createSubcontext(subcontextName1);
923
924         Context test = rootCtx.getSubcontext("/"+subcontextName + "/" + subcontextName1);
925
926         TestContextListener testL = new TestContextListener();
927         TestContextListener rootL = new TestContextListener();
928         test.addContextListener(testL);
929         rootCtx.addContextListener(rootL);
930
931
932         test.setAttribute(null, attributeName, attributeName);
933         test.setAttribute(bindingName, attributeName, attributeName);
934         assertEquals(0, testL.a.size());
935         assertEquals(0, testL.all.size());
936         assertEquals(0, rootL.a.size());
937         assertEquals(0, rootL.all.size());
938
939         rootCtx.setAttribute(null, attributeName, attributeName);
940         rootCtx.setAttribute(bindingName, attributeName, attributeName);
941         assertEquals(0, testL.a.size());
942         assertEquals(0, testL.all.size());
943         assertEquals(0, rootL.a.size());
944         assertEquals(0, rootL.all.size());
945     }
946
947
948     public void testCtxChangeFollowedByAttributeChange() throws Exception JavaDoc {
949         Context rootCtx = getMergeContext();
950
951         Context ctx1 = getActiveDelegate ();
952         Context ctx2 = getReadOnlyDelegate1 ();
953         Context ctx3 = getReadOnlyDelegate2 ();
954
955
956         String JavaDoc subcontextName = "testCtxChangeFollowedByAttributeChange";
957         ctx1 = ctx1.createSubcontext(subcontextName);
958         ctx1.setAttribute(null, "b1","b1");
959
960         ctx2 = ctx2.createSubcontext(subcontextName);
961         ctx2.setAttribute(null, "b2","b2");
962
963         ctx3 = ctx3.createSubcontext(subcontextName);
964         ctx3.setAttribute(null, "b3","b3");
965
966         Context hard = rootCtx.getSubcontext(subcontextName);
967         assertNotNull(hard);
968
969         TestContextListener l = new TestContextListener();
970         rootCtx.addContextListener(l);
971
972         ctx1 = ctx1.getParentContext();
973         assertNotNull(ctx1);
974
975
976         System.setProperty("key", "key");
977         System.setProperty("test1", "test1");
978         ctx1.destroySubcontext(subcontextName);
979         System.setProperty("test1", "");
980         System.setProperty("key", "");
981
982         assertEquals(1,l.a.size());
983         assertEquals(1,l.all.size());
984     }
985
986     public void testCtxChangeNotFollowedByAttributeChange() throws Exception JavaDoc {
987         Context rootCtx = getMergeContext();
988
989         Context ctx1 = getActiveDelegate ();
990         Context ctx2 = getReadOnlyDelegate1 ();
991         Context ctx3 = getReadOnlyDelegate2 ();
992
993         String JavaDoc subcontextName = "testCtxChangeNotFollowedByAttributeChange";
994         ctx1 = ctx1.createSubcontext(subcontextName);
995         ctx1.setAttribute(null, "b1","b1");
996
997         ctx2 = ctx2.createSubcontext(subcontextName);
998         ctx2.setAttribute(null, "b2","b2");
999
1000        ctx3 = ctx3.createSubcontext(subcontextName);
1001        ctx3.setAttribute(null, "b3","b3");
1002
1003        Context hard = rootCtx.getSubcontext(subcontextName);
1004        assertNotNull(hard);
1005
1006        TestContextListener l = new TestContextListener();
1007        rootCtx.addContextListener(l);
1008
1009        hard = hard.getParentContext();
1010        assertNotNull(hard);
1011
1012
1013        System.setProperty("test1", "test1");
1014        hard.destroySubcontext(subcontextName);
1015        System.setProperty("test1", "");
1016
1017        assertEquals(0,l.a.size());
1018        assertEquals(1,l.s.size());
1019        assertEquals(1,l.all.size());
1020    }
1021
1022    private static class TestContextListener implements ContextListener {
1023
1024        List s = new ArrayList();
1025        List b = new ArrayList();
1026        List a = new ArrayList();
1027
1028        List all = new ArrayList();
1029
1030        public void subcontextChanged(SubcontextEvent evt) {
1031            s.add(evt);
1032            all.add(evt);
1033            if (evt.getType() == SubcontextEvent.SUBCONTEXT_ADDED) {
1034                assertNotNull(evt.getContext().getSubcontext(evt.getSubcontextName()));
1035                Collection names = evt.getContext().getSubcontextNames();
1036                assertTrue(names.contains(evt.getSubcontextName()));
1037            }
1038
1039            if (evt.getType() == SubcontextEvent.SUBCONTEXT_REMOVED) {
1040                assertNull(evt.getContext().getSubcontext(evt.getSubcontextName()));
1041                Collection names = evt.getContext().getSubcontextNames();
1042                assertFalse(names.contains(evt.getSubcontextName()));
1043            }
1044        }
1045
1046        public void bindingChanged(BindingEvent evt) {
1047            b.add(evt);
1048            all.add(evt);
1049            if (evt.getType() == BindingEvent.BINDING_ADDED || evt.getType() == BindingEvent.BINDING_MODIFIED) {
1050
1051                if ("tst".equals(System.getProperty("tst"))) {
1052                    System.out.println("ERR: " + evt + " value: "+ evt.getContext().getObject(evt.getBindingName(), null));
1053                }
1054
1055                assertNotNull(evt.getContext().getObject(evt.getBindingName(), null));
1056                Collection names = evt.getContext().getBindingNames();
1057                assertTrue(names.contains(evt.getBindingName()));
1058            }
1059
1060            if (evt.getType() == BindingEvent.BINDING_REMOVED) {
1061                assertEquals("defVal",evt.getContext().getObject(evt.getBindingName(), "defVal"));
1062                Collection names = evt.getContext().getBindingNames();
1063                assertFalse(names.contains(evt.getBindingName()));
1064            }
1065        }
1066
1067        public void attributeChanged(AttributeEvent evt) {
1068            a.add(evt);
1069            all.add(evt);
1070
1071            if (evt.getType() == AttributeEvent.ATTRIBUTE_ADDED || evt.getType() == AttributeEvent.ATTRIBUTE_MODIFIED) {
1072                assertNotNull(evt.getContext().getAttribute(evt.getBindingName(), evt.getAttributeName(), null));
1073                Collection names = evt.getContext().getAttributeNames(evt.getBindingName());
1074                assertTrue(names.contains(evt.getAttributeName()));
1075            }
1076
1077            if (evt.getType() == AttributeEvent.ATTRIBUTE_REMOVED) {
1078                assertEquals("defVal", evt.getContext().getAttribute(evt.getBindingName(),evt.getAttributeName(), "defVal"));
1079                Collection names = evt.getContext().getAttributeNames(evt.getBindingName());
1080                assertFalse(names.contains(evt.getAttributeName()));
1081            }
1082        }
1083    }
1084
1085}
1086
Popular Tags