Lightweight DB allows you to run PostgreSQL, Oracle, MySQL, MS SQL Server... databases for fast use purposes. It's very suitable for unit and integration Testing.
All currently supported databases are :
- PostgreSQL
- Oracle
- MySQL
- MS SQL Server
- DB2
- Derby
- HSQLDB
- H2
This is how you can create an embedded PostgreSQL database and do common SQL tasks on it.
final DataSource source = new EmbeddedPostgreSQLDataSource();
try (
Connection connection = source.getConnection();
Statement s = connection.createStatement()
) {
s.execute(
String.join(
" ",
"CREATE TABLE accounting_chart (",
" id BIGSERIAL NOT NULL,",
" type VARCHAR(25) NOT NULL,",
" state VARCHAR(10) NOT NULL,",
" version VARCHAR(10) NOT NULL,",
" CONSTRAINT accounting_chart_pkey PRIMARY KEY (id)",
")"
)
);
s.execute(
String.join(
" ",
"INSERT INTO accounting_chart (type, state, version)",
"VALUES ('SYSCOHADA', 'ACTIVE', '2018');"
)
);
}
You can use Liquibase to execute theses operations like this (provided that liquibase
folder is at the root of folder resources
) :
final DataSource source =
new LiquibaseDataSource(
new EmbeddedPostgreSQLDataSource(),
"liquibase/db.changelog-master-test.xml"
);
Simply import Easy-liquibase4j to achieve that (with Maven by example):
<dependency>
<groupId>com.baudoliver7</groupId>
<artifactId>easy-liquibase4j</artifactId>
<version><!-- latest version --></version>
<scope>test</scope>
</dependency>
If you're using Maven, you should add this to your pom.xml
dependencies:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version><!-- 1.4.200 or above --></version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baudoliver7</groupId>
<artifactId>lightweight-db</artifactId>
<version><!-- latest version --></version>
<scope>test</scope>
</dependency>
If you're using Gradle, you should add this to your build.gradle
file:
repositories {
mavenCentral()
}
dependencies {
testImplementation 'com.h2database:h2:/* 1.4.200 or above */'
testImplementation 'com.baudoliver7:lightweight-db:/* latest version */'
}
Fork repository, make changes, send us a pull request. We will review
your changes and apply them to the main
branch shortly, provided
they don't violate our quality standards. To avoid frustration, before
sending us your pull request please run full Maven build:
mvn clean install -Pqulice
Keep in mind that JDK 8 and Maven 3.1.0 are the lowest versions you may use.
If you have questions or general suggestions, don't hesitate to submit a new Github issue.