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);
}
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);
}
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();
}
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);
}
}
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());
}
}
Aggregations