KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > registry > SpiUtilsTest


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.spi.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.api.registry.*;
24 import org.netbeans.api.registry.fs.FileSystemContextFactory;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.junit.NbTestSuite;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.LocalFileSystem;
29 import org.openide.filesystems.Repository;
30 import org.openide.modules.ModuleInfo;
31 import org.openide.util.Lookup;
32
33 import javax.swing.*;
34
35 public class SpiUtilsTest extends NbTestCase {
36
37     private FileObject root = null;
38     
39     public SpiUtilsTest(String JavaDoc name) {
40         super (name);
41     }
42     
43     public static void main(String JavaDoc[] args) {
44         TestRunner.run(new NbTestSuite(SpiUtilsTest.class));
45     }
46     
47     protected void setUp () throws Exception JavaDoc {
48         Lookup.getDefault().lookup(ModuleInfo.class);
49         root = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
50     }
51     
52     public void testRootContextCreation() throws Exception JavaDoc {
53         BasicContext rootCtx = FileSystemContextFactory.createContext(root);
54         Context ctx = SpiUtils.createContext(rootCtx);
55         Context subctx = ctx.createSubcontext("abcd");
56         FileObject fo = Repository.getDefault().getDefaultFileSystem().getRoot().getFileObject("abcd");
57         assertTrue ("Cannot create initial context", fo != null);
58         ctx.destroySubcontext("abcd");
59     }
60     
61     public void testArbitraryRootContextCreation() throws Exception JavaDoc {
62         LocalFileSystem lfs = new LocalFileSystem ();
63         lfs.setRootDirectory (getWorkDir ());
64         FileObject r = lfs.getRoot ();
65         BasicContext rootCtx = FileSystemContextFactory.createContext(r);
66         Context ctx = SpiUtils.createContext(rootCtx);
67         Context subctx = ctx.createSubcontext("aaaaa");
68         FileObject fo = r.getFileObject("aaaaa");
69         assertTrue ("Cannot create initial context on arbitrary fileObject", fo != null);
70     }
71     
72     public void testCreateException() throws Exception JavaDoc {
73         BasicContext ctx = FileSystemContextFactory.createContext(root);
74         BasicContext ctx2 = ctx;
75         ctx = ctx.createSubcontext("something");
76         ContextException e = SpiUtils.createContextException(ctx, "abcd");
77         assertTrue ("ContextException does not have correct context "+e.getContext(), e.getContext().getContextName().equals("something"));
78         ctx2.destroySubcontext("something");
79     }
80     
81     public void testCreateSubcontextEvent() throws Exception JavaDoc {
82         BasicContext ctx = FileSystemContextFactory.createContext(root);
83         BasicContext ctx2 = ctx;
84         ctx = ctx.createSubcontext("something2");
85         SubcontextEvent e = SpiUtils.createSubcontextEvent(ctx, "abcd2", SubcontextEvent.SUBCONTEXT_ADDED);
86         assertTrue ("SubcontextEvent context does not match "+e, e.getContext().getContextName().equals("something2"));
87         assertTrue ("SubcontextEvent subcontext does not match "+e, e.getSubcontextName().equals("abcd2"));
88         assertTrue ("SubcontextEvent type does not match "+e, e.getType() == SubcontextEvent.SUBCONTEXT_ADDED);
89         ctx2.destroySubcontext("something2");
90     }
91     
92     public void testCreateBindingEvent() throws Exception JavaDoc {
93         BasicContext ctx = FileSystemContextFactory.createContext(root);
94         BasicContext ctx2 = ctx;
95         ctx = ctx.createSubcontext("something3");
96         BindingEvent e = SpiUtils.createBindingEvent(ctx, "abcd3", BindingEvent.BINDING_REMOVED);
97         assertTrue ("BindingEvent context does not match "+e, e.getContext().getContextName().equals("something3"));
98         assertTrue ("BindingEvent binding does not match "+e, e.getBindingName().equals("abcd3"));
99         assertTrue ("BindingEvent type does not match "+e, e.getType() == BindingEvent.BINDING_REMOVED);
100         ctx2.destroySubcontext("something3");
101     }
102     
103     public void testCreateAttributeEvent() throws Exception JavaDoc {
104         BasicContext ctx = FileSystemContextFactory.createContext(root);
105         BasicContext ctx2 = ctx;
106         ctx = ctx.createSubcontext("something4");
107         AttributeEvent e = SpiUtils.createAttributeEvent(ctx, "abcd4", "abcd5", AttributeEvent.ATTRIBUTE_MODIFIED);
108         assertTrue ("AttributeEvent context does not match "+e, e.getContext().getContextName().equals("something4"));
109         assertTrue ("AttributeEvent binding does not match "+e, e.getBindingName().equals("abcd4"));
110         assertTrue ("AttributeEvent attrname does not match "+e, e.getAttributeName().equals("abcd5"));
111         assertTrue ("AttributeEvent type does not match "+e, e.getType() == AttributeEvent.ATTRIBUTE_MODIFIED);
112         ctx2.destroySubcontext("something4");
113     }
114     
115     public void testCreateObjectRef() throws Exception JavaDoc {
116         Object JavaDoc o = new JLabel("my label X");
117         BasicContext rootCtx = FileSystemContextFactory.createContext(root);
118         BasicContext ctx2 = rootCtx;
119         BasicContext ctx = rootCtx.createSubcontext("something5");
120         ctx.bindObject("obj25", o);
121         ObjectRef or = SpiUtils.createObjectRef(ctx,"obj25");
122         assertTrue ("ObjectRef context does not match "+or, or.getContext().getContextName().equals("something5"));
123         assertTrue ("ObjectRef binding does not match "+or, or.getBindingName().equals("obj25"));
124         assertTrue ("ObjectRef absolutename does not match "+or, or.getContextAbsoluteName().equals("/something5"));
125         assertTrue ("ObjectRef isValid does not match "+or, or.isValid() == true);
126         assertTrue ("ObjectRef getObject does not match "+or, or.getObject().equals(o));
127         ctx2.destroySubcontext("something5");
128         
129         ctx2 = rootCtx;
130         ctx = rootCtx.createSubcontext("something6");
131         ctx.bindObject("obj26", o);
132         or = SpiUtils.createObjectRef(rootCtx, "/something6", "obj26");
133         assertTrue ("ObjectRef context does not match "+or, or.getContext().getContextName().equals("something6"));
134         assertTrue ("ObjectRef binding does not match "+or, or.getBindingName().equals("obj26"));
135         assertTrue ("ObjectRef absolutename does not match "+or.getContextAbsoluteName(), or.getContextAbsoluteName().equals("/something6"));
136         assertTrue ("ObjectRef isValid does not match "+or, or.isValid() == true);
137         assertTrue ("ObjectRef getObject does not match "+or, or.getObject().equals(o));
138         ctx2.destroySubcontext("something6");
139     }
140     
141 }
142
Popular Tags