/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
/** * 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; }
/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
/** * 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 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 isModifiedNotToggledWhenSameScriptIsSet() throws Exception { source.setScript(SCRIPT_TEXT); assertFalse("Script must not be flagged as 'modified' when same script is passed in.", source.isModified()); }
@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 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 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 testChangeScriptWithNoRefreshableBeanFunctionality() throws Exception { BeanDefinition processorBeanDefinition = createScriptFactoryPostProcessor(false); 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); assertEquals("Script seems to have been refreshed (must not be as no refreshCheckDelay set on ScriptFactoryPostProcessor)", MESSAGE_TEXT, refreshedMessenger.getMessage()); }
/** * Create a new StaticScriptSource for the given script. * @param script the script String */ public StaticScriptSource(String script) { setScript(script); }
/** * 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; }