public String getMessage() { return this.messenger.getMessage(); } }
@Test public void refreshableFromTag() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); Messenger messenger = (Messenger) ctx.getBean("refreshableMessenger"); assertEquals("Hello World!", messenger.getMessage()); assertTrue("Messenger should be Refreshable", messenger instanceof Refreshable); }
@Test public void testJsr223FromTagWithInterface() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerWithInterface")); Messenger messenger = (Messenger) ctx.getBean("messengerWithInterface"); assertFalse(AopUtils.isAopProxy(messenger)); assertEquals("Hello World!", messenger.getMessage()); }
@Test public void testJsr223FromTag() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messenger")); Messenger messenger = (Messenger) ctx.getBean("messenger"); assertFalse(AopUtils.isAopProxy(messenger)); assertEquals("Hello World!", messenger.getMessage()); }
@Test public void staticWithScriptReturningInstance() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerInstance")); Messenger messenger = (Messenger) ctx.getBean("messengerInstance"); String desiredMessage = "Hello World!"; assertEquals("Message is incorrect", desiredMessage, messenger.getMessage()); assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger)); ctx.close(); assertNull(messenger.getMessage()); }
@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()); }
@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)); }
@Test public void staticScriptImplementingInterface() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bshContext.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("messengerImpl")); Messenger messenger = (Messenger) ctx.getBean("messengerImpl"); String desiredMessage = "Hello World!"; assertEquals("Message is incorrect", desiredMessage, messenger.getMessage()); assertTrue(ctx.getBeansOfType(Messenger.class).values().contains(messenger)); ctx.close(); assertNull(messenger.getMessage()); }
@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)); }
@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)); }
@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)); }
@Test public void applicationEventListener() { ApplicationContext ctx = new ClassPathXmlApplicationContext("bsh-with-xsd.xml", getClass()); Messenger eventListener = (Messenger) ctx.getBean("eventListener"); ctx.publishEvent(new MyEvent(ctx)); assertEquals("count=2", eventListener.getMessage()); }
@Test public void testRefreshableJsr223FromTagWithInterface() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("jsr223-with-xsd.xml", getClass()); assertTrue(Arrays.asList(ctx.getBeanNamesForType(Messenger.class)).contains("refreshableMessengerWithInterface")); Messenger messenger = (Messenger) ctx.getBean("refreshableMessengerWithInterface"); assertTrue(AopUtils.isAopProxy(messenger)); assertTrue(messenger instanceof Refreshable); assertEquals("Hello World!", messenger.getMessage()); }
@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); }
@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)); }
@Test public void testRefreshableJsr223FromTag() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd-jsr223.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()); }
@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)); }
@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()); }
@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()); }
@Test public void testResourceScriptFromTag() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass()); Messenger messenger = (Messenger) ctx.getBean("messenger"); CallCounter countingAspect = (CallCounter) ctx.getBean("getMessageAspect"); assertTrue(AopUtils.isAopProxy(messenger)); assertFalse(messenger instanceof Refreshable); assertEquals(0, countingAspect.getCalls()); assertEquals("Hello World!", messenger.getMessage()); assertEquals(1, countingAspect.getCalls()); ctx.close(); assertEquals(-200, countingAspect.getCalls()); }