2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dao.util.dbcp;
28 import org.apache.commons.dbcp2.BasicDataSource;
29 import org.junit.After;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ExpectedException;
35 import org.mockito.Mockito;
36 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
37 import org.powermock.reflect.Whitebox;
38 import java.sql.Connection;
39 import java.sql.SQLException;
43 public class DBConnectionPoolTest {
44 private final String connectURI = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
45 private final String username = "sa";
46 private final String password = "sa";
47 private final String driverClass = "org.h2.Driver";
49 private DBConnectionPool dbcp;
50 private DBConnectionPool dbcp2;
51 private Connection connection;
54 public ExpectedException expectedEx = ExpectedException.none();
57 public void setUp() throws Exception {
58 dbcp = new DBConnectionPool(connectURI, username, password, driverClass);
59 dbcp2 = new DBConnectionPool(connectURI, username, password, driverClass);
63 public void testGetConnection() {
65 connection = dbcp.getConnection();
66 } catch (DBConnectionPoolException e) {
67 Assert.fail(e.getMessage());
69 Assert.assertNotNull(connection);
73 public void testGetConnectionSQLExceptionFlow() throws SQLException {
74 DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
75 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
76 Mockito.when(mockDataSource.getConnection()).thenThrow(new SQLException());
77 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
78 expectedEx.expect(SQLException.class);
79 connection = dbcpSpy.getConnection();
83 public void testGetConnectionDBConnectionPoolExceptionFlow() throws SQLException {
84 DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass));
85 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
86 Mockito.when(mockDataSource.getConnection()).thenReturn(null);
87 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
88 expectedEx.expect(DBConnectionPoolException.class);
89 connection = dbcpSpy.getConnection();
93 public void testGetDataSourceStatus() {
94 Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus();
95 Assert.assertNotNull(dataSourceStatus);
98 @Test(expected = DBConnectionPoolException.class)
99 public void testShutdown() throws DBConnectionPoolException {
101 connection = dbcp2.getConnection();
102 Assert.assertNull(connection);
106 public void testShutdownException() throws SQLException {
107 DBConnectionPool dbcpSpy = Mockito.spy(new DBConnectionPool(connectURI, username, password, driverClass,
109 BasicDataSource mockDataSource = Mockito.mock(BasicDataSource.class);
110 Mockito.doThrow(new SQLException()).when(mockDataSource).close();
111 Whitebox.setInternalState(dbcpSpy, "dataSource", mockDataSource);
113 Mockito.verify(mockDataSource).close();
117 public void clean() {
124 if (connection != null) {
127 } catch (SQLException e) {