Search in sources :

Example 1 with DependencyInjector

use of cn.taketoday.beans.factory.support.DependencyInjector in project today-infrastructure by TAKETODAY.

the class ParameterHandlerTests method resolveParameter.

@Test
void resolveParameter() throws NoSuchMethodException {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.register(ParameterHandlerBean.class);
        context.refresh();
        StandardBeanFactory beanFactory = context.getBeanFactory();
        DependencyInjector argumentsResolver = new DependencyInjector(beanFactory);
        Method test = ParameterHandlerTests.class.getDeclaredMethod("test", ParameterHandlerBean.class);
        Object[] args = argumentsResolver.resolveArguments(test);
        assertThat(args).hasSize(1);
        assertThat(args[0]).isInstanceOf(ParameterHandlerBean.class);
    }
}
Also used : DependencyInjector(cn.taketoday.beans.factory.support.DependencyInjector) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) Method(java.lang.reflect.Method) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with DependencyInjector

use of cn.taketoday.beans.factory.support.DependencyInjector in project today-framework by TAKETODAY.

the class ContextUtilsTest method testResolveParameter.

@Test
@Disabled
@SuppressWarnings("unchecked")
public void testResolveParameter() throws Exception {
    try (StandardApplicationContext applicationContext = new StandardApplicationContext("cn.taketoday.util")) {
        final BeanFactory beanFactory = applicationContext.getBeanFactory();
        final ConfigurableEnvironment environment = applicationContext.getEnvironment();
        // placeHolder
        PropertySources propertySources = environment.getPropertySources();
        Properties properties = new Properties();
        propertySources.addLast(new PropertiesPropertySource("properties", properties));
        properties.setProperty("placeHolder", "12345");
        final Constructor<Config>[] declaredConstructors = (Constructor<Config>[]) Config.class.getDeclaredConstructors();
        Constructor<Config> constructor = null;
        for (final Constructor<Config> declaredConstructor : declaredConstructors) {
            if (declaredConstructor.getParameterCount() > 0) {
                constructor = declaredConstructor;
            }
        }
        // properties.list(System.err);
        // System.err.println(properties.get("placeHolder"));
        Object[] parameters = new DependencyInjector(beanFactory).resolveArguments(constructor);
        Config newInstance = constructor.newInstance(parameters);
        assert parameters.length == 14;
        assert parameters[0] instanceof UserModel;
        UserModel userModel = (UserModel) parameters[0];
        assert userModel.getAge() == 21;
        assert userModel.getUserId().equals("666");
        assert userModel.getUserName().equals("TODAY");
        assert parameters[1] instanceof Properties;
        // collection > 5
        final List<Object> objectList = applicationContext.getBeans(Object.class);
        final int allSize = objectList.size();
        final Object collectionParameter = parameters[5];
        assert collectionParameter instanceof Collection;
        final int size = ((Collection<?>) collectionParameter).size();
        assert size == allSize;
        final Object setParameter = parameters[6];
        assert setParameter instanceof Set;
        assert ((Set<?>) setParameter).size() == allSize;
        final Object hashSetParameter = parameters[7];
        assert hashSetParameter instanceof HashSet;
        assert ((Set<?>) hashSetParameter).size() == allSize;
        final Object listParameter = parameters[8];
        assert listParameter instanceof List;
        assert ((List<?>) listParameter).size() == allSize;
        final Object arrayListParameter = parameters[9];
        assert arrayListParameter instanceof ArrayList;
        assert ((ArrayList<?>) arrayListParameter).size() == allSize;
        final Object mapParameter = parameters[10];
        assert mapParameter instanceof Map;
        assert ((Map) mapParameter).size() == allSize;
        final Object hashMapParameter = parameters[11];
        assert hashMapParameter instanceof HashMap;
        assert ((HashMap) hashMapParameter).size() == allSize;
    }
}
Also used : DependencyInjector(cn.taketoday.beans.factory.support.DependencyInjector) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) NoArgsConstructor(lombok.NoArgsConstructor) ArrayList(java.util.ArrayList) Properties(java.util.Properties) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) PropertiesPropertySource(cn.taketoday.core.env.PropertiesPropertySource) BeanFactory(cn.taketoday.beans.factory.BeanFactory) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with DependencyInjector

use of cn.taketoday.beans.factory.support.DependencyInjector in project today-framework by TAKETODAY.

the class ParameterHandlerTests method resolveParameter.

@Test
void resolveParameter() throws NoSuchMethodException {
    try (StandardApplicationContext context = new StandardApplicationContext()) {
        context.register(ParameterHandlerBean.class);
        context.refresh();
        StandardBeanFactory beanFactory = context.getBeanFactory();
        DependencyInjector argumentsResolver = new DependencyInjector(beanFactory);
        Method test = ParameterHandlerTests.class.getDeclaredMethod("test", ParameterHandlerBean.class);
        Object[] args = argumentsResolver.resolveArguments(test);
        assertThat(args).hasSize(1);
        assertThat(args[0]).isInstanceOf(ParameterHandlerBean.class);
    }
}
Also used : DependencyInjector(cn.taketoday.beans.factory.support.DependencyInjector) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) Method(java.lang.reflect.Method) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 4 with DependencyInjector

use of cn.taketoday.beans.factory.support.DependencyInjector in project today-infrastructure by TAKETODAY.

the class ContextUtilsTest method testResolveParameter.

@Test
@Disabled
@SuppressWarnings("unchecked")
public void testResolveParameter() throws Exception {
    try (StandardApplicationContext applicationContext = new StandardApplicationContext("cn.taketoday.util")) {
        final BeanFactory beanFactory = applicationContext.getBeanFactory();
        final ConfigurableEnvironment environment = applicationContext.getEnvironment();
        // placeHolder
        PropertySources propertySources = environment.getPropertySources();
        Properties properties = new Properties();
        propertySources.addLast(new PropertiesPropertySource("properties", properties));
        properties.setProperty("placeHolder", "12345");
        final Constructor<Config>[] declaredConstructors = (Constructor<Config>[]) Config.class.getDeclaredConstructors();
        Constructor<Config> constructor = null;
        for (final Constructor<Config> declaredConstructor : declaredConstructors) {
            if (declaredConstructor.getParameterCount() > 0) {
                constructor = declaredConstructor;
            }
        }
        // properties.list(System.err);
        // System.err.println(properties.get("placeHolder"));
        Object[] parameters = new DependencyInjector(beanFactory).resolveArguments(constructor);
        Config newInstance = constructor.newInstance(parameters);
        assert parameters.length == 14;
        assert parameters[0] instanceof UserModel;
        UserModel userModel = (UserModel) parameters[0];
        assert userModel.getAge() == 21;
        assert userModel.getUserId().equals("666");
        assert userModel.getUserName().equals("TODAY");
        assert parameters[1] instanceof Properties;
        // collection > 5
        final List<Object> objectList = applicationContext.getBeans(Object.class);
        final int allSize = objectList.size();
        final Object collectionParameter = parameters[5];
        assert collectionParameter instanceof Collection;
        final int size = ((Collection<?>) collectionParameter).size();
        assert size == allSize;
        final Object setParameter = parameters[6];
        assert setParameter instanceof Set;
        assert ((Set<?>) setParameter).size() == allSize;
        final Object hashSetParameter = parameters[7];
        assert hashSetParameter instanceof HashSet;
        assert ((Set<?>) hashSetParameter).size() == allSize;
        final Object listParameter = parameters[8];
        assert listParameter instanceof List;
        assert ((List<?>) listParameter).size() == allSize;
        final Object arrayListParameter = parameters[9];
        assert arrayListParameter instanceof ArrayList;
        assert ((ArrayList<?>) arrayListParameter).size() == allSize;
        final Object mapParameter = parameters[10];
        assert mapParameter instanceof Map;
        assert ((Map) mapParameter).size() == allSize;
        final Object hashMapParameter = parameters[11];
        assert hashMapParameter instanceof HashMap;
        assert ((HashMap) hashMapParameter).size() == allSize;
    }
}
Also used : DependencyInjector(cn.taketoday.beans.factory.support.DependencyInjector) StandardApplicationContext(cn.taketoday.context.support.StandardApplicationContext) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) NoArgsConstructor(lombok.NoArgsConstructor) ArrayList(java.util.ArrayList) Properties(java.util.Properties) PropertySources(cn.taketoday.core.env.PropertySources) ConfigurableEnvironment(cn.taketoday.core.env.ConfigurableEnvironment) PropertiesPropertySource(cn.taketoday.core.env.PropertiesPropertySource) BeanFactory(cn.taketoday.beans.factory.BeanFactory) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

DependencyInjector (cn.taketoday.beans.factory.support.DependencyInjector)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)4 Test (org.junit.jupiter.api.Test)4 BeanFactory (cn.taketoday.beans.factory.BeanFactory)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 ConfigurableEnvironment (cn.taketoday.core.env.ConfigurableEnvironment)2 PropertiesPropertySource (cn.taketoday.core.env.PropertiesPropertySource)2 PropertySources (cn.taketoday.core.env.PropertySources)2 Constructor (java.lang.reflect.Constructor)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Set (java.util.Set)2 NoArgsConstructor (lombok.NoArgsConstructor)2 Disabled (org.junit.jupiter.api.Disabled)2