Search in sources :

Example 6 with Messenger

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

the class BshScriptFactoryTests method staticScript.

@Test
public void staticScript() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Calculator.class)).contains("calculator"));
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger"));
    Calculator calc = (Calculator) ctx.getBean("calculator");
    Messenger messenger = (Messenger) ctx.getBean("messenger");
    assertFalse("Scripted object should not be instance of Refreshable", calc instanceof Refreshable);
    assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
    assertEquals(calc, calc);
    assertEquals(messenger, messenger);
    assertTrue(!messenger.equals(calc));
    assertTrue(messenger.hashCode() != calc.hashCode());
    assertTrue(!messenger.toString().equals(calc.toString()));
    assertEquals(5, calc.add(2, 3));
    String desiredMessage = "Hello World!";
    assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
    assertTrue(ctx.getBeansOfType(Calculator.class).values().contains(calc));
    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) Calculator(org.springframework.scripting.Calculator) 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 7 with Messenger

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

the class BshScriptFactoryTests method nonStaticScript.

@Test
public void nonStaticScript() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.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 : 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 8 with Messenger

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

the class BshScriptFactoryTests method staticScriptImplementingInterface.

@Test
public void staticScriptImplementingInterface() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerImpl"));
    Messenger messenger = (Messenger) ctx.getBean("messengerImpl");
    String desiredMessage = "Hello World!";
    assertEquals("Message is incorrect", desiredMessage, 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) Test(org.junit.Test)

Example 9 with Messenger

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

the class GroovyScriptFactoryTests method testWithTwoClassesDefinedInTheOneGroovyFile_CorrectClassFirst.

@Test
public void testWithTwoClassesDefinedInTheOneGroovyFile_CorrectClassFirst() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesCorrectOneFirst.xml", getClass());
    Messenger messenger = (Messenger) ctx.getBean("messenger");
    assertNotNull(messenger);
    assertEquals("Hello World!", messenger.getMessage());
    // Check can cast to GroovyObject
    GroovyObject goo = (GroovyObject) messenger;
    assertNotNull(goo);
}
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) GroovyObject(groovy.lang.GroovyObject) Test(org.junit.Test)

Example 10 with Messenger

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

the class GroovyScriptFactoryTests method testAnonymousScriptDetected.

@Test
public void testAnonymousScriptDetected() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
    Map<?, Messenger> beans = ctx.getBeansOfType(Messenger.class);
    assertEquals(4, beans.size());
    assertTrue(ctx.getBean(MyBytecodeProcessor.class).processed.contains("org.springframework.scripting.groovy.GroovyMessenger2"));
}
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) 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