Search in sources :

Example 1 with Calculator

use of org.springframework.scripting.Calculator 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 2 with Calculator

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

the class GroovyScriptFactoryTests method testInlineScriptFromTag.

@Test
public void testInlineScriptFromTag() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
    BeanDefinition bd = ctx.getBeanFactory().getBeanDefinition("calculator");
    assertTrue(ObjectUtils.containsElement(bd.getDependsOn(), "messenger"));
    Calculator calculator = (Calculator) ctx.getBean("calculator");
    assertNotNull(calculator);
    assertFalse(calculator instanceof Refreshable);
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Calculator(org.springframework.scripting.Calculator) Refreshable(org.springframework.aop.target.dynamic.Refreshable) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 3 with Calculator

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

the class GroovyScriptFactoryTests method testMetaClass.

private void testMetaClass(String xmlFile) {
    // expect the exception we threw in the custom metaclass to show it got invoked
    try {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(xmlFile);
        Calculator calc = (Calculator) ctx.getBean("delegatingCalculator");
        calc.add(1, 2);
        fail("expected IllegalStateException");
    } catch (IllegalStateException ex) {
        assertEquals("Gotcha", ex.getMessage());
    }
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Calculator(org.springframework.scripting.Calculator)

Example 4 with Calculator

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

the class GroovyScriptFactoryTests method testStaticScript.

@Test
public void testStaticScript() throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.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 5 with Calculator

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

Aggregations

ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 Calculator (org.springframework.scripting.Calculator)6 Test (org.junit.Test)5 Refreshable (org.springframework.aop.target.dynamic.Refreshable)5 ApplicationContext (org.springframework.context.ApplicationContext)5 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)3 Messenger (org.springframework.scripting.Messenger)3 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 TestBeanAwareMessenger (org.springframework.scripting.TestBeanAwareMessenger)1