1 19 20 package org.openide.loaders; 21 22 import javax.naming.Binding ; 23 import javax.naming.Context ; 24 import javax.naming.InvalidNameException ; 25 import javax.naming.Name ; 26 import javax.naming.NameAlreadyBoundException ; 27 import javax.naming.NamingEnumeration ; 28 import junit.textui.TestRunner; 29 import org.netbeans.junit.*; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileSystem; 32 33 37 public final class DefaultSettingsContextTest extends NbTestCase { 38 39 private FileSystem lfs; 40 private DataObject dobj; 41 42 43 public DefaultSettingsContextTest(String name) { 44 super(name); 45 } 46 47 protected void setUp() throws java.lang.Exception { 48 super.setUp(); 49 clearWorkDir(); 50 51 String fsstruct [] = new String [] { 52 "AA/a.test" 53 }; 54 55 TestUtilHid.destroyLocalFileSystem(getName()); 56 lfs = TestUtilHid.createLocalFileSystem(getWorkDir(), fsstruct); 57 58 FileObject fo = lfs.findResource("AA/a.test"); 59 assertNotNull("file not found", fo); 60 dobj = DataObject.find(fo); 61 62 } 63 64 public void testOperations() throws Exception { 65 Context ctx = new DefaultSettingsContext(dobj); 66 String val = "attrVal"; 67 String name = "attrName"; 68 69 ctx.bind(name, val); 71 assertEquals("lookup bound", val, ctx.lookup(name)); 72 assertEquals("lookupLink", val, ctx.lookupLink(name)); 73 74 NameAlreadyBoundException nabex = null; 75 try { 76 ctx.bind(name, val); 77 } catch (NameAlreadyBoundException ex) { 78 nabex = ex; 79 } 80 assertNotNull("bind with the same name", nabex); 81 82 String newVal = "attrNewVal"; 84 String newName = "attrNewName"; 85 ctx.rebind(name, val); 86 assertEquals("lookup orig", val, ctx.lookup(name)); 87 ctx.rebind(name, newVal); 88 assertEquals("lookup rebound", newVal, ctx.lookup(name)); 89 ctx.rebind(name, val); 90 assertEquals("lookup rebound", val, ctx.lookup(name)); 91 92 ctx.rename(name, newName); 94 assertEquals("lookup old name", null, ctx.lookup(name)); 95 assertEquals("lookup renamed", val, ctx.lookup(newName)); 96 97 ctx.bind(name, val); 98 assertEquals("lookup bound", val, ctx.lookup(name)); 99 nabex = null; 100 try { 101 ctx.rename(newName, name); 102 } catch (NameAlreadyBoundException ex) { 103 nabex = ex; 104 } 105 assertNotNull("rename to existing name", nabex); 106 107 ctx.unbind(newName); 109 assertEquals("lookup unbound", null, ctx.lookup(newName)); 110 } 111 112 public void testListing() throws Exception { 113 Context ctx = new DefaultSettingsContext(dobj); 114 String [] names = new String [] {"attrName1", "attrName2", "attrName3"}; 115 String [] vals = new String [] {"attrVal1", "attrVal2", "attrVal3"}; 116 117 NamingEnumeration en = ctx.listBindings("."); 118 assertNotNull(en); 119 assertTrue("listBindings is not empty", !en.hasMore()); 120 121 en = ctx.list("."); 122 assertNotNull(en); 123 assertTrue("list is not empty", !en.hasMore()); 124 125 for (int i = 0; i < names.length; i++) { 126 ctx.bind(names[i], vals[i]); 127 } 128 129 en = ctx.listBindings("."); 130 assertNotNull(en); 131 java.util.List namesLst = java.util.Arrays.asList(names); 132 java.util.Set binds = new java.util.HashSet (); 133 for (int i = 0; i < names.length; i++) { 134 assertTrue("expected attr: " + names[i], en.hasMore()); 135 Binding bi = (Binding ) en.next(); 136 int index = namesLst.indexOf(bi.getName()); 137 assertTrue("wrong name: " + bi.getName(), index >= 0); 138 assertEquals("wrong value", vals[index], bi.getObject()); 139 binds.add(bi.getName()); 140 } 141 assertEquals("wrong set of bindings: " + binds, names.length, binds.size()); 142 143 InvalidNameException iex = null; 144 try { 145 ctx.listBindings("../name"); 146 } catch (InvalidNameException ex) { 147 iex = ex; 148 } 149 assertNotNull("name was not invalidated: ../name", iex); 150 151 } 152 153 public void testParse() throws Exception { 154 DefaultSettingsContext ctx = new DefaultSettingsContext(dobj); 155 156 javax.naming.Name n = ctx.parse("attrName"); 157 assertNotNull(n); 158 assertEquals("size", 1, n.size()); 159 String name = getRelativeName(ctx, n); 160 assertEquals("size", "attrName", name); 161 162 n = ctx.parse("././attrName"); 163 assertNotNull(n); 164 assertEquals("size", 3, n.size()); 165 name = getRelativeName(ctx, n); 166 assertEquals("size", "attrName", name); 167 168 n = ctx.parse("../attrName"); 169 assertNotNull(n); 170 assertEquals("size", 2, n.size()); 171 InvalidNameException iex = null; 172 try { 173 name = getRelativeName(ctx, n); 174 } catch (InvalidNameException ex) { 175 iex = ex; 176 } 177 assertNotNull("name was not invalidated: ../attrName", iex); 178 } 179 180 public void testEnvironmentFind() { 181 Context ctx = Environment.findSettingsContext(dobj); 182 assertNotNull("missing default impl", ctx); 183 assertEquals("unknown impl", DefaultSettingsContext.class, ctx.getClass()); 184 } 185 186 private String getRelativeName(DefaultSettingsContext ctx, Name name) throws Exception { 187 java.lang.reflect.Method m = ctx.getClass().getDeclaredMethod( 188 "getRelativeName", new Class [] {Name .class}); 189 try { 190 m.setAccessible(true); 191 return (String ) m.invoke(ctx, new Object [] {name}); 192 } catch (java.lang.reflect.InvocationTargetException ite) { 193 throw (Exception ) ite.getTargetException(); 194 } 195 } 196 197 } 198 | Popular Tags |