use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.
the class CommonAnnotationBeanPostProcessorTests method testResourceInjectionFromJndi.
@Test
public void testResourceInjectionFromJndi() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CommonAnnotationBeanPostProcessor bpp = new CommonAnnotationBeanPostProcessor();
SimpleJndiBeanFactory resourceFactory = new SimpleJndiBeanFactory();
ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
TestBean tb = new TestBean();
jndiTemplate.addObject("java:comp/env/testBean", tb);
TestBean tb2 = new TestBean();
jndiTemplate.addObject("java:comp/env/testBean2", tb2);
resourceFactory.setJndiTemplate(jndiTemplate);
bpp.setResourceFactory(resourceFactory);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ResourceInjectionBean.class));
ResourceInjectionBean bean = (ResourceInjectionBean) bf.getBean("annotatedBean");
assertThat(bean.initCalled).isTrue();
assertThat(bean.init2Called).isTrue();
assertThat(bean.getTestBean()).isSameAs(tb);
assertThat(bean.getTestBean2()).isSameAs(tb2);
bf.destroySingletons();
assertThat(bean.destroyCalled).isTrue();
assertThat(bean.destroy2Called).isTrue();
}
use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.
the class JndiJtaTransactionManagerTests method jtaTransactionManagerWithNotCacheUserTransaction.
@Test
public void jtaTransactionManagerWithNotCacheUserTransaction() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
UserTransaction ut2 = mock(UserTransaction.class);
given(ut2.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
JtaTransactionManager ptm = new JtaTransactionManager();
ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut));
ptm.setCacheUserTransaction(false);
ptm.afterPropertiesSet();
assertThat(ptm.getUserTransaction()).isEqualTo(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertThat(ptm.getTransactionSynchronization()).isEqualTo(JtaTransactionManager.SYNCHRONIZATION_ALWAYS);
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
}
});
ptm.setJndiTemplate(new ExpectedLookupTemplate("java:comp/UserTransaction", ut2));
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
}
});
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
verify(ut).begin();
verify(ut).commit();
verify(ut2).begin();
verify(ut2).commit();
}
use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.
the class JndiObjectFactoryBeanTests method testLookupWithSchemeNameAndResourceRefFalse.
@Test
public void testLookupWithSchemeNameAndResourceRefFalse() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
Object o = new Object();
jof.setJndiTemplate(new ExpectedLookupTemplate("java:foo", o));
jof.setJndiName("java:foo");
jof.setResourceRef(false);
jof.afterPropertiesSet();
assertThat(jof.getObject() == o).isTrue();
}
use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.
the class JndiObjectFactoryBeanTests method testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch.
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
TestBean tb = new TestBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
jof.setJndiName("foo");
jof.setExpectedType(DerivedTestBean.class);
jof.setProxyInterface(ITestBean.class);
assertThatExceptionOfType(NamingException.class).isThrownBy(jof::afterPropertiesSet).withMessageContaining("org.springframework.beans.testfixture.beans.DerivedTestBean");
}
use of org.springframework.context.testfixture.jndi.ExpectedLookupTemplate in project spring-framework by spring-projects.
the class JndiObjectFactoryBeanTests method testLookupWithDefaultObjectAndExpectedType.
@Test
public void testLookupWithDefaultObjectAndExpectedType() throws Exception {
JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
jof.setJndiName("myFoo");
jof.setExpectedType(String.class);
jof.setDefaultObject("myString");
jof.afterPropertiesSet();
assertThat(jof.getObject()).isEqualTo("myString");
}
Aggregations