Search in sources :

Example 31 with Messenger

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

the class GroovyScriptFactoryTests method testResourceScriptFromTag.

@Test
public void testResourceScriptFromTag() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
    Messenger messenger = (Messenger) ctx.getBean("messenger");
    CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect");
    assertTrue(AopUtils.isAopProxy(messenger));
    assertFalse(messenger instanceof Refreshable);
    assertEquals(0, countingAspect.getCalls());
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals(1, countingAspect.getCalls());
    ctx.close();
    assertEquals(-200, countingAspect.getCalls());
}
Also used : 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 32 with Messenger

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

the class GroovyScriptFactoryTests method testStaticScriptUsingJsr223.

@Test
public void testStaticScriptUsingJsr223() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.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("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(calc));
    assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(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()));
    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 : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Calculator(org.springframework.scripting.Calculator) Messenger(org.springframework.scripting.Messenger) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Refreshable(org.springframework.aop.target.dynamic.Refreshable) Test(org.junit.Test)

Example 33 with Messenger

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

the class GroovyScriptFactoryTests method testInlineJsr223FromTagWithInterface.

@Test
public void testInlineJsr223FromTagWithInterface() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessengerWithInterface"));
    Messenger messenger = (Messenger) ctx.getBean("inlineMessengerWithInterface");
    assertFalse(AopUtils.isAopProxy(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) Test(org.junit.Test)

Example 34 with Messenger

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

the class BshScriptFactoryTests method staticWithScriptReturningInstance.

@Test
public void staticWithScriptReturningInstance() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
    assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
    Messenger messenger = (Messenger) ctx.getBean("messengerInstance");
    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 35 with Messenger

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

the class BshScriptFactoryTests method resourceScriptFromTag.

@Test
public void resourceScriptFromTag() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
    TestBean testBean = (TestBean) ctx.getBean("testBean");
    Collection<String> beanNames = Arrays.asList(ctx.getBeanNamesForType(Messenger.class));
    assertTrue(beanNames.contains("messenger"));
    assertTrue(beanNames.contains("messengerImpl"));
    assertTrue(beanNames.contains("messengerInstance"));
    Messenger messenger = (Messenger) ctx.getBean("messenger");
    assertEquals("Hello World!", messenger.getMessage());
    assertFalse(messenger instanceof Refreshable);
    Messenger messengerImpl = (Messenger) ctx.getBean("messengerImpl");
    assertEquals("Hello World!", messengerImpl.getMessage());
    Messenger messengerInstance = (Messenger) ctx.getBean("messengerInstance");
    assertEquals("Hello World!", messengerInstance.getMessage());
    TestBeanAwareMessenger messengerByType = (TestBeanAwareMessenger) ctx.getBean("messengerByType");
    assertEquals(testBean, messengerByType.getTestBean());
    TestBeanAwareMessenger messengerByName = (TestBeanAwareMessenger) ctx.getBean("messengerByName");
    assertEquals(testBean, messengerByName.getTestBean());
    Collection<Messenger> beans = ctx.getBeansOfType(Messenger.class).values();
    assertTrue(beans.contains(messenger));
    assertTrue(beans.contains(messengerImpl));
    assertTrue(beans.contains(messengerInstance));
    assertTrue(beans.contains(messengerByType));
    assertTrue(beans.contains(messengerByName));
    ctx.close();
    assertNull(messenger.getMessage());
    assertNull(messengerImpl.getMessage());
    assertNull(messengerInstance.getMessage());
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) TestBean(org.springframework.tests.sample.beans.TestBean) TestBeanAwareMessenger(org.springframework.scripting.TestBeanAwareMessenger) 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)

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