org.springframework.scripting.Messenger Java Examples

The following examples show how to use org.springframework.scripting.Messenger. 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: JRubyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticScriptUsingJsr223() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("jrubyContextWithJsr223.xml", getClass());
	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));
	assertNotSame(messenger.hashCode(), calc.hashCode());
	assertTrue(!messenger.toString().equals(calc.toString()));

	assertEquals(3, calc.add(1, 2));
	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
 
Example #2
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void nonStaticScript() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass());
	Messenger messenger = (Messenger) ctx.getBean("messenger");

	assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
	assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());

	Refreshable refreshable = (Refreshable) messenger;
	refreshable.refresh();

	assertEquals("Message is incorrect after refresh", desiredMessage, messenger.getMessage());
	assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
 
Example #3
Source File: ScriptFactoryPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example #4
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
// Test for SPR-6268
public void testRefreshableFromTagProxyTargetClass() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-proxy-target-class.xml",
			getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));

	Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");

	assertTrue(AopUtils.isAopProxy(messenger));
	assertTrue(messenger instanceof Refreshable);
	assertEquals("Hello World!", messenger.getMessage());

	assertTrue(ctx.getBeansOfType(ConcreteMessenger.class).values().contains(messenger));

	// Check that AnnotationUtils works with concrete proxied script classes
	assertNotNull(AnnotationUtils.findAnnotation(messenger.getClass(), Component.class));
}
 
Example #5
Source File: ScriptFactoryPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeScriptedBean() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());

	BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
	scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
	scriptedBeanBuilder.addPropertyReference("messenger", "messenger");

	final String BEAN_WITH_DEPENDENCY_NAME = "needsMessenger";
	ctx.registerBeanDefinition(BEAN_WITH_DEPENDENCY_NAME, scriptedBeanBuilder.getBeanDefinition());
	ctx.registerBeanDefinition("scriptProcessor", createScriptFactoryPostProcessor(true));
	ctx.refresh();

	Messenger messenger1 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	Messenger messenger2 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	assertNotSame(messenger1, messenger2);
}
 
Example #6
Source File: ScriptFactoryPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example #7
Source File: BshScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void scriptThatCompilesButIsJustPlainBad() throws IOException {
	ScriptSource script = mock(ScriptSource.class);
	final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
	given(script.getScriptAsString()).willReturn(badScript);
	given(script.isModified()).willReturn(true);
	BshScriptFactory factory = new BshScriptFactory(
			ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class);
	try {
		Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class);
		messenger.getMessage();
		fail("Must have thrown a BshScriptUtils.BshExecutionException.");
	}
	catch (BshScriptUtils.BshExecutionException expected) {
	}
}
 
Example #8
Source File: ScriptFactoryPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeScriptedBean() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	ctx.registerBeanDefinition("messenger", BeanDefinitionBuilder.rootBeanDefinition(StubMessenger.class).getBeanDefinition());

	BeanDefinitionBuilder scriptedBeanBuilder = BeanDefinitionBuilder.rootBeanDefinition(GroovyScriptFactory.class);
	scriptedBeanBuilder.setScope(BeanDefinition.SCOPE_PROTOTYPE);
	scriptedBeanBuilder.addConstructorArgValue(DELEGATING_SCRIPT);
	scriptedBeanBuilder.addPropertyReference("messenger", "messenger");

	final String BEAN_WITH_DEPENDENCY_NAME = "needsMessenger";
	ctx.registerBeanDefinition(BEAN_WITH_DEPENDENCY_NAME, scriptedBeanBuilder.getBeanDefinition());
	ctx.registerBeanDefinition("scriptProcessor", createScriptFactoryPostProcessor(true));
	ctx.refresh();

	Messenger messenger1 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	Messenger messenger2 = (Messenger) ctx.getBean(BEAN_WITH_DEPENDENCY_NAME);
	assertNotSame(messenger1, messenger2);
}
 
Example #9
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testNonStaticScript() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyRefreshableContext.xml", getClass());
	Messenger messenger = (Messenger) ctx.getBean("messenger");

	assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
	assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());

	Refreshable refreshable = (Refreshable) messenger;
	refreshable.refresh();

	assertEquals("Message is incorrect after refresh.", desiredMessage, messenger.getMessage());
	assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
 
Example #10
Source File: JRubyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticScript() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("jrubyContext.xml", getClass());
	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));
	assertNotSame(messenger.hashCode(), calc.hashCode());
	assertTrue(!messenger.toString().equals(calc.toString()));

	assertEquals(3, calc.add(1, 2));
	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
}
 
Example #11
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scriptThatCompilesButIsJustPlainBad() throws Exception {
	ScriptSource script = mock(ScriptSource.class);
	final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
	given(script.getScriptAsString()).willReturn(badScript);
	given(script.isModified()).willReturn(true);
	BshScriptFactory factory = new BshScriptFactory(
			ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class);
	try {
		Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class);
		messenger.getMessage();
		fail("Must have thrown a BshScriptUtils.BshExecutionException.");
	}
	catch (BshScriptUtils.BshExecutionException expected) {
	}
}
 
Example #12
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRefreshableFromTag() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));

	Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
	CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect");

	assertTrue(AopUtils.isAopProxy(messenger));
	assertTrue(messenger instanceof Refreshable);
	assertEquals(0, countingAspect.getCalls());
	assertEquals("Hello World!", messenger.getMessage());
	assertEquals(1, countingAspect.getCalls());

	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #13
Source File: GroovyScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-6268
public void testRefreshableFromTagProxyTargetClass() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-proxy-target-class.xml",
			getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));

	Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");

	assertTrue(AopUtils.isAopProxy(messenger));
	assertTrue(messenger instanceof Refreshable);
	assertEquals("Hello World!", messenger.getMessage());

	assertTrue(ctx.getBeansOfType(ConcreteMessenger.class).values().contains(messenger));

	// Check that AnnotationUtils works with concrete proxied script classes
	assertNotNull(AnnotationUtils.findAnnotation(messenger.getClass(), Component.class));
}
 
Example #14
Source File: ScriptFactoryPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #15
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRefreshableFromTag() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessenger"));

	Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger");
	CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect");

	assertTrue(AopUtils.isAopProxy(messenger));
	assertTrue(messenger instanceof Refreshable);
	assertEquals(0, countingAspect.getCalls());
	assertEquals("Hello World!", messenger.getMessage());
	assertEquals(1, countingAspect.getCalls());

	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #16
Source File: ScriptFactoryPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #17
Source File: AdvisedJRubyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdviseWithBeanNameAutoProxyCreator() {
	ClassPathXmlApplicationContext ctx =
		new ClassPathXmlApplicationContext(APC_CONTEXT, CLASS);
	try {
		Messenger bean = (Messenger) ctx.getBean("messenger");
		assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean));
		assertTrue("Bean is not an Advised object", bean instanceof Advised);

		CountingBeforeAdvice advice = (CountingBeforeAdvice) ctx.getBean("advice");
		assertEquals(0, advice.getCalls());
		bean.getMessage();
		assertEquals(1, advice.getCalls());
	} finally {
		ctx.close();
	}
}
 
Example #18
Source File: BshScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scriptThatCompilesButIsJustPlainBad() throws IOException {
	ScriptSource script = mock(ScriptSource.class);
	final String badScript = "String getMessage() { throw new IllegalArgumentException(); }";
	given(script.getScriptAsString()).willReturn(badScript);
	given(script.isModified()).willReturn(true);
	BshScriptFactory factory = new BshScriptFactory(
			ScriptFactoryPostProcessor.INLINE_SCRIPT_PREFIX + badScript, Messenger.class);
	try {
		Messenger messenger = (Messenger) factory.getScriptedObject(script, Messenger.class);
		messenger.getMessage();
		fail("Must have thrown a BshScriptUtils.BshExecutionException.");
	}
	catch (BshScriptUtils.BshExecutionException expected) {
	}
}
 
Example #19
Source File: BshScriptFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nonStaticScript() {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("bshRefreshableContext.xml", getClass());
	Messenger messenger = (Messenger) ctx.getBean("messenger");

	assertTrue("Should be a proxy for refreshable scripts", AopUtils.isAopProxy(messenger));
	assertTrue("Should be an instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());

	Refreshable refreshable = (Refreshable) messenger;
	refreshable.refresh();

	assertEquals("Message is incorrect after refresh", desiredMessage, messenger.getMessage());
	assertEquals("Incorrect refresh count", 2, refreshable.getRefreshCount());
}
 
Example #20
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticScriptWithInlineDefinedInstanceUsingJsr223() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstanceInline"));
	Messenger messenger = (Messenger) ctx.getBean("messengerInstanceInline");

	assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
	assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #21
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testStaticScriptWithInstanceUsingJsr223() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContextWithJsr223.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
	Messenger messenger = (Messenger) ctx.getBean("messengerInstance");

	assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
	assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #22
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void staticScriptWithTwoInterfacesSpecified() throws Exception {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithConfigExtra"));

	ConfigurableMessenger messenger = (ConfigurableMessenger) ctx.getBean("messengerWithConfigExtra");
	messenger.setMessage(null);
	assertNull(messenger.getMessage());
	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));

	ctx.close();
	assertNull(messenger.getMessage());
}
 
Example #23
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testStaticScriptWithInlineDefinedInstance() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstanceInline"));
	Messenger messenger = (Messenger) ctx.getBean("messengerInstanceInline");

	assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
	assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #24
Source File: BshScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resourceScriptFromTag() throws Exception {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass());
	TestBean testBean = (TestBean) ctx.getBean("testBean");

	Collection<String> beanNames = Arrays.asList(ctx.getBeanNamesForType(Messenger.class));
	assertTrue(beanNames.contains("messenger"));
	assertTrue(beanNames.contains("messengerImpl"));
	assertTrue(beanNames.contains("messengerInstance"));

	Messenger messenger = (Messenger) ctx.getBean("messenger");
	assertEquals("Hello World!", messenger.getMessage());
	assertFalse(messenger instanceof Refreshable);

	Messenger messengerImpl = (Messenger) ctx.getBean("messengerImpl");
	assertEquals("Hello World!", messengerImpl.getMessage());

	Messenger messengerInstance = (Messenger) ctx.getBean("messengerInstance");
	assertEquals("Hello World!", messengerInstance.getMessage());

	TestBeanAwareMessenger messengerByType = (TestBeanAwareMessenger) ctx.getBean("messengerByType");
	assertEquals(testBean, messengerByType.getTestBean());

	TestBeanAwareMessenger messengerByName = (TestBeanAwareMessenger) ctx.getBean("messengerByName");
	assertEquals(testBean, messengerByName.getTestBean());

	Collection<Messenger> beans = ctx.getBeansOfType(Messenger.class).values();
	assertTrue(beans.contains(messenger));
	assertTrue(beans.contains(messengerImpl));
	assertTrue(beans.contains(messengerInstance));
	assertTrue(beans.contains(messengerByType));
	assertTrue(beans.contains(messengerByName));

	ctx.close();
	assertNull(messenger.getMessage());
	assertNull(messengerImpl.getMessage());
	assertNull(messengerInstance.getMessage());
}
 
Example #25
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testWithTwoClassesDefinedInTheOneGroovyFile_CorrectClassFirst() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesCorrectOneFirst.xml", getClass());
	Messenger messenger = (Messenger) ctx.getBean("messenger");
	assertNotNull(messenger);
	assertEquals("Hello World!", messenger.getMessage());

	// Check can cast to GroovyObject
	GroovyObject goo = (GroovyObject) messenger;
	assertNotNull(goo);
}
 
Example #26
Source File: GroovyScriptFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example #27
Source File: StandardScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInlineJsr223FromTagWithInterface() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("inlineMessengerWithInterface"));
	Messenger messenger = (Messenger) ctx.getBean("inlineMessengerWithInterface");
	assertFalse(AopUtils.isAopProxy(messenger));
	assertEquals("Hello World!", messenger.getMessage());
}
 
Example #28
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 #29
Source File: GroovyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticScriptWithInstance() throws Exception {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("groovyContext.xml", getClass());
	assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance"));
	Messenger messenger = (Messenger) ctx.getBean("messengerInstance");

	assertFalse("Shouldn't get proxy when refresh is disabled", AopUtils.isAopProxy(messenger));
	assertFalse("Scripted object should not be instance of Refreshable", messenger instanceof Refreshable);

	String desiredMessage = "Hello World!";
	assertEquals("Message is incorrect", desiredMessage, messenger.getMessage());
	assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger));
}
 
Example #30
Source File: JRubyScriptFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCtorWithWhitespacedScriptSourceLocator() throws Exception {
	try {
		new JRubyScriptFactory("\n   ", Messenger.class);
		fail("Must have thrown exception by this point.");
	}
	catch (IllegalArgumentException expected) {
	}
}