use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class ProxyFactoryTests method testAddRepeatedInterface.
@Test
public void testAddRepeatedInterface() {
TimeStamped tst = () -> {
throw new UnsupportedOperationException("getTimeStamp");
};
ProxyFactory pf = new ProxyFactory(tst);
// We've already implicitly added this interface.
// This call should be ignored without error
pf.addInterface(TimeStamped.class);
// All cool
assertThat(pf.getProxy()).isInstanceOf(TimeStamped.class);
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
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 cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class DelegatingIntroductionInterceptorTests method testIntroductionMasksTargetImplementation.
// Test when target implements the interface: should get interceptor by preference.
@Test
public void testIntroductionMasksTargetImplementation() throws Exception {
final long t = 1001L;
@SuppressWarnings("serial")
class TestII extends DelegatingIntroductionInterceptor implements TimeStamped {
@Override
public long getTimeStamp() {
return t;
}
}
DelegatingIntroductionInterceptor ii = new TestII();
// != t
TestBean target = new TargetClass(t + 1);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ii));
TimeStamped ts = (TimeStamped) pf.getProxy();
// From introduction interceptor, not target
assertThat(ts.getTimeStamp() == t).isTrue();
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithInterfaceHierarchy.
@Test
public void testIntroductionInterceptorWithInterfaceHierarchy() throws Exception {
TestBean raw = new TestBean();
assertThat(!(raw instanceof SubTimeStamped)).isTrue();
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = mock(SubTimeStamped.class);
long timestamp = 111L;
given(ts.getTimeStamp()).willReturn(timestamp);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), SubTimeStamped.class));
SubTimeStamped tsp = (SubTimeStamped) factory.getProxy();
assertThat(tsp.getTimeStamp() == timestamp).isTrue();
}
use of cn.taketoday.core.testfixture.TimeStamped in project today-infrastructure by TAKETODAY.
the class DelegatingIntroductionInterceptorTests method testIntroductionInterceptorWithSuperInterface.
@Test
public void testIntroductionInterceptorWithSuperInterface() throws Exception {
TestBean raw = new TestBean();
assertThat(!(raw instanceof TimeStamped)).isTrue();
ProxyFactory factory = new ProxyFactory(raw);
TimeStamped ts = mock(SubTimeStamped.class);
long timestamp = 111L;
given(ts.getTimeStamp()).willReturn(timestamp);
factory.addAdvisor(0, new DefaultIntroductionAdvisor(new DelegatingIntroductionInterceptor(ts), TimeStamped.class));
TimeStamped tsp = (TimeStamped) factory.getProxy();
assertThat(!(tsp instanceof SubTimeStamped)).isTrue();
assertThat(tsp.getTimeStamp() == timestamp).isTrue();
}
Aggregations