org.springframework.tests.aop.advice.CountingBeforeAdvice Java Examples
The following examples show how to use
org.springframework.tests.aop.advice.CountingBeforeAdvice.
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: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
Example #2
Source File: AdvisedJRubyScriptFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testAdviseWithProxyFactoryBean() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(FACTORYBEAN_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 #3
Source File: AdvisedJRubyScriptFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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 #4
Source File: ProxyFactoryBeanTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testGetObjectTypeWithDirectTarget() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS)); // We have a counting before advice here CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice"); assertEquals(0, cba.getCalls()); ITestBean tb = (ITestBean) bf.getBean("directTarget"); assertTrue(tb.getName().equals("Adam")); assertEquals(1, cba.getCalls()); ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget"); assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType())); }
Example #5
Source File: AopNamespaceHandlerTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testAdviceInvokedCorrectly() throws Exception { CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter"); CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter"); ITestBean bean = getTestBean(); assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName")); bean.getAge(); assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge")); bean.getName(); assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName")); assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName")); }
Example #6
Source File: CglibProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
Example #7
Source File: AopNamespaceHandlerTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testAdviceInvokedCorrectly() throws Exception { CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter"); CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter"); ITestBean bean = getTestBean(); assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName")); bean.getAge(); assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge")); bean.getName(); assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName")); assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName")); }
Example #8
Source File: CglibProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
Example #9
Source File: ProxyFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testRemoveAdvisorByReference() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(cba); pf.addAdvice(nop); pf.addAdvisor(advisor); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); assertEquals(1, cba.getCalls()); assertEquals(1, nop.getCount()); assertTrue(pf.removeAdvisor(advisor)); assertEquals(5, proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(2, nop.getCount()); assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); }
Example #10
Source File: ProxyFactoryBeanTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testGetObjectTypeWithDirectTarget() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS)); // We have a counting before advice here CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice"); assertEquals(0, cba.getCalls()); ITestBean tb = (ITestBean) bf.getBean("directTarget"); assertTrue(tb.getName().equals("Adam")); assertEquals(1, cba.getCalls()); ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget"); assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType())); }
Example #11
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.contains(advisor.toString())); assertTrue(proxyConfigString.contains("1 interface")); }
Example #12
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.contains(advisor.toString())); assertTrue(proxyConfigString.contains("1 interface")); }
Example #13
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(ITestBean.class); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
Example #14
Source File: ProxyFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testRemoveAdvisorByReference() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(cba); pf.addAdvice(nop); pf.addAdvisor(advisor); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); assertEquals(1, cba.getCalls()); assertEquals(1, nop.getCount()); assertTrue(pf.removeAdvisor(advisor)); assertEquals(5, proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(2, nop.getCount()); assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); }
Example #15
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Check that the string is informative. */ @Test public void testProxyConfigString() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(new Class<?>[] {ITestBean.class}); pc.addAdvice(new NopInterceptor()); MethodBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut(), mba); pc.addAdvisor(advisor); ITestBean proxied = (ITestBean) createProxy(pc); String proxyConfigString = ((Advised) proxied).toProxyConfigString(); assertTrue(proxyConfigString.indexOf(advisor.toString()) != -1); assertTrue(proxyConfigString.indexOf("1 interface") != -1); }
Example #16
Source File: ProxyFactoryBeanTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testGetObjectTypeWithDirectTarget() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(TARGETSOURCE_CONTEXT, CLASS)); // We have a counting before advice here CountingBeforeAdvice cba = (CountingBeforeAdvice) bf.getBean("countingBeforeAdvice"); assertEquals(0, cba.getCalls()); ITestBean tb = (ITestBean) bf.getBean("directTarget"); assertTrue(tb.getName().equals("Adam")); assertEquals(1, cba.getCalls()); ProxyFactoryBean pfb = (ProxyFactoryBean) bf.getBean("&directTarget"); assertTrue("Has correct object type", TestBean.class.isAssignableFrom(pfb.getObjectType())); }
Example #17
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testCanPreventCastToAdvisedUsingOpaque() { TestBean target = new TestBean(); ProxyFactory pc = new ProxyFactory(target); pc.setInterfaces(new Class<?>[] {ITestBean.class}); pc.addAdvice(new NopInterceptor()); CountingBeforeAdvice mba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba); pc.addAdvisor(advisor); assertFalse("Opaque defaults to false", pc.isOpaque()); pc.setOpaque(true); assertTrue("Opaque now true for this config", pc.isOpaque()); ITestBean proxied = (ITestBean) createProxy(pc); proxied.setAge(10); assertEquals(10, proxied.getAge()); assertEquals(1, mba.getCalls()); assertFalse("Cannot be cast to Advised", proxied instanceof Advised); }
Example #18
Source File: CglibProxyTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
Example #19
Source File: AopNamespaceHandlerTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testAdviceInvokedCorrectly() throws Exception { CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter"); CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter"); ITestBean bean = getTestBean(); assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName")); bean.getAge(); assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge")); assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge")); bean.getName(); assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName")); assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName")); }
Example #20
Source File: ProxyFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testRemoveAdvisorByReference() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); Advisor advisor = new DefaultPointcutAdvisor(cba); pf.addAdvice(nop); pf.addAdvisor(advisor); ITestBean proxied = (ITestBean) pf.getProxy(); proxied.setAge(5); assertEquals(1, cba.getCalls()); assertEquals(1, nop.getCount()); assertTrue(pf.removeAdvisor(advisor)); assertEquals(5, proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(2, nop.getCount()); assertFalse(pf.removeAdvisor(new DefaultPointcutAdvisor(null))); }
Example #21
Source File: ProxyFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testIndexOfMethods() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); Advised advised = (Advised) pf.getProxy(); // Can use advised and ProxyFactory interchangeably advised.addAdvice(nop); pf.addAdvisor(advisor); assertEquals(-1, pf.indexOf(new NopInterceptor())); assertEquals(0, pf.indexOf(nop)); assertEquals(1, pf.indexOf(advisor)); assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null))); }
Example #22
Source File: ProxyFactoryTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testReplaceAdvisor() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); CountingBeforeAdvice cba1 = new CountingBeforeAdvice(); CountingBeforeAdvice cba2 = new CountingBeforeAdvice(); Advisor advisor1 = new DefaultPointcutAdvisor(cba1); Advisor advisor2 = new DefaultPointcutAdvisor(cba2); pf.addAdvisor(advisor1); pf.addAdvice(nop); ITestBean proxied = (ITestBean) pf.getProxy(); // Use the type cast feature // Replace etc methods on advised should be same as on ProxyFactory Advised advised = (Advised) proxied; proxied.setAge(5); assertEquals(1, cba1.getCalls()); assertEquals(0, cba2.getCalls()); assertEquals(1, nop.getCount()); assertFalse(advised.replaceAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()), advisor2)); assertTrue(advised.replaceAdvisor(advisor1, advisor2)); assertEquals(advisor2, pf.getAdvisors()[0]); assertEquals(5, proxied.getAge()); assertEquals(1, cba1.getCalls()); assertEquals(2, nop.getCount()); assertEquals(1, cba2.getCalls()); assertFalse(pf.replaceAdvisor(new DefaultPointcutAdvisor(null), advisor1)); }
Example #23
Source File: AdvisorAutoProxyCreatorTests.java From java-technology-stack with MIT License | 5 votes |
@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()); }
Example #24
Source File: CglibProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testProxyProtectedMethod() throws Exception { CountingBeforeAdvice advice = new CountingBeforeAdvice(); ProxyFactory proxyFactory = new ProxyFactory(new MyBean()); proxyFactory.addAdvice(advice); proxyFactory.setProxyTargetClass(true); MyBean proxy = (MyBean) proxyFactory.getProxy(); assertEquals(4, proxy.add(1, 3)); assertEquals(1, advice.getCalls("add")); }
Example #25
Source File: BeanNameAutoProxyCreatorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Also has counting before advice. */ private void cglibAssertions(TestBean tb) { CountingBeforeAdvice cba = (CountingBeforeAdvice) beanFactory.getBean("countingBeforeAdvice"); NopInterceptor nop = (NopInterceptor) beanFactory.getBean("nopInterceptor"); assertEquals(0, cba.getCalls()); assertEquals(0, nop.getCount()); assertTrue(AopUtils.isCglibProxy(tb)); int age = 5; tb.setAge(age); assertEquals(age, tb.getAge()); assertEquals(2, nop.getCount()); assertEquals(2, cba.getCalls()); }
Example #26
Source File: AdvisorAutoProxyCreatorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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()); }
Example #27
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testCanCastProxyToProxyConfig() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(tb); NopInterceptor di = new NopInterceptor(); pc.addAdvice(0, di); ITestBean t = (ITestBean) createProxy(pc); assertEquals(0, di.getCount()); t.setAge(23); assertEquals(23, t.getAge()); assertEquals(2, di.getCount()); Advised advised = (Advised) t; assertEquals("Have 1 advisor", 1, advised.getAdvisors().length); assertEquals(di, advised.getAdvisors()[0].getAdvice()); NopInterceptor di2 = new NopInterceptor(); advised.addAdvice(1, di2); t.getName(); assertEquals(3, di.getCount()); assertEquals(1, di2.getCount()); // will remove di advised.removeAdvisor(0); t.getAge(); // Unchanged assertEquals(3, di.getCount()); assertEquals(2, di2.getCount()); CountingBeforeAdvice cba = new CountingBeforeAdvice(); assertEquals(0, cba.getCalls()); advised.addAdvice(cba); t.setAge(16); assertEquals(16, t.getAge()); assertEquals(2, cba.getCalls()); }
Example #28
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testBeforeAdvisorIsInvoked() { CountingBeforeAdvice cba = new CountingBeforeAdvice(); @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) { @Override public boolean matches(Method m, Class<?> targetClass) { return m.getParameterTypes().length == 0; } }; TestBean target = new TestBean(); target.setAge(80); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new NopInterceptor()); pf.addAdvisor(matchesNoArgs); assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]); ITestBean proxied = (ITestBean) createProxy(pf); assertEquals(0, cba.getCalls()); assertEquals(0, cba.getCalls("getAge")); assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(1, cba.getCalls("getAge")); assertEquals(0, cba.getCalls("setAge")); // Won't be advised proxied.setAge(26); assertEquals(1, cba.getCalls()); assertEquals(26, proxied.getAge()); }
Example #29
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testBeforeAdvisorIsInvoked() { CountingBeforeAdvice cba = new CountingBeforeAdvice(); @SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) { @Override public boolean matches(Method m, @Nullable Class<?> targetClass) { return m.getParameterCount() == 0; } }; TestBean target = new TestBean(); target.setAge(80); ProxyFactory pf = new ProxyFactory(target); pf.addAdvice(new NopInterceptor()); pf.addAdvisor(matchesNoArgs); assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]); ITestBean proxied = (ITestBean) createProxy(pf); assertEquals(0, cba.getCalls()); assertEquals(0, cba.getCalls("getAge")); assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, cba.getCalls()); assertEquals(1, cba.getCalls("getAge")); assertEquals(0, cba.getCalls("setAge")); // Won't be advised proxied.setAge(26); assertEquals(1, cba.getCalls()); assertEquals(26, proxied.getAge()); }
Example #30
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testCanCastProxyToProxyConfig() throws Throwable { TestBean tb = new TestBean(); ProxyFactory pc = new ProxyFactory(tb); NopInterceptor di = new NopInterceptor(); pc.addAdvice(0, di); ITestBean t = (ITestBean) createProxy(pc); assertEquals(0, di.getCount()); t.setAge(23); assertEquals(23, t.getAge()); assertEquals(2, di.getCount()); Advised advised = (Advised) t; assertEquals("Have 1 advisor", 1, advised.getAdvisors().length); assertEquals(di, advised.getAdvisors()[0].getAdvice()); NopInterceptor di2 = new NopInterceptor(); advised.addAdvice(1, di2); t.getName(); assertEquals(3, di.getCount()); assertEquals(1, di2.getCount()); // will remove di advised.removeAdvisor(0); t.getAge(); // Unchanged assertEquals(3, di.getCount()); assertEquals(2, di2.getCount()); CountingBeforeAdvice cba = new CountingBeforeAdvice(); assertEquals(0, cba.getCalls()); advised.addAdvice(cba); t.setAge(16); assertEquals(16, t.getAge()); assertEquals(2, cba.getCalls()); }