|
| 1 | +package org.baeldung.customscope; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.hamcrest.CoreMatchers.not; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 12 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 13 | + |
| 14 | +public class TenantScopeTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() { |
| 18 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 19 | + try{ |
| 20 | + ctx.register(TenantScopeConfig.class); |
| 21 | + ctx.register(TenantBeansConfig.class); |
| 22 | + ctx.refresh(); |
| 23 | + |
| 24 | + TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class); |
| 25 | + foo.sayHello(); |
| 26 | + TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class); |
| 27 | + bar.sayHello(); |
| 28 | + Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class); |
| 29 | + |
| 30 | + assertThat(foo, not(equalTo(bar))); |
| 31 | + assertThat(foos.size(), equalTo(2)); |
| 32 | + assertTrue(foos.containsValue(foo)); |
| 33 | + assertTrue(foos.containsValue(bar)); |
| 34 | + |
| 35 | + BeanDefinition fooDefinition = ctx.getBeanDefinition("foo"); |
| 36 | + BeanDefinition barDefinition = ctx.getBeanDefinition("bar"); |
| 37 | + |
| 38 | + assertThat(fooDefinition.getScope(), equalTo("tenant")); |
| 39 | + assertThat(barDefinition.getScope(), equalTo("tenant")); |
| 40 | + } |
| 41 | + finally { |
| 42 | + ctx.close(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public final void whenComponentScan_thenContextContainsFooAndBar() { |
| 48 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 49 | + try{ |
| 50 | + ctx.scan("org.baeldung.customscope"); |
| 51 | + ctx.refresh(); |
| 52 | + |
| 53 | + TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class); |
| 54 | + foo.sayHello(); |
| 55 | + TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class); |
| 56 | + bar.sayHello(); |
| 57 | + Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class); |
| 58 | + |
| 59 | + assertThat(foo, not(equalTo(bar))); |
| 60 | + assertThat(foos.size(), equalTo(2)); |
| 61 | + assertTrue(foos.containsValue(foo)); |
| 62 | + assertTrue(foos.containsValue(bar)); |
| 63 | + |
| 64 | + BeanDefinition fooDefinition = ctx.getBeanDefinition("foo"); |
| 65 | + BeanDefinition barDefinition = ctx.getBeanDefinition("bar"); |
| 66 | + |
| 67 | + assertThat(fooDefinition.getScope(), equalTo("tenant")); |
| 68 | + assertThat(barDefinition.getScope(), equalTo("tenant")); |
| 69 | + } |
| 70 | + finally { |
| 71 | + ctx.close(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments