Search in sources :

Example 21 with Messenger

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());
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Refreshable(org.springframework.aop.target.dynamic.Refreshable) Test(org.junit.Test)

Example 22 with Messenger

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));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Refreshable(org.springframework.aop.target.dynamic.Refreshable) Test(org.junit.Test)

Example 23 with 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));
    }
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) FatalBeanException(org.springframework.beans.FatalBeanException) Messenger(org.springframework.scripting.Messenger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 24 with Messenger

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());
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Messenger(org.springframework.scripting.Messenger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 25 with Messenger

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);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Messenger(org.springframework.scripting.Messenger) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)35 Messenger (org.springframework.scripting.Messenger)35 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)29 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)27 ApplicationContext (org.springframework.context.ApplicationContext)24 Refreshable (org.springframework.aop.target.dynamic.Refreshable)16 TestBeanAwareMessenger (org.springframework.scripting.TestBeanAwareMessenger)10 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)4 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)3 Calculator (org.springframework.scripting.Calculator)3 CallCounter (org.springframework.scripting.CallCounter)2 GroovyObject (groovy.lang.GroovyObject)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 ScriptSource (org.springframework.scripting.ScriptSource)1 Component (org.springframework.stereotype.Component)1 TestBean (org.springframework.tests.sample.beans.TestBean)1