use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.
the class DelegatingIntroductionInterceptorTests method testAutomaticInterfaceRecognitionInDelegate.
@Test
public void testAutomaticInterfaceRecognitionInDelegate() throws Exception {
final long t = 1001L;
class Tester implements TimeStamped, ITester {
@Override
public void foo() throws Exception {
}
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new DelegatingIntroductionInterceptor(new Tester());
TestBean target = new TestBean();
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
// assertTrue(Arrays.binarySearch(pf.getProxiedInterfaces(), TimeStamped.class) != -1);
TimeStamped ts = (TimeStamped) pf.getProxy();
assertThat(ts.getTimeStamp() == t).isTrue();
((ITester) ts).foo();
((ITestBean) ts).getAge();
}
use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.
the class CreatesTestBean method jdkIntroduction.
@Test
void jdkIntroduction() {
ITestBean tb = (ITestBean) beanFactory.getBean("introductionUsingJdk");
NopInterceptor nop = (NopInterceptor) beanFactory.getBean("introductionNopInterceptor");
assertThat(nop.getCount()).isEqualTo(0);
assertThat(AopUtils.isJdkDynamicProxy(tb)).isTrue();
int age = 5;
tb.setAge(age);
assertThat(tb.getAge()).isEqualTo(age);
boolean condition = tb instanceof TimeStamped;
assertThat(condition).as("Introduction was made").isTrue();
assertThat(((TimeStamped) tb).getTimeStamp()).isEqualTo(0);
assertThat(nop.getCount()).isEqualTo(3);
assertThat(tb.getName()).isEqualTo("introductionUsingJdk");
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.core.testfixture.TimeStamped 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.core.testfixture.TimeStamped in project spring-framework by spring-projects.
the class ProxyFactoryTests method testCanAddAndRemoveAspectInterfacesOnSingleton.
/**
* Should see effect immediately on behavior.
*/
@Test
public void testCanAddAndRemoveAspectInterfacesOnSingleton() {
ProxyFactory config = new ProxyFactory(new TestBean());
assertThat(config.getProxy() instanceof TimeStamped).as("Shouldn't implement TimeStamped before manipulation").isFalse();
long time = 666L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();
ti.setTime(time);
// Add to front of interceptor chain
int oldCount = config.getAdvisors().length;
config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
assertThat(config.getAdvisors().length == oldCount + 1).isTrue();
TimeStamped ts = (TimeStamped) config.getProxy();
assertThat(ts.getTimeStamp() == time).isTrue();
// Can remove
config.removeAdvice(ti);
assertThat(config.getAdvisors().length == oldCount).isTrue();
assertThatExceptionOfType(RuntimeException.class).as("Existing object won't implement this interface any more").isThrownBy(// Existing reference will fail
ts::getTimeStamp);
assertThat(config.getProxy() instanceof TimeStamped).as("Should no longer implement TimeStamped").isFalse();
// Now check non-effect of removing interceptor that isn't there
config.removeAdvice(new DebugInterceptor());
assertThat(config.getAdvisors().length == oldCount).isTrue();
ITestBean it = (ITestBean) ts;
DebugInterceptor debugInterceptor = new DebugInterceptor();
config.addAdvice(0, debugInterceptor);
it.getSpouse();
assertThat(debugInterceptor.getCount()).isEqualTo(1);
config.removeAdvice(debugInterceptor);
it.getSpouse();
// not invoked again
assertThat(debugInterceptor.getCount() == 1).isTrue();
}
use of org.springframework.core.testfixture.TimeStamped in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAdviceImplementsIntroductionInfo.
@Test
public void testAdviceImplementsIntroductionInfo() throws Throwable {
TestBean tb = new TestBean();
String name = "tony";
tb.setName(name);
ProxyFactory pc = new ProxyFactory(tb);
NopInterceptor di = new NopInterceptor();
pc.addAdvice(di);
final long ts = 37;
pc.addAdvice(new DelegatingIntroductionInterceptor((TimeStamped) () -> ts));
ITestBean proxied = (ITestBean) createProxy(pc);
assertThat(proxied.getName()).isEqualTo(name);
TimeStamped intro = (TimeStamped) proxied;
assertThat(intro.getTimeStamp()).isEqualTo(ts);
}
Aggregations