-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDatabaseRule.java
More file actions
48 lines (37 loc) · 1.26 KB
/
DatabaseRule.java
File metadata and controls
48 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package org.utplsql.api.rules;
import org.junit.rules.ExternalResource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Vinicius on 13/04/2017.
*/
public class DatabaseRule extends ExternalResource {
private static String sUrl;
private static String sUser;
private static String sPass;
static {
sUrl = System.getenv("DB_URL") != null ? System.getenv("DB_URL") : "192.168.99.100:1521:XE";
sUser = System.getenv("DB_USER") != null ? System.getenv("DB_USER") : "app";
sPass = System.getenv("DB_PASS") != null ? System.getenv("DB_PASS") : "app";
}
private List<Connection> connectionList;
public DatabaseRule() {
connectionList = new ArrayList<>();
}
public String getUser() {
return sUser;
}
public synchronized Connection newConnection() throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + sUrl, sUser, sPass);
connectionList.add(conn);
return conn;
}
@Override
protected void after() {
for (Connection conn : connectionList)
try { conn.close(); } catch (SQLException ignored) {}
}
}