/** * Configures {@link jodd.petite.PetiteContainer} with specified class path. */ public void configure() { long elapsed = System.currentTimeMillis(); final ClassScanner classScanner = new ClassScanner(); classScanner.detectEntriesMode(true); classScanner.scanDefaultClasspath(); classScannerConsumers.accept(classScanner); registerAsConsumer(classScanner); try { classScanner.start(); } catch (Exception ex) { throw new PetiteException("Scan classpath error", ex); } elapsed = System.currentTimeMillis() - elapsed; log.info("Petite configured in " + elapsed + " ms. Total beans: " + container.beansCount()); }
@Test void testShutdown() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(SomeService.class, null, null, null, false, null); pc.registerPetiteBean(PojoBean.class, "pojo", null, null, false, null); assertEquals(2, pc.beansCount()); pc.shutdown(); assertEquals(0, pc.beansCount()); } }
@Test void testSelf() { PetiteContainer pc = new PetiteContainer(); pc.addSelf(); assertEquals(1, pc.beansCount()); PetiteContainer pc2 = pc.getBean(PetiteContainer.PETITE_CONTAINER_REF_NAME); assertEquals(pc2, pc); }
@Test void testInterface() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(Foo.class, null, null, null, false, null); pc.registerPetiteBean(DefaultIoo.class, "ioo", null, null, false, null); assertEquals(2, pc.beansCount()); Ioo ioo = pc.getBean("ioo"); assertNotNull(ioo); assertNotNull(ioo.getFoo()); assertEquals(DefaultIoo.class, ioo.getClass()); }
@Test void testTwo() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(DefaultBizImpl.class, null, null, null, false, null); assertEquals(1, pc.beansCount()); Object bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertFalse(bizI instanceof DefaultBiz); assertTrue(bizI instanceof DefaultBizImpl); //pc = new PetiteContainer(); // same container!!! pc.registerPetiteBean(DefaultBiz.class, null, null, null, false, null); // override! instance will be removed from the scope assertEquals(1, pc.beansCount()); bizI = pc.getBean("biz"); assertTrue(bizI instanceof Biz); assertFalse(bizI instanceof DefaultBizImpl); assertTrue(bizI instanceof DefaultBiz); }
@Test void testSingleImplementation() { final PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(BizUser.class, null, null, null, false, null); pc.registerPetiteBean(OneBiz.class, null, null, null, false, null); assertEquals(2, pc.beansCount()); final BizUser bizUser = pc.getBean(BizUser.class); assertNotNull(bizUser); assertNotNull(bizUser.biz); assertTrue(bizUser.biz instanceof OneBiz); } }
@Test void testWire_withCache() { PetiteContainer pc = new PetiteContainer(); pc.setExternalsCache(TypeCache.createDefault()); pc.registerPetiteBean(InBean.class); BeBean beBean = new BeBean(); assertNull(beBean.inBean); pc.wire(beBean); assertNotNull(beBean.inBean); assertNotNull(pc.lookupBeanDefinition("inBean")); assertNull(pc.lookupBeanDefinition("beBean")); assertEquals(1, pc.beansCount()); // repeating beBean = new BeBean(); pc.wire(beBean); assertNotNull(beBean.inBean); assertEquals(1, pc.beansCount()); assertEquals(1, pc.externalsCache.size()); } }
@Test void testManualDefinitionUsingAnnotations() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(SomeService.class, null, null, null, false, null); pc.registerPetiteBean(PojoAnnBean.class, "pojo", null, null, true, null); assertEquals(2, pc.beansCount()); PojoAnnBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertNull(pojoBean.service); assertNull(pojoBean.service2); assertEquals(0, pojoBean.count); }
@Test void testOptionalAndNotAllReferences() { PetiteContainer pc = new PetiteContainer(); pc.config().setDefaultWiringMode(WiringMode.OPTIONAL); pc.config().setUseFullTypeNames(false); pc.config().setLookupReferences(PetiteReferenceType.NAME); registerBean(pc, Koo.class); registerBean(pc, Joo.class); assertEquals(2, pc.beansCount()); Koo koo = pc.getBean(Koo.class); assertNotNull(koo); Joo joo = pc.getBean(Joo.class); assertNotNull(joo); assertNull(koo.someNoJooName); assertNotNull(koo.joo); koo = pc.getBean(Koo.class.getName()); assertNull(koo); joo = pc.getBean(Joo.class.getName()); assertNull(joo); } }
@Test void testManualRegistrationUsingAnnotations() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(SomeService.class, null, null, null, false, null); pc.registerPetiteBean(PojoAnnBean.class, "pojo", null, null, false, null); assertEquals(2, pc.beansCount()); PojoAnnBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertSame(ss, pojoBean.service); assertSame(ss, pojoBean.service2); assertEquals(1, pojoBean.count); }
@Test void testWire() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(InBean.class); BeBean beBean = new BeBean(); assertNull(beBean.inBean); pc.wire(beBean); assertNotNull(beBean.inBean); assertNotNull(pc.lookupBeanDefinition("inBean")); assertNull(pc.lookupBeanDefinition("beBean")); assertEquals(1, pc.beansCount()); assertEquals(0, pc.externalsCache.size()); }
@Test void testFullTypeProperty() { PetiteContainer pc = new PetiteContainer(); pc.config().setUseFullTypeNames(true); registerBean(pc, Koo.class); registerBean(pc, Joo.class); assertEquals(2, pc.beansCount()); assertNull(pc.getBean("koo")); assertNull(pc.getBean("joo")); Koo koo = pc.getBean(Koo.class); assertNotNull(koo); Joo joo = pc.getBean(Joo.class); assertNotNull(joo); koo = pc.getBean(Koo.class.getName()); assertNotNull(koo); joo = pc.getBean(Joo.class.getName()); assertNotNull(joo); assertNotNull(koo.joo); assertNotNull(koo.someNoJooName); assertEquals(joo, koo.joo); assertEquals(joo, koo.someNoJooName); }
@Test @DisabledOnJava(value = 9, description = "Automagic configuration only works with MR-JAR jars as they don't work in exploded mode.") void testContainer() { PetiteContainer pc = new PetiteContainer(); AutomagicPetiteConfigurator petiteConfigurator = new AutomagicPetiteConfigurator(pc); petiteConfigurator.withScanner(classScanner -> classScanner .excludeAllEntries(true) .includeEntries("jodd.petite.fixtures.*") .excludeEntries("jodd.petite.fixtures.data.*", "jodd.petite.fixtures.tst3.*", "jodd.petite.fixtures.tst.Ses") .excludeEntries( "jodd.petite.fixtures.data.*", "jodd.petite.fixtures.tst6.*", "jodd.petite.fixtures.tst.Ses", "*Public*", "*Secret*", "*$*", "jodd.petite.proxy.*", "jodd.petite.fixtures.rainbow.*")); petiteConfigurator.configure(); assertEquals(1, pc.beansCount()); assertEquals(1, pc.scopesCount()); assertEquals(0, Foo.instanceCounter); Foo foo = pc.getBean("foo"); assertNotNull(foo); }
@Test void testManualDefinition() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(SomeService.class, null, null, null, false, null); pc.registerPetiteBean(PojoBean.class, "pojo", null, null, true, null); assertEquals(2, pc.beansCount()); pc.registerPetiteCtorInjectionPoint("pojo", null, null); pc.registerPetitePropertyInjectionPoint("pojo", "service", "someService"); pc.registerPetiteMethodInjectionPoint("pojo", "injectService", null, new String[] {"someService"}); pc.registerPetiteInitMethods("pojo", POST_INITIALIZE, "init"); PojoBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertSame(ss, pojoBean.service); assertSame(ss, pojoBean.service2); assertEquals(1, pojoBean.count); }
@Test void testManualRegistrationUsingAnnotations2() { PetiteContainer pc = new PetiteContainer(); PetiteRegistry.of(pc).bean(SomeService.class).register(); PetiteRegistry.of(pc).bean(PojoAnnBean.class).name("pojo").register(); assertEquals(2, pc.beansCount()); PojoAnnBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertSame(ss, pojoBean.service); assertSame(ss, pojoBean.service2); assertEquals(1, pojoBean.count); }
@Test void testManualDefinitionUsingAnnotations2() { PetiteContainer pc = new PetiteContainer(); PetiteRegistry.of(pc).bean(SomeService.class).register(); PetiteRegistry.of(pc).bean(PojoAnnBean.class).name("pojo").define().register(); assertEquals(2, pc.beansCount()); PojoAnnBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertNull(pojoBean.service); assertNull(pojoBean.service2); assertEquals(0, pojoBean.count); }
@Test void testCreate() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(Foo.class, null, null, null, false, null); pc.registerPetiteBean(Zoo.class, null, null, null, false, null); pc.registerPetiteBean(Boo.class, null, null, null, false, null); assertEquals(3, pc.beansCount()); assertEquals(1, pc.scopesCount()); assertEquals(0, Foo.instanceCounter); Boo boo = pc.createBean(Boo.class); assertNotNull(boo); assertNotNull(boo.getFoo()); assertNotNull(boo.zoo); assertNotSame(boo.zoo.boo, boo); // not equal instances!!! assertEquals(1, boo.getFoo().hello()); assertEquals(1, boo.getCount()); }
@Test void testAutowire() { PetiteContainer pc = new PetiteContainer(); pc.registerPetiteBean(Goo.class, null, ProtoScope.class, null, false, null); pc.registerPetiteBean(Loo.class, null, null, null, false, null); assertEquals(2, pc.beansCount()); Goo goo = pc.getBean("goo"); assertNotNull(goo); assertNotNull(goo.looCustom); assertNull(goo.foo); pc.registerPetiteBean(Foo.class, null, null, null, false, null); goo = pc.getBean("goo"); assertNotNull(goo); assertNotNull(goo.looCustom); assertNull(goo.foo); pc = new PetiteContainer(); pc.config().setDefaultWiringMode(WiringMode.AUTOWIRE); pc.registerPetiteBean(Goo.class, null, ProtoScope.class, null, false, null); pc.registerPetiteBean(Loo.class, null, null, null, false, null); pc.registerPetiteBean(Foo.class, null, null, null, false, null); goo = pc.getBean("goo"); assertNotNull(goo); assertNotNull(goo.looCustom); assertNotNull(goo.foo); pc.removeBean(Goo.class); }
@Test void testManualDefinition2() { PetiteRegistry petiteRegistry = PetiteRegistry.of(new PetiteContainer()); petiteRegistry.bean(SomeService.class).register(); petiteRegistry.bean(PojoBean.class).name("pojo").define().register(); assertEquals(2, petiteRegistry.petiteContainer().beansCount()); petiteRegistry.wire("pojo").ctor().bind(); petiteRegistry.wire("pojo").property("service").ref("someService").bind(); petiteRegistry.wire("pojo").method("injectService").ref("someService").bind(); petiteRegistry.init("pojo").invoke(POST_INITIALIZE).methods("init").register(); PojoBean pojoBean = petiteRegistry.petiteContainer().getBean("pojo"); SomeService ss = petiteRegistry.petiteContainer().getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertSame(ss, pojoBean.service); assertSame(ss, pojoBean.service2); assertEquals(1, pojoBean.count); }
@Test void testManualRegistration2() { PetiteContainer pc = new PetiteContainer(); PetiteRegistry.of(pc).bean(SomeService.class).register(); PetiteRegistry.of(pc).bean(PojoBean.class).name("pojo").register(); assertEquals(2, pc.beansCount()); PetiteRegistry.of(pc).wire("pojo").ctor().bind(); PetiteRegistry.of(pc).wire("pojo").property("service").ref("someService").bind(); PetiteRegistry.of(pc).wire("pojo").method("injectService").ref("someService").bind(); PetiteRegistry.of(pc).init("pojo").invoke(POST_INITIALIZE).methods("init").register(); PojoBean pojoBean = pc.getBean("pojo"); SomeService ss = pc.getBean("someService"); assertNotNull(pojoBean); assertNotNull(ss); assertSame(ss, pojoBean.fservice); assertSame(ss, pojoBean.service); assertSame(ss, pojoBean.service2); assertEquals(1, pojoBean.count); }