Search in sources :

Example 1 with BeanDefinition

use of cn.taketoday.beans.factory.config.BeanDefinition in project today-framework by TAKETODAY.

the class ScriptFactoryPostProcessorTests method testForRefreshedScriptHavingErrorPickedUpOnFirstCall.

@Test
void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
    BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
    BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
    BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
    collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
    ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
    final String collaboratorBeanName = "collaborator";
    ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
    ctx.refresh();
    Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    assertThat(messenger.getMessage()).isEqualTo(MESSAGE_TEXT);
    // cool; now let's change the script and check the refresh behaviour...
    pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
    StaticScriptSource source = getScriptSource(ctx);
    // needs The Sundays compiler; must NOT throw any exception here...
    source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
    Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
    assertThatExceptionOfType(FatalBeanException.class).isThrownBy(refreshedMessenger::getMessage).matches(ex -> ex.contains(ScriptCompilationException.class));
}
Also used : BeanDefinitionBuilder(cn.taketoday.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) Messenger(cn.taketoday.scripting.Messenger) ScriptCompilationException(cn.taketoday.scripting.ScriptCompilationException) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 2 with BeanDefinition

use of cn.taketoday.beans.factory.config.BeanDefinition in project today-framework by TAKETODAY.

the class GroovyClassLoadingTests method classLoading.

@Test
@SuppressWarnings("resource")
public void classLoading() throws Exception {
    StaticApplicationContext context = new StaticApplicationContext();
    GroovyClassLoader gcl = new GroovyClassLoader();
    Class<?> class1 = gcl.parseClass("class TestBean { def myMethod() { \"foo\" } }");
    Class<?> class2 = gcl.parseClass("class TestBean { def myMethod() { \"bar\" } }");
    context.registerBeanDefinition("testBean", new BeanDefinition(class1));
    Object testBean1 = context.getBean("testBean");
    Method method1 = class1.getDeclaredMethod("myMethod", new Class<?>[0]);
    Object result1 = ReflectionUtils.invokeMethod(method1, testBean1);
    assertThat(result1).isEqualTo("foo");
    context.removeBeanDefinition("testBean");
    context.registerBeanDefinition("testBean", new BeanDefinition(class2));
    Object testBean2 = context.getBean("testBean");
    Method method2 = class2.getDeclaredMethod("myMethod", new Class<?>[0]);
    Object result2 = ReflectionUtils.invokeMethod(method2, testBean2);
    assertThat(result2).isEqualTo("bar");
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) Method(java.lang.reflect.Method) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) Test(org.junit.jupiter.api.Test)

Example 3 with BeanDefinition

use of cn.taketoday.beans.factory.config.BeanDefinition in project today-framework by TAKETODAY.

the class MyPostProcessor method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableBeanFactory beanFactory) throws BeansException {
    BeanDefinition bd = beanFactory.getBeanDefinition("someDependency");
    bd.setDescription("post processed by MyPostProcessor");
}
Also used : BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition)

Example 4 with BeanDefinition

use of cn.taketoday.beans.factory.config.BeanDefinition in project today-framework by TAKETODAY.

the class GenericStoredProcedureTests method testAddInvoices.

/*
	<bean id="dataSource" class="cn.taketoday.jdbc.datasource.TestDataSourceWrapper"/>
	<bean id="genericProcedure" class="cn.taketoday.jdbc.object.GenericStoredProcedure">
		<property name="dataSource" ref="dataSource"/>
		<property name="sql" value="add_invoice"/>
		<property name="parameters">
			<list>
				<bean class="cn.taketoday.jdbc.core.SqlParameter">
					<constructor-arg index="0" value="amount"/>
					<constructor-arg index="1">
						<util:constant static-field="java.sql.Types.INTEGER"/>
					</constructor-arg>
				</bean>
				<bean class="cn.taketoday.jdbc.core.SqlParameter">
					<constructor-arg index="0" value="custid"/>
					<constructor-arg index="1">
						<util:constant static-field="java.sql.Types.INTEGER"/>
					</constructor-arg>
				</bean>
				<bean class="cn.taketoday.jdbc.core.SqlOutParameter">
					<constructor-arg index="0" value="newid"/>
					<constructor-arg index="1">
						<util:constant static-field="java.sql.Types.INTEGER"/>
					</constructor-arg>
				</bean>
			</list>
		</property>
	</bean>
   */
@Test
public void testAddInvoices() throws Exception {
    StandardBeanFactory bf = new StandardBeanFactory();
    bf.registerBeanDefinition(new BeanDefinition("dataSource", TestDataSourceWrapper.class));
    bf.registerBeanDefinition(new BeanDefinition("genericProcedure", GenericStoredProcedure.class).addPropertyValue("dataSource", RuntimeBeanReference.from("dataSource")).addPropertyValue("sql", "add_invoice").addPropertyValue("parameters", List.of(new SqlParameter("amount", java.sql.Types.INTEGER), new SqlParameter("custid", java.sql.Types.INTEGER), new SqlOutParameter("newid", java.sql.Types.INTEGER))));
    Connection connection = mock(Connection.class);
    DataSource dataSource = mock(DataSource.class);
    given(dataSource.getConnection()).willReturn(connection);
    CallableStatement callableStatement = mock(CallableStatement.class);
    TestDataSourceWrapper testDataSource = (TestDataSourceWrapper) bf.getBean("dataSource");
    testDataSource.setTarget(dataSource);
    given(callableStatement.execute()).willReturn(false);
    given(callableStatement.getUpdateCount()).willReturn(-1);
    given(callableStatement.getObject(3)).willReturn(4);
    given(connection.prepareCall("{call " + "add_invoice" + "(?, ?, ?)}")).willReturn(callableStatement);
    StoredProcedure adder = (StoredProcedure) bf.getBean("genericProcedure");
    Map<String, Object> in = new HashMap<>(2);
    in.put("amount", 1106);
    in.put("custid", 3);
    Map<String, Object> out = adder.execute(in);
    Integer id = (Integer) out.get("newid");
    assertThat(id.intValue()).isEqualTo(4);
    verify(callableStatement).setObject(1, 1106, Types.INTEGER);
    verify(callableStatement).setObject(2, 3, Types.INTEGER);
    verify(callableStatement).registerOutParameter(3, Types.INTEGER);
    verify(callableStatement).close();
}
Also used : SqlParameter(cn.taketoday.jdbc.core.SqlParameter) HashMap(java.util.HashMap) Connection(java.sql.Connection) SqlOutParameter(cn.taketoday.jdbc.core.SqlOutParameter) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) DataSource(javax.sql.DataSource) CallableStatement(java.sql.CallableStatement) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) TestDataSourceWrapper(cn.taketoday.jdbc.datasource.TestDataSourceWrapper) Test(org.junit.jupiter.api.Test)

Example 5 with BeanDefinition

use of cn.taketoday.beans.factory.config.BeanDefinition in project today-framework by TAKETODAY.

the class AsyncAnnotationBeanPostProcessorTests method taskExecutorByBeanType.

@Test
public void taskExecutorByBeanType() {
    StaticApplicationContext context = new StaticApplicationContext();
    BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
    context.registerBeanDefinition("postProcessor", processorDefinition);
    BeanDefinition executorDefinition = new RootBeanDefinition(ThreadPoolTaskExecutor.class);
    executorDefinition.getPropertyValues().add("threadNamePrefix", "testExecutor");
    context.registerBeanDefinition("myExecutor", executorDefinition);
    BeanDefinition targetDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessorTests.TestBean.class);
    context.registerBeanDefinition("target", targetDefinition);
    context.refresh();
    ITestBean testBean = context.getBean("target", ITestBean.class);
    testBean.test();
    testBean.await(3000);
    Thread asyncThread = testBean.getThread();
    assertThat(asyncThread.getName().startsWith("testExecutor")).isTrue();
    context.close();
}
Also used : StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) BeanDefinition(cn.taketoday.beans.factory.config.BeanDefinition) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)151 Test (org.junit.jupiter.api.Test)117 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)37 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)26 AnnotatedBeanDefinition (cn.taketoday.beans.factory.annotation.AnnotatedBeanDefinition)17 PropertyValues (cn.taketoday.beans.PropertyValues)16 ScannedGenericBeanDefinition (cn.taketoday.context.annotation.ScannedGenericBeanDefinition)16 ClassPathScanningCandidateComponentProvider (cn.taketoday.context.loader.ClassPathScanningCandidateComponentProvider)16 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)15 AnnotationTypeFilter (cn.taketoday.core.type.filter.AnnotationTypeFilter)14 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)13 DefaultAdvisorAutoProxyCreator (cn.taketoday.aop.proxy.DefaultAdvisorAutoProxyCreator)12 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)12 AssignableTypeFilter (cn.taketoday.core.type.filter.AssignableTypeFilter)12 Component (cn.taketoday.lang.Component)12 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)11 ProfileMetaAnnotatedComponent (example.profilescan.ProfileMetaAnnotatedComponent)11 Properties (java.util.Properties)11 DefaultResourceLoader (cn.taketoday.core.io.DefaultResourceLoader)10 DevComponent (example.profilescan.DevComponent)10