KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > database > TestConnectionPool


1
2 package org.jahia.services.database;
3
4 import junit.framework.*;
5 import java.sql.*;
6 import org.apache.log4j.*;
7 import java.util.*;
8
9 /**
10  * JUnit test class to stress-test the database connection pool class.
11  * <p>Title: </p>
12  * <p>Description: </p>
13  * <p>Copyright: Copyright (c) 2002</p>
14  * <p>Company: </p>
15  * @author Serge Huber.
16  * @version 1.0
17  */

18 public class TestConnectionPool extends TestCase {
19
20     private static org.apache.log4j.Logger logger =
21             org.apache.log4j.Logger.getLogger(TestConnectionPool.class);
22
23     private ConnectionPool connectionPool;
24     private String JavaDoc dbDriverClass;
25     private String JavaDoc dbURL;
26     private String JavaDoc dbUserName;
27     private String JavaDoc dbPassword;
28     private int initialConnections;
29     private int maxConnections;
30     private boolean waitIfBusy;
31     private boolean verbose;
32     private boolean veryVerbose;
33
34     public TestConnectionPool(String JavaDoc s) {
35         super(s);
36     }
37
38     protected void setUp() {
39         BasicConfigurator.resetConfiguration();
40         BasicConfigurator.configure();
41         dbDriverClass= "com.mysql.jdbc.Driver";
42         dbURL= "jdbc:mysql://localhost/jahia?useUnicode=true&characterEncoding=UTF-8";
43         /*
44         dbDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
45         dbURL="jdbc:odbc:jahia";
46         */

47         dbUserName= "jahia";
48         dbPassword= "jahia";
49
50         initialConnections= 10;
51         maxConnections= 10;
52         waitIfBusy= true;
53         verbose= true;
54         veryVerbose= false;
55         try {
56             connectionPool = new ConnectionPool(dbDriverClass,
57                     dbURL, dbUserName, dbPassword, initialConnections,
58                     maxConnections, waitIfBusy, verbose, veryVerbose);
59         } catch (SQLException sqle) {
60             sqle.printStackTrace();
61             Assert.assertTrue(false);
62         }
63     }
64
65     protected void tearDown() {
66         connectionPool.closeAllConnections();
67     }
68
69     public void testCreatePool() {
70         Assert.assertTrue(!connectionPool.equals(null));
71     }
72
73     public void testGetOneConnection() {
74         try {
75             int debugID1= 0;
76             Connection connectionRet = connectionPool.getConnection(debugID1);
77             Assert.assertTrue(!connectionRet.equals(null));
78         } catch(Exception JavaDoc e) {
79             e.printStackTrace();
80             Assert.assertTrue(false);
81         }
82     }
83
84
85     public void testOneConnection() {
86         try {
87             int debugID1= 0;
88             Connection connectionRet = connectionPool.getConnection(debugID1);
89             if (connectionRet != null) {
90                 connectionPool.free(connectionRet);
91             } else {
92                 Assert.assertTrue(false);
93             }
94         } catch(Exception JavaDoc e) {
95             e.printStackTrace();
96             Assert.assertTrue(false);
97         }
98     }
99
100     public void testMultipleOneConnectionCycles() {
101         try {
102             int debugID1= 0;
103             logger.debug("Trying 1000 cycles of one get/free...");
104             for (int i=0; i < 1000; i++) {
105                 Connection connectionRet = connectionPool.getConnection(debugID1);
106                 if (connectionRet != null) {
107                     connectionPool.free(connectionRet);
108                 } else {
109                     Assert.assertTrue(false);
110                 }
111             }
112         } catch(Exception JavaDoc e) {
113             e.printStackTrace();
114             Assert.assertTrue(false);
115         }
116     }
117
118     public void testMultipleExhaustConnectionCycles() {
119         try {
120             int debugID1= 0;
121             logger.debug("Trying 10 cycles of exhaust get/free...");
122             for (int i=0; i < 10; i++) {
123                 Vector connections = new Vector();
124                 for (int j=0; j < maxConnections; j++) {
125                     Connection connectionRet = connectionPool.getConnection(debugID1);
126                     connections.add(connectionRet);
127                 }
128                 if (connections.size() == maxConnections) {
129                     Enumeration connectionEnum = connections.elements();
130                     while (connectionEnum.hasMoreElements()) {
131                         Connection connectionRet = (Connection) connectionEnum.nextElement();
132                         connectionPool.free(connectionRet);
133                     }
134                 } else {
135                     Assert.assertTrue(false);
136                 }
137             }
138         } catch(Exception JavaDoc e) {
139             e.printStackTrace();
140             Assert.assertTrue(false);
141         }
142     }
143
144     private boolean createTable() {
145         try {
146             int debugID1= 0;
147             Connection connectionRet = connectionPool.getConnection(debugID1);
148             connectionRet.setAutoCommit(false);
149             if (connectionRet != null) {
150                 PreparedStatement stmt = connectionRet.prepareStatement("CREATE TABLE JAHIA_TEST (TEST VARCHAR(255))");
151                 // PreparedStatement stmt = connectionRet.prepareStatement("CREATE TABLE JAHIA_TEST (TEST TEXT(250))");
152
stmt.execute();
153                 stmt.close();
154                 connectionRet.commit();
155                 connectionRet.setAutoCommit(true);
156                 connectionPool.free(connectionRet);
157                 return true;
158             }
159         } catch(Exception JavaDoc e) {
160             e.printStackTrace();
161             Assert.assertTrue(false);
162         }
163         return false;
164     }
165
166     private boolean dropTableNoError() {
167         try {
168             int debugID1= 0;
169             Connection connectionRet = connectionPool.getConnection(debugID1);
170             connectionRet.setAutoCommit(false);
171             if (connectionRet != null) {
172                 PreparedStatement stmt = connectionRet.prepareStatement("DROP TABLE JAHIA_TEST");
173                 try {
174                     stmt.execute();
175                 } catch (SQLException sqle) {
176                     logger.debug("Expected exception because the table jahia_test never existed.");
177                 }
178                 stmt.close();
179                 connectionRet.commit();
180                 connectionRet.setAutoCommit(true);
181                 connectionPool.free(connectionRet);
182                 return true;
183             }
184         } catch(Exception JavaDoc e) {
185             e.printStackTrace();
186             Assert.assertTrue(false);
187         }
188         return false;
189     }
190
191     private boolean dropTable() {
192         try {
193             int debugID1= 0;
194             Connection connectionRet = connectionPool.getConnection(debugID1);
195             connectionRet.setAutoCommit(false);
196             if (connectionRet != null) {
197                 PreparedStatement stmt = connectionRet.prepareStatement("DROP TABLE JAHIA_TEST");
198                 stmt.execute();
199                 stmt.close();
200                 connectionRet.commit();
201                 connectionRet.setAutoCommit(true);
202                 connectionPool.free(connectionRet);
203                 return true;
204             }
205         } catch(Exception JavaDoc e) {
206             e.printStackTrace();
207             Assert.assertTrue(false);
208         }
209         return false;
210     }
211
212     private boolean makeQuery() {
213         try {
214             int debugID1= 0;
215             Connection connectionRet = connectionPool.getConnection(debugID1);
216             connectionRet.setAutoCommit(false);
217             if (connectionRet != null) {
218                 PreparedStatement stmt = connectionRet.prepareStatement("SELECT * FROM JAHIA_TEST WHERE TEST=?");
219                 stmt.setString(1, "test");
220                 ResultSet rs = stmt.executeQuery();
221                 while (rs.next()) {
222                     String JavaDoc curTest = rs.getString("test");
223                 }
224                 stmt.close();
225                 stmt = connectionRet.prepareStatement("INSERT INTO JAHIA_TEST VALUES (?)");
226                 stmt.setString(1, "test");
227                 stmt.execute();
228                 stmt.close();
229                 connectionRet.commit();
230                 connectionRet.setAutoCommit(true);
231                 connectionPool.free(connectionRet);
232             }
233             connectionRet = connectionPool.getConnection(debugID1);
234             connectionRet.setAutoCommit(false);
235             if (connectionRet != null) {
236                 PreparedStatement stmt = connectionRet.prepareStatement("UPDATE JAHIA_TEST SET TEST=? WHERE TEST=?");
237                 stmt.setString(1, "test2");
238                 stmt.setString(2, "test");
239                 stmt.executeUpdate();
240                 stmt.close();
241                 stmt = connectionRet.prepareStatement("DELETE FROM JAHIA_TEST");
242                 stmt.execute();
243                 stmt.close();
244                 connectionRet.commit();
245                 connectionRet.setAutoCommit(true);
246                 connectionPool.free(connectionRet);
247                 return true;
248             }
249         } catch(Exception JavaDoc e) {
250             e.printStackTrace();
251             Assert.assertTrue(false);
252         }
253         return false;
254     }
255
256     public void testOneWriting() {
257         boolean drop = dropTableNoError();
258         boolean create = createTable();
259         boolean query = makeQuery();
260         boolean dropAgain = dropTable();
261         Assert.assertTrue(create && query && dropAgain);
262     }
263
264     public void testMultipleWriting() {
265         boolean drop = dropTableNoError();
266         boolean create = createTable();
267         boolean query = true;
268         for (int i=0; i < 100; i++) {
269             boolean result = makeQuery();
270             if (!result) {
271                 query = false;
272             }
273         }
274         boolean dropAgain = dropTable();
275         Assert.assertTrue(create && query && dropAgain);
276     }
277
278     public void testMultipleThreadWriting() {
279         boolean drop = dropTableNoError();
280         boolean create = createTable();
281         int maxSleepTime = 50;
282         logger.debug("Launching "+ (maxConnections *2 ) + " worker threads...maxSleepTime=" + maxSleepTime);
283         boolean query = true;
284         Vector threads = new Vector();
285         ThreadGroup JavaDoc testGroup = new ThreadGroup JavaDoc("testPoolThreads");
286         Random myRandom = new Random();
287         for (int i=0; i < (maxConnections * 2); i++) {
288             PoolTestThread curPoolThread = new PoolTestThread(connectionPool, myRandom, maxSleepTime);
289             Thread JavaDoc curThread = new Thread JavaDoc(testGroup, curPoolThread);
290             curThread.start();
291             threads.add(curPoolThread);
292         }
293         logger.debug("Waiting for threads to finish...");
294         while (testGroup.activeCount() > 0) {
295
296         }
297         logger.debug("All threads have finished executing, let's gather the results...");
298         Enumeration poolThreadEnum = threads.elements();
299         while (poolThreadEnum.hasMoreElements()) {
300             PoolTestThread curPoolThread = (PoolTestThread) poolThreadEnum.nextElement();
301             if (!curPoolThread.getResult()) {
302                 query = false;
303             }
304         }
305         boolean dropAgain = dropTable();
306         Assert.assertTrue(create && query && dropAgain);
307     }
308
309     // put at the end because it tests one time expiration.
310
/*
311     public void testExhaustGetConnections() {
312         try {
313             int debugID1= 0;
314             boolean success = true;
315             logger.debug("Acquiring " + (maxConnections+1) + " connections...");
316             int exceptionCount = 0;
317             for (int i=0; i < (maxConnections+1); i++) {
318                 Connection connectionRet;
319                 try {
320                     connectionRet = connectionPool.getConnection(i);
321                     if (connectionRet == null) {
322                         success = false;
323                     }
324                 } catch (SQLException sqle) {
325                     logger.debug("Catching expected SQLException : " + sqle.getMessage());
326                     exceptionCount++;
327                 }
328             }
329             if (exceptionCount != 1) {
330                 success = false;
331             }
332             Assert.assertTrue(success);
333         } catch(Exception e) {
334             e.printStackTrace();
335             Assert.assertTrue(false);
336         }
337
338     }
339     */

340
341
342 }
Popular Tags