1 | package gov.va.med.edp.vistalink.locator;
|
---|
2 |
|
---|
3 | import junit.framework.TestCase;
|
---|
4 | import org.easymock.MockControl;
|
---|
5 | import org.springframework.beans.factory.BeanFactory;
|
---|
6 | import org.springframework.context.support.StaticApplicationContext;
|
---|
7 | import org.springframework.dao.DataAccessResourceFailureException;
|
---|
8 |
|
---|
9 | import javax.resource.cci.ConnectionFactory;
|
---|
10 |
|
---|
11 | public 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 | }
|
---|