use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.
the class GroovyScriptFactoryTests method testNonStaticScript.
@Test
public void testNonStaticScript() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyRefreshableContext.xml", getClass());
Messenger messenger = (Messenger) ctx.getBean("messenger");
assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
Refreshable refreshable = (Refreshable) messenger;
refreshable.refresh();
assertEquals("Message is incorrect after refresh.", desiredMessage, messenger.getMessage());
assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.
the class GroovyScriptFactoryTests method testStaticScriptWithInstance.
@Test
public void testStaticScriptWithInstance() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
String desiredMessage = "Hello World!";
assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.
the class ScriptFactoryPostProcessorTests method testForRefreshedScriptHavingErrorPickedUpOnFirstCall.
@Test
public 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);
assertEquals(MESSAGE_TEXT, messenger.getMessage());
// 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);
try {
refreshedMessenger.getMessage();
fail("Must have thrown an Exception (invalid script)");
} catch (FatalBeanException expected) {
assertTrue(expected.contains(ScriptCompilationException.class));
}
}
use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.
the class ScriptFactoryPostProcessorTests method testRefreshedScriptReferencePropagatesToCollaborators.
@Test
public void testRefreshedScriptReferencePropagatesToCollaborators() 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);
assertEquals(MESSAGE_TEXT, messenger.getMessage());
// cool; now let's change the script and check the refresh behaviour...
pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
StaticScriptSource source = getScriptSource(ctx);
source.setScript(CHANGED_SCRIPT);
Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
// the updated script surrounds the message in quotes before returning...
assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, refreshedMessenger.getMessage());
// ok, is this change reflected in the reference that the collaborator has?
DefaultMessengerService collaborator = (DefaultMessengerService) ctx.getBean(collaboratorBeanName);
assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, collaborator.getMessage());
}
use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.
the class ScriptFactoryPostProcessorTests method testPrototypeScriptedBean.
@Test
public void testPrototypeScriptedBean() throws Exception {
GenericApplicationContext ctx = new GenericApplicationContext();
ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());
BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
scriptedBeanBuilder.addPropertyReference("messenger", "messenger");
final String BEAN_WITH_DEPENDENCY_NAME = "needsMessenger";
ctx.registerBeanDefinition(BEAN_WITH_DEPENDENCY_NAME, scriptedBeanBuilder.getBeanDefinition());
ctx.registerBeanDefinition("scriptProcessor", createScriptFactoryPostProcessor(true));
ctx.refresh();
Messenger messenger1 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
Messenger messenger2 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
assertNotSame(messenger1, messenger2);
}
Aggregations