/** * @param scriptText The script text */ public void setScriptText(String scriptText) { this.scriptSource = new StaticScriptSource(scriptText); }
/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
@Test public void gettingScriptTogglesIsModified() throws Exception { source.getScriptAsString(); assertFalse("Script must be flagged as 'not modified' after script is read.", source.isModified()); }
@Test public void gettingScriptViaToStringDoesNotToggleIsModified() throws Exception { boolean isModifiedState = source.isModified(); source.toString(); assertEquals("Script's 'modified' flag must not change after script is read via toString().", isModifiedState, source.isModified()); }
@Test public void isModifiedToggledWhenDifferentScriptIsSet() throws Exception { source.setScript("use warnings;"); assertTrue("Script must be flagged as 'modified' when different script is passed in.", source.isModified()); }
@Test public void isModifiedIsTrueByDefault() throws Exception { assertTrue("Script must be flagged as 'modified' when first created.", source.isModified()); }
@Test public void isModifiedNotToggledWhenSameScriptIsSet() throws Exception { source.setScript(SCRIPT_TEXT); assertFalse("Script must not be flagged as 'modified' when same script is passed in.", source.isModified()); }
/** * Convert the given script source locator to a ScriptSource instance. * <p>By default, supported locators are Spring resource locations * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") * and inline scripts ("inline:myScriptText..."). * @param beanName the name of the scripted bean * @param scriptSourceLocator the script source locator * @param resourceLoader the ResourceLoader to use (if necessary) * @return the ScriptSource instance */ protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader) { if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) { return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName); } else { return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator)); } }
/** * Create a new StaticScriptSource for the given script. * @param script the script String * @param className the suggested class name for the script * (may be {@code null}) */ public StaticScriptSource(String script, @Nullable String className) { setScript(script); this.className = className; }
/** * <p> * Sets the provided {@link String} as the script source code to use. * </p> * * @param scriptSource the {@link String} form of the script source code to use. * @param language the language of the script. */ public void setScriptSource(String scriptSource, String language) { Assert.hasText(language, "Language must contain the script language"); Assert.hasText(scriptSource, "Script source must contain the script source to evaluate"); this.language = language; this.scriptSource = new StaticScriptSource(scriptSource); }
/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
/** * Convert the given script source locator to a ScriptSource instance. * <p>By default, supported locators are Spring resource locations * (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") * and inline scripts ("inline:myScriptText..."). * @param beanName the name of the scripted bean * @param scriptSourceLocator the script source locator * @param resourceLoader the ResourceLoader to use (if necessary) * @return the ScriptSource instance */ protected ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader) { if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) { return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName); } else { return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator)); } }
/** * Create a new StaticScriptSource for the given script. * @param script the script String * @param className the suggested class name for the script * (may be {@code null}) */ public StaticScriptSource(String script, @Nullable String className) { setScript(script); this.className = className; }
@Test(expected = IllegalArgumentException.class) public void createWithNullScript() throws Exception { new StaticScriptSource(null); }
@Test public void testRefreshedScriptReferencePropagatesToCollaborators() 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); source.setScript(CHANGED_SCRIPT); Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); // the updated script surrounds the message in quotes before returning... assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, refreshedMessenger.getMessage()); // ok, is this change reflected in the reference that the collaborator has? DefaultMessengerService collaborator = (DefaultMessengerService) ctx.getBean(collaboratorBeanName); assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, collaborator.getMessage()); }
@Test(expected = IllegalArgumentException.class) public void createWithWhitespaceOnlyScript() throws Exception { new StaticScriptSource(" \n\n\t \t\n"); }
@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)); } }
@Test(expected = IllegalArgumentException.class) public void createWithEmptyScript() throws Exception { new StaticScriptSource(""); }
@Test public void testChangeScriptWithRefreshableBeanFunctionality() throws Exception { BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(true); BeanDefinition scriptedBeanDefinition = createScriptedGroovyBean(); GenericApplicationContext ctx = new GenericApplicationContext(); ctx.registerBeanDefinition(PROCESSOR_BEAN_NAME, processorBeanDefinition); ctx.registerBeanDefinition(MESSENGER_BEAN_NAME, scriptedBeanDefinition); 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); source.setScript(CHANGED_SCRIPT); Messenger refreshedMessenger = (Messenger) ctx.getBean(MESSENGER_BEAN_NAME); // the updated script surrounds the message in quotes before returning... assertEquals(EXPECTED_CHANGED_MESSAGE_TEXT, refreshedMessenger.getMessage()); }
@Test public void testGroovyScriptWithArguments() { ScriptEvaluator evaluator = new GroovyScriptEvaluator(); Map<String, Object> arguments = new HashMap<>(); arguments.put("a", 3); arguments.put("b", 2); Object result = evaluator.evaluate(new StaticScriptSource("return a * b"), arguments); assertEquals(6, result); }