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));
}
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");
}
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");
}
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();
}
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();
}
Aggregations