You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleJDBCConnectionPoolTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.vaadin.v7.data.util.sqlcontainer.connection;
  2. import static org.junit.Assert.assertFalse;
  3. import static org.junit.Assert.assertNotNull;
  4. import static org.junit.Assert.assertTrue;
  5. import static org.junit.Assert.fail;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8. import org.easymock.EasyMock;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import com.vaadin.v7.data.util.sqlcontainer.SQLTestsConstants;
  12. import com.vaadin.v7.data.util.sqlcontainer.query.ValidatingSimpleJDBCConnectionPool;
  13. public class SimpleJDBCConnectionPoolTest {
  14. private JDBCConnectionPool connectionPool;
  15. @Before
  16. public void setUp() throws SQLException {
  17. connectionPool = new ValidatingSimpleJDBCConnectionPool(
  18. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
  19. SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 2, 2);
  20. }
  21. @Test
  22. public void reserveConnection_reserveNewConnection_returnsConnection()
  23. throws SQLException {
  24. Connection conn = connectionPool.reserveConnection();
  25. assertNotNull(conn);
  26. }
  27. @Test
  28. public void releaseConnection_releaseUnused_shouldNotThrowException()
  29. throws SQLException {
  30. Connection conn = connectionPool.reserveConnection();
  31. connectionPool.releaseConnection(conn);
  32. assertFalse(conn.isClosed());
  33. }
  34. @Test(expected = SQLException.class)
  35. public void reserveConnection_noConnectionsLeft_shouldFail()
  36. throws SQLException {
  37. try {
  38. connectionPool.reserveConnection();
  39. connectionPool.reserveConnection();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. fail("Exception before all connections used! " + e.getMessage());
  43. }
  44. connectionPool.reserveConnection();
  45. fail("Reserving connection didn't fail even though no connections are available!");
  46. }
  47. @Test
  48. public void reserveConnection_oneConnectionLeft_returnsConnection()
  49. throws SQLException {
  50. try {
  51. connectionPool.reserveConnection();
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. fail("Exception before all connections used! " + e.getMessage());
  55. }
  56. Connection conn = connectionPool.reserveConnection();
  57. assertNotNull(conn);
  58. }
  59. @Test
  60. public void reserveConnection_oneConnectionJustReleased_returnsConnection()
  61. throws SQLException {
  62. Connection conn2 = null;
  63. try {
  64. connectionPool.reserveConnection();
  65. conn2 = connectionPool.reserveConnection();
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. fail("Exception before all connections used! " + e.getMessage());
  69. }
  70. connectionPool.releaseConnection(conn2);
  71. connectionPool.reserveConnection();
  72. }
  73. @Test(expected = IllegalArgumentException.class)
  74. public void construct_allParametersNull_shouldFail() throws SQLException {
  75. SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool(null, null,
  76. null, null);
  77. }
  78. @Test(expected = IllegalArgumentException.class)
  79. public void construct_onlyDriverNameGiven_shouldFail() throws SQLException {
  80. SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool(
  81. SQLTestsConstants.dbDriver, null, null, null);
  82. }
  83. @Test(expected = IllegalArgumentException.class)
  84. public void construct_onlyDriverNameAndUrlGiven_shouldFail()
  85. throws SQLException {
  86. SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool(
  87. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL, null,
  88. null);
  89. }
  90. @Test(expected = IllegalArgumentException.class)
  91. public void construct_onlyDriverNameAndUrlAndUserGiven_shouldFail()
  92. throws SQLException {
  93. SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool(
  94. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
  95. SQLTestsConstants.dbUser, null);
  96. }
  97. @Test(expected = RuntimeException.class)
  98. public void construct_nonExistingDriver_shouldFail() throws SQLException {
  99. SimpleJDBCConnectionPool cp = new SimpleJDBCConnectionPool("foo",
  100. SQLTestsConstants.dbURL, SQLTestsConstants.dbUser,
  101. SQLTestsConstants.dbPwd);
  102. }
  103. @Test
  104. public void reserveConnection_newConnectionOpened_shouldSucceed()
  105. throws SQLException {
  106. connectionPool = new SimpleJDBCConnectionPool(
  107. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
  108. SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 0, 2);
  109. Connection c = connectionPool.reserveConnection();
  110. assertNotNull(c);
  111. }
  112. @Test
  113. public void releaseConnection_nullConnection_shouldDoNothing() {
  114. connectionPool.releaseConnection(null);
  115. }
  116. @Test
  117. public void releaseConnection_failingRollback_shouldCallClose()
  118. throws SQLException {
  119. Connection c = EasyMock.createMock(Connection.class);
  120. c.getAutoCommit();
  121. EasyMock.expectLastCall().andReturn(false);
  122. c.rollback();
  123. EasyMock.expectLastCall().andThrow(new SQLException("Rollback failed"));
  124. c.close();
  125. EasyMock.expectLastCall().atLeastOnce();
  126. EasyMock.replay(c);
  127. // make sure the connection pool is initialized
  128. // Bypass validation
  129. JDBCConnectionPool realPool = ((ValidatingSimpleJDBCConnectionPool) connectionPool)
  130. .getRealPool();
  131. realPool.reserveConnection();
  132. realPool.releaseConnection(c);
  133. EasyMock.verify(c);
  134. }
  135. @Test
  136. public void destroy_shouldCloseAllConnections() throws SQLException {
  137. Connection c1 = connectionPool.reserveConnection();
  138. Connection c2 = connectionPool.reserveConnection();
  139. try {
  140. connectionPool.destroy();
  141. } catch (RuntimeException e) {
  142. // The test connection pool throws an exception when the pool was
  143. // not empty but only after cleanup of the real pool has been done
  144. }
  145. assertTrue(c1.isClosed());
  146. assertTrue(c2.isClosed());
  147. }
  148. @Test
  149. public void destroy_shouldCloseAllConnections2() throws SQLException {
  150. Connection c1 = connectionPool.reserveConnection();
  151. Connection c2 = connectionPool.reserveConnection();
  152. connectionPool.releaseConnection(c1);
  153. connectionPool.releaseConnection(c2);
  154. connectionPool.destroy();
  155. assertTrue(c1.isClosed());
  156. assertTrue(c2.isClosed());
  157. }
  158. }