001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.metainf;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import java.io.PrintStream;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.logging.Handler;
050: import java.util.logging.Level;
051: import java.util.logging.LogRecord;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.ui.OpenProjects;
054: import org.netbeans.modules.apisupport.project.NbModuleProject;
055: import org.netbeans.modules.apisupport.project.TestBase;
056: import org.netbeans.modules.apisupport.project.metainf.ServiceNodeHandler.ServiceRootChildren;
057: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
058: import org.netbeans.modules.apisupport.project.ui.customizer.SuiteProperties;
059: import org.netbeans.spi.project.support.ant.EditableProperties;
060: import org.openide.filesystems.FileLock;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileSystem.AtomicAction;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.nodes.Node;
065:
066: /**
067: *
068: * @author pzajac
069: */
070: public class ServiceNodeHandlerTest extends TestBase {
071: NbModuleProject prj;
072: ServiceNodeHandler nodeHandler;
073: ServiceNodeHandler.ServiceRootNode serviceRootNode;
074: LoggingHandler log;
075:
076: public ServiceNodeHandlerTest(String testName) {
077: super (testName);
078: }
079:
080: protected @Override
081: void setUp() throws Exception {
082: super .setUp();
083: log = new LoggingHandler();
084: SUtil.getLogger().setLevel(Level.INFO);
085: SUtil.getLogger().addHandler(log);
086: }
087:
088: private void setUpSimpleSuite() throws Exception {
089: SuiteProject suite = TestBase.generateSuite(getWorkDir(),
090: "suite");
091: EditableProperties ep = suite.getHelper().getProperties(
092: "nbproject/platform.properties");
093: ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY,
094: CLUSTER_IDE + "," + CLUSTER_ENTERPRISE);
095: suite.getHelper().putProperties(
096: "nbproject/platform.properties", ep);
097: ProjectManager.getDefault().saveProject(suite);
098:
099: prj = TestBase.generateSuiteComponent(suite, "prj1");
100: nodeHandler = prj.getLookup().lookup(ServiceNodeHandler.class);
101: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
102: .createServiceRootNode();
103: }
104:
105: private void setUpStandaloneModule() throws Exception {
106: prj = TestBase.generateStandaloneModule(getWorkDir(), "prj1");
107: nodeHandler = prj.getLookup().lookup(ServiceNodeHandler.class);
108: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
109: .createServiceRootNode();
110: }
111:
112: public void testSuiteWithManyProjects() throws IOException,
113: Exception {
114: SuiteProject suite = TestBase.generateSuite(getWorkDir(),
115: "suite");
116:
117: EditableProperties ep = suite.getHelper().getProperties(
118: "nbproject/platform.properties");
119: ep.setProperty(SuiteProperties.DISABLED_CLUSTERS_PROPERTY,
120: CLUSTER_IDE + "," + CLUSTER_ENTERPRISE + ","
121: + CLUSTER_PLATFORM);
122: suite.getHelper().putProperties(
123: "nbproject/platform.properties", ep);
124: ProjectManager.getDefault().saveProject(suite);
125:
126: NbModuleProject prj1 = TestBase.generateSuiteComponent(suite,
127: "prj1");
128: NbModuleProject prj2 = TestBase.generateSuiteComponent(suite,
129: "prj2");
130: NbModuleProject prj3 = TestBase.generateSuiteComponent(suite,
131: "prj3");
132: nodeHandler = prj1.getLookup().lookup(ServiceNodeHandler.class);
133: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
134: .createServiceRootNode();
135: // SuiteProjectTest.openSuite(suite);
136: exploreNodes(nodeHandler.moduleChild);
137: exploreNodes(nodeHandler.allInContextChild);
138:
139: assertNodes("org.myservice", "", "");
140:
141: writeServices(prj2, "org.myservice", "impl");
142: assertNodes("org.myservice", "",
143: "org.myservice,org.myservice,impl,impl,");
144:
145: nodeHandler = prj3.getLookup().lookup(ServiceNodeHandler.class);
146: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
147: .createServiceRootNode();
148: // SuiteProjectTest.openSuite(suite);
149: exploreNodes(nodeHandler.moduleChild);
150: exploreNodes(nodeHandler.allInContextChild);
151:
152: assertNodes("org.myservice", "",
153: "org.myservice,org.myservice,impl,impl,");
154:
155: nodeHandler = prj2.getLookup().lookup(ServiceNodeHandler.class);
156: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
157: .createServiceRootNode();
158: exploreNodes(nodeHandler.moduleChild);
159: exploreNodes(nodeHandler.allInContextChild);
160: Service service = (Service) ((List) nodeHandler.moduleServiceMap
161: .get("org.myservice")).get(0);
162: assertNotNull("prj2 contains org.myservice", service);
163: service.getClasses().clear();
164: service.write(prj2);
165: assertNull("org.myservice is deleted", prj2
166: .getSourceDirectory().getFileObject(
167: "META-INF/services/org.myservice"));
168:
169: // test for NPE : service folder deleted #87049
170: deleteServiceFolder(prj1);
171: deleteServiceFolder(prj2);
172: deleteServiceFolder(prj3);
173:
174: }
175:
176: private static void writeServices(NbModuleProject prj,
177: String serviceType, String classes) throws IOException {
178: FileObject servicesFolder = SUtil.getServicesFolder(prj, true);
179: FileObject fo = FileUtil
180: .createData(servicesFolder, serviceType);
181: System.out.println("writeServices:" + fo.getPath());
182: FileLock lock = fo.lock();
183: OutputStream os = fo.getOutputStream(lock);
184: PrintStream ps = new PrintStream(os);
185: ps.println(classes);
186: ps.close();
187: os.close();
188: lock.releaseLock();
189: }
190:
191: /** use case 1
192: * register some services in allInSuiteMap
193: * explore node with services
194: * create META-INF folder
195: * create a service (file too)
196: * check if all the nodes were updadtes
197: * add a service to file (check it)
198: * delete a service from file
199: * delete file with services
200: */
201:
202: public void testAModuleInSuite() throws Exception {
203: FileLock lock = null;
204: try {
205: setUpSimpleSuite();
206: lock = doSingleModule();
207: } finally {
208: if (lock != null) {
209: lock.releaseLock();
210: }
211: }
212: }
213:
214: public void testStandaloneModule() throws Exception {
215: FileLock lock = null;
216: try {
217: setUpStandaloneModule();
218: lock = doSingleModule();
219: } finally {
220: if (lock != null) {
221: lock.releaseLock();
222: }
223: }
224: }
225:
226: private void deleteService(FileObject prjRoot, String name)
227: throws IOException {
228: FileObject fo = prjRoot.getFileObject("src/META-INF/services/"
229: + name);
230: if (fo != null) {
231: fo.delete();
232: }
233: }
234:
235: public void testNbOrgModule() throws Exception {
236: FileObject prjFo = nbRoot()
237: .getFileObject("openide.filesystems");
238: FileObject prj2Fo = nbRoot().getFileObject("masterfs");
239: deleteService(prjFo, "org.myservice");
240: deleteService(prj2Fo, "org.myservice");
241: try {
242: NbModuleProject prj1 = (NbModuleProject) ProjectManager
243: .getDefault().findProject(prjFo);
244: NbModuleProject prj2 = (NbModuleProject) ProjectManager
245: .getDefault().findProject(prj2Fo);
246: OpenProjects.getDefault().open(
247: new NbModuleProject[] { prj1, prj2 }, false);
248: nodeHandler = prj1.getLookup().lookup(
249: ServiceNodeHandler.class);
250: serviceRootNode = (ServiceNodeHandler.ServiceRootNode) nodeHandler
251: .createServiceRootNode();
252: exploreNodes(nodeHandler.moduleChild);
253: exploreNodes(nodeHandler.allInContextChild);
254:
255: assertNodes("org.myservice", "", "");
256: writeServices(prj1, "org.myservice", "impl");
257: assertNodes("org.myservice",
258: "org.myservice,org.myservice,impl,impl,",
259: "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>,");
260: // try modify different project
261: OpenProjects.getDefault().open(
262: new NbModuleProject[] { prj2 }, false);
263: writeServices(prj2, "org.myservice", "impl2");
264: assertNodes("org.myservice",
265: "org.myservice,org.myservice,impl,impl,",
266: "org.myservice,<b>org.myservice</b>,impl,<b>impl</b>,impl2,impl2,");
267:
268: } finally {
269: deleteService(prjFo, "org.myservice");
270: deleteService(prj2Fo, "org.myservice");
271: }
272: }
273:
274: private FileLock doSingleModule() throws IOException, Exception {
275: FileLock lock;
276:
277: assertTrue("testing equals of ServiceNodeHandler", nodeHandler
278: .equals(nodeHandler));
279:
280: //
281: // setup the project
282: FileObject servicesFolder = SUtil.getServicesFolder(prj, true);
283: servicesFolder.delete();
284: servicesFolder = SUtil.getServicesFolder(prj, true);
285:
286: exploreNodes(nodeHandler.moduleChild);
287: exploreNodes(nodeHandler.allInContextChild);
288: assertNotNull(nodeHandler.moduleServiceMap);
289: String serviceType = "org.openide.filesystems.Repository";
290: assertNodes(
291: serviceType,
292: "",
293: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
294: FileObject fo = servicesFolder.createData(serviceType);
295: //printNodes();
296: assertNodes(
297: serviceType,
298: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,",
299: "org.openide.filesystems.Repository,<b>org.openide.filesystems.Repository</b>,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
300:
301: lock = fo.lock();
302: OutputStream os = fo.getOutputStream(lock);
303: PrintStream ps = new PrintStream(os);
304: ps.println("org.nic.Repository");
305: ps.close();
306: os.close();
307: assertNodes(
308: serviceType,
309: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic.Repository,org.nic.Repository,",
310: "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>,");
311:
312: os = fo.getOutputStream(lock);
313: ps = new PrintStream(os);
314: ps.println("org.nic.Repository");
315: ps.println("-org.netbeans.core.startup.NbRepository");
316: ps.close();
317: os.close();
318: assertNodes(
319: serviceType,
320: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,-org.netbeans.core.startup.NbRepository,-org.netbeans.core.startup.NbRepository,org.nic.Repository,org.nic.Repository,",
321: "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>,");
322:
323: os = fo.getOutputStream(lock);
324: ps = new PrintStream(os);
325: ps.println("org.nic2.ErrorManager");
326: ps.close();
327: os.close();
328: assertNodes(
329: serviceType,
330: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.nic2.ErrorManager,org.nic2.ErrorManager,",
331: "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>,");
332: fo.delete(lock);
333: assertNodes(
334: serviceType,
335: "",
336: "org.openide.filesystems.Repository,org.openide.filesystems.Repository,org.netbeans.core.startup.NbRepository,org.netbeans.core.startup.NbRepository,");
337: return lock;
338: }
339:
340: /** @param serviceType - service name (file name in META-INF/xx)
341: * @param modulesNode nodes in services for current module -
342: * "service.name,service.displayName,class.name,class.displayName,...."
343: * @param modulesInContextNode nodes in services for all modules -
344: * "service.name,service.displayName,class.name,class.displayName,...."
345: */
346: private void assertNodes(String serviceType, String moduleNodes,
347: String moduleInContextNodes) {
348: ServiceNodeHandler.ServiceRootChildren children = nodeHandler.moduleChild;
349: log("module nodes");
350: assertEquals(moduleNodes, printChildren(serviceType, children));
351: children = nodeHandler.allInContextChild;
352: log("module in context nodes");
353: assertEquals(moduleInContextNodes, printChildren(serviceType,
354: children));
355: }
356:
357: private String printChildren(final String serviceType,
358: final ServiceRootChildren serviceRootChildren) {
359: StringBuffer buff = new StringBuffer();
360: Node nodes[] = null; //serviceRootChildren.getNodes(true);
361: final Node nodesRef[][] = new Node[][] { null };
362:
363: nodesRef[0] = serviceRootChildren.getNodes(true);
364: nodes = nodesRef[0];
365: for (int i = 0; i < nodes.length; i++) {
366: if (nodes[i].getName().equals(serviceType)) {
367: printNode(nodes[i], buff);
368: }
369: }
370: log(buff.toString());
371: return buff.toString();
372: }
373:
374: private void printNodes(Node[] nodes, StringBuffer buff) {
375: for (int i = 0; i < nodes.length; i++) {
376: printNode(nodes[i], buff);
377: }
378: }
379:
380: private void printNode(Node node, StringBuffer buff) {
381: buff.append(node.getName() + ",");
382: buff.append(node.getHtmlDisplayName() + ",");
383: Node nodes[] = node.getChildren().getNodes(true);
384: printNodes(nodes, buff);
385: }
386:
387: private Node[] exploreNodes(
388: ServiceNodeHandler.ServiceRootChildren children)
389: throws Exception {
390: List<String> events = new ArrayList<String>();
391: events.add(SUtil.LOG_COMPUTE_KEYS);
392: events.add(SUtil.LOG_END_COMPUTE_KEYS);
393: log.setEvents(events);
394: children.getNodes(true);
395: log.waitToEvents();
396: SUtil.log("Test.exploreNodes : node computed");
397: return children.getNodes(true);
398: }
399:
400: private static void deleteServiceFolder(NbModuleProject prj)
401: throws IOException {
402: FileObject srcDir = prj.getSourceDirectory();
403: // XXX huh?!
404: FileObject serviceFo = FileUtil.createData(srcDir,
405: "META-INF/services");
406: final FileObject miFo = FileUtil.createData(srcDir, "META-INF");
407: miFo.getFileSystem().runAtomicAction(new AtomicAction() {
408: public void run() throws IOException {
409: miFo.delete();
410: }
411: });
412: }
413:
414: static class LoggingHandler extends Handler {
415: private List events;
416: private int index;
417: private boolean bWait;
418:
419: public void publish(LogRecord rec) {
420: synchronized (this ) {
421: if (events != null && index < events.size()
422: && rec.getMessage().equals(events.get(index))) {
423: index++;
424: }
425: if (events != null && index == events.size() && bWait) {
426: bWait = false;
427: System.out.println("notify");
428: notify();
429: System.out.println("end not");
430: }
431:
432: }
433: }
434:
435: public synchronized void setEvents(List events) {
436: this .events = events;
437: index = 0;
438:
439: }
440:
441: public synchronized void waitToEvents() {
442: if (events != null && index < events.size()) {
443: try {
444: bWait = true;
445: System.out.println("wait");
446: wait();
447: } catch (InterruptedException ex) {
448: java.util.logging.Logger.getLogger("global").log(
449: java.util.logging.Level.SEVERE,
450: ex.getMessage(), ex);
451: }
452: } else {
453: System.out.println("no wait");
454: }
455: }
456:
457: public void flush() {
458:
459: }
460:
461: public void close() throws SecurityException {
462: }
463: }
464:
465: // public static ServiceNodeHandler.ServiceRootNode getServiceNodeRoot(Project prj) {
466: // ModuleLogicalView mlv = (ModuleLogicalView)prj.getLookup().lookup(ModuleLogicalView.class);
467: // Node mlvNode = mlv.createLogicalView(); registerFileObjectListener();
468: // Node nodes[] = mlvNode.getChildren().getNodes(true);
469: // for (int nIt = 0 ; nIt < nodes.length ; nIt++ ) {
470: // Node node = nodes[nIt];
471: // System.out.println(node.getName());
472: // if (node.getName().equals("important.files")) {
473: // nodes = node.getChildren().getNodes(true);
474: // System.out.println(nodes.length);
475: // for (int mIt = 0 ; mIt < nodes.length ; mIt++ ) {
476: // node = nodes[mIt];
477: // System.out.println(node.getName());
478: // if (node.getName().equals(ServiceNodeHandler.ROOT_NODE_NAME)) {
479: // return (ServiceNodeHandler.ServiceRootNode) node;
480: // }
481: // }
482: // }
483: // }
484: // return null;
485: // }
486: }
|