|
| 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 | +} |
0 commit comments