use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testValuesStick.
/**
* Simple test that if we set values we can get them out again.
*/
@Test
public void testValuesStick() {
int age1 = 33;
int age2 = 37;
String name = "tony";
TestBean target1 = new TestBean();
target1.setAge(age1);
ProxyFactory pf1 = new ProxyFactory(target1);
pf1.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
pf1.addAdvisor(new DefaultPointcutAdvisor(new TimestampIntroductionInterceptor()));
ITestBean tb = (ITestBean) pf1.getProxy();
assertThat(tb.getAge()).isEqualTo(age1);
tb.setAge(age2);
assertThat(tb.getAge()).isEqualTo(age2);
assertThat(tb.getName()).isNull();
tb.setName(name);
assertThat(tb.getName()).isEqualTo(name);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testBeforeAdvisorIsInvoked.
@Test
public void testBeforeAdvisorIsInvoked() {
CountingBeforeAdvice cba = new CountingBeforeAdvice();
@SuppressWarnings("serial") Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cba) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getParameterCount() == 0;
}
};
TestBean target = new TestBean();
target.setAge(80);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvisor(matchesNoArgs);
assertThat(pf.getAdvisors()[1]).as("Advisor was added").isEqualTo(matchesNoArgs);
ITestBean proxied = (ITestBean) createProxy(pf);
assertThat(cba.getCalls()).isEqualTo(0);
assertThat(cba.getCalls("getAge")).isEqualTo(0);
assertThat(proxied.getAge()).isEqualTo(target.getAge());
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(cba.getCalls("getAge")).isEqualTo(1);
assertThat(cba.getCalls("setAge")).isEqualTo(0);
// Won't be advised
proxied.setAge(26);
assertThat(cba.getCalls()).isEqualTo(1);
assertThat(proxied.getAge()).isEqualTo(26);
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testSerializationAdviceNotSerializable.
@Test
public void testSerializationAdviceNotSerializable() throws Exception {
SerializablePerson sp = new SerializablePerson();
assertThat(SerializationTestUtils.isSerializable(sp)).isTrue();
ProxyFactory pf = new ProxyFactory(sp);
// This isn't serializable
Advice i = new NopInterceptor();
pf.addAdvice(i);
assertThat(SerializationTestUtils.isSerializable(i)).isFalse();
Object proxy = createAopProxy(pf).getProxy();
assertThat(SerializationTestUtils.isSerializable(proxy)).isFalse();
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testStaticMethodPointcut.
@Test
public void testStaticMethodPointcut() throws Throwable {
TestBean tb = new TestBean();
ProxyFactory pc = new ProxyFactory();
pc.addInterface(ITestBean.class);
NopInterceptor di = new NopInterceptor();
TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
pc.addAdvisor(sp);
pc.setTarget(tb);
ITestBean it = (ITestBean) createProxy(pc);
assertThat(0).isEqualTo(di.getCount());
it.getAge();
assertThat(1).isEqualTo(di.getCount());
it.setAge(11);
assertThat(11).isEqualTo(it.getAge());
assertThat(2).isEqualTo(di.getCount());
}
use of cn.taketoday.aop.NopInterceptor in project today-framework by TAKETODAY.
the class AbstractAopProxyTests method testOverloadedMethodsWithDifferentAdvice.
@SuppressWarnings("serial")
@Test
public void testOverloadedMethodsWithDifferentAdvice() throws Throwable {
Overloads target = new Overloads();
ProxyFactory pc = new ProxyFactory(target);
NopInterceptor overLoadVoids = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadVoids) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 0;
}
});
NopInterceptor overLoadInts = new NopInterceptor();
pc.addAdvisor(new StaticMethodMatcherPointcutAdvisor(overLoadInts) {
@Override
public boolean matches(Method m, Class<?> targetClass) {
return m.getName().equals("overload") && m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(int.class);
}
});
IOverloads proxy = (IOverloads) createProxy(pc);
assertThat(overLoadInts.getCount()).isEqualTo(0);
assertThat(overLoadVoids.getCount()).isEqualTo(0);
proxy.overload();
assertThat(overLoadInts.getCount()).isEqualTo(0);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
assertThat(proxy.overload(25)).isEqualTo(25);
assertThat(overLoadInts.getCount()).isEqualTo(1);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
proxy.noAdvice();
assertThat(overLoadInts.getCount()).isEqualTo(1);
assertThat(overLoadVoids.getCount()).isEqualTo(1);
}
Aggregations