Search in sources :

Example 16 with Messenger

use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.

the class GroovyScriptFactoryTests method testRefreshableFromTagProxyTargetClass.

// SPR-6268
@Test
public void testRefreshableFromTagProxyTargetClass() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-proxy-target-class.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
    Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
    assertTrue(AopUtils.isAopProxy(messenger));
    assertTrue(messenger instanceof Refreshable);
    assertEquals("Hello World!", messenger.getMessage());
    assertTrue(ctx.getBeansOfType(ConcreteMessenger.class).values().contains(messenger));
    // Check that AnnotationUtils works with concrete proxied script classes
    assertNotNull(AnnotationUtils.findAnnotation(messenger.getClass(), Component.class));
}
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) Component(org.springframework.stereotype.Component) Test(org.junit.Test)

Example 17 with Messenger

use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.

the class GroovyScriptFactoryTests method testRefreshableFromTag.

@Test
public void testRefreshableFromTag() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
    Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
    CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect");
    assertTrue(AopUtils.isAopProxy(messenger));
    assertTrue(messenger instanceof Refreshable);
    assertEquals(0, countingAspect.getCalls());
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals(1, countingAspect.getCalls());
    assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) CallCounter(org.springframework.scripting.CallCounter) 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 18 with Messenger

use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessorTests method testChangeScriptWithRefreshableBeanFunctionality.

@Test
public void testChangeScriptWithRefreshableBeanFunctionality() throws Exception {
    BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
    BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
    ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
    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());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Messenger(org.springframework.scripting.Messenger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 19 with Messenger

use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.

the class ScriptFactoryPostProcessorTests method testChangeScriptWithNoRefreshableBeanFunctionality.

@Test
public void testChangeScriptWithNoRefreshableBeanFunctionality() throws Exception {
    BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(false);
    BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
    GenericApplicationContext ctx = new GenericApplicationContext();
    ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
    ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
    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);
    assertEquals("Script seems to have been refreshed (must not be as no refreshCheckDelay set on ScriptFactoryPostProcessor)", MESSAGE_TEXT, refreshedMessenger.getMessage());
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Messenger(org.springframework.scripting.Messenger) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 20 with Messenger

use of org.springframework.scripting.Messenger in project spring-framework by spring-projects.

the class GroovyScriptFactoryTests method testRefreshableJsr223FromTag.

@Test
public void testRefreshableJsr223FromTag() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));
    Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
    assertTrue(AopUtils.isAopProxy(messenger));
    assertTrue(messenger instanceof Refreshable);
    assertEquals("Hello World!", messenger.getMessage());
}
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)

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