Search in sources :

Example 56 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPublicExtendedPersistenceContextSetterWithOverriding.

@Test
public void testPublicExtendedPersistenceContextSetterWithOverriding() {
    EntityManager mockEm2 = mock(EntityManager.class);
    GenericApplicationContext gac = new GenericApplicationContext();
    gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
    gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
    RootBeanDefinition bd = new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class);
    bd.getPropertyValues().add("entityManager", mockEm2);
    gac.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(), bd);
    gac.refresh();
    DefaultPublicPersistenceContextSetter bean = (DefaultPublicPersistenceContextSetter) gac.getBean(DefaultPublicPersistenceContextSetter.class.getName());
    assertSame(mockEm2, bean.em);
}
Also used : EntityManager(javax.persistence.EntityManager) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 57 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPublicPersistenceUnitSetter.

@Test
public void testPublicPersistenceUnitSetter() {
    GenericApplicationContext gac = new GenericApplicationContext();
    gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
    gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
    gac.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(), new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
    gac.refresh();
    DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter) gac.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
    assertSame(mockEmf, bean.emf);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 58 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class PersistenceInjectionTests method testPublicSpecificExtendedPersistenceContextSetter.

@Test
public void testPublicSpecificExtendedPersistenceContextSetter() throws Exception {
    EntityManagerFactory mockEmf2 = mock(EntityManagerFactory.class);
    EntityManager mockEm2 = mock(EntityManager.class);
    given(mockEmf2.createEntityManager()).willReturn(mockEm2);
    GenericApplicationContext gac = new GenericApplicationContext();
    gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
    gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2);
    gac.registerBeanDefinition("annotationProcessor", new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
    gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(), new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class));
    gac.refresh();
    SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean(SpecificPublicPersistenceContextSetter.class.getName());
    assertNotNull(bean.getEntityManager());
    bean.getEntityManager().flush();
    verify(mockEm2).getTransaction();
    verify(mockEm2).flush();
}
Also used : EntityManager(javax.persistence.EntityManager) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 59 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class AbstractExpressionEvaluatingCondition method evaluateExpression.

private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext, Class<A> annotationType, ExtensionContext extensionContext) {
    AnnotatedElement element = extensionContext.getElement().get();
    GenericApplicationContext gac = null;
    ApplicationContext applicationContext;
    if (loadContext) {
        applicationContext = SpringExtension.getApplicationContext(extensionContext);
    } else {
        gac = new GenericApplicationContext();
        gac.refresh();
        applicationContext = gac;
    }
    if (!(applicationContext instanceof ConfigurableApplicationContext)) {
        if (logger.isWarnEnabled()) {
            String contextType = (applicationContext != null ? applicationContext.getClass().getName() : "null");
            logger.warn(String.format("@%s(\"%s\") could not be evaluated on [%s] since the test " + "ApplicationContext [%s] is not a ConfigurableApplicationContext", annotationType.getSimpleName(), expression, element, contextType));
        }
        return false;
    }
    ConfigurableBeanFactory configurableBeanFactory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
    BeanExpressionResolver expressionResolver = configurableBeanFactory.getBeanExpressionResolver();
    BeanExpressionContext beanExpressionContext = new BeanExpressionContext(configurableBeanFactory, null);
    Object result = expressionResolver.evaluate(configurableBeanFactory.resolveEmbeddedValue(expression), beanExpressionContext);
    if (gac != null) {
        gac.close();
    }
    if (result instanceof Boolean) {
        return ((Boolean) result).booleanValue();
    } else if (result instanceof String) {
        String str = ((String) result).trim().toLowerCase();
        if ("true".equals(str)) {
            return true;
        }
        Assert.state("false".equals(str), () -> String.format("@%s(\"%s\") on %s must evaluate to \"true\" or \"false\", not \"%s\"", annotationType.getSimpleName(), expression, element, result));
        return false;
    } else {
        String message = String.format("@%s(\"%s\") on %s must evaluate to a String or a Boolean, not %s", annotationType.getSimpleName(), expression, element, (result != null ? result.getClass().getName() : "null"));
        throw new IllegalStateException(message);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) ApplicationContext(org.springframework.context.ApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanExpressionResolver(org.springframework.beans.factory.config.BeanExpressionResolver) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) AnnotatedElement(java.lang.reflect.AnnotatedElement) BeanExpressionContext(org.springframework.beans.factory.config.BeanExpressionContext)

Example 60 with GenericApplicationContext

use of org.springframework.context.support.GenericApplicationContext in project spring-framework by spring-projects.

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        GenericBeanDefinition bd1 = new GenericBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        GenericBeanDefinition bd2 = new GenericBeanDefinition();
        bd2.setBeanClass(DerivedTestBean.class);
        bd2.setScope("tx");
        context.registerBeanDefinition("txScopedObject2", bd2);
        context.refresh();
        CallCountingTransactionManager tm = new CallCountingTransactionManager();
        TransactionTemplate tt = new TransactionTemplate(tm);
        Set<DerivedTestBean> finallyDestroy = new HashSet<>();
        tt.execute(status -> {
            TestBean bean1 = context.getBean(TestBean.class);
            assertSame(bean1, context.getBean(TestBean.class));
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertSame(bean2, context.getBean(DerivedTestBean.class));
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertTrue(bean2.wasDestroyed());
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertSame(bean2a, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2a);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertFalse(bean2a.wasDestroyed());
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertSame(bean2b, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2b);
            assertNotSame(bean2a, bean2b);
            Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
            tt2.execute(status2 -> {
                DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
                immediatelyDestroy.add(bean2c);
                assertSame(bean2c, context.getBean(DerivedTestBean.class));
                assertNotSame(bean2, bean2c);
                assertNotSame(bean2a, bean2c);
                assertNotSame(bean2b, bean2c);
                return null;
            });
            assertTrue(immediatelyDestroy.iterator().next().wasDestroyed());
            assertFalse(bean2b.wasDestroyed());
            return null;
        });
        assertTrue(finallyDestroy.iterator().next().wasDestroyed());
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)245 Test (org.junit.Test)190 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)80 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)50 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)38 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ClassPathResource (org.springframework.core.io.ClassPathResource)19 TestBean (org.springframework.tests.sample.beans.TestBean)17 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)15 DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator)12 MBeanExporter (org.springframework.jmx.export.MBeanExporter)11 BeansException (org.springframework.beans.BeansException)8 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)8 HashMap (java.util.HashMap)7 UnsatisfiedDependencyException (org.springframework.beans.factory.UnsatisfiedDependencyException)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Before (org.junit.Before)6 Properties (java.util.Properties)5 EntityManager (javax.persistence.EntityManager)5 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)5