Top Description Fields Constructors Methods
org.jooq.tools.jdbc

public Class MockFileDatabase

extends Object
implements MockDataProvider
Class Inheritance
All Implemented Interfaces
org.jooq.tools.jdbc.MockDataProvider
Imports
java.io.File, .FileNotFoundException, .IOException, .InputStream, .Reader, java.sql.SQLException, .SQLFeatureNotSupportedException, java.util.ArrayList, .LinkedHashMap, .List, .Map, .Map.Entry, java.util.regex.Matcher, .Pattern, .PatternSyntaxException, org.jooq.DSLContext, .SQLDialect, .Source, org.jooq.exception.MockFileDatabaseException, org.jooq.impl.DSL, org.jooq.tools.JooqLogger

A file-based MockDataProvider.

This data provider reads a database model from a text file, as documented in the below sample file:


# Comments start off with a hash

# Statement strings have no prefix and should be ended with a semi-colon
select 'A' from dual;
# Statements may be followed by results, using >
> A
> -
> A
# Statements should be followed by "@ rows: [N]" indicating the update count
@ rows: 1

# New statements can be listed int his file
select 'A', 'B' from dual;
> A B
> - -
> A B
@ rows: 1

# Beware of the exact syntax (e.g. using quotes)
select "TABLE1"."ID1", "TABLE1"."NAME1" from "TABLE1";
> ID1 NAME1
> --- -----
> 1   X
> 2   Y
@ rows: 2

# Statements can return several results
> F1  F2  F3 is a bit more complex
> --- --  ----------------------------
> 1   2   and a string containing data
> 1.1 x   another string
@ rows: 2

> A B "C D"
> - - -----
> x y z
@ rows: 1

Results can be loaded using several techniques:

Disclaimer

The general idea of mocking a JDBC connection with this jOOQ API is to provide quick workarounds, injection points, etc. using a very simple JDBC abstraction. It is NOT RECOMMENDED to emulate an entire database (including complex state transitions, transactions, locking, etc.) using this mock API. Once you have this requirement, please consider using an actual database instead for integration testing (e.g. using https://www.testcontainers.org), rather than implementing your test database inside of a MockDataProvider.

Authors
Lukas Eder, Samy Deghou

Field Summary

Modifier and TypeField and Description
private final MockFileDatabaseConfiguration
private final DSLContext
private static final Pattern
private static final JooqLogger
private final Map<String, List<MockResult>>
private final Map<Pattern, List<MockResult>>
private String
nullLiteral

Deprecated

Constructor Summary

AccessConstructor and Description
public
public
MockFileDatabase(File file, String encoding)

public
public
public
public
public
public

Method Summary

Modifier and TypeMethod and Description
public MockResult[]
execute(MockExecuteContext
The execution context.
ctx
)

Implements org.jooq.tools.jdbc.MockDataProvider.execute.

Execution callback for a JDBC query execution.
private void
load()

public MockFileDatabase
nullLiteral(String literal)

Specify the null literal, i.e. the string that should be parsed as a null reference, rather than as the string itself.
public Map<String, List<MockResult>>
queries()

Deprecated - Experimental: Do not use. Subject to change.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

configurationback to summary
private final MockFileDatabaseConfiguration configuration
createback to summary
private final DSLContext create
END_OF_STATEMENTback to summary
private static final Pattern END_OF_STATEMENT
logback to summary
private static final JooqLogger log
matchExactlyback to summary
private final Map<String, List<MockResult>> matchExactly
matchPatternback to summary
private final Map<Pattern, List<MockResult>> matchPattern
nullLiteralback to summary
private String nullLiteral

Deprecated

Annotations
@Deprecated

Constructor Detail

MockFileDatabaseback to summary
public MockFileDatabase(File file) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(File file, String encoding) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(InputStream stream) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(InputStream stream, String encoding) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(Reader reader) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(String string) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(Source source) throws IOException
MockFileDatabaseback to summary
public MockFileDatabase(MockFileDatabaseConfiguration configuration) throws IOException

Method Detail

executeback to summary
public MockResult[] execute(MockExecuteContext ctx) throws SQLException

Implements org.jooq.tools.jdbc.MockDataProvider.execute.

Doc from org.jooq.tools.jdbc.MockDataProvider.execute.

Execution callback for a JDBC query execution.

This callback will be called by MockStatement upon the various statement execution methods. These include:

The various execution modes are unified into this simple method. Implementations should adhere to this contract:

Parameters
ctx:MockExecuteContext

The execution context.

Returns:MockResult[]

The execution results. If a null or an empty MockResult[] is returned, this has the same effect as returning new MockResult[] { new MockResult(-1, null) }

Annotations
@Override
Exceptions
SQLException:
A SQLException that is passed through to jOOQ.
loadback to summary
private void load() throws FileNotFoundException, IOException
nullLiteralback to summary
public MockFileDatabase nullLiteral(String literal)

Specify the null literal, i.e. the string that should be parsed as a null reference, rather than as the string itself.

Annotations
@Deprecated
See Also
DSLContext#fetchFromTXT(String, String)
queriesback to summary
public Map<String, List<MockResult>> queries()

Deprecated

- Experimental: Do not use. Subject to change.

Annotations
@Deprecated