use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class AopNamespaceHandlerScopeIntegrationTests method testSingletonScoping.
@Test
void testSingletonScoping() throws Exception {
assertThat(AopUtils.isAopProxy(singletonScoped)).as("Should be AOP proxy").isTrue();
boolean condition = singletonScoped instanceof TestBean;
assertThat(condition).as("Should be target class proxy").isTrue();
String rob = "Rob Harrop";
String bram = "Bram Smeets";
assertThat(singletonScoped.getName()).isEqualTo(rob);
singletonScoped.setName(bram);
assertThat(singletonScoped.getName()).isEqualTo(bram);
ITestBean deserialized = SerializationTestUtils.serializeAndDeserialize(singletonScoped);
assertThat(deserialized.getName()).isEqualTo(bram);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class PerThisAspect method namedPointcuts.
private void namedPointcuts(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);
assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(-1);
assertThat(target.getAge()).isEqualTo(realAge);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class PerThisAspect method declarePrecedenceNotSupported.
@Test
void declarePrecedenceNotSupported() {
TestBean target = new TestBean();
assertThatIllegalArgumentException().isThrownBy(() -> {
MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(new DeclarePrecedenceShouldSucceed(), "someBean");
createProxy(target, getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
});
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class PerThisAspect method perThisAspect.
@Test
void perThisAspect() throws Exception {
TestBean target = new TestBean();
int realAge = 65;
target.setAge(realAge);
TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")), TestBean.class);
assertThat(itb.getAge()).as("Around advice must NOT apply").isEqualTo(realAge);
Advised advised = (Advised) itb;
// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
assertThat(advised.getAdvisors().length).isEqualTo(4);
ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia = (ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
assertThat(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
LazySingletonAspectInstanceFactoryDecorator maaif = (LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
assertThat(maaif.isMaterialized()).isFalse();
// Check that the perclause pointcut is valid
assertThat(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)).isTrue();
assertThat(imapa.getPointcut()).isNotSameAs(imapa.getDeclaredPointcut());
// Hit the method in the per clause to instantiate the aspect
itb.getSpouse();
assertThat(maaif.isMaterialized()).isTrue();
assertThat(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null)).isTrue();
assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(0);
assertThat(itb.getAge()).as("Around advice must apply").isEqualTo(1);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class PerThisAspect method twoAdvicesOnOneAspect.
@Test
void twoAdvicesOnOneAspect() {
TestBean target = new TestBean();
TwoAdviceAspect twoAdviceAspect = new TwoAdviceAspect();
List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(twoAdviceAspect, "someBean"));
assertThat(advisors.size()).as("Two advice methods found").isEqualTo(2);
ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);
itb.setName("");
assertThat(itb.getAge()).isEqualTo(0);
int newAge = 32;
itb.setAge(newAge);
assertThat(itb.getAge()).isEqualTo(1);
}
Aggregations