Created
February 26, 2013 22:29
-
-
Save jandrewthompson/5042920 to your computer and use it in GitHub Desktop.
Mocking java.sql.Connection with JOOQ file based input provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a sample test database for MockFileDatabase | |
# Its syntax is inspired from H2's test script files | |
# When this query is executed... | |
select id, description, amount from account; | |
> id description amount | |
> -- ----------- ------ | |
> 1 slushy 200 | |
> 2 foo 100 | |
@ rows: 2 | |
select id, name, email from user; | |
> id name email | |
> -- ---- ----- | |
> 1 andrew jathom5@ilstu.edu | |
> 2 lester lester@tester.edu | |
> 3 chester chester@fester.edu | |
@ rows: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jthompson; | |
import java.io.File; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import lombok.extern.java.Log; | |
import org.jooq.tools.jdbc.MockConnection; | |
import org.jooq.tools.jdbc.MockDataProvider; | |
import org.jooq.tools.jdbc.MockFileDatabase; | |
import org.testng.annotations.Test; | |
@Log | |
public class TestJooqConnection | |
{ | |
@Test | |
public void TestConn() throws Exception | |
{ | |
MockDataProvider provider = new MockFileDatabase(new File("src/test/java/com/jthompson/input.sql")); | |
Connection conn = new MockConnection(provider); | |
ResultSet rs = conn.createStatement().executeQuery("select id, description, amount from account"); | |
while(rs.next()) | |
{ | |
log.info("ID: " + rs.getString("id")); | |
log.info("DESCRIPTION: " + rs.getString("description")); | |
log.info("AMOUNT: " + rs.getString("amount")); | |
} | |
ResultSet rs2 = conn.createStatement().executeQuery("select id, name, email from user"); | |
while(rs2.next()) | |
{ | |
log.info("ID: " + rs2.getString("id")); | |
log.info("NAME: " + rs2.getString("name")); | |
log.info("EMAIL: " + rs2.getString("email")); | |
} | |
} | |
} |
That is pretty slick. Do you know how it behaves if/when the SQL statement is multi-line? I'm going to have to play with this.
@jandrewthompson: The current "silly" implementation is a CSV file, with whitespaces as separators. You can delimit values using quotes: "several words"
. This will be improved by jOOQ 3.0-RC2
Any feedback on GitHub or on the jOOQ user group is very welcome!
https://groups.google.com/forum/?fromgroups#!forum/jooq-user
Looks like its current form doesn't play well with Spring JDBC due to SQLFeatureNotSupportedExceptions. Additional effort will be needed to fake those.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The current implementation seems to require single space separated values for the result set data in the input file, with no way to choose an alternate separator. This could be problematic for data that contains spaces.