Java Code Examples for org.springframework.tests.mock.jndi.ExpectedLookupTemplate#addObject()

The following examples show how to use org.springframework.tests.mock.jndi.ExpectedLookupTemplate#addObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 2
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSinglePersistenceUnitFromJndi() {
	Map<String, String> persistenceUnits = new HashMap<String, String>();
	persistenceUnits.put("Person", "pu1");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf, bean2.emf);
}
 
Example 3
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSinglePersistenceUnitFromJndi() {
	Map<String, String> persistenceUnits = new HashMap<>();
	persistenceUnits.put("Person", "pu1");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf, bean2.emf);
}
 
Example 4
Source File: CommonAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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");
	assertTrue(bean.initCalled);
	assertTrue(bean.init2Called);
	assertSame(tb, bean.getTestBean());
	assertSame(tb2, bean.getTestBean2());
	bf.destroySingletons();
	assertTrue(bean.destroyCalled);
	assertTrue(bean.destroy2Called);
}
 
Example 5
Source File: JndiJtaTransactionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void jtaTransactionManagerWithCustomJndiLookups() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

	TransactionManager tm = mock(TransactionManager.class);

	JtaTransactionManager ptm = new JtaTransactionManager();
	ptm.setUserTransactionName("jndi-ut");
	ptm.setTransactionManagerName("jndi-tm");
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	jndiTemplate.addObject("jndi-ut", ut);
	jndiTemplate.addObject("jndi-tm", tm);
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	assertEquals(ut, ptm.getUserTransaction());
	assertEquals(tm, ptm.getTransactionManager());

	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		}
	});
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	verify(ut).begin();
	verify(ut).commit();
}
 
Example 6
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void uowManagerAndUserTransactionFoundInJndi() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn( Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut);
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	TransactionStatus ts = ptm.getTransaction(definition);
	ptm.commit(ts);
	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			return "result";
		}
	}));

	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
	verify(ut).begin();
	verify(ut).commit();
}
 
Example 7
Source File: JndiJtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void jtaTransactionManagerWithCustomJndiLookups() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

	TransactionManager tm = mock(TransactionManager.class);

	JtaTransactionManager ptm = new JtaTransactionManager();
	ptm.setUserTransactionName("jndi-ut");
	ptm.setTransactionManagerName("jndi-tm");
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	jndiTemplate.addObject("jndi-ut", ut);
	jndiTemplate.addObject("jndi-tm", tm);
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	assertEquals(ut, ptm.getUserTransaction());
	assertEquals(tm, ptm.getTransactionManager());

	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		}
	});
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

	verify(ut).begin();
	verify(ut).commit();
}
 
Example 8
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPersistenceContextsFromJndiWithDefaultUnit() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);
	EntityManager mockEm3 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<>();
	persistenceContexts.put("System", "pc1");
	persistenceContexts.put("Person", "pc2");
	Map<String, String> extendedPersistenceContexts = new HashMap<>();
	extendedPersistenceContexts .put("System", "pc3");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);
	jt.addObject("java:comp/env/pc3", mockEm3);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setDefaultPersistenceUnitName("System");
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson)
			bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
	DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
	assertSame(mockEm3, bean3.em);
}
 
Example 9
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPersistenceContextsFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);
	EntityManager mockEm3 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<String, String>();
	persistenceContexts.put("", "pc1");
	persistenceContexts.put("Person", "pc2");
	Map<String, String> extendedPersistenceContexts = new HashMap<String, String>();
	extendedPersistenceContexts .put("", "pc3");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);
	jt.addObject("java:comp/env/pc3", mockEm3);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson)
			bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
	DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
	assertSame(mockEm3, bean3.em);
}
 
Example 10
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPersistenceUnitsFromJndiWithDefaultUnit() {
	EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);

	Map<String, String> persistenceUnits = new HashMap<>();
	persistenceUnits.put("System", "pu1");
	persistenceUnits.put("Person", "pu2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);
	jt.addObject("java:comp/env/pu2", mockEmf2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setDefaultPersistenceUnitName("System");
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf2, bean2.emf);
}
 
Example 11
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPersistenceContextsFromJndiWithDefaultUnit() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);
	EntityManager mockEm3 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<String, String>();
	persistenceContexts.put("System", "pc1");
	persistenceContexts.put("Person", "pc2");
	Map<String, String> extendedPersistenceContexts = new HashMap<String, String>();
	extendedPersistenceContexts .put("System", "pc3");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);
	jt.addObject("java:comp/env/pc3", mockEm3);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setDefaultPersistenceUnitName("System");
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextFieldNamedPerson.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPrivatePersistenceContextFieldNamedPerson bean2 = (DefaultPrivatePersistenceContextFieldNamedPerson)
			bf.getBean(DefaultPrivatePersistenceContextFieldNamedPerson.class.getName());
	DefaultPublicPersistenceContextSetter bean3 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
	assertSame(mockEm3, bean3.em);
}
 
Example 12
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testSinglePersistenceContextFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<String, String>();
	persistenceContexts.put("System", "pc1");
	Map<String, String> extendedPersistenceContexts = new HashMap<String, String>();
	extendedPersistenceContexts .put("System", "pc2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean2 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
}
 
Example 13
Source File: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void uowManagerAndUserTransactionFoundInJndi() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn( Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut);
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	TransactionStatus ts = ptm.getTransaction(definition);
	ptm.commit(ts);
	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			return "result";
		}
	}));

	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
	verify(ut).begin();
	verify(ut).commit();
}
 
Example 14
Source File: PersistenceInjectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSinglePersistenceContextFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	EntityManager mockEm2 = mock(EntityManager.class);

	Map<String, String> persistenceContexts = new HashMap<>();
	persistenceContexts.put("System", "pc1");
	Map<String, String> extendedPersistenceContexts = new HashMap<>();
	extendedPersistenceContexts .put("System", "pc2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pc1", mockEm);
	jt.addObject("java:comp/env/pc2", mockEm2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceContexts(persistenceContexts);
	bpp.setExtendedPersistenceContexts(extendedPersistenceContexts);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPrivatePersistenceContextField bean1 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean2 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEm, bean1.em);
	assertSame(mockEm2, bean2.em);
}
 
Example 15
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void uowManagerAndUserTransactionFoundInJndi() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn( Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut);
	jndiTemplate.addObject(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	TransactionStatus ts = ptm.getTransaction(definition);
	ptm.commit(ts);
	assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {
		@Override
		public String doInTransaction(TransactionStatus status) {
			return "result";
		}
	}));

	assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
	assertFalse(manager.getJoined());
	assertFalse(manager.getRollbackOnly());
	verify(ut).begin();
	verify(ut).commit();
}
 
Example 16
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
@Ignore
public void testPersistenceUnitsFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	given(mockEmf.createEntityManager()).willReturn(mockEm);

	EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);

	Map<String, String> persistenceUnits = new HashMap<>();
	persistenceUnits.put("", "pu1");
	persistenceUnits.put("Person", "pu2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);
	jt.addObject("java:comp/env/pu2", mockEmf2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	DefaultPrivatePersistenceContextField bean3 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean4 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf2, bean2.emf);
	assertNotNull(bean3.em);
	assertNotNull(bean4.em);
}
 
Example 17
Source File: JndiJtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt)
		throws Exception {

	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	if (defaultUt) {
		given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
	}
	else {
		given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
	}

	JtaTransactionManager ptm = new JtaTransactionManager();
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	if (defaultUt) {
		jndiTemplate.addObject("java:comp/UserTransaction", ut);
	}
	jndiTemplate.addObject(tmName, tm);
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	if (tmFound) {
		assertEquals(tm, ptm.getTransactionManager());
	}
	else {
		assertNull(ptm.getTransactionManager());
	}

	if (defaultUt) {
		assertEquals(ut, ptm.getUserTransaction());
	}
	else {
		assertTrue(ptm.getUserTransaction() instanceof UserTransactionAdapter);
		UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
		assertEquals(tm, uta.getTransactionManager());
	}

	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		}
	});
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());


	if (defaultUt) {
		verify(ut).begin();
		verify(ut).commit();
	}
	else {
		verify(tm).begin();
		verify(tm).commit();
	}

}
 
Example 18
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
@Ignore
public void testPersistenceUnitsFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	given(mockEmf.createEntityManager()).willReturn(mockEm);

	EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);

	Map<String, String> persistenceUnits = new HashMap<String, String>();
	persistenceUnits.put("", "pu1");
	persistenceUnits.put("Person", "pu2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);
	jt.addObject("java:comp/env/pu2", mockEmf2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	DefaultPrivatePersistenceContextField bean3 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean4 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf2, bean2.emf);
	assertNotNull(bean3.em);
	assertNotNull(bean4.em);
}
 
Example 19
Source File: JndiJtaTransactionManagerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void doTestJtaTransactionManagerWithDefaultJndiLookups(String tmName, boolean tmFound, boolean defaultUt)
		throws Exception {

	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	if (defaultUt) {
		given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
	}
	else {
		given(tm.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
	}

	JtaTransactionManager ptm = new JtaTransactionManager();
	ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate();
	if (defaultUt) {
		jndiTemplate.addObject("java:comp/UserTransaction", ut);
	}
	jndiTemplate.addObject(tmName, tm);
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	if (tmFound) {
		assertEquals(tm, ptm.getTransactionManager());
	}
	else {
		assertNull(ptm.getTransactionManager());
	}

	if (defaultUt) {
		assertEquals(ut, ptm.getUserTransaction());
	}
	else {
		assertTrue(ptm.getUserTransaction() instanceof UserTransactionAdapter);
		UserTransactionAdapter uta = (UserTransactionAdapter) ptm.getUserTransaction();
		assertEquals(tm, uta.getTransactionManager());
	}

	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
		}
	});
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
	assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());


	if (defaultUt) {
		verify(ut).begin();
		verify(ut).commit();
	}
	else {
		verify(tm).begin();
		verify(tm).commit();
	}

}
 
Example 20
Source File: PersistenceInjectionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
@Ignore
public void testPersistenceUnitsFromJndi() {
	EntityManager mockEm = mock(EntityManager.class);
	given(mockEmf.createEntityManager()).willReturn(mockEm);

	EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);

	Map<String, String> persistenceUnits = new HashMap<>();
	persistenceUnits.put("", "pu1");
	persistenceUnits.put("Person", "pu2");
	ExpectedLookupTemplate jt = new ExpectedLookupTemplate();
	jt.addObject("java:comp/env/pu1", mockEmf);
	jt.addObject("java:comp/env/pu2", mockEmf2);

	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	PersistenceAnnotationBeanPostProcessor bpp = new PersistenceAnnotationBeanPostProcessor();
	bpp.setPersistenceUnits(persistenceUnits);
	bpp.setJndiTemplate(jt);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetter.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceUnitSetterNamedPerson.class));
	bf.registerBeanDefinition(DefaultPrivatePersistenceContextField.class.getName(),
			new RootBeanDefinition(DefaultPrivatePersistenceContextField.class));
	bf.registerBeanDefinition(DefaultPublicPersistenceContextSetter.class.getName(),
			new RootBeanDefinition(DefaultPublicPersistenceContextSetter.class));

	DefaultPublicPersistenceUnitSetter bean = (DefaultPublicPersistenceUnitSetter)
			bf.getBean(DefaultPublicPersistenceUnitSetter.class.getName());
	DefaultPublicPersistenceUnitSetterNamedPerson bean2 = (DefaultPublicPersistenceUnitSetterNamedPerson)
			bf.getBean(DefaultPublicPersistenceUnitSetterNamedPerson.class.getName());
	DefaultPrivatePersistenceContextField bean3 = (DefaultPrivatePersistenceContextField)
			bf.getBean(DefaultPrivatePersistenceContextField.class.getName());
	DefaultPublicPersistenceContextSetter bean4 = (DefaultPublicPersistenceContextSetter)
			bf.getBean(DefaultPublicPersistenceContextSetter.class.getName());
	assertSame(mockEmf, bean.emf);
	assertSame(mockEmf2, bean2.emf);
	assertNotNull(bean3.em);
	assertNotNull(bean4.em);
}