/** * Resolve a component from its class. * @param componentClass Type of the component * @return Component */ default <C> C resolve(final Class<C> componentClass) { final String normalizedId = StringUtil.first2LowerCase(componentClass.getSimpleName()); return resolve(normalizedId, componentClass); } }
/** * Inject all the definitions provided by components. * @return a stream of definitions */ public Stream<Definition> createDefinitionsFromComponents() { return componentSpace.keySet() .stream() .map(key -> componentSpace.resolve(key, Component.class)) .filter(component -> DefinitionProvider.class.isAssignableFrom(component.getClass())) .flatMap(component -> ((DefinitionProvider) component).get(definitionSpace).stream()) .map(defitionSupplier -> defitionSupplier.get(definitionSpace)); }
private void testBioManager(final AppConfig appConfig) { try (AutoCloseableApp app = new AutoCloseableApp(appConfig)) { assertEquals(app, app); assertTrue(app.getComponentSpace().contains("bioManager")); final BioManager bioManager = app.getComponentSpace().resolve(BioManager.class); final int res = bioManager.add(1, 2, 3); assertEquals(366, res); assertTrue(bioManager.isActive()); } } }
@Test public final void testUnwrapp() { final AopPlugin aopPlugin = getApp().getConfig().getBootConfig().getAopPlugin(); final F f = getApp().getComponentSpace().resolve(F.class); // Il y a des aspects sur la classe donc elle doit être dewrappable assertNotEquals(F.class.getName(), f.getClass().getName()); assertEquals(F.class.getName(), aopPlugin.unwrap(f).getClass().getName()); // Il y a pas d'aspect final A myA = getApp().getComponentSpace().resolve(A.class); assertEquals(A.class.getName(), myA.getClass().getName()); assertEquals(A.class.getName(), aopPlugin.unwrap(myA).getClass().getName()); }
@Override public List<? extends Definition> provideDefinitions(final DefinitionSpace definitionSpace) { // we need to unwrap the component to scan the real class and not the enhanced version final AopPlugin aopPlugin = Home.getApp().getConfig().getBootConfig().getAopPlugin(); return Home.getApp().getComponentSpace().keySet() .stream() .flatMap(id -> createDaemonDefinitions(Home.getApp().getComponentSpace().resolve(id, Component.class), aopPlugin).stream()) .collect(Collectors.toList()); }
@Ignore // ignored for now we need heartbeat of node update to be parametized for shorter tests void testUpdate() throws InterruptedException { final NodeManager nodeManager = Home.getApp().getComponentSpace().resolve(NodeManager.class); // --- final Instant firstTouch = nodeManager.find("nodeTest1").get().getLastTouch(); Thread.sleep(7 * 1000L); final Instant secondTouch = nodeManager.find("nodeTest1").get().getLastTouch(); // --- Assertions.assertTrue(secondTouch.isAfter(firstTouch)); }
@Override public List<? extends Definition> provideDefinitions(final DefinitionSpace definitionSpace) { // we need to unwrap the component to scan the real class and not the enhanced version final AopPlugin aopPlugin = Home.getApp().getConfig().getBootConfig().getAopPlugin(); return Home.getApp().getComponentSpace().keySet() .stream() .flatMap(id -> createEventSubscriptions(id, Home.getApp().getComponentSpace().resolve(id, Component.class), aopPlugin).stream()) .collect(Collectors.toList()); }
@Test void testRegisterNode() { final NodeManager nodeManager = Home.getApp().getComponentSpace().resolve(NodeManager.class); final List<Node> nodesWithDbSkill = nodeManager.locateSkills("db"); final List<Node> nodesWithOtherSkill = nodeManager.locateSkills("other"); // --- Assertions.assertEquals(1, nodesWithDbSkill.size()); Assertions.assertEquals(0, nodesWithOtherSkill.size()); }
@Override public List<? extends Definition> provideDefinitions(final DefinitionSpace definitionSpace) { // here all // we need to unwrap the component to scan the real class and not the enhanced version final AopPlugin aopPlugin = Home.getApp().getConfig().getBootConfig().getAopPlugin(); return Home.getApp().getComponentSpace().keySet() .stream() .flatMap(id -> Stream.concat( //health HealthAnalyticsUtil.createHealthCheckDefinitions(id, Home.getApp().getComponentSpace().resolve(id, Component.class), aopPlugin).stream(), //metrics MetricAnalyticsUtil.createMetricDefinitions(id, Home.getApp().getComponentSpace().resolve(id, Component.class), aopPlugin).stream())) .collect(Collectors.toList()); }
@Test public final void testNo() { final Computer computer = getApp().getComponentSpace().resolve(Computer.class); assertEquals(66, computer.no(66)); }
@Test public void testComponentSpace() { final ComponentSpace componentSpace = Home.getApp().getComponentSpace(); assertEquals(componentSpace.keySet().size(), 2 + 3); //ParamManager and ResourceManager are automaticaly declared final DiscoveryB discoveryB = componentSpace.resolve(DiscoveryB.class); //--- assertTrue(DiscoveryA.class.getName().equals(discoveryB.getClass().getName())); //--- final DiscoveryD discoveryD = componentSpace.resolve(DiscoveryD.class); assertTrue(discoveryD.isTrue()); }
@Test public void testProxyWithInterfaceMyException() { Assertions.assertThrows(MyException.class, () -> { b = getApp().getComponentSpace().resolve("b", B.class); b.throwMyException(); }); } }
@Test public final void testOneMoreOnMethod() { final Computer comp = getApp().getComponentSpace().resolve(Computer.class); //On vérifie que l'intercepteur ajoute bien 1 à la somme de 2+3 assertEquals(6, comp.sum(2, 3)); }
@Test public final void testOneMoreTenMore() { final Computer comp = getApp().getComponentSpace().resolve(Computer.class); //On vérifie que l'intercepteur ajoute bien 1 à la somme de 2+3 assertEquals(17, comp.multi(2, 3)); }
@Test public void testBeanMyException() { Assertions.assertThrows(MyException.class, () -> { a = getApp().getComponentSpace().resolve("a", A.class); a.throwMyException(); }); }
localeManager = Home.getApp().getComponentSpace().resolve(LocaleManager.class); locale = localeManager.getCurrentLocale(); msg = localeManager.getMessage(key, locale);
@Test public void testInjectPluginsConstructorOrder() { final AppConfig appConfig = createHomeWithInjectPluginsConstructor(false); try (AutoCloseableApp app = new AutoCloseableApp(appConfig)) { final FunctionManager functionManager = app.getComponentSpace().resolve(FunctionManager.class); assertEquals(26, functionManager.computeAll(3)); } }
@Test public void testInjectPluginsAttributeOrder() { final AppConfig appConfig = createHomeWithInjectPluginsAttribute(false); try (AutoCloseableApp app = new AutoCloseableApp(appConfig)) { final FunctionManager functionManager = app.getComponentSpace().resolve(FunctionManager.class); assertEquals(26, functionManager.computeAll(3)); } }
@Test public void testProxyWithObjectInterface() { c = getApp().getComponentSpace().resolve("c", C.class); assertTrue(c.isInitialized()); assertFalse(c.isFinalized()); }