Skip to content

Commit

Permalink
Fix AutoProxyLazyInitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed May 12, 2020
1 parent 3c3e8e6 commit b313b33
Showing 1 changed file with 57 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,83 +18,93 @@

import javax.annotation.PreDestroy;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link BeanNameAutoProxyCreator} and
* {@link LazyInitTargetSourceCreator}.
*
* @author Juergen Hoeller
* @author Arrault Fabien
* @author Sam Brannen
*/
public class AutoProxyLazyInitTests {
class AutoProxyLazyInitTests {

@Test
public void withStaticBeanMethod() {
@BeforeEach
void resetBeans() {
MyBeanImpl.initialized = false;
}

ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
@Test
void withStaticBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
MyBean bean = ctx.getBean(MyBean.class);

assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();

ctx.close();
}

@Test
public void withStaticBeanMethodAndInterface() {
MyBeanImpl.initialized = false;

ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withStaticBeanMethodAndInterface() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
MyBean bean = ctx.getBean(MyBean.class);

assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();

ctx.close();
}

@Test
public void withNonStaticBeanMethod() {
MyBeanImpl.initialized = false;

ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withNonStaticBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
MyBean bean = ctx.getBean(MyBean.class);

assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();

ctx.close();
}

@Test
public void withNonStaticBeanMethodAndInterface() {
MyBeanImpl.initialized = false;

ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withNonStaticBeanMethodAndInterface() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
MyBean bean = ctx.getBean(MyBean.class);

assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();

ctx.close();
}


public static interface MyBean {
interface MyBean {

public String doIt();
String doIt();
}


public static class MyBeanImpl implements MyBean {
static class MyBeanImpl implements MyBean {

public static boolean initialized = false;
static boolean initialized = false;

public MyBeanImpl() {
MyBeanImpl() {
initialized = true;
}

Expand All @@ -110,46 +120,48 @@ public void destroy() {


@Configuration
public static class ConfigWithStatic {
static class ConfigWithStatic {

@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}

@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}

@Bean
@Lazy
public static MyBean myBean() {
static MyBean myBean() {
return new MyBeanImpl();
}
}


@Configuration
public static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {

@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}

@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}

@Bean
@Lazy
public static MyBean myBean() {
static MyBean myBean() {
return new MyBeanImpl();
}

Expand All @@ -160,46 +172,48 @@ public void onApplicationEvent(ApplicationContextEvent event) {


@Configuration
public static class ConfigWithNonStatic {
static class ConfigWithNonStatic {

@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}

@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}

@Bean
@Lazy
public MyBean myBean() {
MyBean myBean() {
return new MyBeanImpl();
}
}


@Configuration
public static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {

@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}

@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}

@Bean
@Lazy
public MyBean myBean() {
MyBean myBean() {
return new MyBeanImpl();
}

Expand Down

0 comments on commit b313b33

Please sign in to comment.