org.springframework.scripting.ScriptCompilationException Java Examples

The following examples show how to use org.springframework.scripting.ScriptCompilationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardScriptFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected Object evaluateScript(ScriptSource scriptSource) {
	try {
		ScriptEngine scriptEngine = this.scriptEngine;
		if (scriptEngine == null) {
			scriptEngine = retrieveScriptEngine(scriptSource);
			if (scriptEngine == null) {
				throw new IllegalStateException("Could not determine script engine for " + scriptSource);
			}
			this.scriptEngine = scriptEngine;
		}
		return scriptEngine.eval(scriptSource.getScriptAsString());
	}
	catch (Exception ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #2
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptedClassThatHasNoPublicNoArgCtor() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	final String badScript = "class Foo { protected 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 oublic no-arg ctor in scripted class).");
	}
	catch (ScriptCompilationException expected) {
		assertTrue(expected.contains(IllegalAccessException.class));
	}
}
 
Example #3
Source File: BshScriptFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (scriptSource.isModified()) {
				// New script content: Let's check whether it evaluates to a Class.
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = BshScriptUtils.determineBshObjectType(
						scriptSource.getScriptAsString(), this.beanClassLoader);
			}
			return this.scriptClass;
		}
		catch (EvalError ex) {
			this.scriptClass = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #4
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testScriptedClassThatDoesNotHaveANoArgCtor() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	final 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(InstantiationException.class));
	}
}
 
Example #5
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
	}
}
 
Example #6
Source File: BshScriptFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (scriptSource.isModified()) {
				// New script content: Let's check whether it evaluates to a Class.
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = BshScriptUtils.determineBshObjectType(
						scriptSource.getScriptAsString(), this.beanClassLoader);
			}
			return this.scriptClass;
		}
		catch (EvalError ex) {
			this.scriptClass = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #7
Source File: StandardScriptFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected Object evaluateScript(ScriptSource scriptSource) {
	try {
		ScriptEngine scriptEngine = this.scriptEngine;
		if (scriptEngine == null) {
			scriptEngine = retrieveScriptEngine(scriptSource);
			if (scriptEngine == null) {
				throw new IllegalStateException("Could not determine script engine for " + scriptSource);
			}
			this.scriptEngine = scriptEngine;
		}
		return scriptEngine.eval(scriptSource.getScriptAsString());
	}
	catch (Exception ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #8
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
	}
}
 
Example #9
Source File: BshScriptFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	try {
		synchronized (this.scriptClassMonitor) {
			if (scriptSource.isModified()) {
				// New script content: Let's check whether it evaluates to a Class.
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = BshScriptUtils.determineBshObjectType(
						scriptSource.getScriptAsString(), this.beanClassLoader);
			}
			return this.scriptClass;
		}
	}
	catch (EvalError ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #10
Source File: BshScriptFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (scriptSource.isModified()) {
				// New script content: Let's check whether it evaluates to a Class.
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = BshScriptUtils.determineBshObjectType(
						scriptSource.getScriptAsString(), this.beanClassLoader);
			}
			return this.scriptClass;
		}
		catch (EvalError ex) {
			this.scriptClass = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #11
Source File: StandardScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object adaptToInterfaces(Object script, ScriptSource scriptSource, Class<?>... actualInterfaces) {
	Class<?> adaptedIfc;
	if (actualInterfaces.length == 1) {
		adaptedIfc = actualInterfaces[0];
	}
	else {
		adaptedIfc = ClassUtils.createCompositeInterface(actualInterfaces, this.beanClassLoader);
	}

	if (adaptedIfc != null) {
		if (!(this.scriptEngine instanceof Invocable)) {
			throw new ScriptCompilationException(scriptSource,
					"ScriptEngine must implement Invocable in order to adapt it to an interface: " +
							this.scriptEngine);
		}
		Invocable invocable = (Invocable) this.scriptEngine;
		if (script != null) {
			script = invocable.getInterface(script, adaptedIfc);
		}
		if (script == null) {
			script = invocable.getInterface(adaptedIfc);
			if (script == null) {
				throw new ScriptCompilationException(scriptSource,
						"Could not adapt script to interface [" + adaptedIfc.getName() + "]");
			}
		}
	}

	return script;
}
 
Example #12
Source File: StandardScriptFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
protected Object adaptToInterfaces(
		@Nullable Object script, ScriptSource scriptSource, Class<?>... actualInterfaces) {

	Class<?> adaptedIfc;
	if (actualInterfaces.length == 1) {
		adaptedIfc = actualInterfaces[0];
	}
	else {
		adaptedIfc = ClassUtils.createCompositeInterface(actualInterfaces, this.beanClassLoader);
	}

	if (adaptedIfc != null) {
		ScriptEngine scriptEngine = this.scriptEngine;
		if (!(scriptEngine instanceof Invocable)) {
			throw new ScriptCompilationException(scriptSource,
					"ScriptEngine must implement Invocable in order to adapt it to an interface: " + scriptEngine);
		}
		Invocable invocable = (Invocable) scriptEngine;
		if (script != null) {
			script = invocable.getInterface(script, adaptedIfc);
		}
		if (script == null) {
			script = invocable.getInterface(adaptedIfc);
			if (script == null) {
				throw new ScriptCompilationException(scriptSource,
						"Could not adapt script to interface [" + adaptedIfc.getName() + "]");
			}
		}
	}

	return script;
}
 
Example #13
Source File: GroovyScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (this.scriptClass == null || scriptSource.isModified()) {
				// New script content...
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = getGroovyClassLoader().parseClass(
						scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

				if (Script.class.isAssignableFrom(this.scriptClass)) {
					// A Groovy script, probably creating an instance: let's execute it.
					Object result = executeScript(scriptSource, this.scriptClass);
					this.scriptResultClass = (result != null ? result.getClass() : null);
					this.cachedResult = new CachedResultHolder(result);
				}
				else {
					this.scriptResultClass = this.scriptClass;
				}
			}
			return this.scriptResultClass;
		}
		catch (CompilationFailedException ex) {
			this.scriptClass = null;
			this.scriptResultClass = null;
			this.cachedResult = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #14
Source File: StandardScriptFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected Object evaluateScript(ScriptSource scriptSource) {
	try {
		if (this.scriptEngine == null) {
			this.scriptEngine = retrieveScriptEngine(scriptSource);
			if (this.scriptEngine == null) {
				throw new IllegalStateException("Could not determine script engine for " + scriptSource);
			}
		}
		return this.scriptEngine.eval(scriptSource.getScriptAsString());
	}
	catch (Exception ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #15
Source File: StandardScriptFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected Object adaptToInterfaces(Object script, ScriptSource scriptSource, Class<?>... actualInterfaces) {
	Class<?> adaptedIfc;
	if (actualInterfaces.length == 1) {
		adaptedIfc = actualInterfaces[0];
	}
	else {
		adaptedIfc = ClassUtils.createCompositeInterface(actualInterfaces, this.beanClassLoader);
	}

	if (adaptedIfc != null) {
		if (!(this.scriptEngine instanceof Invocable)) {
			throw new ScriptCompilationException(scriptSource,
					"ScriptEngine must implement Invocable in order to adapt it to an interface: " +
							this.scriptEngine);
		}
		Invocable invocable = (Invocable) this.scriptEngine;
		if (script != null) {
			script = invocable.getInterface(script, adaptedIfc);
		}
		if (script == null) {
			script = invocable.getInterface(adaptedIfc);
			if (script == null) {
				throw new ScriptCompilationException(scriptSource,
						"Could not adapt script to interface [" + adaptedIfc.getName() + "]");
			}
		}
	}

	return script;
}
 
Example #16
Source File: GroovyScriptFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	try {
		synchronized (this.scriptClassMonitor) {
			if (this.scriptClass == null || scriptSource.isModified()) {
				// New script content...
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = getGroovyClassLoader().parseClass(
						scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

				if (Script.class.isAssignableFrom(this.scriptClass)) {
					// A Groovy script, probably creating an instance: let's execute it.
					Object result = executeScript(scriptSource, this.scriptClass);
					this.scriptResultClass = (result != null ? result.getClass() : null);
					this.cachedResult = new CachedResultHolder(result);
				}
				else {
					this.scriptResultClass = this.scriptClass;
				}
			}
			return this.scriptResultClass;
		}
	}
	catch (CompilationFailedException ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #17
Source File: JRubyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptCompilationException() throws Exception {
	try {
		new ClassPathXmlApplicationContext("jrubyBrokenContext.xml", getClass());
		fail("Should throw exception for broken script file");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.contains(ScriptCompilationException.class));
	}
}
 
Example #18
Source File: ScriptFactoryPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
	BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
	BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
	BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
	collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);

	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
	ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
	final String collaboratorBeanName = "collaborator";
	ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
	ctx.refresh();

	Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	assertEquals(MESSAGE_TEXT, messenger.getMessage());
	// cool; now let's change the script and check the refresh behaviour...
	pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
	StaticScriptSource source = getScriptSource(ctx);
	// needs The Sundays compiler; must NOT throw any exception here...
	source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
	Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	try {
		refreshedMessenger.getMessage();
		fail("Must have thrown an Exception (invalid script)");
	}
	catch (FatalBeanException expected) {
		assertTrue(expected.contains(ScriptCompilationException.class));
	}
}
 
Example #19
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testScriptCompilationException() throws Exception {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/groovy/groovyBrokenContext.xml");
		fail("Should throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue("Wrong root cause: " + ex, ex.contains(ScriptCompilationException.class));
	}
}
 
Example #20
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void scriptCompilationException() throws Exception {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/bsh/bshBrokenContext.xml");
		fail("Must throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue(ex.contains(ScriptCompilationException.class));
	}
}
 
Example #21
Source File: StandardScriptFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Object evaluateScript(ScriptSource scriptSource) {
	try {
		if (this.scriptEngine == null) {
			this.scriptEngine = retrieveScriptEngine(scriptSource);
			if (this.scriptEngine == null) {
				throw new IllegalStateException("Could not determine script engine for " + scriptSource);
			}
		}
		return this.scriptEngine.eval(scriptSource.getScriptAsString());
	}
	catch (Exception ex) {
		throw new ScriptCompilationException(scriptSource, ex);
	}
}
 
Example #22
Source File: BshScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void scriptCompilationException() {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/bsh/bshBrokenContext.xml");
		fail("Must throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue(ex.contains(ScriptCompilationException.class));
	}
}
 
Example #23
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testScriptCompilationException() throws Exception {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/groovy/groovyBrokenContext.xml");
		fail("Should throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue("Wrong root cause: " + ex, ex.contains(ScriptCompilationException.class));
	}
}
 
Example #24
Source File: ScriptFactoryPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
	BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
	BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
	BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
	collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);

	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
	ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
	final String collaboratorBeanName = "collaborator";
	ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
	ctx.refresh();

	Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	assertEquals(MESSAGE_TEXT, messenger.getMessage());
	// cool; now let's change the script and check the refresh behaviour...
	pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
	StaticScriptSource source = getScriptSource(ctx);
	// needs The Sundays compiler; must NOT throw any exception here...
	source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
	Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	try {
		refreshedMessenger.getMessage();
		fail("Must have thrown an Exception (invalid script)");
	}
	catch (FatalBeanException expected) {
		assertTrue(expected.contains(ScriptCompilationException.class));
	}
}
 
Example #25
Source File: GroovyScriptFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	synchronized (this.scriptClassMonitor) {
		try {
			if (this.scriptClass == null || scriptSource.isModified()) {
				// New script content...
				this.wasModifiedForTypeCheck = true;
				this.scriptClass = getGroovyClassLoader().parseClass(
						scriptSource.getScriptAsString(), scriptSource.suggestedClassName());

				if (Script.class.isAssignableFrom(this.scriptClass)) {
					// A Groovy script, probably creating an instance: let's execute it.
					Object result = executeScript(scriptSource, this.scriptClass);
					this.scriptResultClass = (result != null ? result.getClass() : null);
					this.cachedResult = new CachedResultHolder(result);
				}
				else {
					this.scriptResultClass = this.scriptClass;
				}
			}
			return this.scriptResultClass;
		}
		catch (CompilationFailedException ex) {
			this.scriptClass = null;
			this.scriptResultClass = null;
			this.cachedResult = null;
			throw new ScriptCompilationException(scriptSource, ex);
		}
	}
}
 
Example #26
Source File: StandardScriptFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Class<?> getScriptedObjectType(ScriptSource scriptSource)
		throws IOException, ScriptCompilationException {

	return null;
}
 
Example #27
Source File: StandardScriptFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
protected Object adaptToInterfaces(
		@Nullable Object script, ScriptSource scriptSource, Class<?>... actualInterfaces) {

	Class<?> adaptedIfc;
	if (actualInterfaces.length == 1) {
		adaptedIfc = actualInterfaces[0];
	}
	else {
		adaptedIfc = ClassUtils.createCompositeInterface(actualInterfaces, this.beanClassLoader);
	}

	if (adaptedIfc != null) {
		ScriptEngine scriptEngine = this.scriptEngine;
		if (!(scriptEngine instanceof Invocable)) {
			throw new ScriptCompilationException(scriptSource,
					"ScriptEngine must implement Invocable in order to adapt it to an interface: " + scriptEngine);
		}
		Invocable invocable = (Invocable) scriptEngine;
		if (script != null) {
			script = invocable.getInterface(script, adaptedIfc);
		}
		if (script == null) {
			script = invocable.getInterface(adaptedIfc);
			if (script == null) {
				throw new ScriptCompilationException(scriptSource,
						"Could not adapt script to interface [" + adaptedIfc.getName() + "]");
			}
		}
	}

	return script;
}
 
Example #28
Source File: BshScriptFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void scriptCompilationException() {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/bsh/bshBrokenContext.xml");
		fail("Must throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue(ex.contains(ScriptCompilationException.class));
	}
}
 
Example #29
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testScriptCompilationException() throws Exception {
	try {
		new ClassPathXmlApplicationContext("org/springframework/scripting/groovy/groovyBrokenContext.xml");
		fail("Should throw exception for broken script file");
	}
	catch (NestedRuntimeException ex) {
		assertTrue("Wrong root cause: " + ex, ex.contains(ScriptCompilationException.class));
	}
}
 
Example #30
Source File: ScriptFactoryPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testForRefreshedScriptHavingErrorPickedUpOnFirstCall() throws Exception {
	BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true);
	BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean();
	BeanDefinitionBuilder collaboratorBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultMessengerService.class);
	collaboratorBuilder.addPropertyReference(MESSENGER_BEAN_NAME, MESSENGER_BEAN_NAME);

	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition);
	ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition);
	final String collaboratorBeanName = "collaborator";
	ctx.registerBeanDefinition(collaboratorBeanName, collaboratorBuilder.getBeanDefinition());
	ctx.refresh();

	Messenger messenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	assertEquals(MESSAGE_TEXT, messenger.getMessage());
	// cool; now let's change the script and check the refresh behaviour...
	pauseToLetRefreshDelayKickIn(DEFAULT_SECONDS_TO_PAUSE);
	StaticScriptSource source = getScriptSource(ctx);
	// needs The Sundays compiler; must NOT throw any exception here...
	source.setScript("I keep hoping you are the same as me, and I'll send you letters and come to your house for tea");
	Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME);
	try {
		refreshedMessenger.getMessage();
		fail("Must have thrown an Exception (invalid script)");
	}
	catch (FatalBeanException expected) {
		assertTrue(expected.contains(ScriptCompilationException.class));
	}
}