1 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 ; 36 import java.beans.PropertyChangeSupport ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 40 public class SetDelegatesTest extends NbTestCase{ 41 public SetDelegatesTest (String name) { 42 super (name); 43 } 44 45 public static void main(String [] args) { 46 TestRunner.run(new NbTestSuite(SetDelegatesTest.class)); 47 } 48 49 protected void setUp () throws Exception { 50 Lookup.getDefault().lookup(ModuleInfo.class); 51 } 52 53 public void testSetDelegates () throws Exception { 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 subEvtList = new ArrayList (); 72 final List bindingEvtList = new ArrayList (); 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 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 pListenerSuport; 127 public MergeContextProviderImpl (Context[] delegates){ 128 pListenerSuport = new PropertyChangeSupport (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 listener) { 146 pListenerSuport.addPropertyChangeListener(listener); 147 } 148 149 public void removePropertyChangeListener(PropertyChangeListener listener) { 150 pListenerSuport.removePropertyChangeListener(listener); 151 } 152 153 public BasicContext[] getDelegates() { 154 return delegates; 155 } 156 } 157 } 158 | Popular Tags |