test.aop.PerTargetAspect Java Examples

The following examples show how to use test.aop.PerTargetAspect. 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: AspectJPointcutAdvisorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPerTarget() throws SecurityException, NoSuchMethodException {
	AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
	ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);

	InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(
			ajexp, TestBean.class.getMethod("getAge"), af,
			new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean"),
			1, "someBean");

	assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut);
	assertTrue(ajpa.isPerInstance());

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class));
	assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getAge"), TestBean.class));

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getSpouse"), TestBean.class));
}
 
Example #2
Source File: AspectJPointcutAdvisorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPerTarget() throws SecurityException, NoSuchMethodException {
	AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
	ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);

	InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(
			ajexp, TestBean.class.getMethod("getAge"), af,
			new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean"),
			1, "someBean");

	assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut);
	assertTrue(ajpa.isPerInstance());

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class));
	assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getAge"), TestBean.class));

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getSpouse"), TestBean.class));
}
 
Example #3
Source File: AspectJPointcutAdvisorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPerTarget() throws SecurityException, NoSuchMethodException {
	AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
	ajexp.setExpression(AspectJExpressionPointcutTests.MATCH_ALL_METHODS);

	InstantiationModelAwarePointcutAdvisorImpl ajpa = new InstantiationModelAwarePointcutAdvisorImpl(af, ajexp,
			new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(),"someBean"), null, 1, "someBean");
	assertNotSame(Pointcut.TRUE, ajpa.getAspectMetadata().getPerClausePointcut());
	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut() instanceof AspectJExpressionPointcut);
	assertTrue(ajpa.isPerInstance());

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getClassFilter().matches(TestBean.class));
	assertFalse(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getAge", (Class[]) null),
			TestBean.class));

	assertTrue(ajpa.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(
			TestBean.class.getMethod("getSpouse", (Class[]) null),
			TestBean.class));
}
 
Example #4
Source File: AspectMetadataTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() {
	AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean");
	assertTrue(am.isPerThisOrPerTarget());
	assertNotSame(Pointcut.TRUE, am.getPerClausePointcut());
	assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind());
}
 
Example #5
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example #6
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example #7
Source File: AspectMetadataTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() {
	AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean");
	assertTrue(am.isPerThisOrPerTarget());
	assertNotSame(Pointcut.TRUE, am.getPerClausePointcut());
	assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind());
}
 
Example #8
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #9
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #10
Source File: AspectMetadataTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerTargetAspect() {
	AspectMetadata am = new AspectMetadata(PerTargetAspect.class,"someBean");
	assertTrue(am.isPerThisOrPerTarget());
	assertNotSame(Pointcut.TRUE, am.getPerClausePointcut());
	assertEquals(PerClauseKind.PERTARGET, am.getAjType().getPerClause().getKind());
}
 
Example #11
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
	SyntheticInstantiationAdvisor sia = (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());
}
 
Example #12
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiplePerTargetAspects() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);

	List<Advisor> advisors = new LinkedList<Advisor>();
	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());
}