KickJava   Java API By Example, From Geeks To Geeks.

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


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.BindingEvent;
23 import org.netbeans.api.registry.Context;
24 import org.netbeans.api.registry.ContextAdapter;
25 import org.netbeans.api.registry.SubcontextEvent;
26 import org.netbeans.junit.NbTestCase;
27 import org.netbeans.junit.NbTestSuite;
28 import org.netbeans.modules.registry.ApiContextFactory;
29 import org.netbeans.spi.registry.BasicContext;
30 import org.netbeans.spi.registry.MergedContextProvider;
31 import org.netbeans.spi.registry.SpiUtils;
32 import org.openide.modules.ModuleInfo;
33 import org.openide.util.Lookup;
34
35 import java.beans.PropertyChangeListener JavaDoc;
36 import java.beans.PropertyChangeSupport JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 public class SetDelegatesTest extends NbTestCase{
41     public SetDelegatesTest (String JavaDoc name) {
42         super (name);
43     }
44
45     public static void main(String JavaDoc[] args) {
46         TestRunner.run(new NbTestSuite(SetDelegatesTest.class));
47     }
48
49     protected void setUp () throws Exception JavaDoc {
50         Lookup.getDefault().lookup(ModuleInfo.class);
51     }
52
53     public void testSetDelegates () throws Exception JavaDoc {
54         Context defCtx = Context.getDefault();
55         Context delegCtx1 = defCtx.createSubcontext("first");
56         Context delegCtx2 = defCtx.createSubcontext("second");
57         Context delegCtx3 = defCtx.createSubcontext("third");
58
59         delegCtx1.createSubcontext("A");
60         delegCtx2.createSubcontext("B");
61         delegCtx3.createSubcontext("C");
62
63         MergeContextProviderImpl mergeProvider =
64                 new MergeContextProviderImpl (new Context[] {delegCtx1, delegCtx2, delegCtx3});
65         Context rootCtx = SpiUtils.createContext(SpiUtils.merge(mergeProvider));
66
67         assertTrue(null != rootCtx.getSubcontext("A"));
68         assertTrue(null != rootCtx.getSubcontext("B"));
69         assertTrue (null != rootCtx.getSubcontext("C"));
70
71         final List JavaDoc subEvtList = new ArrayList JavaDoc ();
72         final List JavaDoc bindingEvtList = new ArrayList JavaDoc ();
73
74         rootCtx.addContextListener(new ContextAdapter() {
75             public void subcontextChanged(SubcontextEvent evt) {
76                 subEvtList.add(evt);
77             }
78
79             public void bindingChanged(BindingEvent evt) {
80                 bindingEvtList.add(evt);
81             }
82         });
83
84         rootCtx.getSubcontextNames();
85         String JavaDoc binding = "stBinding";
86         delegCtx3.putString(binding, binding);
87         assertEquals(binding,rootCtx.getObject(binding, "defValue"));
88         assertEquals(1, bindingEvtList.size());
89         BindingEvent be = (BindingEvent)bindingEvtList.get(0);
90         assertEquals(BindingEvent.BINDING_ADDED, be.getType());
91         bindingEvtList.clear();
92
93
94         mergeProvider.setDelegates(new Context[] {delegCtx1, delegCtx2});
95         assertEquals("defValue",rootCtx.getObject(binding, "defValue"));
96         assertTrue(null != rootCtx.getSubcontext("A"));
97         assertTrue(null != rootCtx.getSubcontext("B"));
98         assertEquals(null, rootCtx.getSubcontext("C"));
99         assertEquals(1, subEvtList.size());
100         SubcontextEvent se = (SubcontextEvent)subEvtList.get(0);
101         assertEquals(SubcontextEvent.SUBCONTEXT_REMOVED, se.getType());
102         assertEquals(1, bindingEvtList.size());
103         be = (BindingEvent)bindingEvtList.get(0);
104         assertEquals(BindingEvent.BINDING_REMOVED, be.getType());
105         subEvtList.clear();
106         bindingEvtList.clear();
107
108
109         mergeProvider.setDelegates(new Context[] {delegCtx1, delegCtx2, delegCtx3});
110         assertEquals(binding,rootCtx.getObject(binding, "defValue"));
111         assertTrue(null != rootCtx.getSubcontext("A"));
112         assertTrue(null != rootCtx.getSubcontext("B"));
113         assertTrue (null != rootCtx.getSubcontext("C"));
114         assertEquals(1, subEvtList.size());
115         se = (SubcontextEvent)subEvtList.get(0);
116         assertEquals(SubcontextEvent.SUBCONTEXT_ADDED, se.getType());
117         assertEquals(1, bindingEvtList.size());
118         be = (BindingEvent)bindingEvtList.get(0);
119         assertEquals(BindingEvent.BINDING_ADDED, be.getType());
120         subEvtList.clear();
121         bindingEvtList.clear();
122     }
123
124     private static final class MergeContextProviderImpl implements MergedContextProvider {
125         BasicContext[] delegates;
126         PropertyChangeSupport JavaDoc pListenerSuport;
127         public MergeContextProviderImpl (Context[] delegates){
128             pListenerSuport = new PropertyChangeSupport JavaDoc(this);
129             setDelegates(delegates, false);
130         }
131
132         public void setDelegates(Context[] delegates) {
133             setDelegates(delegates, true);
134         }
135
136         private void setDelegates(Context[] delegates, boolean fire) {
137             this.delegates = new BasicContext[delegates.length];
138             for (int i = 0; i < delegates.length; i++) {
139                 this.delegates[i] = ApiContextFactory.DEFAULT.getBasicContext(delegates[i]);
140             }
141             if (fire)
142                 pListenerSuport.firePropertyChange(PROP_DELEGATES, null, null);
143         }
144
145         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
146             pListenerSuport.addPropertyChangeListener(listener);
147         }
148
149         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
150             pListenerSuport.removePropertyChangeListener(listener);
151         }
152
153         public BasicContext[] getDelegates() {
154             return delegates;
155         }
156     }
157 }
158
Popular Tags