KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > enode > ExtensibleLookupTest


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 Nokia. Portions Copyright 2003 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.modules.enode;
21
22 import junit.textui.TestRunner;
23
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26
27 import org.openide.ErrorManager;
28 import org.openide.filesystems.*;
29 import org.openide.modules.ModuleInfo;
30 import org.openide.nodes.Node;
31 import org.openide.util.Lookup;
32
33 import org.netbeans.api.enode.ExtensibleNode;
34 import org.netbeans.spi.enode.LookupContentFactory;
35 import org.netbeans.modules.enode.test.*;
36
37 /**
38  * Tests contained in this class should cover the content
39  * of the lookup obtained as <code>ExtensibleNode.getLookup()</code>.<p>
40  * This class uses classes from package test as testing
41  * classes for the content of the lookup.
42  * @author David Strupl
43  */

44 public class ExtensibleLookupTest extends NbTestCase {
45     /** root folder FileObject */
46     private FileObject root;
47
48     public ExtensibleLookupTest(String JavaDoc name) {
49         super(name);
50     }
51     
52     public static void main(String JavaDoc[] args) {
53         TestRunner.run(new NbTestSuite(ExtensibleLookupTest.class));
54     }
55     
56     /**
57      * Sets up the testing environment by creating testing folders
58      * on the system file system.
59      */

60     protected void setUp () throws Exception JavaDoc {
61         Lookup.getDefault().lookup(ModuleInfo.class);
62         String JavaDoc baseFolder = ExtensibleNode.E_NODE_LOOKUP.substring(1, ExtensibleNode.E_NODE_LOOKUP.length()-1);
63         root = Repository.getDefault().getDefaultFileSystem().findResource(baseFolder);
64         if (root == null) {
65             String JavaDoc s = baseFolder.substring(0, baseFolder.lastIndexOf('/'));
66             FileObject f1 = Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject(s);
67             if (f1 == null) {
68                 f1 = Repository.getDefault().getDefaultFileSystem().getRoot().createFolder(s);
69             }
70             root = f1.createFolder(baseFolder.substring(baseFolder.lastIndexOf('/')+1));
71         }
72     }
73     
74     /**
75      * Deletes the folders created in method setUp().
76      */

77     protected void tearDown() throws Exception JavaDoc {
78 // root.getParent().delete();
79
}
80     
81     /**
82      * Test the ability to get an object from the declarative specification into
83      * the content of the lookup <code>ExtensibleNode.getLookup()</code>.
84      * The test performs following steps:
85      * <OL> <LI> Create an ExtensibleNode with path "test"
86      * <LI> Check the content of its lookup (should be empty)
87      * <LI> Use Filesystems API to create an object on the system file system
88      * in folder "test".
89      * The configuration file tells the lookup to create an instance of MONodeEnhancer
90      * <LI> MONodeEnhancer should be found in the lookup
91      * <LI> Delete the configuration file
92      * <LI> The lookup should not return the object (it was deleted).
93      * </OL>
94      */

95     public void testFindObjectInLookup() throws Exception JavaDoc {
96         ExtensibleNode en1 = new ExtensibleNode("test", false);
97         assertNull("No objects at the start", en1.getLookup().lookup(MONodeEnhancer.class));
98         FileObject test = root.getFileObject("test");
99         if (test == null) {
100             test = root.createFolder("test");
101         }
102         FileObject a1 = test.createData("cookie1.instance");
103         a1.setAttribute("instanceCreate", org.netbeans.spi.enode.LookupContentFactoryManager.create(a1));
104         a1.setAttribute("factoryClass", "org.netbeans.modules.enode.test.C1Factory");
105         a1.setAttribute("implements", "org.netbeans.modules.enode.test.MONodeEnhancer");
106         assertNotNull("Object not found", en1.getLookup().lookup(MONodeEnhancer.class));
107         a1.delete();
108         assertNull("Object found but should be gone.", en1.getLookup().lookup(MONodeEnhancer.class));
109     }
110
111     /**
112      * This test is almost the same as <code>testFindObjectInLookup()</code>. The only
113      * difference is that the object is created in parent folder of the folder
114      * specified as argument to constructor of the ExtensibleNode. In this setup
115      * the hierarchical usage of the the folders should be tested.
116      */

117     public void testMergingContentOfFolders() throws Exception JavaDoc {
118         ExtensibleNode en1 = new ExtensibleNode("test/t2", true);
119         assertNull("No objects at the start", en1.getLookup().lookup(MONodeEnhancer.class));
120         FileObject test = root.getFileObject("test");
121         if (test == null) {
122             test = root.createFolder("test");
123         }
124         FileObject a1 = test.createData("cookie1.instance");
125         a1.setAttribute("instanceCreate", org.netbeans.spi.enode.LookupContentFactoryManager.create(a1));
126         a1.setAttribute("factoryClass", "org.netbeans.modules.enode.test.C1Factory");
127         a1.setAttribute("implements", "org.netbeans.modules.enode.test.MONodeEnhancer");
128         assertNotNull("Object not found", en1.getLookup().lookup(MONodeEnhancer.class));
129         
130         a1.delete();
131         assertNull("Object found but should be gone.", en1.getLookup().lookup(MONodeEnhancer.class));
132     }
133 }
134
Popular Tags