1 23 24 package org.objectweb.fractal.julia.conform; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.NoSuchInterfaceException; 28 import org.objectweb.fractal.api.control.BindingController; 29 import org.objectweb.fractal.api.control.ContentController; 30 import org.objectweb.fractal.api.control.IllegalBindingException; 31 import org.objectweb.fractal.api.control.IllegalContentException; 32 import org.objectweb.fractal.api.factory.GenericFactory; 33 import org.objectweb.fractal.api.type.TypeFactory; 34 import org.objectweb.fractal.api.type.ComponentType; 35 import org.objectweb.fractal.api.type.InterfaceType; 36 37 import org.objectweb.fractal.julia.conform.components.I; 38 import org.objectweb.fractal.julia.conform.components.C; 39 import org.objectweb.fractal.julia.conform.components.J; 40 41 import org.objectweb.fractal.util.Fractal; 42 43 import java.util.Arrays ; 44 import java.util.HashSet ; 45 46 public class TestBindingController extends Test { 47 48 protected Component boot; 49 protected TypeFactory tf; 50 protected GenericFactory gf; 51 52 protected ComponentType t, u; 53 protected Component c, d, e; 54 55 protected boolean isTemplate; 56 57 61 public TestBindingController (final String name) { 62 super(name); 63 } 64 65 protected void setUp () throws Exception { 66 boot = Fractal.getBootstrapComponent(); 67 tf = Fractal.getTypeFactory(boot); 68 gf = Fractal.getGenericFactory(boot); 69 t = tf.createFcType(new InterfaceType[] { 70 tf.createFcItfType("server", I.class.getName(), false, false, false), 71 tf.createFcItfType("servers", I.class.getName(), false, false, true), 72 tf.createFcItfType("client", I.class.getName(), true, false, false), 73 tf.createFcItfType("clients", I.class.getName(), true, false, true) 74 }); 75 u = tf.createFcType(new InterfaceType[] { 76 tf.createFcItfType("serverI", I.class.getName(), false, true, false), 77 tf.createFcItfType("serverJ", J.class.getName(), false, false, false), 78 }); 79 setUpComponents(); 80 } 81 82 protected void setUpComponents () throws Exception { 83 c = gf.newFcInstance(t, "flatPrimitive", C.class.getName()); 84 d = gf.newFcInstance(t, "flatPrimitive", C.class.getName()); 85 e = gf.newFcInstance(u, "flatPrimitive", C.class.getName()); 86 } 87 88 92 public void testList () throws Exception { 93 BindingController bc = Fractal.getBindingController(c); 94 checkList(bc, new String [] { "client" }); 95 } 96 97 public void testBindLookupUnbind () throws Exception { 98 BindingController bc = Fractal.getBindingController(c); 99 bc.bindFc("client", d.getFcInterface("server")); 100 checkList(bc, new String [] { "client" }); 101 assertEquals(d.getFcInterface("server"), bc.lookupFc("client")); 102 bc.unbindFc("client"); 103 assertEquals(null, bc.lookupFc("client")); 104 } 105 106 public void testCollectionBindLookupUnbind () throws Exception { 107 BindingController bc = Fractal.getBindingController(c); 108 bc.bindFc("clients0", d.getFcInterface("server")); 109 checkList(bc, new String [] { "client", "clients0" }); 110 assertEquals(d.getFcInterface("server"), bc.lookupFc("clients0")); 111 bc.unbindFc("clients0"); 112 try { 113 assertEquals(null, bc.lookupFc("clients0")); 114 } catch (NoSuchInterfaceException e) { 115 checkList(bc, new String [] { "client" }); 116 } 117 } 118 119 protected void checkList (BindingController bc, String [] expected) { 120 String [] names = bc.listFc(); 121 HashSet nameSet = new HashSet (); 122 for (int i = 0; i < names.length; ++i) { 123 String name = names[i]; 124 if (!nameSet.add(name)) { 125 fail("Duplicated interface name: " + name); 126 } 127 } 128 assertEquals(new HashSet (Arrays.asList(expected)), nameSet); 129 } 130 131 135 public void testNoSuchInterfaceLookup () throws Exception { 136 try { 137 Fractal.getBindingController(c).lookupFc("c"); 138 fail(); 139 } catch (NoSuchInterfaceException e) { 140 } 141 } 142 143 public void testNoSuchInterfaceBind () throws Exception { 144 try { 145 Fractal.getBindingController(c).bindFc("c", d.getFcInterface("server")); 146 fail(); 147 } catch (NoSuchInterfaceException e) { 148 } 149 } 150 151 public void testNotAServerInterface () throws Exception { 152 try { 153 Fractal.getBindingController(c).bindFc("client", c.getFcInterface("client")); 154 fail(); 155 } catch (IllegalBindingException e) { 156 } 157 } 158 159 public void testWrongType () throws Exception { 160 try { 161 Fractal.getBindingController(c).bindFc("client", e.getFcInterface("serverJ")); 162 fail(); 163 } catch (IllegalBindingException e) { 164 } 165 } 166 167 public void testMandatoryToOptional () throws Exception { 168 try { 169 Fractal.getBindingController(c).bindFc("client", e.getFcInterface("serverI")); 170 fail(); 171 } catch (IllegalBindingException e) { 172 } 173 } 174 175 public void testAlreadyBound () throws Exception { 176 BindingController bc = Fractal.getBindingController(c); 177 bc.bindFc("client", d.getFcInterface("server")); 178 try { 179 bc.bindFc("client", d.getFcInterface("server")); 180 fail(); 181 } catch (IllegalBindingException e) { 182 } 183 bc.bindFc("clients0", d.getFcInterface("server")); 184 try { 185 bc.bindFc("clients0", d.getFcInterface("server")); 186 fail(); 187 } catch (IllegalBindingException e) { 188 } 189 } 190 191 public void testNoSuchInterfaceUnind () throws Exception { 192 try { 193 Fractal.getBindingController(c).unbindFc("c"); 194 fail(); 195 } catch (NoSuchInterfaceException e) { 196 } 197 } 198 199 public void testNotBound () throws Exception { 200 try { 201 Fractal.getBindingController(c).unbindFc("client"); 202 fail(); 203 } catch (IllegalBindingException e) { 204 } 205 } 206 207 209 public static class Composite extends TestBindingController { 210 211 protected Component r; 212 213 public Composite (String name) { 214 super(name); 215 } 216 217 protected void setUpComponents () throws Exception { 218 r = gf.newFcInstance(t, "composite", null); 219 c = gf.newFcInstance(t, "primitive", C.class.getName()); 220 d = gf.newFcInstance(t, "composite", null); 221 e = gf.newFcInstance(u, "composite", null); 222 Fractal.getContentController(r).addFcSubComponent(c); 223 Fractal.getContentController(r).addFcSubComponent(d); 224 Fractal.getContentController(r).addFcSubComponent(e); 225 } 226 227 public void testCompositeList () throws Exception { 228 BindingController bc = Fractal.getBindingController(r); 229 if (isTemplate) { 230 checkList(bc, new String [] { "client", "server", "factory" }); 231 } else { 232 checkList(bc, new String [] { "client", "server" }); 233 } 234 } 235 236 public void testCompositeExportBindLookupUnbind () throws Exception { 237 BindingController bc = Fractal.getBindingController(r); 238 bc.bindFc("server", c.getFcInterface("server")); 239 if (isTemplate) { 240 checkList(bc, new String [] { "client", "server", "factory" }); 241 } else { 242 checkList(bc, new String [] { "client", "server" }); 243 } 244 assertEquals(c.getFcInterface("server"), bc.lookupFc("server")); 245 bc.unbindFc("server"); 246 assertEquals(null, bc.lookupFc("server")); 247 } 248 249 public void testCompositeCollectionExportBindLookupUnbind () throws Exception { 250 BindingController bc = Fractal.getBindingController(r); 251 bc.bindFc("servers0", c.getFcInterface("server")); 252 if (isTemplate) { 253 checkList(bc, new String [] { "client", "server", "servers0", "factory" }); 254 } else { 255 checkList(bc, new String [] { "client", "server", "servers0" }); 256 } 257 assertEquals(c.getFcInterface("server"), bc.lookupFc("servers0")); 258 bc.unbindFc("servers0"); 259 try { 260 assertEquals(null, bc.lookupFc("servers0")); 261 } catch (NoSuchInterfaceException e) { 262 checkList(bc, new String [] { "client", "server" }); 263 } 264 } 265 266 public void testCompositeImportBindLookupUnbind () throws Exception { 267 ContentController cc = Fractal.getContentController(r); 268 BindingController bc = Fractal.getBindingController(d); 269 bc.bindFc("client", cc.getFcInternalInterface("client")); 270 if (isTemplate) { 271 checkList(bc, new String [] { "client", "server", "factory" }); 272 } else { 273 checkList(bc, new String [] { "client", "server" }); 274 } 275 assertEquals(cc.getFcInternalInterface("client"), bc.lookupFc("client")); 276 bc.unbindFc("client"); 277 assertEquals(null, bc.lookupFc("client")); 278 } 279 280 public void testCompositeCollectionImportBindLookupUnbind () throws Exception { 281 ContentController cc = Fractal.getContentController(r); 282 BindingController bc = Fractal.getBindingController(d); 283 bc.bindFc("clients0", cc.getFcInternalInterface("client")); 284 if (isTemplate) { 285 checkList(bc, new String [] { "client", "clients0", "server", "factory" }); 286 } else { 287 checkList(bc, new String [] { "client", "clients0", "server" }); 288 } 289 assertEquals(cc.getFcInternalInterface("client"), bc.lookupFc("clients0")); 290 bc.unbindFc("clients0"); 291 try { 292 assertEquals(null, bc.lookupFc("clients0")); 293 } catch (NoSuchInterfaceException e) { 294 checkList(bc, new String [] { "client", "server" }); 295 } 296 } 297 298 public void testCompositeSelfBindLookupUnbind () throws Exception { 299 Object itf = 300 Fractal.getContentController(r).getFcInternalInterface("client"); 301 BindingController bc = Fractal.getBindingController(r); 302 bc.bindFc("server", itf); 303 if (isTemplate) { 304 checkList(bc, new String [] { "client", "server", "factory" }); 305 } else { 306 checkList(bc, new String [] { "client", "server" }); 307 } 308 assertEquals(itf, bc.lookupFc("server")); 309 bc.unbindFc("server"); 310 assertEquals(null, bc.lookupFc("server")); 311 } 312 313 public void testCompositeCollectionSelfBindLookupUnbind () throws Exception { 314 Object itf = 315 Fractal.getContentController(r).getFcInternalInterface("clients0"); 316 BindingController bc = Fractal.getBindingController(r); 317 bc.bindFc("servers0", itf); 318 if (isTemplate) { 319 checkList(bc, new String [] { "client", "server", "servers0", "factory" }); 320 } else { 321 checkList(bc, new String [] { "client", "server", "servers0" }); 322 } 323 assertEquals(itf, bc.lookupFc("servers0")); 324 bc.unbindFc("servers0"); 325 try { 326 assertEquals(null, bc.lookupFc("servers0")); 327 } catch (NoSuchInterfaceException e) { 328 checkList(bc, new String [] { "client", "server" }); 329 } 330 } 331 332 public void testCompositeNoSuchInterface () throws Exception { 333 try { 334 Fractal.getBindingController(r).lookupFc("c"); 335 fail(); 336 } catch (NoSuchInterfaceException e) { 337 } 338 } 339 340 public void testAlreadyBound () throws Exception { 341 super.testAlreadyBound(); 342 BindingController bc = Fractal.getBindingController(d); 343 bc.bindFc("client", c.getFcInterface("server")); 344 try { 345 bc.bindFc("client", c.getFcInterface("server")); 346 fail(); 347 } catch (IllegalBindingException e) { 348 } 349 bc.bindFc("clients0", c.getFcInterface("server")); 350 try { 351 bc.bindFc("clients0", c.getFcInterface("server")); 352 fail(); 353 } catch (IllegalBindingException e) { 354 } 355 } 356 357 public void testNotBound () throws Exception { 358 super.testNotBound(); 359 try { 360 Fractal.getBindingController(d).unbindFc("client"); 361 fail(); 362 } catch (IllegalBindingException e) { 363 } 364 } 365 366 public void testInvalidExportBinding () throws Exception { 367 Fractal.getContentController(r).removeFcSubComponent(c); 368 BindingController bc = Fractal.getBindingController(r); 369 try { 370 bc.bindFc("server", c.getFcInterface("server")); 371 fail(); 372 } catch (IllegalBindingException e) { 373 } 374 } 375 376 public void testInvalidNormalBinding () throws Exception { 377 Fractal.getContentController(r).removeFcSubComponent(d); 378 BindingController bc = Fractal.getBindingController(c); 379 try { 380 bc.bindFc("client", d.getFcInterface("server")); 381 fail(); 382 } catch (IllegalBindingException e) { 383 } 384 } 385 386 public void testInvalidImportBinding () throws Exception { 387 ContentController cc = Fractal.getContentController(r); 388 BindingController bc = Fractal.getBindingController(d); 389 cc.removeFcSubComponent(d); 390 try { 391 bc.bindFc("client", cc.getFcInternalInterface("client")); 392 fail(); 393 } catch (IllegalBindingException e) { 394 } 395 } 396 397 public void testWouldCreateInvalidExportBinding () throws Exception { 398 Fractal.getBindingController(r).bindFc("server", c.getFcInterface("server")); 399 try { 400 Fractal.getContentController(r).removeFcSubComponent(c); 401 fail(); 402 } catch (IllegalContentException e) { 403 } 404 } 405 406 public void testWouldCreateInvalidLocalBinding () throws Exception { 407 Fractal.getBindingController(c).bindFc("client", d.getFcInterface("server")); 408 try { 409 Fractal.getContentController(r).removeFcSubComponent(c); 410 fail(); 411 } catch (IllegalContentException e) { 412 } 413 } 414 415 public void testWouldCreateInvalidImportBinding () throws Exception { 416 ContentController cc = Fractal.getContentController(r); 417 Fractal.getBindingController(d).bindFc("client", cc.getFcInternalInterface("client")); 418 try { 419 cc.removeFcSubComponent(d); 420 fail(); 421 } catch (IllegalContentException e) { 422 } 423 } 424 } 425 426 public static class Template extends TestBindingController { 427 428 public Template (String name) { 429 super(name); 430 isTemplate = true; 431 } 432 433 protected void setUpComponents () throws Exception { 434 c = gf.newFcInstance(t, "flatPrimitiveTemplate", new Object [] { "flatPrimitive", C.class.getName() }); 435 d = gf.newFcInstance(t, "flatPrimitiveTemplate", new Object [] { "flatPrimitive", C.class.getName() }); 436 e = gf.newFcInstance(u, "flatPrimitiveTemplate", new Object [] { "flatPrimitive", C.class.getName() }); 437 } 438 } 439 440 public static class CompositeTemplate extends Composite { 441 442 public CompositeTemplate (String name) { 443 super(name); 444 isTemplate = true; 445 } 446 447 protected void setUpComponents () throws Exception { 448 r = gf.newFcInstance(t, "compositeTemplate", new Object [] { "composite", null }); 449 c = gf.newFcInstance(t, "primitiveTemplate", new Object [] { "primitive", C.class.getName() }); 450 d = gf.newFcInstance(t, "compositeTemplate", new Object [] { "composite", null }); 451 e = gf.newFcInstance(u, "compositeTemplate", new Object [] { "composite", null }); 452 Fractal.getContentController(r).addFcSubComponent(c); 453 Fractal.getContentController(r).addFcSubComponent(d); 454 Fractal.getContentController(r).addFcSubComponent(e); 455 } 456 457 public void testInstanceBinding () throws Exception { 458 ContentController cc = Fractal.getContentController(r); 459 Fractal.getContentController(e).addFcSubComponent(c); 460 461 Fractal.getBindingController(r).bindFc("server", c.getFcInterface("server")); 462 Fractal.getBindingController(c).bindFc("client", d.getFcInterface("server")); 463 Fractal.getBindingController(d).bindFc("client", cc.getFcInternalInterface("client")); 464 465 Fractal.getBindingController(r).bindFc("servers0", c.getFcInterface("servers0")); 466 Fractal.getBindingController(c).bindFc("clients0", d.getFcInterface("servers0")); 467 Fractal.getBindingController(d).bindFc("clients0", cc.getFcInternalInterface("clients0")); 468 469 Component rComp = Fractal.getFactory(r).newFcInstance(); 470 471 cc = Fractal.getContentController(rComp); 472 Component[] comps = cc.getFcSubComponents(); 473 474 Component cComp = comps[0]; 475 Component dComp = comps[1]; 476 477 assertEquals( 478 Fractal.getBindingController(rComp).lookupFc("server"), 479 cComp.getFcInterface("server")); 480 assertEquals( 481 Fractal.getBindingController(cComp).lookupFc("client"), 482 dComp.getFcInterface("server")); 483 assertEquals( 484 Fractal.getBindingController(dComp).lookupFc("client"), 485 cc.getFcInternalInterface("client")); 486 487 assertEquals( 488 Fractal.getBindingController(rComp).lookupFc("servers0"), 489 cComp.getFcInterface("servers0")); 490 assertEquals( 491 Fractal.getBindingController(cComp).lookupFc("clients0"), 492 dComp.getFcInterface("servers0")); 493 assertEquals( 494 Fractal.getBindingController(dComp).lookupFc("clients0"), 495 cc.getFcInternalInterface("clients0")); 496 } 497 } 498 } 499 | Popular Tags |