source: EDIS/tags/ed/tracking-server-vista/src/test/java/gov/va/med/edp/vistalink/locator/BeanFactoryConnectionFactoryLocatorTest.java@ 1240

Last change on this file since 1240 was 1240, checked in by George Lilly, 13 years ago

new version from the VA

File size: 3.7 KB
Line 
1package gov.va.med.edp.vistalink.locator;
2
3import junit.framework.TestCase;
4import org.easymock.MockControl;
5import org.springframework.beans.factory.BeanFactory;
6import org.springframework.context.support.StaticApplicationContext;
7import org.springframework.dao.DataAccessResourceFailureException;
8
9import javax.resource.cci.ConnectionFactory;
10
11public class BeanFactoryConnectionFactoryLocatorTest extends TestCase {
12
13 public void testConnectionFactoryNotFound() {
14 StaticApplicationContext beanFactory = new StaticApplicationContext();
15
16 BeanFactoryConnectionFactoryLocator locator = new BeanFactoryConnectionFactoryLocator();
17 locator.setBeanFactory(beanFactory);
18
19 try {
20 locator.getConnectionFactory("982");
21 fail("expected exception");
22 } catch (DataAccessResourceFailureException e) {
23 assertTrue(true);
24 }
25 }
26
27 public void testGetConnectionFactoryForStationNumber() {
28 testGetConnectionFactory("982", null, null);
29 }
30
31 public void testGetConnectionFactoryWithPrefix() {
32 testGetConnectionFactory("982", "pre", null);
33 }
34
35 public void testGetConnectionFactoryWithSuffix() {
36 testGetConnectionFactory("982", null, "suf");
37 }
38
39 public void testGetConnectionFactoryWithPrefixAndSuffix() {
40 testGetConnectionFactory("982", "pre", "suf");
41 }
42
43 private void testGetConnectionFactory(String stationNumber, String prefix, String suffix) {
44 MockControl beanFactoryControl = MockControl.createControl(BeanFactory.class);
45 BeanFactory beanFactory = (BeanFactory) beanFactoryControl.getMock();
46
47 MockControl mockConnectionFactoryControl = MockControl.createControl(ConnectionFactory.class);
48 ConnectionFactory mockConnectionFactory = (ConnectionFactory) mockConnectionFactoryControl.getMock();
49
50 beanFactoryControl.expectAndReturn(beanFactory.getBean((prefix != null ? prefix : "")+ stationNumber + (suffix != null ? suffix : ""), ConnectionFactory.class), mockConnectionFactory);
51
52 beanFactoryControl.replay();
53 mockConnectionFactoryControl.replay();
54
55 BeanFactoryConnectionFactoryLocator locator = new BeanFactoryConnectionFactoryLocator();
56 locator.setBeanFactory(beanFactory);
57 if (prefix != null) locator.setPrefix(prefix);
58 if (suffix != null) locator.setSuffix(suffix);
59
60 ConnectionFactory cf = locator.getConnectionFactory(stationNumber);
61 assertSame(mockConnectionFactory, cf);
62
63 beanFactoryControl.verify();
64 mockConnectionFactoryControl.verify();
65 }
66
67 // this is for java 5 and newer version of easymock
68// private void testGetConnectionFactory(String stationNumber, String prefix, String suffix) {
69// BeanFactory beanFactory = EasyMock.createMock(BeanFactory.class);
70// ConnectionFactory mockConnectionFactory = EasyMock.createMock(ConnectionFactory.class);
71// EasyMock.expect(beanFactory.getBean((prefix != null? prefix : "") + stationNumber + (suffix != null? suffix : ""), ConnectionFactory.class)).andReturn(mockConnectionFactory);
72// EasyMock.replay(beanFactory, mockConnectionFactory);
73//
74// BeanFactoryConnectionFactoryLocator locator = new BeanFactoryConnectionFactoryLocator();
75// locator.setBeanFactory(beanFactory);
76// if (prefix != null) locator.setPrefix(prefix);
77// if (suffix != null) locator.setSuffix(suffix);
78//
79// ConnectionFactory cf = locator.getConnectionFactory(stationNumber);
80// assertSame(mockConnectionFactory, cf);
81//
82// EasyMock.verify(beanFactory, mockConnectionFactory);
83// }
84}
Note: See TracBrowser for help on using the repository browser.