1 19 20 package org.netbeans.api.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.fs.FileSystemContextFactory; 24 import org.netbeans.junit.NbTestCase; 25 import org.netbeans.junit.NbTestSuite; 26 import org.netbeans.spi.registry.BasicContext; 27 import org.netbeans.spi.registry.SpiUtils; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.Repository; 30 import org.openide.loaders.DataObject; 31 import org.openide.modules.ModuleInfo; 32 import org.openide.util.Lookup; 33 34 import javax.swing.*; 35 import java.awt.*; 36 import java.net.URL ; 37 38 43 public class BindingTest extends NbTestCase { 44 private static final String MY_NULL = new String ("MY_NULL"); 45 46 public BindingTest (String name) { 47 super (name); 48 } 49 50 public static void main(String [] args) { 51 TestRunner.run(new NbTestSuite(BindingTest.class)); 52 } 53 54 protected void setUp () throws Exception { 55 Lookup.getDefault().lookup(ModuleInfo.class); 56 } 57 58 public void testBindObject() throws Exception { 59 implBindObject(getRootContext(), "foo", getRoot(), new JLabel("obj 11"), new JLabel("second OBJ")); 60 } 61 62 public void testBindObject2() throws Exception { 63 Context ctx = getRootContext().createSubcontext("a/b/c"); 64 FileObject fo = findResource ("a/b/c"); 65 implBindObject(ctx, "foo", fo, new JLabel("objQ"), new JLabel("secondW")) ; 66 getRootContext().destroySubcontext("a"); 67 } 68 69 public void _EXCLUDE_issue36334_testLongNameObject() throws Exception { 71 implBindObject(getRootContext(), "ThisIsVeryLongNameOfInstanceFileToTestIssueDealingWithInstanceNamesWhichLenghtIsOver50Characters", getRoot(), new JLabel("objQ1"), new JLabel("secondW1")); 72 } 73 74 public void _EXCLUDE_testStrangeNameObject() throws Exception { 76 implBindObject(getRootContext(), ":[]<>?*|.\"\u0020\u007E#", getRoot(), new JLabel("objQ111"), new JLabel("secondW111")); 77 } 78 79 public void implBindObject(Context context, String bindingName, FileObject rootFO, Object objA, Object objA2) throws Exception { 80 FileObject f; 81 82 context.putObject(bindingName, objA); 83 String fileName = escapeAndCut(bindingName); 84 f = rootFO.getFileObject (fileName, "settings"); 85 assertTrue ("Instance file wasn't created for bound object", f != null); 86 assertTrue (f.isData ()); 87 88 DataObject ido = DataObject.find (f); 91 92 Object obj2 = context.getObject(bindingName, MY_NULL); 93 assertTrue ("Bound object wasn't looked up", obj2 != null); 94 assertTrue ("Bind obj and lookup result are different", objA == obj2); 95 96 try { 98 context.createSubcontext (bindingName); 99 } catch (Exception e) { 100 fail ("Context with same name as object binding must coexist"); 101 } 102 103 context.destroySubcontext (bindingName); 104 obj2 = context.getObject (bindingName, MY_NULL); 105 assertTrue ("Subcontext destroyed, but object can't be found, obj2=" + obj2 + " objA=" + objA, obj2 == objA); 106 107 { 109 context.putObject("ctx1/"+bindingName, objA); 111 Object o = context.getObject ("ctx1/"+bindingName, MY_NULL); 112 assertTrue ("ContextException expected when slash character is used in name", 113 o == MY_NULL); 114 } 115 116 Object objB = objA2; 118 context.putObject (bindingName, objB); 119 Object obj = context.getObject (bindingName, MY_NULL); 120 assertEquals ("New object not found", obj, objB); 121 122 context.putObject (bindingName, null); 124 f = rootFO.getFileObject (bindingName, "settings"); 125 assertTrue ("Instance file wasn't destroyed for unbound object", f == null); 126 127 obj2 = context.getObject(bindingName, MY_NULL); 129 assertTrue("Object is still reachable even if unbound", obj2 == MY_NULL); 130 131 context.putObject (bindingName, null); 133 } 134 135 public void testBindPrimitive() throws Exception { 136 implBindPrimitive(getRootContext(), "foo", getRoot(), new String ("11"), new String ("789")); 137 } 138 139 public void testBindPrimitive2() throws Exception { 140 Context ctx = getRootContext().createSubcontext("a/b/c"); 141 FileObject fo = findResource ("a/b/c"); 142 implBindPrimitive(ctx, "foo", fo, new String ("171"), new String ("7879")) ; 143 getRootContext().destroySubcontext("a"); 144 } 145 146 public void testLongNamePrimitive() throws Exception { 148 implBindPrimitive(getRootContext(), "ThisIsVeryLongNameOfInstanceFileToTestIssueDealingWithInstanceNamesWhichLenghtIsOver50Characters", getRoot(), new String ("119"), new String ("9789")); 149 } 150 151 public void _EXCLUDE_testStrangeNamePrimitive() throws Exception { 153 implBindPrimitive(getRootContext(), ":[]<>?*|.\"\u0020\u007E#", getRoot(), new String ("1175"), new String ("7895")); 154 } 155 156 public void implBindPrimitive(Context context, String bindingName, FileObject rootFO, String objA, String objA2) throws Exception { 157 FileObject f; 158 159 context.putObject(bindingName, objA); 160 161 String obj2 = (String )context.getObject(bindingName, MY_NULL); 162 assertTrue ("Bound object wasn't looked up", obj2 != null); 163 assertTrue ("Bind obj and lookup result are different: A="+objA+" B="+obj2, objA.equals(obj2)); 164 165 try { 167 context.createSubcontext (bindingName); 168 } catch (Exception e) { 169 fail ("Context with same name as object binding must coexist"); 170 } 171 172 context.destroySubcontext (bindingName); 173 obj2 = (String )context.getObject (bindingName, MY_NULL); 174 assertTrue ("Subcontext destroyed, but object can't be found, obj2=" + obj2 + " objA=" + objA, obj2.equals(objA)); 175 176 { 178 context.putObject("ctx1/"+bindingName, objA); 180 String o = (String )context.getObject ("ctx1/"+bindingName, MY_NULL); 181 assertTrue ("ContextException expected when slash character is used in name", 182 o == MY_NULL); 183 } 184 185 String objB = (String )objA2; 187 context.putObject (bindingName, objB); 188 String obj = (String )context.getObject (bindingName, MY_NULL); 189 assertEquals ("New object not found", obj, objB); 190 191 context.putObject (bindingName, null); 193 f = rootFO.getFileObject (bindingName, "settings"); 194 assertTrue ("Instance file wasn't destroyed for unbound object", f == null); 195 196 obj2 = (String )context.getObject(bindingName, MY_NULL); 198 assertTrue("Object is still reachable even if unbound", obj2 == MY_NULL); 199 200 context.putObject (bindingName, null); 202 } 203 204 public void testPrimitive() throws Exception { 205 implTestPrimitive(getRootContext()); 206 } 207 208 public void testPrimitive2() throws Exception { 209 Context ctx = getRootContext().createSubcontext("a/b/c"); 210 FileObject fo = findResource ("a/b/c"); 211 implTestPrimitive(ctx); 212 getRootContext().destroySubcontext("a"); 213 } 214 215 public void implTestPrimitive(Context ctx) throws Exception { 216 String s = "bsdfmdbmfd"; 217 ctx.putString("b1", s); 218 String s_ = ctx.getString("b1", null); 219 assertTrue("Values do not match", s_.equals(s)); 220 assertTrue("Object type does not match", ctx.getObject("b1", null) instanceof String ); 221 222 int i = 1889; 223 ctx.putInt("b2", i); 224 int i_ = ctx.getInt("b2", 1112); 225 assertTrue("Values do not match", i_ == i); 226 assertTrue("Object type does not match", ctx.getObject("b2", null) instanceof Integer ); 227 228 long l = 188918891L; 229 ctx.putLong("b3", l); 230 long l_ = ctx.getLong("b3", 11121112L); 231 assertTrue("Values do not match", l_ == l); 232 assertTrue("Object type does not match", ctx.getObject("b3", null) instanceof Long ); 233 234 float f = 18891111.1889F; 235 ctx.putFloat("b4", f); 236 float f_ = ctx.getFloat("b4", 1112.1112F); 237 assertTrue("Values do not match: "+f_+" "+f, f_ == f); 238 assertTrue("Object type does not match", ctx.getObject("b4", null) instanceof Float ); 239 240 double d = 181889333333.18989989891889D; 241 ctx.putDouble("b5", d); 242 double d_ = ctx.getDouble("b5", 11121112.11121112D); 243 assertTrue("Values do not match", d_ == d); 244 assertTrue("Object type does not match", ctx.getObject("b5", null) instanceof Double ); 245 246 boolean b = true; 247 ctx.putBoolean("b6", b); 248 boolean b_ = ctx.getBoolean("b6", false); 249 assertTrue("Values do not match", b_ == b); 250 assertTrue("Object type does not match", ctx.getObject("b6", null) instanceof Boolean ); 251 252 Font ff = new Font("Dialog", Font.ITALIC, 31); 253 ctx.putFont("b7", ff); 254 Font ff_ = ctx.getFont("b7", null); 255 assertEquals("Values do not match", ff, ff_ ); 256 assertTrue("Object type does not match", ctx.getObject("b7", null) instanceof Font); 257 258 Color c = new Color(10, 20, 30); 259 ctx.putColor("b8", c); 260 Color c_ = ctx.getColor("b8", new Color(1, 2, 3)); 261 assertTrue("Values do not match", c_.getRGB() == c.getRGB()); 262 assertTrue("Object type does not match", ctx.getObject("b8", null) instanceof Color); 263 264 URL u = new URL ("http://www.netbeans.org/download/"); 265 ctx.putURL("b9", u); 266 URL u_ = ctx.getURL("b9", null); 267 assertEquals("Values do not match", u_ , u); 268 assertTrue("Object type does not match", ctx.getObject("b9", null) instanceof URL ); 269 270 String sa[] = new String []{"aaa", "bbb", "ccc"}; 271 ctx.putStringArray("b0", '#', sa); 272 String sa_[] = ctx.getStringArray("b0", '#', new String []{"1", "2", "3"}); 273 assertEquals("Values do not match", sa_[0] , "aaa"); 274 assertEquals("Values do not match", sa_[1] , "bbb"); 275 assertEquals("Values do not match", sa_[2] , "ccc"); 276 277 } 278 279 280 public void testObjectRef() throws Exception { 281 BasicContext rootCtx = FileSystemContextFactory.createContext(getRoot()); 282 BasicContext basicCtx = rootCtx.createSubcontext("someCOTOXO"); 283 BasicContext basicCtx2 = basicCtx.createSubcontext("subocontexto"); 284 Context ctx = SpiUtils.createContext(basicCtx); 285 Context ctx2 = SpiUtils.createContext(basicCtx2); 286 287 Object o = new JLabel("my label Y"); 288 ctx.putObject("obj25", o); 289 ObjectRef or = SpiUtils.createObjectRef(basicCtx, "obj25"); 290 291 ctx.putObject("ref", or);; 292 Object val = ctx.getObject("ref", MY_NULL); 293 assertTrue("Retrieved object cannot be null", val != MY_NULL); 294 assertTrue("Retrived object is not the same", val instanceof JLabel); 295 assertTrue("Retrived object is not the same", ((JLabel)val).getText().equals("my label Y")); 296 297 ObjectRef or2 = ctx.getRef("ref"); 298 assertTrue("Retrieved object cannot be null", or2 != null); 299 assertTrue("Retrived object is not the same", or2.getBindingName().equals(or.getBindingName())); 300 assertTrue("Retrived object is not the same", or2.getContextAbsoluteName().equals(or.getContextAbsoluteName())); 301 302 ObjectRef or3 = ctx.getRef("obj25"); 303 assertTrue("Retrieved object must be null", or3 == null); 304 305 ObjectRef or4 = SpiUtils.createObjectRef(basicCtx, "ref"); 306 ctx2.putObject("refref", or4); 307 ObjectRef or5 = SpiUtils.createObjectRef(basicCtx2, "refref"); 308 ctx2.putObject("refrefref", or5); 309 val = ctx2.getObject("refrefref", MY_NULL); 310 assertTrue("Retrieved object cannot be null", val != MY_NULL); 311 assertTrue("Retrived object is not the same", val instanceof JLabel); 312 assertTrue("Retrived object is not the same", ((JLabel)val).getText().equals("my label Y")); 313 314 ObjectRef or6 = ctx2.getRef("refrefref"); 315 assertTrue("Retrieved object cannot be null", or6 != null); 316 assertTrue("Retrived object is not the same", or6.equals(or5)); 317 318 getRootContext().destroySubcontext("someCOTOXO"); 319 } 320 321 322 private static final char OPEN = '['; 327 private static final char CLOSE = ']'; 328 private static final int MAX_FILENAME_LENGTH = 50; 329 private static String escapeAndCut (String name) { 330 int maxLen = MAX_FILENAME_LENGTH; 331 332 String ename = escape(name); 333 if (ename.length() <= maxLen) return ename; 334 String hash = Integer.toHexString(ename.hashCode()); 335 maxLen = (maxLen > hash.length()) ? (maxLen-hash.length()) / 2 :1; 336 String start = ename.substring(0, maxLen); 337 String end = ename.substring(ename.length() - maxLen); 338 339 return start + hash + end; 340 } 341 342 private static String escape (String text) { 343 boolean spacenasty = text.startsWith(" ") || text.endsWith(" ") || text.indexOf(" ") != -1; int len = text.length (); 345 StringBuffer escaped = new StringBuffer (len); 346 for (int i = 0; i < len; i++) { 347 char c = text.charAt (i); 348 if (c == '/' || c == ':' || c == '\\' || c == OPEN || c == CLOSE || c == '<' || c == '>' || 350 c == '?' || c == '*' || c == '|' || 352 (c == ' ' && spacenasty) || 353 c == '.' || c == '"' || c < '\u0020' || c > '\u007E' || c == '#') { 354 escaped.append ('#'); 356 String hex = Integer.toString (c, 16).toUpperCase (); 357 if (hex.length () < 4) escaped.append ('0'); 358 if (hex.length () < 3) escaped.append ('0'); 359 if (hex.length () < 2) escaped.append ('0'); 360 escaped.append (hex); 361 } else { 362 escaped.append (c); 363 } 364 } 365 return escaped.toString (); 366 } 367 368 protected Context getRootContext() { 369 return Context.getDefault(); 370 } 371 372 protected FileObject getRoot() { 373 return Repository.getDefault ().getDefaultFileSystem ().getRoot (); 374 } 375 376 protected FileObject findResource(String resource) { 377 return Repository.getDefault ().getDefaultFileSystem().findResource (resource); 378 } 379 } 380 | Popular Tags |