KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > enabledisabletest > EnableDisableTest


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.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 JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  *
43  * @author David Konecny
44  */

45 public class EnableDisableTest extends NbTestCase {
46     
47     public EnableDisableTest(String JavaDoc name) {
48         super (name);
49     }
50     
51     public static void main(String JavaDoc[] args) {
52         TestRunner.run(new NbTestSuite(EnableDisableTest.class));
53     }
54     
55     protected void setUp () throws Exception JavaDoc {
56         clearWorkDir();
57     }
58     
59     public void testEnableDisable() throws Exception JavaDoc {
60         LocalFileSystem lfs = new LocalFileSystem();
61         lfs.setRootDirectory(getWorkDir());
62
63         URL JavaDoc 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         // this should fire context change that two bindings were added
80
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 JavaDoc o = createBook("David Scott", "Zen");
92         ctx.putObject("newbook", o);
93         Collection JavaDoc 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 JavaDoc newBindingNames = ctx.getBindingNames();
104         assertEquals("There must be 2 items", 2, newBindingNames.size());
105
106         oldBindingNames.removeAll(newBindingNames);
107         for (Iterator JavaDoc iterator = oldBindingNames.iterator(); iterator.hasNext();) {
108             String JavaDoc name = (String JavaDoc) 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 JavaDoc createBook(String JavaDoc author, String JavaDoc title) throws Exception JavaDoc {
160         Class JavaDoc clazz = findClass("org.bookmodule.Book");
161         assertTrue("found class must be owned by its module "+clazz.getClassLoader(), ModuleUtils.DEFAULT.getBookModule().owns(clazz));
162         Constructor JavaDoc c = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class});
163         return c.newInstance(new Object JavaDoc[]{author, title});
164     }
165     
166     private Object JavaDoc createCD(String JavaDoc artits, String JavaDoc album) throws Exception JavaDoc {
167         Class JavaDoc clazz = findClass("org.cdmodule.CD");
168         assertTrue("found class must be owned by its module "+clazz.getClassLoader(), ModuleUtils.DEFAULT.getCDModule().owns(clazz));
169         Constructor JavaDoc c = clazz.getConstructor(new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class});
170         return c.newInstance(new Object JavaDoc[]{artits, album});
171     }
172     
173     private Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
174         ClassLoader JavaDoc c = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.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 JavaDoc s;
185         ArrayList JavaDoc a;
186         ArrayList JavaDoc b;
187         ArrayList JavaDoc all;
188         String JavaDoc error = null;
189         
190         public FileSystemListener() {
191             reset();
192         }
193         
194         public void reset() {
195             s = new ArrayList JavaDoc();
196             b = new ArrayList JavaDoc();
197             a = new ArrayList JavaDoc();
198             all = new ArrayList JavaDoc();
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