@Override protected Class getServiceClass(T ref) { if (AopUtils.isAopProxy(ref)) { return AopUtils.getTargetClass(ref); } return super.getServiceClass(ref); }
@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 testIntroductionWasMade() { assertTrue(AopUtils.isAopProxy(testBeanProxy)); assertFalse("Introduction should not be proxied", AopUtils.isAopProxy(introductionObject)); assertTrue("Introduction must have been made", testBeanProxy instanceof Lockable); }
@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)); }
@Before public void onSetUp() throws Exception { ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); AroundAdviceBindingTestAspect aroundAdviceAspect = ((AroundAdviceBindingTestAspect) ctx.getBean("testAspect")); ITestBean injectedTestBean = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(injectedTestBean)); this.testBeanProxy = injectedTestBean; // we need the real target too, not just the proxy... this.testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); mockCollaborator = mock(AroundAdviceBindingCollaborator.class); aroundAdviceAspect.setCollaborator(mockCollaborator); }
@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 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()); }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); afterAdviceAspect = (AfterReturningAdviceBindingTestAspect) ctx.getBean("testAspect"); mockCollaborator = mock(AfterReturningAdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... this.testBeanTarget = (TestBean) ((Advised)testBeanProxy).getTargetSource().getTarget(); }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); AdviceBindingTestAspect beforeAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); mockCollaborator = mock(AdviceBindingCollaborator.class); beforeAdviceAspect.setCollaborator(mockCollaborator); }
@Before public void setup() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass()); AdviceBindingTestAspect afterAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect"); testBeanProxy = (ITestBean) ctx.getBean("testBean"); assertTrue(AopUtils.isAopProxy(testBeanProxy)); // we need the real target too, not just the proxy... testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget(); mockCollaborator = mock(AdviceBindingCollaborator.class); afterAdviceAspect.setCollaborator(mockCollaborator); }
@Test public void transactionProxyIsCreated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( EnableTxConfig.class, TxManagerConfig.class); TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class); assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); Map<?,?> services = ctx.getBeansWithAnnotation(Service.class); assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); ctx.close(); }
@Test public void testWithOptimizedProxy() throws Exception { BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS); ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean"); assertTrue(AopUtils.isAopProxy(testBean)); CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice"); testBean.setAge(23); testBean.getAge(); assertEquals("Incorrect number of calls to proxy", 2, beforeAdvice.getCalls()); }
@Test public void proxyingOccursWithMockitoStub() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AsyncConfigWithMockito.class, AsyncBeanUser.class); ctx.refresh(); AsyncBeanUser asyncBeanUser = ctx.getBean(AsyncBeanUser.class); AsyncBean asyncBean = asyncBeanUser.getAsyncBean(); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); ctx.close(); }
@Test public void testWithAbstractFactoryBeanAreApplied() { ClassPathXmlApplicationContext bf = newContext("aspectsWithAbstractBean.xml"); ITestBean adrian = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian)); assertEquals(68, adrian.getAge()); }
@Test public void testIncludeMechanism() { ClassPathXmlApplicationContext bf = newContext("usesInclude.xml"); ITestBean adrian = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian)); assertEquals(68, adrian.getAge()); }
/** * We have custom TargetSourceCreators but there's no match, and * hence no proxying, for this bean */ @Test public void testCustomTargetSourceNoMatch() throws Exception { BeanFactory bf = new ClassPathXmlApplicationContext(CUSTOM_TARGETSOURCE_CONTEXT, CLASS); ITestBean test = (ITestBean) bf.getBean("test"); assertFalse(AopUtils.isAopProxy(test)); assertEquals("Rod", test.getName()); assertEquals("Kerry", test.getSpouse().getName()); }
@Test public void testAspectsAreApplied() { ClassPathXmlApplicationContext bf = newContext("aspects.xml"); ITestBean tb = (ITestBean) bf.getBean("adrian"); assertEquals(68, tb.getAge()); MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean"); assertTrue(AopUtils.isAopProxy(factoryBean.getTargetObject())); assertEquals(68, ((ITestBean) factoryBean.getTargetObject()).getAge()); }
@Test public void testIsProxy() throws Exception { ITestBean bean = getTestBean(); assertTrue("Bean is not a proxy", AopUtils.isAopProxy(bean)); // check the advice details Advised advised = (Advised) bean; Advisor[] advisors = advised.getAdvisors(); assertTrue("Advisors should not be empty", advisors.length > 0); }
@Test public void testProxy() throws Exception { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(CONTEXT); beanFactory.preInstantiateSingletons(); ITestBean bean = (ITestBean)beanFactory.getBean("testBean"); assertTrue(AopUtils.isAopProxy(bean)); } }
@Test public void testWithLazyConnectionAndNoAccess() throws Exception { MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean(); bean.setServiceUrl(serviceUrl); bean.setConnectOnStartup(false); bean.afterPropertiesSet(); MBeanServerConnection connection = bean.getObject(); assertTrue(AopUtils.isAopProxy(connection)); bean.destroy(); }