Package org.springframework.context.support

Examples of org.springframework.context.support.StaticApplicationContext


* @since 02.10.2003
*/
public class BeanFactoryPostProcessorTests extends TestCase {

  public void testRegisteredBeanFactoryPostProcessor() {
    StaticApplicationContext ac = new StaticApplicationContext();
    ac.registerSingleton("tb1", TestBean.class);
    ac.registerSingleton("tb2", TestBean.class);
    TestBeanFactoryPostProcessor bfpp = new TestBeanFactoryPostProcessor();
    ac.addBeanFactoryPostProcessor(bfpp);
    assertFalse(bfpp.wasCalled);
    ac.refresh();
    assertTrue(bfpp.wasCalled);
  }
View Full Code Here


    ac.refresh();
    assertTrue(bfpp.wasCalled);
  }
 
  public void testDefinedBeanFactoryPostProcessor() {
    StaticApplicationContext ac = new StaticApplicationContext();
    ac.registerSingleton("tb1", TestBean.class);
    ac.registerSingleton("tb2", TestBean.class);
    ac.registerSingleton("bfpp", TestBeanFactoryPostProcessor.class);
    ac.refresh();
    TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp");
    assertTrue(bfpp.wasCalled);
  }
View Full Code Here

    TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp");
    assertTrue(bfpp.wasCalled);
  }

  public void testMultipleDefinedBeanFactoryPostProcessors() {
    StaticApplicationContext ac = new StaticApplicationContext();
    ac.registerSingleton("tb1", TestBean.class);
    ac.registerSingleton("tb2", TestBean.class);
    MutablePropertyValues pvs1 = new MutablePropertyValues();
    pvs1.addPropertyValue("initValue", "${key}");
    ac.registerSingleton("bfpp1", TestBeanFactoryPostProcessor.class, pvs1);
    MutablePropertyValues pvs2 = new MutablePropertyValues();
    pvs2.addPropertyValue("properties", "key=value");
    ac.registerSingleton("bfpp2", PropertyPlaceholderConfigurer.class, pvs2);
    ac.refresh();
    TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp1");
    assertEquals("value", bfpp.initValue);
    assertTrue(bfpp.wasCalled);
  }
View Full Code Here

* @author Rick Evans
*/
public class ServiceLocatorFactoryBeanTests extends TestCase {

  public void testNoArgGetter() {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
    TestService testService = factory.getTestService();
    assertNotNull(testService);
  }
View Full Code Here

    TestService testService = factory.getTestService();
    assertNotNull(testService);
  }

  public void testErrorOnTooManyOrTooFew() throws Exception {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
    ctx.registerSingleton("testServiceInstance2", TestService.class, new MutablePropertyValues());
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
    ctx.registerSingleton("factory2", ServiceLocatorFactoryBean.class, mpv);
    mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestService2Locator.class));
    ctx.registerSingleton("factory3", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    final TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
    new AssertThrows(NoSuchBeanDefinitionException.class, "Must fail on more than one matching type") {
      public void test() throws Exception {
        factory.getTestService();
      }
    }.runTest();

    final TestServiceLocator2 factory2 = (TestServiceLocator2) ctx.getBean("factory2");
    new AssertThrows(NoSuchBeanDefinitionException.class, "Must fail on more than one matching type") {
      public void test() throws Exception {
        factory2.getTestService(null);
      }
    }.runTest();
    final TestService2Locator factory3 = (TestService2Locator) ctx.getBean("factory3");
    new AssertThrows(NoSuchBeanDefinitionException.class, "Must fail on no matching types") {
      public void test() throws Exception {
        factory3.getTestService();
      }
    }.runTest();
View Full Code Here

      }
    }.runTest();
  }

  public void testErrorOnTooManyOrTooFewWithCustomServiceLocatorException() {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
    ctx.registerSingleton("testServiceInstance2", TestService.class, new MutablePropertyValues());
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator.class));
    mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException1.class));
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
    mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException2.class));
    ctx.registerSingleton("factory2", ServiceLocatorFactoryBean.class, mpv);
    mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestService2Locator.class));
    mpv.addPropertyValue(new PropertyValue("serviceLocatorExceptionClass", CustomServiceLocatorException3.class));
    ctx.registerSingleton("factory3", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    TestServiceLocator factory = (TestServiceLocator) ctx.getBean("factory");
    try {
      factory.getTestService();
      fail("Must fail on more than one matching type");
    }
    catch (CustomServiceLocatorException1 expected) {
      assertTrue(expected.getCause() instanceof NoSuchBeanDefinitionException);
    }
    TestServiceLocator2 factory2 = (TestServiceLocator2) ctx.getBean("factory2");
    try {
      factory2.getTestService(null);
      fail("Must fail on more than one matching type");
    }
    catch (CustomServiceLocatorException2 expected) {
      assertTrue(expected.getCause() instanceof NoSuchBeanDefinitionException);
    }
    final TestService2Locator factory3 = (TestService2Locator) ctx.getBean("factory3");
    new AssertThrows(CustomServiceLocatorException3.class, "Must fail on no matching types") {
      public void test() throws Exception {
        factory3.getTestService();
      }
    }.runTest();
View Full Code Here

      }
    }.runTest();
  }

  public void testStringArgGetter() throws Exception {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerSingleton("testService", TestService.class, new MutablePropertyValues());
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue(new PropertyValue("serviceLocatorInterface", TestServiceLocator2.class));
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    // test string-arg getter with null id
    final TestServiceLocator2 factory = (TestServiceLocator2) ctx.getBean("factory");
    TestService testBean = factory.getTestService(null);
    // now test with explicit id
    testBean = factory.getTestService("testService");
    // now verify failure on bad id
    new AssertThrows(NoSuchBeanDefinitionException.class, "Illegal operation allowed") {
View Full Code Here

      }
    }.runTest();
  }

  public void testCombinedLocatorInterface() {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerPrototype("testService", TestService.class, new MutablePropertyValues());
    ctx.registerAlias("testService", "1");
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory");
    TestService testBean1 = factory.getTestService();
    TestService testBean2 = factory.getTestService("testService");
    TestService testBean3 = factory.getTestService(1);
    TestService testBean4 = factory.someFactoryMethod();
    assertNotSame(testBean1, testBean2);
View Full Code Here

    assertTrue(factory.toString().indexOf("TestServiceLocator3") != -1);
  }

  public void testServiceMappings() {
    StaticApplicationContext ctx = new StaticApplicationContext();
    ctx.registerPrototype("testService1", TestService.class, new MutablePropertyValues());
    ctx.registerPrototype("testService2", ExtendedTestService.class, new MutablePropertyValues());
    MutablePropertyValues mpv = new MutablePropertyValues();
    mpv.addPropertyValue("serviceLocatorInterface", TestServiceLocator3.class);
    mpv.addPropertyValue("serviceMappings", "=testService1\n1=testService1\n2=testService2");
    ctx.registerSingleton("factory", ServiceLocatorFactoryBean.class, mpv);
    ctx.refresh();

    TestServiceLocator3 factory = (TestServiceLocator3) ctx.getBean("factory");
    TestService testBean1 = factory.getTestService();
    TestService testBean2 = factory.getTestService("testService1");
    TestService testBean3 = factory.getTestService(1);
    TestService testBean4 = factory.getTestService(2);
    assertNotSame(testBean1, testBean2);
View Full Code Here

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        this.beanFactory = new DefaultListableBeanFactory();
        this.applicationContext = new StaticApplicationContext();
        this.finder = new SpringBeanFinder();
        this.finder.setBeanName(BEAN_NAME);
    }
View Full Code Here

TOP

Related Classes of org.springframework.context.support.StaticApplicationContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.