Search in sources :

Example 1 with Messenger

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

the class BshScriptFactoryTests method scriptThatCompilesButIsJustPlainBad.

@Test
public void scriptThatCompilesButIsJustPlainBad() throws Exception {
    ScriptSource script = mock(ScriptSource.class);
    final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
    given(script.getScriptAsString()).willReturn(badScript);
    given(script.isModified()).willReturn(true);
    BshScriptFactory factory = new BshScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class);
    try {
        Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class);
        messenger.getMessage();
        fail("Must have thrown a BshScriptUtils.BshExecutionException.");
    } catch (BshScriptUtils.BshExecutionException expected) {
    }
}
Also used : TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) ScriptSource(org.springframework.scripting.ScriptSource) Test(org.junit.Test)

Example 2 with Messenger

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

the class BshScriptFactoryTests method staticScriptWithTwoInterfacesSpecified.

@Test
public void staticScriptWithTwoInterfacesSpecified() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfigExtra"));
    ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerWithConfigExtra");
    messenger.setMessage(null);
    assertNull(messenger.getMessage());
    assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
    ctx.close();
    assertNull(messenger.getMessage());
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Test(org.junit.Test)

Example 3 with Messenger

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

the class BshScriptFactoryTests method refreshableFromTag.

@Test
public void refreshableFromTag() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
    Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
    assertEquals("Hello World!", messenger.getMessage());
    assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) Refreshable(org.springframework.aop.target.dynamic.Refreshable) Test(org.junit.Test)

Example 4 with Messenger

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

the class BshScriptFactoryTests method applicationEventListener.

@Test
public void applicationEventListener() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
    Messenger eventListener = (Messenger) ctx.getBean("eventListener");
    ctx.publishEvent(new MyEvent(ctx));
    assertEquals("count=2", eventListener.getMessage());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) Test(org.junit.Test)

Example 5 with Messenger

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

the class BshScriptFactoryTests method staticScriptWithNullReturnValue.

@Test
public void staticScriptWithNullReturnValue() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfig"));
    ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerWithConfig");
    messenger.setMessage(null);
    assertNull(messenger.getMessage());
    assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) 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