private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) { Advised advised = (Advised) tb; Advisor advisor = advised.getAdvisors()[0]; return (SimpleBeforeAdviceImpl) advisor.getAdvice(); }
private void assertCacheProxying(AnnotationConfigApplicationContext ctx) { FooRepository repo = ctx.getBean(FooRepository.class); boolean isCacheProxy = false; if (AopUtils.isAopProxy(repo)) { for (Advisor advisor : ((Advised)repo).getAdvisors()) { if (advisor instanceof BeanFactoryCacheOperationSourceAdvisor) { isCacheProxy = true; break; } } } assertTrue("FooRepository is not a cache proxy", isCacheProxy); }
protected void checkWillTranslateExceptions(Object o) { assertTrue(o instanceof Advised); Advised a = (Advised) o; for (Advisor advisor : a.getAdvisors()) { if (advisor instanceof PersistenceExceptionTranslationAdvisor) { return; } } fail("No translation"); }
private void assertTxProxying(AnnotationConfigApplicationContext ctx) { FooRepository repo = ctx.getBean(FooRepository.class); boolean isTxProxy = false; if (AopUtils.isAopProxy(repo)) { for (Advisor advisor : ((Advised)repo).getAdvisors()) { if (advisor instanceof BeanFactoryTransactionAttributeSourceAdvisor) { isTxProxy = true; break; } } } assertTrue("FooRepository is not a TX proxy", isTxProxy); // trigger a transaction repo.findAll(); }
@Test public void customAsyncAnnotationIsPropagated() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CustomAsyncAnnotationConfig.class, CustomAsyncBean.class); ctx.refresh(); Object bean = ctx.getBean(CustomAsyncBean.class); assertTrue(AopUtils.isAopProxy(bean)); boolean isAsyncAdvised = false; for (Advisor advisor : ((Advised) bean).getAdvisors()) { if (advisor instanceof AsyncAnnotationAdvisor) { isAsyncAdvised = true; break; } } assertTrue("bean was not async advised as expected", isAsyncAdvised); ctx.close(); }
@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); }
/** * The instances are equal, but do not have object identity. * Interceptors and interfaces and the target are the same. */ @Test public void testSingletonInstancesAreEqual() { ITestBean test1 = (ITestBean) factory.getBean("test1"); ITestBean test1_1 = (ITestBean) factory.getBean("test1"); //assertTrue("Singleton instances ==", test1 == test1_1); assertEquals("Singleton instances ==", test1, test1_1); test1.setAge(25); assertEquals(test1.getAge(), test1_1.getAge()); test1.setAge(250); assertEquals(test1.getAge(), test1_1.getAge()); Advised pc1 = (Advised) test1; Advised pc2 = (Advised) test1_1; assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors()); int oldLength = pc1.getAdvisors().length; NopInterceptor di = new NopInterceptor(); pc1.addAdvice(1, di); assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors()); assertEquals("Now have one more advisor", oldLength + 1, pc2.getAdvisors().length); assertEquals(di.getCount(), 0); test1.setAge(5); assertEquals(test1_1.getAge(), test1.getAge()); assertEquals(di.getCount(), 3); }
private long testAfterReturningAdviceWithoutJoinPoint(String file, int howmany, String technology) { ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS); StopWatch sw = new StopWatch(); sw.start(howmany + " repeated after returning advice invocations with " + technology); ITestBean adrian = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian)); Advised a = (Advised) adrian; assertTrue(a.getAdvisors().length >= 3); // Hits joinpoint adrian.setAge(25); for (int i = 0; i < howmany; i++) { adrian.setAge(i); } sw.stop(); System.out.println(sw.prettyPrint()); return sw.getLastTaskTimeMillis(); }
@Test public void testChainedDecorators() throws Exception { ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean"); assertTestBean(bean); assertTrue(AopUtils.isAopProxy(bean)); Advisor[] advisors = ((Advised) bean).getAdvisors(); assertEquals("Incorrect number of advisors", 2, advisors.length); assertEquals("Incorrect advice class", DebugInterceptor.class, advisors[0].getAdvice().getClass()); assertEquals("Incorrect advice class", NopInterceptor.class, advisors[1].getAdvice().getClass()); }
@Test public void testProxyingDecorator() throws Exception { ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean"); assertTestBean(bean); assertTrue(AopUtils.isAopProxy(bean)); Advisor[] advisors = ((Advised) bean).getAdvisors(); assertEquals("Incorrect number of advisors", 1, advisors.length); assertEquals("Incorrect advice class", DebugInterceptor.class, advisors[0].getAdvice().getClass()); }
private long testBeforeAdviceWithoutJoinPoint(String file, int howmany, String technology) { ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS); StopWatch sw = new StopWatch(); sw.start(howmany + " repeated before advice invocations with " + technology); ITestBean adrian = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian)); Advised a = (Advised) adrian; assertTrue(a.getAdvisors().length >= 3); assertEquals("adrian", adrian.getName()); for (int i = 0; i < howmany; i++) { adrian.getName(); } sw.stop(); System.out.println(sw.prettyPrint()); return sw.getLastTaskTimeMillis(); }
private long testMix(String file, int howmany, String technology) { ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS); StopWatch sw = new StopWatch(); sw.start(howmany + " repeated mixed invocations with " + technology); ITestBean adrian = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian)); Advised a = (Advised) adrian; assertTrue(a.getAdvisors().length >= 3); for (int i = 0; i < howmany; i++) { // Hit all 3 joinpoints adrian.getAge(); adrian.getName(); adrian.setAge(i); // Invoke three non-advised methods adrian.getDoctor(); adrian.getLawyer(); adrian.getSpouse(); } sw.stop(); System.out.println(sw.prettyPrint()); return sw.getLastTaskTimeMillis(); }
@Test public void testSerializable() throws Throwable { testSets(); // Count is now 2 Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(proxied); NopInterceptor nop2 = (NopInterceptor) ((Advised) p2).getAdvisors()[0].getAdvice(); p2.getName(); assertEquals(2, nop2.getCount()); p2.echo(null); assertEquals(3, nop2.getCount()); }
@Test public void testSerializable() throws Exception { DerivedTestBean tb = new DerivedTestBean(); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(ITestBean.class); ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor(); proxyFactory.addAdvice(cti); proxyFactory.setTarget(tb); ITestBean proxy = (ITestBean) proxyFactory.getProxy(); proxy.getAge(); ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy); Advised advised = (Advised) serializedProxy; ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice(); assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit()); serializedProxy.getAge(); }
@Test public void serializable() throws Exception { TestBean1 tb = new TestBean1(); CallCountingTransactionManager ptm = new CallCountingTransactionManager(); AnnotationTransactionAttributeSource tas = new AnnotationTransactionAttributeSource(); TransactionInterceptor ti = new TransactionInterceptor(ptm, tas); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(ITestBean1.class); proxyFactory.addAdvice(ti); proxyFactory.setTarget(tb); ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy(); proxy.getAge(); assertEquals(1, ptm.commits); ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy); serializedProxy.getAge(); Advised advised = (Advised) serializedProxy; TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice(); CallCountingTransactionManager serializedPtm = (CallCountingTransactionManager) serializedTi.getTransactionManager(); assertEquals(2, serializedPtm.commits); }
@Test public void testSessionScoping() throws Exception { MockHttpSession oldSession = new MockHttpSession(); MockHttpSession newSession = new MockHttpSession(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(oldSession); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); ITestBean scoped = (ITestBean) this.context.getBean("sessionScoped"); assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped)); assertFalse("Should not be target class proxy", scoped instanceof TestBean); ITestBean scopedAlias = (ITestBean) this.context.getBean("sessionScopedAlias"); assertSame(scoped, scopedAlias); ITestBean testBean = (ITestBean) this.context.getBean("testBean"); assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean)); assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean); String rob = "Rob Harrop"; String bram = "Bram Smeets"; assertEquals(rob, scoped.getName()); scoped.setName(bram); request.setSession(newSession); assertEquals(rob, scoped.getName()); request.setSession(oldSession); assertEquals(bram, scoped.getName()); assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0); }
@Test public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")), TestBean.class); assertEquals("Around advice must NOT apply", realAge, itb.getAge()); Advised advised = (Advised) itb; ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1]; assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3]; LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory(); assertFalse(maaif.isMaterialized()); // Check that the perclause pointcut is valid assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); assertTrue(maaif.isMaterialized()); assertEquals("Around advice must apply", 0, itb.getAge()); assertEquals("Around advice must apply", 1, itb.getAge()); }
@Test public void testRequestScoping() throws Exception { MockHttpServletRequest oldRequest = new MockHttpServletRequest(); MockHttpServletRequest newRequest = new MockHttpServletRequest(); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest)); ITestBean scoped = (ITestBean) this.context.getBean("requestScoped"); assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped)); assertTrue("Should be target class proxy", scoped instanceof TestBean); ITestBean testBean = (ITestBean) this.context.getBean("testBean"); assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean)); assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean); String rob = "Rob Harrop"; String bram = "Bram Smeets"; assertEquals(rob, scoped.getName()); scoped.setName(bram); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest)); assertEquals(rob, scoped.getName()); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest)); assertEquals(bram, scoped.getName()); assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0); }
@Test public void testCannotAddInterceptorWhenFrozen() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); try { pc.addAdvice(0, new NopInterceptor()); fail("Shouldn't be able to add interceptor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, ((Advised) proxied).getAdvisors().length); }
/** * Check that casting to Advised can't get around advice freeze. */ @Test public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable { TestBean target = new TestBean(); target.setAge(21); ProxyFactory pc = new ProxyFactory(target); assertFalse(pc.isFrozen()); pc.addAdvice(new NopInterceptor()); ITestBean proxied = (ITestBean) createProxy(pc); pc.setFrozen(true); Advised advised = (Advised) proxied; assertTrue(pc.isFrozen()); try { advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())); fail("Shouldn't be able to add Advisor when frozen"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("frozen")); } // Check it still works: proxy factory state shouldn't have been corrupted assertEquals(target.getAge(), proxied.getAge()); assertEquals(1, advised.getAdvisors().length); }