public void method() { Assert.state(this.selfReference != this && AopUtils.isCglibProxy(this.selfReference), "Self reference must be a CGLIB proxy"); this.dependency.method(); }
@Test public void defaultProxyTargetClass() { ApplicationContext context = new ClassPathXmlApplicationContext(PROXY_CONFIG); Object testBean = context.getBean("testBean"); assertTrue(AopUtils.isCglibProxy(testBean)); }
@Test public void withCglibProxy() { ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithCglibProxy.class); aspectIsApplied(ctx); assertThat(AopUtils.isCglibProxy(ctx.getBean(FooService.class)), is(true)); }
@Test public void succeedsWhenSubclassProxyAndScheduledMethodNotPresentOnInterface() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, SubclassProxyTxConfig.class, RepoConfigA.class); ctx.refresh(); Thread.sleep(100); // allow @Scheduled method to be called several times MyRepository repository = ctx.getBean(MyRepository.class); CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class); assertThat("repository is not a proxy", AopUtils.isCglibProxy(repository), equalTo(true)); assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); assertThat("no transactions were committed", txManager.commits, greaterThan(0)); }
@Test public void withAspectConfig() throws InterruptedException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AspectConfig.class, MyRepositoryWithScheduledMethodImpl.class); ctx.refresh(); Thread.sleep(100); // allow @Scheduled method to be called several times MyRepositoryWithScheduledMethod repository = ctx.getBean(MyRepositoryWithScheduledMethod.class); assertThat("repository is not a proxy", AopUtils.isCglibProxy(repository), is(true)); assertThat("@Scheduled method never called", repository.getInvocationCount(), greaterThan(0)); }
@Test public void annotatedServiceWithoutInterface_PTC_false() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCFalse.class, AnnotatedServiceWithoutInterface.class); ctx.refresh(); AnnotatedServiceWithoutInterface s = ctx.getBean(AnnotatedServiceWithoutInterface.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(AnnotatedServiceWithoutInterface.class)); }
@Test public void annotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, NonAnnotatedServiceImpl.class); ctx.refresh(); AnnotatedService s = ctx.getBean(AnnotatedService.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(NonAnnotatedServiceImpl.class)); }
@Test public void repositoryIsClassBasedCacheProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, ProxyTargetClassCachingConfig.class); ctx.refresh(); assertCacheProxying(ctx); assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class)), is(true)); }
@Test public void nonAnnotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, AnnotatedServiceImpl.class); ctx.refresh(); NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(AnnotatedServiceImpl.class)); }
@Test public void annotatedServiceWithoutInterface_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, AnnotatedServiceWithoutInterface.class); ctx.refresh(); AnnotatedServiceWithoutInterface s = ctx.getBean(AnnotatedServiceWithoutInterface.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(AnnotatedServiceWithoutInterface.class)); }
@Test public void repositoryIsClassBasedTxProxy() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, ProxyTargetClassTxConfig.class); ctx.refresh(); assertTxProxying(ctx); assertThat(AopUtils.isCglibProxy(ctx.getBean(FooRepository.class)), is(true)); }
private Foo cglibProxy(Foo foo) { ProxyFactory pf = new ProxyFactory(); pf.setTarget(foo); pf.setProxyTargetClass(true); Foo proxy = (Foo) pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertThat(proxy, instanceOf(FooImpl.class)); return proxy; }
@Test public void setFieldAndGetFieldViaCglibProxy() throws Exception { ProxyFactory pf = new ProxyFactory(this.person); pf.setProxyTargetClass(true); Person proxy = (Person) pf.getProxy(); assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy)); assertSetFieldAndGetFieldBehaviorForProxy(proxy, this.person); }
@Override protected Object createProxy(ProxyCreatorSupport as) { as.setProxyTargetClass(true); Object proxy = as.createAopProxy().getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); return proxy; }
@Test public void requestScopedService() throws Exception { assertTrue("request-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedService)); this.mockMvc.perform(get("/requestScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void sessionScopedService() throws Exception { assertTrue("session-scoped service must be a CGLIB proxy", AopUtils.isCglibProxy(this.sessionScopedService)); this.mockMvc.perform(get("/sessionScopedService").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }
@Test public void testIsClassProxy() { ITestBean bean = getTestBean(); assertTrue("Should be a CGLIB proxy", AopUtils.isCglibProxy(bean)); assertTrue("Should expose proxy", ((Advised) bean).isExposeProxy()); }
@Test public void testCglibPrototypeInstance() { Object prototype = testPrototypeInstancesAreIndependent("cglibPrototype"); assertTrue("It's a cglib proxy", AopUtils.isCglibProxy(prototype)); assertFalse("It's not a dynamic proxy", AopUtils.isJdkDynamicProxy(prototype)); }
@Test public void testGetsAreNotTransactionalWithProxyFactory2Cglib() { ITestBean testBean = (ITestBean) factory.getBean("proxyFactory2Cglib"); assertTrue("testBean is CGLIB advised", AopUtils.isCglibProxy(testBean)); assertTrue(testBean instanceof TransactionalProxy); doTestGetsAreNotTransactional(testBean); }
@Test public void requestScopedController() throws Exception { assertTrue("request-scoped controller must be a CGLIB proxy", AopUtils.isCglibProxy(this.requestScopedController)); this.mockMvc.perform(get("/requestScopedController").requestAttr(FROM_MVC_TEST_MOCK, FROM_MVC_TEST_MOCK)); }