KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > metainf > ServiceNodeHandlerTest


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.modules.apisupport.project.metainf;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Handler JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ui.OpenProjects;
34 import org.netbeans.junit.NbTestSuite;
35 import org.netbeans.modules.apisupport.project.NbModuleProject;
36 import org.netbeans.modules.apisupport.project.TestBase;
37 import org.netbeans.modules.apisupport.project.metainf.SUtil;
38 import org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler.ServiceRootChildren;
39 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
40 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties;
41 import org.netbeans.spi.project.support.ant.EditableProperties;
42 import org.openide.filesystems.FileLock;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileSystem.AtomicAction;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.nodes.Node;
47 /**
48  *
49  * @author pzajac
50  */

51 public class ServiceNodeHandlerTest extends TestBase {
52     NbModuleProject prj;
53     ServiceNodeHandler nodeHandler;
54     ServiceNodeHandler.ServiceRootNode serviceRootNode;
55     LoggingHandler log;
56     
57     public ServiceNodeHandlerTest(java.lang.String JavaDoc testName) {
58         super(testName);
59     }
60         
61     protected void setUp() throws Exception JavaDoc {
62         super.setUp();
63         log = new LoggingHandler();
64         SUtil.getLogger().setLevel(Level.INFO);
65         SUtil.getLogger().addHandler(log);
66     }
67     
68     private void setUpSimpleSuite() throws Exception JavaDoc {
69         SuiteProject suite = TestBase.generateSuite(getWorkDir(), "suite");
70         EditableProperties ep = suite.getHelper().getProperties("nbproject/platform.properties");
71         ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, CLUSTER_IDE + "," + CLUSTER_ENTERPRISE);
72         suite.getHelper().putProperties("nbproject/platform.properties", ep);
73         ProjectManager.getDefault().saveProject(suite);
74          
75         prj = TestBase.generateSuiteComponent(suite, "prj1");
76         nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class);
77         serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
78     }
79     
80      private void setUpStandaloneModule() throws Exception JavaDoc {
81         prj = TestBase.generateStandaloneModule(getWorkDir(), "prj1");
82         nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class);
83         serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
84      }
85     public void testSuiteWithManyProjects() throws IOException JavaDoc, Exception JavaDoc {
86         SuiteProject suite = TestBase.generateSuite(getWorkDir(), "suite");
87   
88         EditableProperties ep = suite.getHelper().getProperties("nbproject/platform.properties");
89         ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY, CLUSTER_IDE + "," + CLUSTER_ENTERPRISE + "," + CLUSTER_PLATFORM);
90         suite.getHelper().putProperties("nbproject/platform.properties", ep);
91         ProjectManager.getDefault().saveProject(suite);
92           
93         NbModuleProject prj1 = TestBase.generateSuiteComponent(suite, "prj1");
94         NbModuleProject prj2 = TestBase.generateSuiteComponent(suite, "prj2");
95         NbModuleProject prj3 = TestBase.generateSuiteComponent(suite,"prj3");
96         nodeHandler = (ServiceNodeHandler) prj1.getLookup().lookup(ServiceNodeHandler.class);
97         serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
98 // SuiteProjectTest.openSuite(suite);
99
exploreNodes(nodeHandler.moduleChild);
100         exploreNodes(nodeHandler.allInContextChild);
101
102         assertNodes("org.myservice","","");
103         
104         writeServices(prj2,"org.myservice","impl");
105         assertNodes("org.myservice",
106                     "",
107                     "org.myservice,org.myservice,impl,impl,"
108                 );
109         
110         nodeHandler = (ServiceNodeHandler) prj3.getLookup().lookup(ServiceNodeHandler.class);
111         serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
112 // SuiteProjectTest.openSuite(suite);
113
exploreNodes(nodeHandler.moduleChild);
114         exploreNodes(nodeHandler.allInContextChild);
115
116         assertNodes("org.myservice",
117                     "",
118                     "org.myservice,org.myservice,impl,impl,"
119                 );
120         
121         nodeHandler = (ServiceNodeHandler) prj2.getLookup().lookup(ServiceNodeHandler.class);
122         serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
123         exploreNodes(nodeHandler.moduleChild);
124         exploreNodes(nodeHandler.allInContextChild);
125         Service service = (Service)((List JavaDoc) nodeHandler.moduleServiceMap.get("org.myservice")).get(0);
126         assertNotNull("prj2 contains org.myservice",service);
127         service.getClasses().clear();
128         service.write(prj2);
129         assertNull("org.myservice is deleted",prj2.getSourceDirectory().getFileObject("META-INF/services/org.myservice"));
130         
131         // test for NPE : service folder deleted #87049
132
deleteServiceFolder(prj1);
133         deleteServiceFolder(prj2);
134         deleteServiceFolder(prj3);
135         
136     }
137     private static void writeServices(NbModuleProject prj,String JavaDoc serviceType,String JavaDoc classes) throws IOException JavaDoc {
138              FileObject servicesFolder = SUtil.getServicesFolder(prj,true);
139              FileObject fo = FileUtil.createData(servicesFolder,serviceType);
140              System.out.println("writeServices:" + fo.getPath());
141          FileLock lock = fo.lock();
142          OutputStream JavaDoc os = fo.getOutputStream(lock);
143          PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
144          ps.println(classes);
145          ps.close();
146          os.close();
147              lock.releaseLock();
148     }
149     /** use case 1
150      * register some services in allInSuiteMap
151      * explore node with services
152      * create META-INF folder
153      * create a service (file too)
154      * check if all the nodes were updadtes
155      * add a service to file (check it)
156      * delete a service from file
157      * delete file with services
158      */

159     
160     public void testAModuleInSuite() throws Exception JavaDoc {
161         FileLock lock = null;
162     try {
163         setUpSimpleSuite();
164             lock = doSingleModule();
165     } finally {
166             if (lock != null) {
167                 lock.releaseLock();
168             }
169         }
170     }
171     public void testStandaloneModule() throws Exception JavaDoc {
172         FileLock lock = null;
173     try {
174         setUpStandaloneModule();
175             lock = doSingleModule();
176     } finally {
177             if (lock != null) {
178                 lock.releaseLock();
179             }
180         }
181     }
182     private void deleteService(FileObject prjRoot,String JavaDoc name) throws IOException JavaDoc {
183        FileObject fo = prjRoot.getFileObject("src/META-INF/services/" + name);
184        if (fo != null) {
185            fo.delete();
186        }
187     }
188     public void testNbOrgModule() throws Exception JavaDoc {
189         FileObject prjFo = nbCVSRoot().getFileObject("openide/fs");
190         FileObject prj2Fo = nbCVSRoot().getFileObject("openide/masterfs");
191         deleteService(prjFo,"org.myservice");
192         deleteService(prj2Fo,"org.myservice");
193         try {
194             NbModuleProject prj = (NbModuleProject) ProjectManager.getDefault().findProject(prjFo);
195             NbModuleProject prj2 = (NbModuleProject) ProjectManager.getDefault().findProject(prj2Fo);
196             OpenProjects.getDefault().open(new NbModuleProject[]{prj,prj2}, false);
197             nodeHandler = (ServiceNodeHandler) prj.getLookup().lookup(ServiceNodeHandler.class);
198             serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler.createServiceRootNode();
199             exploreNodes(nodeHandler.moduleChild);
200             exploreNodes(nodeHandler.allInContextChild);
201              
202             assertNodes("org.myservice","","");
203             writeServices(prj,"org.myservice","impl");
204             assertNodes("org.myservice",
205                         "org.myservice,org.myservice,impl,impl,",
206                         "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>,"
207                     );
208             // try modify different project
209
OpenProjects.getDefault().open(new NbModuleProject[]{prj2}, false);
210             writeServices(prj2,"org.myservice","impl2");
211             assertNodes("org.myservice",
212                         "org.myservice,org.myservice,impl,impl,",
213                          "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>,impl2,impl2,");
214             
215         } finally {
216             deleteService(prjFo,"org.myservice");
217             deleteService(prj2Fo,"org.myservice");
218         }
219     }
220     
221     private FileLock doSingleModule() throws IOException JavaDoc, Exception JavaDoc {
222         FileLock lock;
223         
224         assertTrue("testing equals of ServiceNodeHandler",nodeHandler.equals(nodeHandler));
225         
226         //
227
// setup the project
228
FileObject servicesFolder = SUtil.getServicesFolder(prj,true);
229         servicesFolder.delete();
230         servicesFolder = SUtil.getServicesFolder(prj,true);
231
232         exploreNodes(nodeHandler.moduleChild);
233         exploreNodes(nodeHandler.allInContextChild);
234         assertNotNull(nodeHandler.moduleServiceMap);
235         String JavaDoc serviceType = "org.openide.filesystems.Repository";
236         assertNodes(serviceType,"","org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
237         FileObject fo = servicesFolder.createData(serviceType);
238         //printNodes();
239
assertNodes(serviceType,
240                      "org.openide.filesystems.Repository,org.openide.filesystems.Repository,",
241                      "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
242     
243         lock = fo.lock();
244         OutputStream JavaDoc os = fo.getOutputStream(lock);
245         PrintStream JavaDoc ps = new PrintStream JavaDoc(os);
246         ps.println("org.nic.Repository");
247         ps.close();
248         os.close();
249         assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic.Repository,org.nic.Repository,",
250              "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,org.nic.Repository,<b>org.nic.Repository</b>,");
251
252         os = fo.getOutputStream(lock);
253         ps = new PrintStream JavaDoc(os);
254         ps.println("org.nic.Repository");
255         ps.println("-org.netbeans.core.startup.NbRepository");
256         ps.close();
257         os.close();
258         assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,-org.netbeans.core.startup.NbRepository,-org.netbeans.core.startup.NbRepository,org.nic.Repository,org.nic.Repository,"
259                      ,"org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,<s>org.netbeans.core.startup.NbRepository</s>,org.nic.Repository,<b>org.nic.Repository</b>,");
260                  
261
262         os = fo.getOutputStream(lock);
263         ps = new PrintStream JavaDoc(os);
264         ps.println("org.nic2.ErrorManager");
265         ps.close();
266         os.close();
267         assertNodes(serviceType,"org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic2.ErrorManager,org.nic2.ErrorManager,",
268          "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,org.nic2.ErrorManager,<b>org.nic2.ErrorManager</b>,");
269         fo.delete(lock);
270         assertNodes(serviceType,"","org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
271         return lock;
272     }
273
274     
275     /** @param serviceType - service name (file name in META-INF/xx)
276      * @param modulesNode nodes in services for current module -
277      * "service.name,service.displayName,class.name,class.displayName,...."
278      * @param modulesInContextNode nodes in services for all modules -
279      * "service.name,service.displayName,class.name,class.displayName,...."
280      */

281     private void assertNodes(String JavaDoc serviceType,String JavaDoc moduleNodes,String JavaDoc moduleInContextNodes) {
282     ServiceNodeHandler.ServiceRootChildren children = nodeHandler.moduleChild ;
283     log("module nodes");
284     assertEquals(moduleNodes,printChildren(serviceType,children));
285     children = nodeHandler.allInContextChild;
286     log("module in context nodes");
287     assertEquals(moduleInContextNodes,printChildren(serviceType,children));
288     }
289
290     private String JavaDoc printChildren(final String JavaDoc serviceType,final ServiceRootChildren serviceRootChildren) {
291     StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
292     Node nodes[] = null; //serviceRootChildren.getNodes(true);
293
final Node nodesRef[][] = new Node[][]{null};
294   
295         nodesRef[0] = serviceRootChildren.getNodes(true);
296     nodes = nodesRef[0];
297         for (int i = 0 ; i < nodes.length; i++) {
298             if (nodes[i].getName().equals(serviceType)) {
299                 printNode(nodes[i],buff);
300             }
301     }
302         log(buff.toString());
303     return buff.toString();
304     }
305
306     private void printNodes(Node[] nodes,StringBuffer JavaDoc buff) {
307         for (int i = 0 ; i < nodes.length; i++) {
308         printNode(nodes[i],buff);
309     }
310     }
311
312     private void printNode(Node node,StringBuffer JavaDoc buff) {
313     buff.append(node.getName() + ",");
314     buff.append(node.getHtmlDisplayName() + ",");
315     Node nodes[] = node.getChildren().getNodes(true);
316     printNodes(nodes,buff);
317     }
318     
319     public void log (String JavaDoc string) {
320     System.out.println(string);
321     super.log(string);
322     }
323
324     private Node[] exploreNodes(ServiceNodeHandler.ServiceRootChildren children) throws Exception JavaDoc {
325         List JavaDoc/*<String>*/ events = new ArrayList JavaDoc();
326         events.add(SUtil.LOG_COMPUTE_KEYS);
327         events.add(SUtil.LOG_END_COMPUTE_KEYS);
328         log.setEvents(events);
329         children.getNodes(true);
330         log.waitToEvents();
331         SUtil.log("Test.exploreNodes : node computed");
332         return children.getNodes(true);
333     }
334     
335     private static void deleteServiceFolder(NbModuleProject prj) throws IOException JavaDoc {
336         FileObject srcDir = prj.getSourceDirectory();
337         FileObject serviceFo = FileUtil.createData(srcDir,"META-INF/services");
338         final FileObject miFo = FileUtil.createData(srcDir,"META-INF");
339         miFo.getFileSystem().runAtomicAction(new AtomicAction() {
340             public void run() throws IOException JavaDoc {
341                 miFo.delete();
342             }
343         });
344     }
345     
346     static class LoggingHandler extends Handler JavaDoc {
347     private List JavaDoc events;
348     private int index;
349     private boolean bWait;
350     public void publish(LogRecord JavaDoc rec) {
351        synchronized(this) {
352            if (events != null && index < events.size() && rec.getMessage().equals(events.get(index))) {
353                index++;
354            }
355            if (events != null && index == events.size() && bWait) {
356                bWait = false;
357                System.out.println("notify");
358                notify();
359                System.out.println("end not");
360            }
361
362        }
363     }
364     public synchronized void setEvents(List JavaDoc events) {
365         this.events = events;
366         index = 0;
367         
368     }
369     public synchronized void waitToEvents() {
370         if(events != null && index < events.size() ) {
371             try {
372                 bWait = true;
373                 System.out.println("wait");
374                 wait();
375             } catch (InterruptedException JavaDoc ex) {
376                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
377                                                                  ex.getMessage(),
378                                                                  ex);
379             }
380         } else {
381             System.out.println("no wait");
382         }
383     }
384     public void flush() {
385        
386     }
387     
388     public void close() throws SecurityException JavaDoc {
389     }
390     }
391     
392 // public static ServiceNodeHandler.ServiceRootNode getServiceNodeRoot(Project prj) {
393
// ModuleLogicalView mlv = (ModuleLogicalView)prj.getLookup().lookup(ModuleLogicalView.class);
394
// Node mlvNode = mlv.createLogicalView(); registerFileObjectListener();
395
// Node nodes[] = mlvNode.getChildren().getNodes(true);
396
// for (int nIt = 0 ; nIt < nodes.length ; nIt++ ) {
397
// Node node = nodes[nIt];
398
// System.out.println(node.getName());
399
// if (node.getName().equals("important.files")) {
400
// nodes = node.getChildren().getNodes(true);
401
// System.out.println(nodes.length);
402
// for (int mIt = 0 ; mIt < nodes.length ; mIt++ ) {
403
// node = nodes[mIt];
404
// System.out.println(node.getName());
405
// if (node.getName().equals(ServiceNodeHandler.ROOT_NODE_NAME)) {
406
// return (ServiceNodeHandler.ServiceRootNode) node;
407
// }
408
// }
409
// }
410
// }
411
// return null;
412
// }
413
}
414
Popular Tags