use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class CreatesTestBean method jdkIntroductionAppliesToCreatedObjectsNotFactoryBean.
@Test
void jdkIntroductionAppliesToCreatedObjectsNotFactoryBean() {
ITestBean tb = (ITestBean) beanFactory.getBean("factory-introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertThat(nop.getCount()).as("NOP should not have done any work yet").isEqualTo(0);
assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
int age = 5;
tb.setAge(age);
assertThat(tb.getAge()).isEqualTo(age);
assertThat(tb).as("Introduction was made").isInstanceOf(TimeStamped.class);
assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(3);
ITestBean tb2 = (ITestBean) beanFactory.getBean("second-introductionUsingJdk");
// Check two per-instance mixins were distinct
Lockable lockable1 = (Lockable) tb;
Lockable lockable2 = (Lockable) tb2;
assertThat(lockable1.locked()).isFalse();
assertThat(lockable2.locked()).isFalse();
tb.setAge(65);
assertThat(tb.getAge()).isEqualTo(65);
lockable1.lock();
assertThat(lockable1.locked()).isTrue();
// Shouldn't affect second
assertThat(lockable2.locked()).isFalse();
// Can still mod second object
tb2.setAge(12);
// But can't mod first
assertThatExceptionOfType(LockedException.class).as("mixin should have locked this object").isThrownBy(() -> tb.setAge(6));
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testSingletonInstancesAreEqual.
/**
* 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);
assertThat(test1_1).as("Singleton instances ==").isEqualTo(test1);
test1.setAge(25);
assertThat(test1_1.getAge()).isEqualTo(test1.getAge());
test1.setAge(250);
assertThat(test1_1.getAge()).isEqualTo(test1.getAge());
Advised pc1 = (Advised) test1;
Advised pc2 = (Advised) test1_1;
assertThat(pc2.getAdvisors()).isEqualTo(pc1.getAdvisors());
int oldLength = pc1.getAdvisors().length;
NopInterceptor di = new NopInterceptor();
pc1.addAdvice(1, di);
assertThat(pc2.getAdvisors()).isEqualTo(pc1.getAdvisors());
assertThat(pc2.getAdvisors().length).as("Now have one more advisor").isEqualTo((oldLength + 1));
assertThat(0).isEqualTo(di.getCount());
test1.setAge(5);
assertThat(test1.getAge()).isEqualTo(test1_1.getAge());
assertThat(3).isEqualTo(di.getCount());
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testCanAddAndRemoveAdvicesOnSingleton.
/**
* Note that we can't add or remove interfaces without reconfiguring the
* singleton.
*/
@Test
public void testCanAddAndRemoveAdvicesOnSingleton() {
ITestBean it = (ITestBean) factory.getBean("test1");
Advised pc = (Advised) it;
it.getAge();
NopInterceptor di = new NopInterceptor();
pc.addAdvice(0, di);
assertThat(di.getCount()).isEqualTo(0);
it.setAge(25);
assertThat(it.getAge()).isEqualTo(25);
assertThat(di.getCount()).isEqualTo(2);
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testProxyAProxyWithAdditionalInterface.
@Test
public void testProxyAProxyWithAdditionalInterface() {
ITestBean target = new TestBean();
mockTargetSource.setTarget(target);
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
as.addInterface(Serializable.class);
CglibAopProxy cglib = new CglibAopProxy(as);
ITestBean proxy1 = (ITestBean) cglib.getProxy();
mockTargetSource.setTarget(proxy1);
as = new AdvisedSupport(new Class<?>[] {});
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
cglib = new CglibAopProxy(as);
ITestBean proxy2 = (ITestBean) cglib.getProxy();
assertThat(proxy2 instanceof Serializable).isTrue();
}
use of org.springframework.aop.testfixture.interceptor.NopInterceptor in project spring-framework by spring-projects.
the class UnsupportedInterceptor method testPackageMethodInvocation.
@Test
public void testPackageMethodInvocation() {
PackageMethodTestBean bean = new PackageMethodTestBean();
bean.value = "foo";
mockTargetSource.setTarget(bean);
AdvisedSupport as = new AdvisedSupport();
as.setTargetSource(mockTargetSource);
as.addAdvice(new NopInterceptor());
AopProxy aop = new CglibAopProxy(as);
PackageMethodTestBean proxy = (PackageMethodTestBean) aop.getProxy();
assertThat(AopUtils.isCglibProxy(proxy)).isTrue();
assertThat(bean.getClass().getClassLoader()).isEqualTo(proxy.getClass().getClassLoader());
assertThat(proxy.getString()).isEqualTo("foo");
}
Aggregations