Search in sources :

Example 1 with Boo

use of jodd.petite.tst.Boo in project jodd by oblac.

the class ScopeTest method testThreadLocalScope.

@Test
public void testThreadLocalScope() throws InterruptedException {
    final PetiteContainer pc = new PetiteContainer();
    pc.registerPetiteBean(Foo.class, "foo", null, null, false);
    pc.registerPetiteBean(Zoo.class, null, null, null, false);
    pc.registerPetiteBean(Boo.class, null, ThreadLocalScope.class, null, false);
    assertEquals(3, pc.getTotalBeans());
    assertEquals(2, pc.getTotalScopes());
    final Boo boo = (Boo) pc.getBean("boo");
    final Foo foo = (Foo) pc.getBean("foo");
    assertSame(boo.getFoo(), foo);
    final Semaphore sem = new Semaphore(1);
    sem.acquire();
    Thread thread = new Thread() {

        @Override
        public void run() {
            Boo boo2 = (Boo) pc.getBean("boo");
            Foo foo2 = (Foo) pc.getBean("foo");
            assertSame(foo2, foo);
            assertNotSame(boo2, boo);
            assertSame(foo2, boo2.getFoo());
            sem.release();
        }
    };
    thread.start();
    sem.acquire();
    sem.release();
}
Also used : Boo(jodd.petite.tst.Boo) Foo(jodd.petite.tst.Foo) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 2 with Boo

use of jodd.petite.tst.Boo in project jodd by oblac.

the class ShutdownTest method testSingletonDestroyMethods.

@Test
public void testSingletonDestroyMethods() {
    PetiteContainer pc = new PetiteContainer();
    pc.registerPetiteBean(Foo.class, null, null, null, false);
    pc.registerPetiteBean(Zoo.class, null, null, null, false);
    pc.registerPetiteBean(Boo.class, null, null, null, false);
    Boo boo = (Boo) pc.getBean("boo");
    assertEquals(0, boo.getCount2());
    pc.shutdown();
    assertEquals(2, boo.getCount2());
}
Also used : Boo(jodd.petite.tst.Boo) Test(org.junit.Test)

Example 3 with Boo

use of jodd.petite.tst.Boo in project jodd by oblac.

the class MiscTest method testNoAdd2WithCircDep.

@Test
public void testNoAdd2WithCircDep() {
    Foo.instanceCounter = 0;
    PetiteContainer pc = new PetiteContainer();
    pc.registerPetiteBean(Foo.class, null, null, null, false);
    pc.registerPetiteBean(Zoo.class, null, null, null, false);
    pc.registerPetiteBean(Boo.class, null, null, null, false);
    Boo boo = (Boo) pc.getBean("boo");
    Foo foo = (Foo) pc.getBean("foo");
    Zoo zoo = (Zoo) pc.getBean("zoo");
    assertNotNull(boo.getFoo());
    assertSame(foo, boo.getFoo());
    assertNotNull(zoo.boo);
    assertSame(boo, zoo.boo);
    assertSame(zoo, boo.zoo);
    Boo boo2 = (Boo) pc.getBean("boo");
    assertNotNull(boo2);
    assertSame(boo, boo2);
    assertFalse(boo.orders.isEmpty());
    assertNotNull(boo2.getFoo());
    assertSame(foo, boo2.getFoo());
    assertEquals(1, boo2.getFoo().hello());
    assertEquals(1, boo2.getFoo().getCounter());
    assertEquals("[first, second, third, init, beforeLast, last]", boo.orders.toString());
}
Also used : Boo(jodd.petite.tst.Boo) Foo(jodd.petite.tst.Foo) Zoo(jodd.petite.tst.Zoo) Test(org.junit.Test)

Example 4 with Boo

use of jodd.petite.tst.Boo in project jodd by oblac.

the class MiscTest method testAdd2WithCircDep.

@Test
public void testAdd2WithCircDep() {
    Foo.instanceCounter = 0;
    PetiteContainer pc = new PetiteContainer();
    pc.registerPetiteBean(Foo.class, null, null, null, false);
    pc.registerPetiteBean(Zoo.class, null, null, null, false);
    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);
    // circular dependency
    assertSame(zoo, boo.zoo);
    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());
    // init methods are called again due to re-add
    assertEquals(12, boo.orders.size());
}
Also used : Boo(jodd.petite.tst.Boo) Foo(jodd.petite.tst.Foo) Zoo(jodd.petite.tst.Zoo) Test(org.junit.Test)

Aggregations

Boo (jodd.petite.tst.Boo)4 Test (org.junit.Test)4 Foo (jodd.petite.tst.Foo)3 Zoo (jodd.petite.tst.Zoo)2 Semaphore (java.util.concurrent.Semaphore)1