KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > registry > SubcontextTest


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.api.registry;
21
22 import junit.textui.TestRunner;
23 import org.netbeans.junit.NbTestCase;
24 import org.netbeans.junit.NbTestSuite;
25 import org.openide.filesystems.FileObject;
26 import org.openide.filesystems.Repository;
27 import org.openide.modules.ModuleInfo;
28 import org.openide.util.Lookup;
29
30 /**
31  *
32  * @author Vitezslav Stejskal
33  * @author David Konecny
34  */

35 public class SubcontextTest extends NbTestCase {
36     public SubcontextTest (String JavaDoc name) {
37         super (name);
38     }
39
40     public static void main(String JavaDoc[] args) {
41         TestRunner.run(new NbTestSuite(SubcontextTest.class));
42     }
43     
44     protected void setUp () throws Exception JavaDoc {
45         Lookup.getDefault().lookup(ModuleInfo.class);
46     }
47     
48     public void testSubcontext() throws Exception JavaDoc {
49         implSubcontext(getRootContext(), getRoot());
50     }
51
52     public void testSubcontext2() throws Exception JavaDoc {
53         Context ctx = getRootContext().createSubcontext("aa/bb/cc");
54         FileObject fo = findResource ("aa/bb/cc");
55         implSubcontext(ctx, fo);
56         getRootContext().destroySubcontext("aa");
57     }
58     
59     public void implSubcontext(Context context, FileObject rootFO) throws Exception JavaDoc {
60         // create subcontext
61
Context subctx = context.createSubcontext ("subctx");
62         FileObject f = rootFO.getFileObject ("subctx");
63         assertTrue ("Subcontext doesn't exist", f != null);
64         assertTrue ("Subcontext isn't folder", f.isFolder ());
65         
66         // create another subcontext
67
subctx.createSubcontext ("foo");
68         f = rootFO.getFileObject("subctx").getFileObject("foo");
69         assertTrue ("Subcontext doesn't exist", f != null);
70         assertTrue ("Subcontext isn't folder", f.isFolder ());
71
72         // creating subcontext which already exist must pass
73
{
74             ContextException exc = null;
75             try {
76                 subctx.createSubcontext ("foo");
77             } catch (ContextException e) {
78                 exc = e;
79             }
80             assertTrue ("ContextException expected when creating existing context",
81                 exc == null);
82         }
83         
84         // creating of multiple subcontexts in one step must pass
85
{
86             ContextException exc = null;
87             try {
88                 subctx.createSubcontext ("ctx1/ctx2/ctx3/ctx4");
89             } catch (ContextException e) {
90                 exc = e;
91             }
92             assertTrue ("ContextException expected for invalid subcontext name", exc == null);
93         }
94         
95         // creating of multiple subcontexts in one step must pass
96
{
97             ContextException exc = null;
98             try {
99                 subctx.createSubcontext ("ctx1/ctx2/ctx3/ctx4/ctx5/ctx6/ctx7");
100             } catch (ContextException e) {
101                 exc = e;
102             }
103             assertTrue ("ContextException expected for invalid subcontext name", exc == null);
104         }
105         
106         // destroying non-existing context must throw exception
107
{
108             ContextException exc = null;
109             try {
110                 subctx.destroySubcontext ("abcdctx");
111             } catch (ContextException e) {
112                 exc = e;
113             }
114             assertTrue ("ContextException expected when destroying non-existing ctx",
115                 exc instanceof ContextException);
116         }
117         
118         // destroy multiple context must work
119
{
120             context.destroySubcontext ("subctx");
121             f = rootFO.getFileObject("subctx");
122             assertTrue ("Folder isn't deleted for destroyed context", f == null);
123         }
124         
125     }
126     
127     public void testSubcontext5() throws Exception JavaDoc {
128         Context context = getRootContext();
129         
130         Context ctx = context.getSubcontext("a1/b1/c1/d1/e1");
131         assertTrue ("This context cannot exist", ctx == null);
132         
133         ctx = context.createSubcontext("a1/b1/c1/d1/e1");
134         assertTrue ("Context must be created", ctx != null);
135         
136         FileObject fo = findResource("a1/b1/c1/d1/e1");
137         assertTrue ("Folder must exist", fo != null);
138
139         context.destroySubcontext("a1/b1/c1/d1");
140         fo = findResource("a1/b1/c1/d1");
141         assertTrue ("Folder cannot exist", fo == null);
142         
143         fo = findResource("a1/b1/c1");
144         assertTrue ("Folder must exist", fo != null);
145         
146         context.destroySubcontext("a1");
147         fo = findResource("a1");
148         assertTrue ("Folder cannot exist", fo == null);
149         // test it on diff root context
150
}
151
152     public void testParentContext () throws Exception JavaDoc {
153         Context subctx = getRootContext().createSubcontext ("subctxXXX");
154         Context parent = subctx.getParentContext();
155         assertEquals ("Parent Context not found", parent.getAbsoluteContextName(), getRootContext().getAbsoluteContextName());
156         getRootContext().destroySubcontext ("subctxXXX");
157     }
158
159     protected Context getRootContext () {
160         return Context.getDefault();
161     }
162
163     protected FileObject getRoot() {
164         return Repository.getDefault ().getDefaultFileSystem ().getRoot ();
165     }
166
167     protected FileObject findResource(String JavaDoc resource) {
168         return Repository.getDefault ().findResource (resource);
169     }
170
171 }
172
Popular Tags