Skip to content

Commit c8c7f5c

Browse files
authored
BAEL-8898: Embedded MaraiDB using MariaDB4j (#17948)
* New libraries-data-mariadb4j module * BAEL-8898: Embedded MaraiDB using MariaDB4j * Review updates * Changed to use @tempdir
1 parent dc02540 commit c8c7f5c

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

libraries-data-mariadb4j/pom.xml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>libraries-data-mariadb4j</artifactId>
7+
<name>libraries-data-mariadb4j</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>ch.vorburger.mariaDB4j</groupId>
19+
<artifactId>mariaDB4j</artifactId>
20+
<version>${mariadb4j.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.mariadb.jdbc</groupId>
24+
<artifactId>mariadb-java-client</artifactId>
25+
<version>${mariadb.version}</version>
26+
<scope>runtime</scope>
27+
</dependency>
28+
29+
</dependencies>
30+
31+
<properties>
32+
<mariadb.version>3.5.0</mariadb.version>
33+
<mariadb4j.version>3.1.0</mariadb4j.version>
34+
</properties>
35+
36+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.data.mariadb4j;
2+
3+
import ch.vorburger.mariadb4j.junit.MariaDB4jRule;
4+
import org.junit.ClassRule;
5+
import org.junit.Test;
6+
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
10+
import static org.junit.jupiter.api.Assertions.assertFalse;
11+
12+
public class JUnitClassRuleLiveTest {
13+
@ClassRule
14+
public static MariaDB4jRule dbRule = new MariaDB4jRule(0);
15+
16+
@Test
17+
public void whenRunningATest_thenTheDbIsAvailable() throws Exception {
18+
try (Connection conn = DriverManager.getConnection(dbRule.getURL(), "root", "")) {
19+
assertFalse(conn.isClosed());
20+
}
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.data.mariadb4j;
2+
3+
import ch.vorburger.mariadb4j.junit.MariaDB4jRule;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
7+
import java.sql.Connection;
8+
import java.sql.DriverManager;
9+
10+
import static org.junit.jupiter.api.Assertions.assertFalse;
11+
12+
public class JUnitRuleLiveTest {
13+
@Rule
14+
public MariaDB4jRule dbRule = new MariaDB4jRule(0);
15+
16+
@Test
17+
public void whenRunningATest_thenTheDbIsAvailable() throws Exception {
18+
try (Connection conn = DriverManager.getConnection(dbRule.getURL(), "root", "")) {
19+
assertFalse(conn.isClosed());
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.data.mariadb4j;
2+
3+
import ch.vorburger.mariadb4j.DB;
4+
import ch.vorburger.mariadb4j.DBConfigurationBuilder;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.io.TempDir;
7+
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.sql.Connection;
11+
import java.sql.DriverManager;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
16+
public class JavaLiveTest {
17+
@Test
18+
void whenStartingADatabase_thenTheDatabaseIsUsable() throws Exception {
19+
DB db = DB.newEmbeddedDB(0);
20+
try {
21+
db.start();
22+
23+
assertEquals("jdbc:mariadb://localhost:" + db.getConfiguration().getPort() + "/testing",
24+
db.getConfiguration().getURL("testing"));
25+
} finally {
26+
db.stop();
27+
}
28+
}
29+
30+
@Test
31+
void whenStartingADatabaseWithConfiguration_thenTheDatabaseIsUsable(@TempDir Path tempDir) throws Exception {
32+
DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
33+
config.setPort(13306);
34+
config.setDataDir(Path.of(tempDir.toString(), "data").toString());
35+
config.setBaseDir(Path.of(tempDir.toString(), "base").toString());
36+
37+
DB db = DB.newEmbeddedDB(config.build());
38+
try {
39+
db.start();
40+
41+
db.createDB("testing", "testuser", "testpass");
42+
assertEquals("jdbc:mariadb://localhost:13306/testing", db.getConfiguration().getURL("testing"));
43+
} finally {
44+
db.stop();
45+
}
46+
}
47+
48+
@Test
49+
void whenRunningCommandsAgainstADatabase_thenTheCommandSucceeds() throws Exception {
50+
DB db = DB.newEmbeddedDB(0);
51+
try {
52+
db.start();
53+
db.createDB("testing", "testuser", "testpass");
54+
db.run("show databases");
55+
} finally {
56+
db.stop();
57+
}
58+
}
59+
60+
@Test
61+
void whenRunningAScriptAgainstADatabase_thenTheCommandSucceeds() throws Exception {
62+
DB db = DB.newEmbeddedDB(0);
63+
try {
64+
db.start();
65+
db.createDB("testing", "testuser", "testpass");
66+
db.source("com/baeldung/data/mariadb4j/source.sql", "testuser", "testpass", "testing");
67+
} finally {
68+
db.stop();
69+
}
70+
}
71+
72+
@Test
73+
void whenRunningADatabase_thenICanConnectWithJDBC() throws Exception {
74+
DB db = DB.newEmbeddedDB(0);
75+
try {
76+
db.start();
77+
db.createDB("testing", "testuser", "testpass");
78+
79+
try (Connection conn = DriverManager.getConnection(db.getConfiguration().getURL("testing"), "testuser", "testpass")) {
80+
assertFalse(conn.isClosed());
81+
}
82+
} finally {
83+
db.stop();
84+
}
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE testingTable(id INT NOT NULL);
2+
INSERT INTO testingTable(id) VALUES (1);
3+
SELECT * FROM testingTable;

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@
734734
<module>libraries-data-3</module>
735735
<module>libraries-data-db</module>
736736
<module>libraries-data</module>
737+
<module>libraries-data-mariadb4j</module>
737738
<module>libraries-files</module>
738739
<module>libraries-http</module>
739740
<module>libraries-http-2</module>

0 commit comments

Comments
 (0)