org.springframework.tests.mock.jndi.ExpectedLookupTemplate Java Examples

The following examples show how to use org.springframework.tests.mock.jndi.ExpectedLookupTemplate. 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: PersistenceInjectionTests.java    From spring-analysis-note 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 #2
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 #3
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 #4
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown NamingException");
	}
	catch (NamingException ex) {
		assertTrue(ex.getMessage().indexOf("org.springframework.tests.sample.beans.DerivedTestBean") != -1);
	}
}
 
Example #5
Source File: JndiObjectFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown NamingException");
	}
	catch (NamingException ex) {
		assertTrue(ex.getMessage().contains("org.springframework.tests.sample.beans.DerivedTestBean"));
	}
}
 
Example #6
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("myFoo");
	jof.setProxyInterface(ITestBean.class);
	jof.setDefaultObject(Boolean.TRUE);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}
 
Example #7
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void uowManagerFoundInJndi() {
	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate =
			new ExpectedLookupTemplate(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	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());
}
 
Example #8
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void uowManagerFoundInJndi() {
	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate =
			new ExpectedLookupTemplate(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	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());
}
 
Example #9
Source File: CommonAnnotationBeanPostProcessorTests.java    From java-technology-stack 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 #10
Source File: JndiObjectFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("myFoo");
	jof.setProxyInterface(ITestBean.class);
	jof.setDefaultObject(Boolean.TRUE);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}
 
Example #11
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 #12
Source File: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void uowManagerFoundInJndi() {
	MockUOWManager manager = new MockUOWManager();
	ExpectedLookupTemplate jndiTemplate =
			new ExpectedLookupTemplate(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
	ptm.setJndiTemplate(jndiTemplate);
	ptm.afterPropertiesSet();

	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	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());
}
 
Example #13
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
	assertEquals("myString", jof.getObject());
}
 
Example #14
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithFullNameAndResourceRefTrue() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	Object o = new Object();
	jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
	jof.setJndiName("java:comp/env/foo");
	jof.setResourceRef(true);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() == o);
}
 
Example #15
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithProxyInterface() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setProxyInterface(ITestBean.class);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() instanceof ITestBean);
	ITestBean proxy = (ITestBean) jof.getObject();
	assertEquals(0, tb.getAge());
	proxy.setAge(99);
	assertEquals(99, tb.getAge());
}
 
Example #16
Source File: PersistenceInjectionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPersistenceUnitsFromJndiWithDefaultUnit() {
	EntityManagerFactoryWithInfo mockEmf2 = mock(EntityManagerFactoryWithInfo.class);

	Map<String, String> persistenceUnits = new HashMap<String, String>();
	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 #17
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithDefaultObjectAndExpectedTypeConversion() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
	jof.setJndiName("myFoo");
	jof.setExpectedType(Integer.class);
	jof.setDefaultObject("5");
	jof.afterPropertiesSet();
	assertEquals(new Integer(5), jof.getObject());
}
 
Example #18
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithDefaultObjectAndExpectedTypeConversionViaBeanFactory() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
	jof.setJndiName("myFoo");
	jof.setExpectedType(Integer.class);
	jof.setDefaultObject("5");
	jof.setBeanFactory(new DefaultListableBeanFactory());
	jof.afterPropertiesSet();
	assertEquals(new Integer(5), jof.getObject());
}
 
Example #19
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithDefaultObjectAndExpectedTypeNoMatch() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
	jof.setJndiName("myFoo");
	jof.setExpectedType(Boolean.class);
	jof.setDefaultObject("5");
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}
 
Example #20
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookupWithShortNameAndResourceRefTrue() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	Object o = new Object();
	jof.setJndiTemplate(new ExpectedLookupTemplate("java:comp/env/foo", o));
	jof.setJndiName("foo");
	jof.setResourceRef(true);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() == o);
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
Source File: PersistenceInjectionTests.java    From java-technology-stack with MIT License 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<>();
	persistenceContexts.put("", "pc1");
	persistenceContexts.put("Person", "pc2");
	Map<String, String> extendedPersistenceContexts = new HashMap<>();
	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 #26
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 #27
Source File: JndiObjectFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndMatch() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setExpectedType(TestBean.class);
	jof.setProxyInterface(ITestBean.class);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() instanceof ITestBean);
	ITestBean proxy = (ITestBean) jof.getObject();
	assertEquals(0, tb.getAge());
	proxy.setAge(99);
	assertEquals(99, tb.getAge());
}
 
Example #28
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 #29
Source File: JndiObjectFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testLookupWithProxyInterface() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setProxyInterface(ITestBean.class);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() instanceof ITestBean);
	ITestBean proxy = (ITestBean) jof.getObject();
	assertEquals(0, tb.getAge());
	proxy.setAge(99);
	assertEquals(99, tb.getAge());
}
 
Example #30
Source File: JndiObjectFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testLookupWithDefaultObjectAndExpectedTypeNoMatch() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", ""));
	jof.setJndiName("myFoo");
	jof.setExpectedType(Boolean.class);
	jof.setDefaultObject("5");
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}