Skip to content

Commit 5db33e0

Browse files
AnkBurovrnorth
authored andcommitted
#551: Implement database delegate abstraction for ScriptUtils
Supports #525
1 parent 4edf9e2 commit 5db33e0

File tree

12 files changed

+593
-292
lines changed

12 files changed

+593
-292
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Change Log
22
All notable changes to this project will be documented in this file.
33

4+
## UNRELEASED
5+
6+
### Changed
7+
- Abstracted and changed database init script functionality to support use of SQL-like scripts with non-JDBC connections. ([\#551](https://github.com/testcontainers/testcontainers-java/pull/551))
8+
49
## [1.6.0] - 2018-01-28
510

611
### Fixed

modules/database-commons/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.testcontainers</groupId>
7+
<artifactId>testcontainers-parent</artifactId>
8+
<version>0-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>database-commons</artifactId>
13+
<name>TestContainers :: Database-Commons</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>${project.groupId}</groupId>
18+
<artifactId>testcontainers</artifactId>
19+
<version>${project.version}</version>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.testcontainers.delegate;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* @param <CONNECTION> connection to the database
7+
* @author Eugeny Karpov
8+
*/
9+
public abstract class AbstractDatabaseDelegate<CONNECTION> implements DatabaseDelegate {
10+
11+
/**
12+
* Database connection
13+
*/
14+
private CONNECTION connection;
15+
16+
private boolean isConnectionStarted = false;
17+
18+
/**
19+
* Get or create new connection to the database
20+
*/
21+
protected CONNECTION getConnection() {
22+
if (!isConnectionStarted) {
23+
connection = createNewConnection();
24+
isConnectionStarted = true;
25+
}
26+
return connection;
27+
}
28+
29+
@Override
30+
public void execute(Collection<String> statements, String scriptPath, boolean continueOnError, boolean ignoreFailedDrops) {
31+
int lineNumber = 0;
32+
for (String statement : statements) {
33+
lineNumber++;
34+
execute(statement, scriptPath, lineNumber, continueOnError, ignoreFailedDrops);
35+
}
36+
}
37+
38+
@Override
39+
public void close() {
40+
if (isConnectionStarted) {
41+
closeConnectionQuietly(connection);
42+
isConnectionStarted = false;
43+
}
44+
}
45+
46+
/**
47+
* Quietly close the connection
48+
*/
49+
protected abstract void closeConnectionQuietly(CONNECTION connection);
50+
51+
/**
52+
* Template method for creating new connections to the database
53+
*/
54+
protected abstract CONNECTION createNewConnection();
55+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.testcontainers.delegate;
2+
3+
import java.util.Collection;
4+
5+
/**
6+
* Database delegate
7+
*
8+
* Gives an abstraction from concrete database
9+
*
10+
* @author Eugeny Karpov
11+
*/
12+
public interface DatabaseDelegate extends AutoCloseable {
13+
14+
/**
15+
* Execute statement by the implementation of the delegate
16+
*/
17+
void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops);
18+
19+
/**
20+
* Execute collection of statements
21+
*/
22+
void execute(Collection<String> statements, String scriptPath, boolean continueOnError, boolean ignoreFailedDrops);
23+
24+
/**
25+
* Close connection to the database
26+
*
27+
* Overridden to suppress throwing Exception
28+
*/
29+
@Override
30+
void close();
31+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.testcontainers.exception;
2+
3+
/**
4+
* Inability to create connection to the database
5+
*
6+
* @author Eugeny Karpov
7+
*/
8+
public class ConnectionCreationException extends RuntimeException {
9+
10+
public ConnectionCreationException(String message, Throwable cause) {
11+
super(message, cause);
12+
}
13+
}

0 commit comments

Comments
 (0)