Search in sources :

Example 1 with ConfigurableMessenger

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

the class BshScriptFactoryTests method nonStaticPrototypeScript.

@Test
public void nonStaticPrototypeScript() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass());
    ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
    assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals("Hello World!", messenger2.getMessage());
    messenger.setMessage("Bye World!");
    messenger2.setMessage("Byebye World!");
    assertEquals("Bye World!", messenger.getMessage());
    assertEquals("Byebye World!", messenger2.getMessage());
    Refreshable refreshable = (Refreshable) messenger;
    refreshable.refresh();
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals("Byebye World!", messenger2.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) Refreshable(org.springframework.aop.target.dynamic.Refreshable) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Test(org.junit.Test)

Example 2 with ConfigurableMessenger

use of org.springframework.scripting.ConfigurableMessenger 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 ConfigurableMessenger

use of org.springframework.scripting.ConfigurableMessenger 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)

Example 4 with ConfigurableMessenger

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

the class BshScriptFactoryTests method prototypeScriptFromTag.

@Test
public void prototypeScriptFromTag() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
    ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    assertNotSame(messenger, messenger2);
    assertSame(messenger.getClass(), messenger2.getClass());
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals("Hello World!", messenger2.getMessage());
    messenger.setMessage("Bye World!");
    messenger2.setMessage("Byebye World!");
    assertEquals("Bye World!", messenger.getMessage());
    assertEquals("Byebye World!", messenger2.getMessage());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Test(org.junit.Test)

Example 5 with ConfigurableMessenger

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

the class GroovyScriptFactoryTests method testStaticPrototypeScriptUsingJsr223.

@Test
public void testStaticPrototypeScriptUsingJsr223() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
    ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    ConfigurableMessenger messenger2 = (ConfigurableMessenger) ctx.getBean("messengerPrototype");
    assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
    assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);
    assertNotSame(messenger, messenger2);
    assertSame(messenger.getClass(), messenger2.getClass());
    assertEquals("Hello World!", messenger.getMessage());
    assertEquals("Hello World!", messenger2.getMessage());
    messenger.setMessage("Bye World!");
    messenger2.setMessage("Byebye World!");
    assertEquals("Bye World!", messenger.getMessage());
    assertEquals("Byebye World!", messenger2.getMessage());
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Refreshable(org.springframework.aop.target.dynamic.Refreshable) ConfigurableMessenger(org.springframework.scripting.ConfigurableMessenger) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)9 ApplicationContext (org.springframework.context.ApplicationContext)8 Refreshable (org.springframework.aop.target.dynamic.Refreshable)5 Messenger (org.springframework.scripting.Messenger)2 TestBeanAwareMessenger (org.springframework.scripting.TestBeanAwareMessenger)2