1 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 35 public class SubcontextTest extends NbTestCase { 36 public SubcontextTest (String name) { 37 super (name); 38 } 39 40 public static void main(String [] args) { 41 TestRunner.run(new NbTestSuite(SubcontextTest.class)); 42 } 43 44 protected void setUp () throws Exception { 45 Lookup.getDefault().lookup(ModuleInfo.class); 46 } 47 48 public void testSubcontext() throws Exception { 49 implSubcontext(getRootContext(), getRoot()); 50 } 51 52 public void testSubcontext2() throws Exception { 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 { 60 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 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 { 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 { 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 { 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 { 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 { 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 { 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 } 151 152 public void testParentContext () throws Exception { 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 resource) { 168 return Repository.getDefault ().findResource (resource); 169 } 170 171 } 172 | Popular Tags |