Search in sources :

Example 6 with ScriptSource

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

the class GroovyScriptFactoryTests method testScriptedClassThatHasNoPublicNoArgCtor.

@Test
public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception {
    ScriptSource script = mock(ScriptSource.class);
    String badScript = "class Foo { protected Foo() {} \n String toString() { 'X' }}";
    given(script.getScriptAsString()).willReturn(badScript);
    given(script.suggestedClassName()).willReturn("someName");
    GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript);
    assertEquals("X", factory.getScriptedObject(script).toString());
}
Also used : ScriptSource(org.springframework.scripting.ScriptSource) Test(org.junit.Test)

Example 7 with ScriptSource

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

the class GroovyScriptFactoryTests method testGetScriptedObjectDoesNotChokeOnNullInterfacesBeingPassedIn.

@Test
public void testGetScriptedObjectDoesNotChokeOnNullInterfacesBeingPassedIn() throws Exception {
    ScriptSource script = mock(ScriptSource.class);
    given(script.getScriptAsString()).willReturn("class Bar {}");
    given(script.suggestedClassName()).willReturn("someName");
    GroovyScriptFactory factory = new GroovyScriptFactory("a script source locator (doesn't matter here)");
    Object scriptedObject = factory.getScriptedObject(script);
    assertNotNull(scriptedObject);
}
Also used : GroovyObject(groovy.lang.GroovyObject) ScriptSource(org.springframework.scripting.ScriptSource) Test(org.junit.Test)

Example 8 with ScriptSource

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

the class GroovyScriptFactoryTests method testScriptedClassThatDoesNotHaveANoArgCtor.

@Test
public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception {
    ScriptSource script = mock(ScriptSource.class);
    String badScript = "class Foo { public Foo(String foo) {}}";
    given(script.getScriptAsString()).willReturn(badScript);
    given(script.suggestedClassName()).willReturn("someName");
    GroovyScriptFactory factory = new GroovyScriptFactory(ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript);
    try {
        factory.getScriptedObject(script);
        fail("Must have thrown a ScriptCompilationException (no public no-arg ctor in scripted class).");
    } catch (ScriptCompilationException expected) {
        assertTrue(expected.contains(NoSuchMethodException.class));
    }
}
Also used : ScriptCompilationException(org.springframework.scripting.ScriptCompilationException) ScriptSource(org.springframework.scripting.ScriptSource) Test(org.junit.Test)

Aggregations

ScriptSource (org.springframework.scripting.ScriptSource)8 Test (org.junit.Test)4 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)3 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)3 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)3 ScriptFactory (org.springframework.scripting.ScriptFactory)3 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 BeanCurrentlyInCreationException (org.springframework.beans.factory.BeanCurrentlyInCreationException)2 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)2 BeanDefinitionValidationException (org.springframework.beans.factory.support.BeanDefinitionValidationException)2 GroovyObject (groovy.lang.GroovyObject)1 FactoryBean (org.springframework.beans.factory.FactoryBean)1 ConfigurableMessenger (org.springframework.scripting.ConfigurableMessenger)1 Messenger (org.springframework.scripting.Messenger)1 ScriptCompilationException (org.springframework.scripting.ScriptCompilationException)1 TestBeanAwareMessenger (org.springframework.scripting.TestBeanAwareMessenger)1