You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
72 lines
2.1 KiB
package de.superx.spring.jdbc; |
|
|
|
import static org.junit.Assert.assertEquals; |
|
import static org.junit.Assert.assertNotNull; |
|
import static org.junit.Assert.assertTrue; |
|
|
|
import java.io.File; |
|
import java.io.IOException; |
|
import java.sql.DriverManager; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
import org.duckdb.DuckDBConnection; |
|
import org.junit.AfterClass; |
|
import org.junit.BeforeClass; |
|
import org.junit.Test; |
|
|
|
@SuppressWarnings("static-method") |
|
public class DuckDbJdbcTest { |
|
|
|
static boolean driverLoaded; |
|
|
|
@BeforeClass |
|
public static void setup() { |
|
try { |
|
Class.forName("org.duckdb.DuckDBDriver"); |
|
driverLoaded = true; |
|
} catch (ClassNotFoundException e) { |
|
e.printStackTrace(); |
|
driverLoaded = false; |
|
} |
|
|
|
} |
|
|
|
@Test |
|
public void testJdbcClass() { |
|
assertTrue("JDBC Driver Class found", driverLoaded); |
|
} |
|
|
|
@Test |
|
public void testCreateDb() throws SQLException { |
|
String url = "jdbc:duckdb:./test/resources/db/duckdb_test"; |
|
DuckDBConnection con = (DuckDBConnection) DriverManager.getConnection(url); |
|
assertNotNull("Connection to db", con); |
|
} |
|
|
|
@Test |
|
public void testSql() throws SQLException { |
|
String url = "jdbc:duckdb:./test/resources/db/duckdb_test"; |
|
DuckDBConnection con = (DuckDBConnection) DriverManager.getConnection(url); |
|
Statement st = con.createStatement(); |
|
st.execute("create table foo (id int, val varchar)"); |
|
st.execute("insert into foo(id, val) values (42, 'bar')"); |
|
int id = -1; |
|
String val = null; |
|
try (ResultSet rs = st.executeQuery("select * from foo")) { |
|
rs.next(); |
|
id = rs.getInt(1); |
|
val = rs.getString(2); |
|
} |
|
st.close(); |
|
assertEquals("Select int val", 42, id); |
|
assertEquals("Select String val", "bar", val); |
|
} |
|
|
|
@AfterClass |
|
public static void tearDown() throws IOException { |
|
FileUtils.delete(new File("./test/resources/db/duckdb_test")); |
|
} |
|
}
|
|
|