1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import com.mchange.v2.c3p0.PooledDataSource; |
10 |
| import junit.framework.TestCase; |
11 |
| |
12 |
| import java.sql.Connection; |
13 |
| import java.util.Properties; |
14 |
| |
15 |
| import org.apache.commons.logging.Log; |
16 |
| import org.apache.commons.logging.LogFactory; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class C3p0ConnectionFactoryTest extends TestCase |
24 |
| { |
25 |
| private static final Log log = LogFactory.getLog(C3p0ConnectionFactoryTest.class); |
26 |
| |
27 |
| private C3p0ConnectionFactory cf; |
28 |
| private JDBCCacheLoaderConfig config; |
29 |
| |
30 |
2
| public C3p0ConnectionFactoryTest(String string)
|
31 |
| { |
32 |
2
| super(string);
|
33 |
| } |
34 |
| |
35 |
2
| protected void setUp() throws Exception
|
36 |
| { |
37 |
2
| Properties prop = load("cache-jdbc.properties");
|
38 |
| |
39 |
2
| config = new JDBCCacheLoaderConfig();
|
40 |
2
| config.setProperties(prop);
|
41 |
| } |
42 |
| |
43 |
2
| protected void tearDown() throws Exception
|
44 |
| { |
45 |
2
| cf.stop();
|
46 |
| } |
47 |
| |
48 |
1
| public void testSetConfig() throws Exception
|
49 |
| { |
50 |
1
| config.getProperties().setProperty("c3p0.checkoutTimeout", "10000");
|
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
1
| System.setProperty("c3p0.maxPoolSize", "5");
|
57 |
1
| config.getProperties().setProperty("c3p0.maxPoolSize", "3");
|
58 |
| |
59 |
1
| cf = new C3p0ConnectionFactory();
|
60 |
1
| cf.setConfig(config);
|
61 |
1
| cf.start();
|
62 |
| |
63 |
1
| Connection c1 = cf.getConnection();
|
64 |
1
| Connection c2 = cf.getConnection();
|
65 |
1
| Connection c3 = cf.getConnection();
|
66 |
1
| Connection c4 = cf.getConnection();
|
67 |
1
| Connection c5 = cf.getConnection();
|
68 |
1
| Connection c6 = null;
|
69 |
| |
70 |
1
| try
|
71 |
| { |
72 |
1
| c6 = cf.getConnection();
|
73 |
0
| fail("Should have produced an SQLException indicating that it timed out checking out a Connection");
|
74 |
| } |
75 |
| catch (IllegalStateException ise) |
76 |
| { |
77 |
| } |
78 |
| finally |
79 |
| { |
80 |
1
| cf.close(c1);
|
81 |
1
| cf.close(c2);
|
82 |
1
| cf.close(c3);
|
83 |
1
| cf.close(c4);
|
84 |
1
| cf.close(c5);
|
85 |
1
| cf.close(c6);
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
1
| public void testGetConnection() throws Exception
|
90 |
| { |
91 |
1
| cf = new C3p0ConnectionFactory();
|
92 |
1
| cf.setConfig(config);
|
93 |
1
| cf.start();
|
94 |
1
| PooledDataSource internalDs = (PooledDataSource) cf.getDataSource();
|
95 |
| |
96 |
1
| Connection c1 = cf.getConnection();
|
97 |
1
| Connection c2 = cf.getConnection();
|
98 |
1
| assertEquals("There should be two connections checked out", 2, internalDs.getNumBusyConnectionsDefaultUser());
|
99 |
| |
100 |
1
| cf.close(c1);
|
101 |
1
| Thread.sleep(100);
|
102 |
1
| assertEquals("There should be one connection checked out", 1, internalDs.getNumBusyConnectionsDefaultUser());
|
103 |
| |
104 |
1
| cf.close(c2);
|
105 |
1
| Thread.sleep(100);
|
106 |
1
| assertEquals("There should be no connections checked out", 0, internalDs.getNumBusyConnectionsDefaultUser());
|
107 |
| } |
108 |
| |
109 |
2
| private Properties load(String resource) throws Exception
|
110 |
| { |
111 |
2
| Properties prop = new Properties();
|
112 |
2
| try
|
113 |
| { |
114 |
2
| prop.load(this.getClass().getClassLoader().getResourceAsStream(resource));
|
115 |
2
| return prop;
|
116 |
| } catch (Exception e) |
117 |
| { |
118 |
0
| log("Error loading jdbc properties ");
|
119 |
0
| throw (e);
|
120 |
| } |
121 |
| } |
122 |
| |
123 |
0
| private void log(Object o)
|
124 |
| { |
125 |
0
| System.out.println(o);
|
126 |
| } |
127 |
| } |