[#2900] verload MockFileDatabase constructor to allow for Readers, InputStreams, and Strings

This commit is contained in:
Lukas Eder 2013-12-17 15:34:30 +01:00
parent 78e3ce7038
commit afaad5e559
3 changed files with 39 additions and 17 deletions

View File

@ -47,6 +47,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
@ -121,20 +123,38 @@ public class MockFileDatabase implements MockDataProvider {
private static final JooqLogger log = JooqLogger.getLogger(MockFileDatabase.class);
private final File file;
private final String encoding;
private final LineNumberReader in;
private final Map<String, List<MockResult>> matchExactly;
private final Map<Pattern, List<MockResult>> matchPattern;
private final DSLContext create;
private final DSLContext create;
public MockFileDatabase(File file) throws IOException {
this(file, "UTF-8");
}
@SuppressWarnings("deprecation")
public MockFileDatabase(File file, String encoding) throws IOException {
this.file = file;
this.encoding = encoding;
this(new FileInputStream(file), encoding);
}
public MockFileDatabase(InputStream stream) throws IOException {
this(stream, "UTF-8");
}
public MockFileDatabase(InputStream stream, String encoding) throws IOException {
this(new InputStreamReader(stream, encoding));
}
public MockFileDatabase(Reader reader) throws IOException {
this(new LineNumberReader(reader));
}
public MockFileDatabase(String string) throws IOException {
this(new StringReader(string));
}
@SuppressWarnings("deprecation")
private MockFileDatabase(LineNumberReader reader) throws IOException {
this.in = reader;
this.matchExactly = new LinkedHashMap<String, List<MockResult>>();
this.matchPattern = new LinkedHashMap<Pattern, List<MockResult>>();
this.create = DSL.using(SQLDialect.SQL99);
@ -146,8 +166,6 @@ public class MockFileDatabase implements MockDataProvider {
// Wrap the below code in a local scope
new Object() {
private InputStream is = new FileInputStream(file);
private LineNumberReader in = new LineNumberReader(new InputStreamReader(is, encoding));
private StringBuilder currentSQL = new StringBuilder();
private StringBuilder currentResult = new StringBuilder();
private String previousSQL = null;
@ -218,9 +236,6 @@ public class MockFileDatabase implements MockDataProvider {
}
}
finally {
if (is != null) {
is.close();
}
if (in != null) {
in.close();
}

View File

@ -52,9 +52,11 @@ import static org.jooq.test.data.Table2.FIELD_NAME2;
import static org.jooq.test.data.Table2.TABLE2;
import java.io.File;
import java.io.RandomAccessFile;
import java.sql.SQLException;
import java.util.List;
import org.jooq.Constants;
import org.jooq.DSLContext;
import org.jooq.InsertResultStep;
import org.jooq.Query;
@ -87,8 +89,13 @@ public class MockTest extends AbstractTest {
@BeforeClass
public static void before() throws Exception {
File file = new File(MockTest.class.getResource("/org/jooq/test/data/db.txt").toURI());
MOCK = DSL.using(new MockConnection(new MockFileDatabase(file)), SQLDialect.POSTGRES);
RandomAccessFile f = new RandomAccessFile(new File(MockTest.class.getResource("/org/jooq/test/data/db.txt").toURI()), "r");
byte[] b = new byte[(int) f.length()];
f.readFully(b);
String s = new String(b);
s = s.replace("{version}", Constants.FULL_VERSION);
MOCK = DSL.using(new MockConnection(new MockFileDatabase(s)), SQLDialect.POSTGRES);
}
@Test

View File

@ -6,7 +6,7 @@ select 'A';
> A
@ rows: 1
select 'A' -- SQL rendered with a free trial version of jOOQ 3.3.0;
select 'A' -- SQL rendered with a free trial version of jOOQ {version};
> A
> -
> A
@ -18,7 +18,7 @@ select 'A', 'B';
> A B
@ rows: 1
select 'A', 'B' -- SQL rendered with a free trial version of jOOQ 3.3.0;
select 'A', 'B' -- SQL rendered with a free trial version of jOOQ {version};
> A B
> - -
> A B
@ -31,7 +31,7 @@ select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1";
> 2 Y
@ rows: 2
select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1" -- SQL rendered with a free trial version of jOOQ 3.3.0;
select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1" -- SQL rendered with a free trial version of jOOQ {version};
> ID1 NAME1
> --- -----
> 1 X
@ -47,7 +47,7 @@ select "TABLE2"."ID2", "TABLE2"."NAME2" from "TABLE2";
> +---+-----+
@ rows: 2
select "TABLE2"."ID2", "TABLE2"."NAME2" from "TABLE2" -- SQL rendered with a free trial version of jOOQ 3.3.0;
select "TABLE2"."ID2", "TABLE2"."NAME2" from "TABLE2" -- SQL rendered with a free trial version of jOOQ {version};
> +---+-----+
> |ID2|NAME2|
> +---+-----+