1 19 20 package org.netbeans.core.registry.enabledisabletest; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.*; 24 import org.netbeans.core.registry.ResettableContextImpl; 25 import org.netbeans.core.registry.TestMFS; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.junit.NbTestSuite; 28 import org.netbeans.spi.registry.BasicContext; 29 import org.netbeans.spi.registry.SpiUtils; 30 import org.openide.filesystems.FileSystem; 31 import org.openide.filesystems.LocalFileSystem; 32 import org.openide.filesystems.XMLFileSystem; 33 import org.openide.util.Lookup; 34 35 import java.lang.reflect.Constructor ; 36 import java.net.URL ; 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.Iterator ; 40 41 45 public class EnableDisableTest extends NbTestCase { 46 47 public EnableDisableTest(String name) { 48 super (name); 49 } 50 51 public static void main(String [] args) { 52 TestRunner.run(new NbTestSuite(EnableDisableTest.class)); 53 } 54 55 protected void setUp () throws Exception { 56 clearWorkDir(); 57 } 58 59 public void testEnableDisable() throws Exception { 60 LocalFileSystem lfs = new LocalFileSystem(); 61 lfs.setRootDirectory(getWorkDir()); 62 63 URL u1 = getClass().getResource("data/module_layer.xml"); 64 65 FileSystem xfs1 = new XMLFileSystem( u1 ); 66 FileSystem mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } ); 67 68 BasicContext root = new ResettableContextImpl(mfs.getRoot(), null, xfs1, lfs); 69 Context rootContext = SpiUtils.createContext(root); 70 71 Context ctx = rootContext.getSubcontext("module"); 72 assertEquals("There cannot be more than 2 items because no modules are installed.", 2, ctx.getBindingNames().size()); 73 74 FileSystemListener l = new FileSystemListener(); 75 FileSystemListener l2 = new FileSystemListener(); 76 ctx.addContextListener(l); 77 rootContext.addContextListener(l2); 78 79 ModuleUtils.DEFAULT.install(); 81 ModuleUtils.DEFAULT.enableBookModule(true); 82 assertEquals("Two events must be fired", 2, l.b.size()); 83 assertEquals("Two events must be fired", 2, l.all.size()); 84 assertEquals("Two events must be fired", 2, l2.b.size()); 85 assertEquals("Two events must be fired", 2, l2.all.size()); 86 assertEquals("There must be 4 items", 4, ctx.getBindingNames().size()); 87 88 assertEquals("Objects must be equal", createBook("N/A", "N/A"), ctx.getObject("book1", null)); 89 assertEquals("Objects must be equal", createBook("Jesse, Tim & others", "NetBeans Definitive Guide"), ctx.getObject("book2", null)); 90 91 Object o = createBook("David Scott", "Zen"); 92 ctx.putObject("newbook", o); 93 Collection oldBindingNames = ctx.getBindingNames(); 94 assertEquals("There must be 5 items", 5, oldBindingNames.size()); 95 96 l.reset(); 97 l2.reset(); 98 ModuleUtils.DEFAULT.enableBookModule(false); 99 assertEquals("Three events must be fired", 3, l.b.size()); 100 assertEquals("Three events must be fired", 3, l.all.size()); 101 assertEquals("Three events must be fired", 3, l2.b.size()); 102 assertEquals("Three events must be fired", 3, l2.all.size()); 103 Collection newBindingNames = ctx.getBindingNames(); 104 assertEquals("There must be 2 items", 2, newBindingNames.size()); 105 106 oldBindingNames.removeAll(newBindingNames); 107 for (Iterator iterator = oldBindingNames.iterator(); iterator.hasNext();) { 108 String name = (String ) iterator.next(); 109 assertFalse(newBindingNames.contains(name)); 110 assertNull(ctx.getObject(name, null)); 111 } 112 113 l.reset(); 114 l2.reset(); 115 ModuleUtils.DEFAULT.enableBookModule(true); 116 assertEquals("Three events must be fired", 3, l.b.size()); 117 assertEquals("Three events must be fired", 3, l.all.size()); 118 assertEquals("Three events must be fired", 3, l2.b.size()); 119 assertEquals("Three events must be fired", 3, l2.all.size()); 120 assertEquals("There must be 5 items", 5, ctx.getBindingNames().size()); 121 122 l.reset(); 123 l2.reset(); 124 ModuleUtils.DEFAULT.enableCDModule(true); 125 assertEquals("Two events must be fired", 2, l.b.size()); 126 assertEquals("Two events must be fired", 2, l.all.size()); 127 assertEquals("Two events must be fired", 2, l2.b.size()); 128 assertEquals("Two events must be fired", 2, l2.all.size()); 129 assertEquals("There must be 7 items", 7, ctx.getBindingNames().size()); 130 131 assertEquals("Objects must be equal", createCD("Philip Glass", "The Hours"), ctx.getObject("cd1", null)); 132 assertEquals("Objects must be equal", createCD("Philip Glass", "Music from The Screens"), ctx.getObject("cd2", null)); 133 134 o = createCD("Philip Glass", "Naqoyqatsi"); 135 ctx.putObject("newcd", o); 136 assertEquals("There must be 8 items", 8, ctx.getBindingNames().size()); 137 138 l.reset(); 139 l2.reset(); 140 ModuleUtils.DEFAULT.enableCDModule(false); 141 142 assertEquals("Three events must be fired", 3, l.b.size()); 143 assertEquals("Three events must be fired", 3, l.all.size()); 144 assertEquals("Three events must be fired", 3, l2.b.size()); 145 assertEquals("Three events must be fired", 3, l2.all.size()); 146 assertEquals("There must be 5 items", 5, ctx.getBindingNames().size()); 147 148 l.reset(); 149 l2.reset(); 150 ModuleUtils.DEFAULT.enableCDModule(true); 151 assertEquals("Three events must be fired", 3, l.b.size()); 152 assertEquals("Three events must be fired", 3, l.all.size()); 153 assertEquals("Three events must be fired", 3, l2.b.size()); 154 assertEquals("Three events must be fired", 3, l2.all.size()); 155 assertEquals("There must be 8 items", 8, ctx.getBindingNames().size()); 156 157 } 158 159 private Object createBook(String author, String title) throws Exception { 160 Class clazz = findClass("org.bookmodule.Book"); 161 assertTrue("found class must be owned by its module "+clazz.getClassLoader(), ModuleUtils.DEFAULT.getBookModule().owns(clazz)); 162 Constructor c = clazz.getConstructor(new Class []{String .class, String .class}); 163 return c.newInstance(new Object []{author, title}); 164 } 165 166 private Object createCD(String artits, String album) throws Exception { 167 Class clazz = findClass("org.cdmodule.CD"); 168 assertTrue("found class must be owned by its module "+clazz.getClassLoader(), ModuleUtils.DEFAULT.getCDModule().owns(clazz)); 169 Constructor c = clazz.getConstructor(new Class []{String .class, String .class}); 170 return c.newInstance(new Object []{artits, album}); 171 } 172 173 private Class findClass(String name) throws ClassNotFoundException { 174 ClassLoader c = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 175 if (c == null) { 176 return Class.forName(name, true, null); 177 } else { 178 return Class.forName(name, true, c); 179 } 180 } 181 182 public static class FileSystemListener implements ContextListener { 183 184 ArrayList s; 185 ArrayList a; 186 ArrayList b; 187 ArrayList all; 188 String error = null; 189 190 public FileSystemListener() { 191 reset(); 192 } 193 194 public void reset() { 195 s = new ArrayList (); 196 b = new ArrayList (); 197 a = new ArrayList (); 198 all = new ArrayList (); 199 error = null; 200 } 201 202 public void subcontextChanged(SubcontextEvent evt) { 203 s.add(evt); 204 all.add(evt); 205 if (evt.getType() == SubcontextEvent.SUBCONTEXT_ADDED) { 206 if (evt.getContext().getSubcontext(evt.getSubcontextName()) == null) { 207 error = "Added subcontext must already exist: "+evt; 208 } 209 } 210 if (evt.getType() == SubcontextEvent.SUBCONTEXT_REMOVED) { 211 if (evt.getContext().getSubcontext(evt.getSubcontextName()) != null) { 212 error = "Removed subcontext must be already deleted: "+evt; 213 } 214 } 215 } 216 217 public void bindingChanged(BindingEvent evt) { 218 b.add(evt); 219 all.add(evt); 220 if (evt.getType() == BindingEvent.BINDING_ADDED || evt.getType() == BindingEvent.BINDING_MODIFIED) { 221 if (evt.getContext().getObject(evt.getBindingName(), null) == null) { 222 error = "Added or modified binding cannot have null value: "+evt; 223 } 224 } 225 if (evt.getType() == BindingEvent.BINDING_REMOVED) { 226 if (!evt.getContext().getObject(evt.getBindingName(), "abcd").equals("abcd")) { 227 error = "Removed binding must have null value: "+evt; 228 } 229 } 230 } 231 232 public void attributeChanged(AttributeEvent evt) { 233 a.add(evt); 234 all.add(evt); 235 if (evt.getType() == AttributeEvent.ATTRIBUTE_ADDED || evt.getType() == AttributeEvent.ATTRIBUTE_MODIFIED) { 236 if (evt.getContext().getAttribute(evt.getBindingName(), evt.getAttributeName(), null) == null) { 237 error = "Added or modified attribute cannot have null value: "+evt; 238 } 239 } 240 if (evt.getType() == AttributeEvent.ATTRIBUTE_REMOVED) { 241 if (!evt.getContext().getAttribute(evt.getBindingName(), evt.getAttributeName(), "abcd").equals("abcd")) { 242 error = "Removed attribute must have null value: "+evt; 243 } 244 } 245 } 246 247 } 248 249 } 250 | Popular Tags |