@Test public void testGetPooledConnection() throws Exception { DBConnectionManager dbcManagerSpy = getDBConnectionManagerSpyWithPooling(); PooledDataSource dataSourceMock = mock(PooledDataSource.class); Connection connMock = mock(Connection.class); doReturn(connMock).when(dataSourceMock).getConnection(); dbcManagerSpy.dbmsPoolTable = getHashTableObject2(dataSourceMock, connMock); PowerMockito.mockStatic(TripleDES.class); Mockito.when(TripleDES.encryptPassword(any(String.class))).thenReturn(ENCRYPTED_PASS); assertEquals(connMock, dbcManagerSpy.getPooledConnection(DBType.MYSQL, DB_URL, DHARMA_USER, DHARMA_PASSWORD)); verify(dataSourceMock, times(1)).getConnection(); }
protected Connection getPooledConnection(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException { Connection retCon; String dbmsKey = aDbType + "." + aDbUrl; if (dbmsPoolTable.containsKey(dbmsKey)) { Hashtable<String, DataSource> dsTable = dbmsPoolTable.get(dbmsKey); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException ("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; DataSource ds; if (dsTable.containsKey(dsTableKey)) { ds = dsTable.get(dsTableKey); retCon = ds.getConnection(); } else { ds = this.createDataSource(aDbType, aDbUrl, aUsername, aPassword, dsTable); retCon = ds.getConnection(); dsTable.put(dsTableKey, ds); } } else { PooledDataSource ds = (PooledDataSource) this.createDataSource(aDbType, aDbUrl, aUsername, aPassword); retCon = getPooledConnection(ds, aUsername, aPassword); Hashtable<String, DataSource> dsTable = new Hashtable<>(); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; dsTable.put(dsTableKey, ds); dbmsPoolTable.put(dbmsKey, dsTable); } return retCon; }
DBConnectionManager { protected Connection getPooledConnection(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException { Connection retCon; String dbmsKey = aDbType + "." + aDbUrl; if (dbmsPoolTable.containsKey(dbmsKey)) { Hashtable<String, DataSource> dsTable = dbmsPoolTable.get(dbmsKey); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException ("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; DataSource ds; if (dsTable.containsKey(dsTableKey)) { ds = dsTable.get(dsTableKey); retCon = ds.getConnection(); } else { ds = this.createDataSource(aDbType, aDbUrl, aUsername, aPassword, dsTable); retCon = ds.getConnection(); dsTable.put(dsTableKey, ds); } } else { PooledDataSource ds = (PooledDataSource) this.createDataSource(aDbType, aDbUrl, aUsername, aPassword); retCon = getPooledConnection(ds, aUsername, aPassword); Hashtable<String, DataSource> dsTable = new Hashtable<>(); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; dsTable.put(dsTableKey, ds); dbmsPoolTable.put(dbmsKey, dsTable); } return retCon; } }
DBConnectionManager { protected Connection getPooledConnection(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException { Connection retCon; String dbmsKey = aDbType + "." + aDbUrl; if (dbmsPoolTable.containsKey(dbmsKey)) { Hashtable<String, DataSource> dsTable = dbmsPoolTable.get(dbmsKey); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException ("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; DataSource ds; if (dsTable.containsKey(dsTableKey)) { ds = dsTable.get(dsTableKey); retCon = ds.getConnection(); } else { ds = this.createDataSource(aDbType, aDbUrl, aUsername, aPassword, dsTable); retCon = ds.getConnection(); dsTable.put(dsTableKey, ds); } } else { PooledDataSource ds = (PooledDataSource) this.createDataSource(aDbType, aDbUrl, aUsername, aPassword); retCon = getPooledConnection(ds, aUsername, aPassword); Hashtable<String, DataSource> dsTable = new Hashtable<>(); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; dsTable.put(dsTableKey, ds); dbmsPoolTable.put(dbmsKey, dsTable); } return retCon; } protected  DBConnectionManager(); }
DBConnectionManager { protected Connection getPooledConnection(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException { Connection retCon; String dbmsKey = aDbType + "." + aDbUrl; if (dbmsPoolTable.containsKey(dbmsKey)) { Hashtable<String, DataSource> dsTable = dbmsPoolTable.get(dbmsKey); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException ("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; DataSource ds; if (dsTable.containsKey(dsTableKey)) { ds = dsTable.get(dsTableKey); retCon = ds.getConnection(); } else { ds = this.createDataSource(aDbType, aDbUrl, aUsername, aPassword, dsTable); retCon = ds.getConnection(); dsTable.put(dsTableKey, ds); } } else { PooledDataSource ds = (PooledDataSource) this.createDataSource(aDbType, aDbUrl, aUsername, aPassword); retCon = getPooledConnection(ds, aUsername, aPassword); Hashtable<String, DataSource> dsTable = new Hashtable<>(); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; dsTable.put(dsTableKey, ds); dbmsPoolTable.put(dbmsKey, dsTable); } return retCon; } protected  DBConnectionManager(); static DBConnectionManager getInstance(); void finalize(); synchronized Connection getConnection(DBType aDbType, String aAuthType, String aDbUrl, String aUsername, String aPassword, Properties properties); void cleanDataSources(); synchronized void shutdownDbmsPools(); int getDbmsPoolSize(); int getConnectionSize(DBType aDbType, String aDbUrl); int getCheckedOutConnectionSize(DBType aDbType, String aDbUrl); int getCheckedInConnectionSize(DBType aDbType, String aDbUrl); int getTotalConnectionSize(); int getTotalCheckedOutConnectionSize(); int getTotalCheckedInConnectionSize(); }
DBConnectionManager { protected Connection getPooledConnection(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException { Connection retCon; String dbmsKey = aDbType + "." + aDbUrl; if (dbmsPoolTable.containsKey(dbmsKey)) { Hashtable<String, DataSource> dsTable = dbmsPoolTable.get(dbmsKey); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException ("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; DataSource ds; if (dsTable.containsKey(dsTableKey)) { ds = dsTable.get(dsTableKey); retCon = ds.getConnection(); } else { ds = this.createDataSource(aDbType, aDbUrl, aUsername, aPassword, dsTable); retCon = ds.getConnection(); dsTable.put(dsTableKey, ds); } } else { PooledDataSource ds = (PooledDataSource) this.createDataSource(aDbType, aDbUrl, aUsername, aPassword); retCon = getPooledConnection(ds, aUsername, aPassword); Hashtable<String, DataSource> dsTable = new Hashtable<>(); String encryptedPass; try { encryptedPass = TripleDES.encryptPassword(aPassword); } catch (Exception e) { throw new SQLException("Failed to encrypt password for key = " + dbmsKey, e); } String dsTableKey = aDbUrl + "." + aUsername + "." + encryptedPass; dsTable.put(dsTableKey, ds); dbmsPoolTable.put(dbmsKey, dsTable); } return retCon; } protected  DBConnectionManager(); static DBConnectionManager getInstance(); void finalize(); synchronized Connection getConnection(DBType aDbType, String aAuthType, String aDbUrl, String aUsername, String aPassword, Properties properties); void cleanDataSources(); synchronized void shutdownDbmsPools(); int getDbmsPoolSize(); int getConnectionSize(DBType aDbType, String aDbUrl); int getCheckedOutConnectionSize(DBType aDbType, String aDbUrl); int getCheckedInConnectionSize(DBType aDbType, String aDbUrl); int getTotalConnectionSize(); int getTotalCheckedOutConnectionSize(); int getTotalCheckedInConnectionSize();  }