Exemple #1
0
 @Test
 public void testAdd() {
   PetiteContainer pc = new PetiteContainer();
   Foo foo = new Foo();
   pc.addBean("foo", foo);
   Foo foo2 = (Foo) pc.getBean("foo");
   assertNotNull(foo2);
   assertSame(foo, foo2);
 }
Exemple #2
0
  @Test
  public void testAdd2WithCircDep() {
    Foo.instanceCounter = 0;
    PetiteContainer pc = new PetiteContainer();
    pc.registerBean(Foo.class);
    pc.registerBean(Zoo.class);

    Foo foo = (Foo) pc.getBean("foo");
    Boo boo = new Boo();
    assertNull(boo.getFoo());

    pc.addBean("boo", boo, null);
    assertNotNull(boo.getFoo());
    assertSame(foo, boo.getFoo());
    assertNotNull(boo.zoo);

    Zoo zoo = (Zoo) pc.getBean("zoo");
    assertNotNull(zoo.boo);
    assertSame(zoo, boo.zoo); // circular dependecy
    assertSame(boo, zoo.boo);

    Boo boo2 = (Boo) pc.getBean("boo");
    assertNotNull(boo2);
    assertSame(boo, boo2);
    assertFalse(boo.orders.isEmpty());
    assertEquals(6, boo.orders.size());
    assertEquals("[first, second, third, init, beforeLast, last]", boo.orders.toString());
    assertNotNull(boo2.getFoo());
    assertSame(foo, boo2.getFoo());
    assertEquals(1, boo2.getFoo().hello());
    assertEquals(1, boo2.getFoo().getCounter());

    pc.addBean("boo", boo);
    boo2 = (Boo) pc.getBean("boo");
    assertNotNull(boo2);
    assertSame(boo, boo2);
    assertNotNull(boo2.getFoo());
    assertSame(foo, boo2.getFoo());
    assertEquals(1, boo2.getFoo().hello());
    assertEquals(2, boo2.getFoo().getCounter());
    assertEquals(12, boo.orders.size()); // init methods are called again due to re-add
  }