/** * Add all {@link Advisor Advisors} from the supplied {@link MetadataAwareAspectInstanceFactory} * to the current chain. Exposes any special purpose {@link Advisor Advisors} if needed. * @see AspectJProxyUtils#makeAdvisorChainAspectJCapableIfNecessary(List) */ private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) { List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory); Class<?> targetClass = getTargetClass(); Assert.state(targetClass != null, "Unresolvable target class"); advisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass); AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors); AnnotationAwareOrderComparator.sort(advisors); addAdvisors(advisors); }
@Test public void testIntroductionAdvisorExcludedFromTargetImplementingInterface() { assertTrue(AopUtils.findAdvisorsThatCanApply( getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), CannotBeUnlocked.class).isEmpty()); assertEquals(2, AopUtils.findAdvisorsThatCanApply(getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(),"someBean")), NotLockable.class).size()); }
@Test public void testIntroductionBasedOnAnnotationMatch_SPR5307() { AnnotatedTarget target = new AnnotatedTargetImpl(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeAnnotatedTypeModifiable(), "someBean")); Object proxy = createProxy(target, advisors, AnnotatedTarget.class); System.out.println(advisors.get(1)); assertTrue(proxy instanceof Lockable); Lockable lockable = (Lockable)proxy; lockable.locked(); }
@Test public void testRejectsPerCflowBelowAspect() { try { getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new PerCflowBelowAspect(), "someBean")); fail("Cannot accept cflowbelow"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("PERCFLOWBELOW")); } }
@Test public void testRejectsPerCflowAspect() { try { getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new PerCflowAspect(), "someBean")); fail("Cannot accept cflow"); } catch (AopConfigException ex) { assertTrue(ex.getMessage().contains("PERCFLOW")); } }
@Test public void testIntroductionOnTargetExcludedByTypePattern() { LinkedList<Object> target = new LinkedList<>(); List<?> proxy = (List<?>) createProxy(target, AopUtils.findAdvisorsThatCanApply( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), List.class ), List.class); assertFalse("Type pattern must have excluded mixin", proxy instanceof Lockable); }
@Test public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); List<Advisor> advisors = new LinkedList<>(); PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10(); aspect1.count = 100; advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1"))); PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5(); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2"))); Collections.sort(advisors, new OrderComparator()); TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class); assertEquals("Around advice must NOT apply", realAge, itb.getAge()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); assertEquals("Around advice must apply", 0, itb.getAge()); assertEquals("Around advice must apply", 1, itb.getAge()); }
@Test(expected = IllegalArgumentException.class) public void testDeclarePrecedenceNotSupported() { TestBean target = new TestBean(); MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( new DeclarePrecedenceShouldSucceed(), "someBean"); createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class); }
@Test public void testAspectMethodThrowsExceptionLegalOnSignature() { TestBean target = new TestBean(); UnsupportedOperationException expectedException = new UnsupportedOperationException(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean")); assertEquals("One advice method was found", 1, advisors.size()); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); try { itb.getAge(); fail(); } catch (UnsupportedOperationException ex) { assertSame(expectedException, ex); } }
@Test public void testBindingWithMultipleArgsDifferentlyOrdered() { ManyValuedArgs target = new ManyValuedArgs(); ManyValuedArgs mva = (ManyValuedArgs) createProxy(target, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new ManyValuedArgs(), "someBean")), ManyValuedArgs.class); String a = "a"; int b = 12; int c = 25; String d = "d"; StringBuffer e = new StringBuffer("stringbuf"); String expectedResult = a + b+ c + d + e; assertEquals(expectedResult, mva.mungeArgs(a, b, c, d, e)); }
@Test public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMethodException { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); List<Advisor> advisors = new LinkedList<>(); PerTargetAspect aspect1 = new PerTargetAspect(); aspect1.count = 100; aspect1.setOrder(10); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1"))); PerTargetAspect aspect2 = new PerTargetAspect(); aspect2.setOrder(5); advisors.addAll( getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2"))); Collections.sort(advisors, new OrderComparator()); TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class); assertEquals("Around advice must NOT apply", realAge, itb.getAge()); // Hit the method in the per clause to instantiate the aspect itb.getSpouse(); assertEquals("Around advice must apply", 0, itb.getAge()); assertEquals("Around advice must apply", 1, itb.getAge()); }
@Test public void testAspectMethodThrowsExceptionIllegalOnSignature() { TestBean target = new TestBean(); RemoteException expectedException = new RemoteException(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean")); assertEquals("One advice method was found", 1, advisors.size()); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); try { itb.getAge(); fail(); } catch (UndeclaredThrowableException ex) { assertSame(expectedException, ex.getCause()); } }
@Test public void testFailureWithoutExplicitDeclarePrecedence() { TestBean target = new TestBean(); MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory( new NoDeclarePrecedenceShouldFail(), "someBean"); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class); itb.getAge(); }
private void testNamedPointcuts(Object aspectInstance) { TestBean target = new TestBean(); int realAge = 65; target.setAge(realAge); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, "someBean")), ITestBean.class); assertEquals("Around advice must apply", -1, itb.getAge()); assertEquals(realAge, target.getAge()); }
@Test public void testTwoAdvicesOnOneAspect() { TestBean target = new TestBean(); TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(twoAdviceAspect, "someBean")); assertEquals("Two advice methods found", 2, advisors.size()); ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class); itb.setName(""); assertEquals(0, itb.getAge()); int newAge = 32; itb.setAge(newAge); assertEquals(1, itb.getAge()); }
@Test public void testBindingWithSingleArg() { TestBean target = new TestBean(); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new BindingAspectWithSingleArg(), "someBean")), ITestBean.class); itb.setAge(10); assertEquals("Around advice must apply", 20, itb.getAge()); assertEquals(20,target.getAge()); }
@Test public void testNamedPointcutFromAspectLibraryWithBinding() { TestBean target = new TestBean(); ITestBean itb = (ITestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory( new NamedPointcutAspectFromLibraryWithBinding(), "someBean")), ITestBean.class); itb.setAge(10); assertEquals("Around advice must apply", 20, itb.getAge()); assertEquals(20,target.getAge()); }
@Test public void testIntroductionOnTargetImplementingInterface() { CannotBeUnlocked target = new CannotBeUnlocked(); Lockable proxy = (Lockable) createProxy(target, // Ensure that we exclude AopUtils.findAdvisorsThatCanApply( getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(new MakeLockable(), "someBean")), CannotBeUnlocked.class ), CannotBeUnlocked.class); assertThat(proxy, instanceOf(Lockable.class)); Lockable lockable = proxy; assertTrue("Already locked", lockable.locked()); lockable.lock(); assertTrue("Real target ignores locking", lockable.locked()); try { lockable.unlock(); fail(); } catch (UnsupportedOperationException ex) { // Ok } }
@Test public void testAfterAdviceTypes() throws Exception { Echo target = new Echo(); ExceptionHandling afterReturningAspect = new ExceptionHandling(); List<Advisor> advisors = getFixture().getAdvisors( new SingletonMetadataAwareAspectInstanceFactory(afterReturningAspect, "someBean")); Echo echo = (Echo) createProxy(target, advisors, Echo.class); assertEquals(0, afterReturningAspect.successCount); assertEquals("", echo.echo("")); assertEquals(1, afterReturningAspect.successCount); assertEquals(0, afterReturningAspect.failureCount); try { echo.echo(new FileNotFoundException()); fail(); } catch (FileNotFoundException ex) { // Ok } catch (Exception ex) { fail(); } assertEquals(1, afterReturningAspect.successCount); assertEquals(1, afterReturningAspect.failureCount); assertEquals(afterReturningAspect.failureCount + afterReturningAspect.successCount, afterReturningAspect.afterCount); }
@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()); }