org.springframework.tests.sample.beans.factory.DummyFactory Java Examples

The following examples show how to use org.springframework.tests.sample.beans.factory.DummyFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #2
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #3
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #4
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #5
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #6
Source File: RequestScopedProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFromFactoryBeanInScope() throws Exception {
	String name = "requestScopedFactoryBean";
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isCglibProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.SINGLETON_NAME, bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.class, request.getAttribute("scopedTarget." + name).getClass());
		assertSame(bean, this.beanFactory.getBean(name));
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #7
Source File: RequestScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetFromFactoryBeanInScope() throws Exception {
	String name = "requestScopedFactoryBean";
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isCglibProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.SINGLETON_NAME, bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.class, request.getAttribute("scopedTarget." + name).getClass());
		assertSame(bean, this.beanFactory.getBean(name));
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #8
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #9
Source File: RequestScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetFromFactoryBeanInScope() throws Exception {
	String name = "requestScopedFactoryBean";
	TestBean bean = (TestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isCglibProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.SINGLETON_NAME, bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(DummyFactory.class, request.getAttribute("scopedTarget." + name).getClass());
		assertSame(bean, this.beanFactory.getBean(name));
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #10
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testFactoryBeanDidNotCreatePrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	assertEquals(TestBean.class, lbf.getType("x1"));
	lbf.preInstantiateSingletons();

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	lbf.getBean("x1");
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
 
Example #11
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #12
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example #13
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSingletonFactoryBeanIgnoredByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.(singleton)", "false");
	p.setProperty("x1.singleton", "true");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example #14
Source File: BeanFactoryUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testHierarchicalResolutionWithOverride() throws Exception {
	Object test3 = this.listableBeanFactory.getBean("test3");
	Object test = this.listableBeanFactory.getBean("test");

	Map<String, ?> beans =
			BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, false);
	assertEquals(2, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, false);
	assertEquals(1, beans.size());
	assertEquals(test, beans.get("test"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, false, true);
	Object testFactory1 = this.listableBeanFactory.getBean("testFactory1");
	assertEquals(2, beans.size());
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true, true);
	assertEquals(4, beans.size());
	assertEquals(test3, beans.get("test3"));
	assertEquals(test, beans.get("test"));
	assertEquals(testFactory1, beans.get("testFactory1"));
	assertTrue(beans.get("testFactory2") instanceof TestBean);

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, DummyFactory.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));

	beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, FactoryBean.class, true, true);
	assertEquals(2, beans.size());
	assertEquals(this.listableBeanFactory.getBean("&testFactory1"), beans.get("&testFactory1"));
	assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
}
 
Example #15
Source File: XmlListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanPostProcessor() throws Exception {
	TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
	TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue(kerry.isPostProcessed());
	assertTrue(kathy.isPostProcessed());
	assertTrue(factory.isPostProcessed());
	assertTrue(factoryCreated.isPostProcessed());
}
 
Example #16
Source File: AbstractBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void factorySingleton() throws Exception {
	assertTrue(getBeanFactory().isSingleton("&singletonFactory"));
	assertTrue(getBeanFactory().isSingleton("singletonFactory"));
	TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME));
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue("Singleton references ==", tb == tb2);
	assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null);
}
 
Example #17
Source File: AbstractBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void factoryPrototype() throws Exception {
	assertTrue(getBeanFactory().isSingleton("&prototypeFactory"));
	assertFalse(getBeanFactory().isSingleton("prototypeFactory"));
	TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory");
	assertTrue(!tb.getName().equals(DummyFactory.SINGLETON_NAME));
	TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory");
	assertTrue("Prototype references !=", tb != tb2);
}
 
Example #18
Source File: AbstractBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check that afterPropertiesSet gets called on factory
 * @throws Exception
 */
@Test
public void factoryIsInitialized() throws Exception {
	TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertNotNull(tb);
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	assertTrue("Factory was initialized because it implemented InitializingBean", factory.wasInitialized());
}
 
Example #19
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.(singleton)", "false");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example #20
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example #21
Source File: XmlListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void factoryReferences() {
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");

	DummyReferencer ref = (DummyReferencer) getBeanFactory().getBean("factoryReferencer");
	assertTrue(ref.getTestBean1() == ref.getTestBean2());
	assertTrue(ref.getDummyFactory() == factory);

	DummyReferencer ref2 = (DummyReferencer) getBeanFactory().getBean("factoryReferencerWithConstructor");
	assertTrue(ref2.getTestBean1() == ref2.getTestBean2());
	assertTrue(ref2.getDummyFactory() == factory);
}
 
Example #22
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertTrue(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertFalse(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example #23
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInnerBeansWithoutDestroy() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	// Let's create the outer bean named "innerBean",
	// to check whether it doesn't create any conflicts
	// with the actual inner beans named "innerBean".
	xbf.getBean("innerBean");

	TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeansWithoutDestroy");
	assertEquals(5, hasInnerBeans.getAge());
	TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
	assertNotNull(inner1);
	assertTrue(inner1.getBeanName().startsWith("innerBean"));
	assertEquals("inner1", inner1.getName());
	assertEquals(6, inner1.getAge());

	assertNotNull(hasInnerBeans.getFriends());
	Object[] friends = hasInnerBeans.getFriends().toArray();
	assertEquals(3, friends.length);
	DerivedTestBean inner2 = (DerivedTestBean) friends[0];
	assertEquals("inner2", inner2.getName());
	assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
	assertNotNull(inner2);
	assertEquals(7, inner2.getAge());
	TestBean innerFactory = (TestBean) friends[1];
	assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
	TestBean inner5 = (TestBean) friends[2];
	assertTrue(inner5.getBeanName().startsWith("innerBean"));
}
 
Example #24
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPrototypeFactoryBeanIgnoredByNonEagerTypeMatching() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", DummyFactory.class.getName());
	// Reset static state
	DummyFactory.reset();
	p.setProperty("x1.(singleton)", "false");
	p.setProperty("x1.singleton", "false");
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);

	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
	String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
	assertEquals(0, beanNames.length);
	beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
	assertEquals(0, beanNames.length);

	assertFalse(lbf.containsSingleton("x1"));
	assertTrue(lbf.containsBean("x1"));
	assertTrue(lbf.containsBean("&x1"));
	assertFalse(lbf.isSingleton("x1"));
	assertFalse(lbf.isSingleton("&x1"));
	assertTrue(lbf.isPrototype("x1"));
	assertTrue(lbf.isPrototype("&x1"));
	assertTrue(lbf.isTypeMatch("x1", TestBean.class));
	assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
	assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
	assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
	assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
	assertEquals(TestBean.class, lbf.getType("x1"));
	assertEquals(DummyFactory.class, lbf.getType("&x1"));
	assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
 
Example #25
Source File: AbstractBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check that afterPropertiesSet gets called on factory
 * @throws Exception
 */
@Test
public void factoryIsInitialized() throws Exception {
	TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertNotNull(tb);
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	assertTrue("Factory was initialized because it implemented InitializingBean", factory.wasInitialized());
}
 
Example #26
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);

	TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertSame(tb, tb2);
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	tb2.getAge();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example #27
Source File: AbstractBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void factoryPrototype() throws Exception {
	assertTrue(getBeanFactory().isSingleton("&prototypeFactory"));
	assertFalse(getBeanFactory().isSingleton("prototypeFactory"));
	TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory");
	assertTrue(!tb.getName().equals(DummyFactory.SINGLETON_NAME));
	TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory");
	assertTrue("Prototype references !=", tb != tb2);
}
 
Example #28
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInnerBeansWithoutDestroy() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	// Let's create the outer bean named "innerBean",
	// to check whether it doesn't create any conflicts
	// with the actual inner beans named "innerBean".
	xbf.getBean("innerBean");

	TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeansWithoutDestroy");
	assertEquals(5, hasInnerBeans.getAge());
	TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
	assertNotNull(inner1);
	assertTrue(inner1.getBeanName().startsWith("innerBean"));
	assertEquals("inner1", inner1.getName());
	assertEquals(6, inner1.getAge());

	assertNotNull(hasInnerBeans.getFriends());
	Object[] friends = hasInnerBeans.getFriends().toArray();
	assertEquals(3, friends.length);
	DerivedTestBean inner2 = (DerivedTestBean) friends[0];
	assertEquals("inner2", inner2.getName());
	assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
	assertNotNull(inner2);
	assertEquals(7, inner2.getAge());
	TestBean innerFactory = (TestBean) friends[1];
	assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
	TestBean inner5 = (TestBean) friends[2];
	assertTrue(inner5.getBeanName().startsWith("innerBean"));
}
 
Example #29
Source File: AbstractBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void factorySingleton() throws Exception {
	assertTrue(getBeanFactory().isSingleton("&singletonFactory"));
	assertTrue(getBeanFactory().isSingleton("singletonFactory"));
	TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME));
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue("Singleton references ==", tb == tb2);
	assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null);
}
 
Example #30
Source File: XmlListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessor() {
	TestBean kerry = (TestBean) getBeanFactory().getBean("kerry");
	TestBean kathy = (TestBean) getBeanFactory().getBean("kathy");
	DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
	TestBean factoryCreated = (TestBean) getBeanFactory().getBean("singletonFactory");
	assertTrue(kerry.isPostProcessed());
	assertTrue(kathy.isPostProcessed());
	assertTrue(factory.isPostProcessed());
	assertTrue(factoryCreated.isPostProcessed());
}