|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
43 | 43 | * @author Rod Johnson
|
44 | 44 | * @author Juergen Hoeller
|
45 | 45 | * @author Chris Beams
|
| 46 | + * @author Sam Brannen |
46 | 47 | * @since 04.07.2003
|
47 | 48 | */
|
48 | 49 | public class BeanFactoryUtilsTests {
|
@@ -323,4 +324,106 @@ public void testIntDependencies() {
|
323 | 324 | assertThat(Arrays.equals(new String[] { "buffer" }, deps)).isTrue();
|
324 | 325 | }
|
325 | 326 |
|
| 327 | + @Test |
| 328 | + public void isSingletonAndIsPrototypeWithStaticFactory() { |
| 329 | + StaticListableBeanFactory lbf = new StaticListableBeanFactory(); |
| 330 | + TestBean bean = new TestBean(); |
| 331 | + DummyFactory fb1 = new DummyFactory(); |
| 332 | + DummyFactory fb2 = new DummyFactory(); |
| 333 | + fb2.setSingleton(false); |
| 334 | + TestBeanSmartFactoryBean sfb1 = new TestBeanSmartFactoryBean(true, true); |
| 335 | + TestBeanSmartFactoryBean sfb2 = new TestBeanSmartFactoryBean(true, false); |
| 336 | + TestBeanSmartFactoryBean sfb3 = new TestBeanSmartFactoryBean(false, true); |
| 337 | + TestBeanSmartFactoryBean sfb4 = new TestBeanSmartFactoryBean(false, false); |
| 338 | + lbf.addBean("bean", bean); |
| 339 | + lbf.addBean("fb1", fb1); |
| 340 | + lbf.addBean("fb2", fb2); |
| 341 | + lbf.addBean("sfb1", sfb1); |
| 342 | + lbf.addBean("sfb2", sfb2); |
| 343 | + lbf.addBean("sfb3", sfb3); |
| 344 | + lbf.addBean("sfb4", sfb4); |
| 345 | + |
| 346 | + Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true); |
| 347 | + assertThat(beans.get("bean")).isSameAs(bean); |
| 348 | + assertThat(beans.get("fb1")).isSameAs(fb1.getObject()); |
| 349 | + assertThat(beans.get("fb2")).isInstanceOf(TestBean.class); |
| 350 | + assertThat(beans.get("sfb1")).isInstanceOf(TestBean.class); |
| 351 | + assertThat(beans.get("sfb2")).isInstanceOf(TestBean.class); |
| 352 | + assertThat(beans.get("sfb3")).isInstanceOf(TestBean.class); |
| 353 | + assertThat(beans.get("sfb4")).isInstanceOf(TestBean.class); |
| 354 | + |
| 355 | + assertThat(lbf.getBeanDefinitionCount()).isEqualTo(7); |
| 356 | + assertThat(lbf.getBean("bean")).isInstanceOf(TestBean.class); |
| 357 | + assertThat(lbf.getBean("&fb1")).isInstanceOf(FactoryBean.class); |
| 358 | + assertThat(lbf.getBean("&fb2")).isInstanceOf(FactoryBean.class); |
| 359 | + assertThat(lbf.getBean("&sfb1")).isInstanceOf(SmartFactoryBean.class); |
| 360 | + assertThat(lbf.getBean("&sfb2")).isInstanceOf(SmartFactoryBean.class); |
| 361 | + assertThat(lbf.getBean("&sfb3")).isInstanceOf(SmartFactoryBean.class); |
| 362 | + assertThat(lbf.getBean("&sfb4")).isInstanceOf(SmartFactoryBean.class); |
| 363 | + |
| 364 | + assertThat(lbf.isSingleton("bean")).isTrue(); |
| 365 | + assertThat(lbf.isSingleton("fb1")).isTrue(); |
| 366 | + assertThat(lbf.isSingleton("fb2")).isTrue(); |
| 367 | + assertThat(lbf.isSingleton("sfb1")).isTrue(); |
| 368 | + assertThat(lbf.isSingleton("sfb2")).isTrue(); |
| 369 | + assertThat(lbf.isSingleton("sfb3")).isTrue(); |
| 370 | + assertThat(lbf.isSingleton("sfb4")).isTrue(); |
| 371 | + |
| 372 | + assertThat(lbf.isSingleton("&fb1")).isTrue(); |
| 373 | + assertThat(lbf.isSingleton("&fb2")).isFalse(); |
| 374 | + assertThat(lbf.isSingleton("&sfb1")).isTrue(); |
| 375 | + assertThat(lbf.isSingleton("&sfb2")).isTrue(); |
| 376 | + assertThat(lbf.isSingleton("&sfb3")).isFalse(); |
| 377 | + assertThat(lbf.isSingleton("&sfb4")).isFalse(); |
| 378 | + |
| 379 | + assertThat(lbf.isPrototype("bean")).isFalse(); |
| 380 | + assertThat(lbf.isPrototype("fb1")).isFalse(); |
| 381 | + assertThat(lbf.isPrototype("fb2")).isFalse(); |
| 382 | + assertThat(lbf.isPrototype("sfb1")).isFalse(); |
| 383 | + assertThat(lbf.isPrototype("sfb2")).isFalse(); |
| 384 | + assertThat(lbf.isPrototype("sfb3")).isFalse(); |
| 385 | + assertThat(lbf.isPrototype("sfb4")).isFalse(); |
| 386 | + |
| 387 | + assertThat(lbf.isPrototype("&fb1")).isFalse(); |
| 388 | + assertThat(lbf.isPrototype("&fb2")).isTrue(); |
| 389 | + assertThat(lbf.isPrototype("&sfb1")).isTrue(); |
| 390 | + assertThat(lbf.isPrototype("&sfb2")).isFalse(); |
| 391 | + assertThat(lbf.isPrototype("&sfb3")).isTrue(); |
| 392 | + assertThat(lbf.isPrototype("&sfb4")).isTrue(); |
| 393 | + } |
| 394 | + |
| 395 | + |
| 396 | + static class TestBeanSmartFactoryBean implements SmartFactoryBean<TestBean> { |
| 397 | + |
| 398 | + private final TestBean testBean = new TestBean("enigma", 42); |
| 399 | + private final boolean singleton; |
| 400 | + private final boolean prototype; |
| 401 | + |
| 402 | + TestBeanSmartFactoryBean(boolean singleton, boolean prototype) { |
| 403 | + this.singleton = singleton; |
| 404 | + this.prototype = prototype; |
| 405 | + } |
| 406 | + |
| 407 | + @Override |
| 408 | + public boolean isSingleton() { |
| 409 | + return this.singleton; |
| 410 | + } |
| 411 | + |
| 412 | + @Override |
| 413 | + public boolean isPrototype() { |
| 414 | + return this.prototype; |
| 415 | + } |
| 416 | + |
| 417 | + @Override |
| 418 | + public Class<TestBean> getObjectType() { |
| 419 | + return TestBean.class; |
| 420 | + } |
| 421 | + |
| 422 | + public TestBean getObject() throws Exception { |
| 423 | + // We don't really care if the actual instance is a singleton or prototype |
| 424 | + // for the tests that use this factory. |
| 425 | + return this.testBean; |
| 426 | + } |
| 427 | + } |
| 428 | + |
326 | 429 | }
|
0 commit comments