b9ba1daa79c2a84000296be5cd9f3bcd242ecc98
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dao.util.dbcp;
27
28 import org.junit.After;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
33
34 import java.sql.Connection;
35 import java.sql.SQLException;
36 import java.util.Map;
37
38
39 public class DBConnectionPoolTest {
40     private final String connectURI = "jdbc:h2:mem:~/test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
41     private final String username = "sa";
42     private final String password = "sa";
43     private final String driverClass = "org.h2.Driver";
44
45     private DBConnectionPool dbcp;
46     private DBConnectionPool dbcp2;
47     private Connection connection;
48
49     @Before
50     public void setUp() throws Exception {
51         dbcp = new DBConnectionPool(connectURI, username, password, driverClass);
52         dbcp2 = new DBConnectionPool(connectURI, username, password, driverClass);
53     }
54
55     @Test
56     public void testGetConnection() {
57         try {
58             connection = dbcp.getConnection();
59         } catch (DBConnectionPoolException e) {
60             Assert.fail(e.getMessage());
61         }
62         Assert.assertNotNull(connection);
63     }
64
65     @Test
66     public void testGetDataSourceStatus() {
67         Map<String, Integer> dataSourceStatus = dbcp.getDataSourceStatus();
68         Assert.assertNotNull(dataSourceStatus);
69     }
70
71     @Test(expected = DBConnectionPoolException.class)
72     public void testShutdown() throws DBConnectionPoolException {
73         dbcp2.shutdown();
74         connection = dbcp2.getConnection();
75         Assert.assertNull(connection);
76     }
77
78     @After
79     public void clean() {
80         if (dbcp != null) {
81             dbcp.shutdown();
82         }
83         if (dbcp2 != null) {
84             dbcp2.shutdown();
85         }
86         if (connection != null) {
87             try {
88                 connection.close();
89             } catch (SQLException e) {
90                 e.printStackTrace();
91             }
92         }
93     }
94 }