[#4126] Add void DSLContext.mock(MockDataProvider, MockRunnable) and <T> T mockResult(MockDataProvider, MockCallable<T>)
This commit is contained in:
parent
fa11bb64a4
commit
d6d4fbd579
@ -80,6 +80,9 @@ import org.jooq.exception.InvalidResultException;
|
||||
import org.jooq.exception.MappingException;
|
||||
import org.jooq.exception.TooManyRowsException;
|
||||
import org.jooq.impl.DSL;
|
||||
import org.jooq.tools.jdbc.MockCallable;
|
||||
import org.jooq.tools.jdbc.MockDataProvider;
|
||||
import org.jooq.tools.jdbc.MockRunnable;
|
||||
|
||||
/**
|
||||
* A contextual DSL providing "attached" implementations to the
|
||||
@ -158,7 +161,7 @@ public interface DSLContext extends Scope {
|
||||
Meta meta();
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Transaction API
|
||||
// XXX APIs for creating scope for transactions, mocking, batching, etc.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
@ -166,21 +169,6 @@ public interface DSLContext extends Scope {
|
||||
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
|
||||
* {@link Configuration#transactionProvider()}, and return the
|
||||
* <code>transactional</code>'s outcome.
|
||||
* <p>
|
||||
* Both javac and Eclipse compilers contain bugs when overloading methods
|
||||
* that take both "void-compatible" and "value-compatible" functional
|
||||
* interfaces:
|
||||
* <ul>
|
||||
* <li><a
|
||||
* href="https://bugs.openjdk.java.net/browse/JDK-8029718">JDK-8029718</a></li>
|
||||
* <li><a
|
||||
* href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=434642">Eclipse
|
||||
* 434642</a></li>
|
||||
* </ul>
|
||||
* This is why this method was renamed to <code>transactionResult()</code>.
|
||||
* Future versions of jOOQ may create a better synonym for this, called
|
||||
* <code>transaction()</code>, which doesn't conflict with
|
||||
* {@link #transaction(TransactionalRunnable)}
|
||||
*
|
||||
* @param transactional The transactional code
|
||||
* @return The transactional outcome
|
||||
@ -190,13 +178,26 @@ public interface DSLContext extends Scope {
|
||||
/**
|
||||
* Run a {@link TransactionalRunnable} in the context of this
|
||||
* <code>DSLContext</code>'s underlying {@link #configuration()}'s
|
||||
* {@link Configuration#transactionProvider()}, and return the
|
||||
* <code>transactional</code>'s outcome.
|
||||
* {@link Configuration#transactionProvider()}.
|
||||
*
|
||||
* @param transactional The transactional code
|
||||
*/
|
||||
void transaction(TransactionalRunnable transactional);
|
||||
|
||||
/**
|
||||
* Run a {@link MockRunnable} in the context of this <code>DSLContext</code>
|
||||
* 's underlying {@link #configuration()}'s, and of a
|
||||
* {@link MockDataProvider} and return the <code>mockable</code>'s outcome.
|
||||
*/
|
||||
<T> T mockResult(MockDataProvider provider, MockCallable<T> mockable);
|
||||
|
||||
/**
|
||||
* Run a {@link MockRunnable} in the context of this <code>DSLContext</code>
|
||||
* 's underlying {@link #configuration()}'s, and of a
|
||||
* {@link MockDataProvider}.
|
||||
*/
|
||||
void mock(MockDataProvider provider, MockRunnable mockable);
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX RenderContext and BindContext accessors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -210,6 +210,8 @@ import org.jooq.exception.SQLDialectNotSupportedException;
|
||||
import org.jooq.impl.BatchCRUD.Action;
|
||||
import org.jooq.tools.JooqLogger;
|
||||
import org.jooq.tools.csv.CSVReader;
|
||||
import org.jooq.tools.jdbc.MockConfiguration;
|
||||
import org.jooq.tools.jdbc.MockDataProvider;
|
||||
import org.jooq.tools.reflect.Reflect;
|
||||
import org.jooq.tools.reflect.ReflectException;
|
||||
|
||||
@ -299,7 +301,7 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX Transaction API
|
||||
// XXX APIs for creating scope for transactions, mocking, batching, etc.
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
@ -351,6 +353,30 @@ public class DefaultDSLContext extends AbstractScope implements DSLContext, Seri
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T mockResult(MockDataProvider provider, TransactionalCallable<T> mockable) {
|
||||
try {
|
||||
return mockable.run(new MockConfiguration(configuration, provider));
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (Exception cause) {
|
||||
throw new DataAccessException("Mock failed", cause);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mock(final MockDataProvider provider, final TransactionalRunnable mockable) {
|
||||
mockResult(provider, new TransactionalCallable<Void>() {
|
||||
@Override
|
||||
public Void run(Configuration c) throws Exception {
|
||||
mockable.run(c);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// XXX RenderContext and BindContext accessors
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
62
jOOQ/src/main/java/org/jooq/tools/jdbc/MockCallable.java
Normal file
62
jOOQ/src/main/java/org/jooq/tools/jdbc/MockCallable.java
Normal file
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq.tools.jdbc;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
|
||||
/**
|
||||
* An <code>FunctionalInterface</code> that wraps mockable code.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface MockCallable<T> {
|
||||
|
||||
/**
|
||||
* Run the mockable code.
|
||||
*
|
||||
* @param configuration The {@link MockConfiguration} that executes all
|
||||
* queries using a {@link MockConnection} and the supplied
|
||||
* {@link MockDataProvider}.
|
||||
* @return The outcome of the mockable code.
|
||||
* @throws Exception Any exception.
|
||||
*/
|
||||
T run(Configuration configuration) throws Exception;
|
||||
}
|
||||
60
jOOQ/src/main/java/org/jooq/tools/jdbc/MockRunnable.java
Normal file
60
jOOQ/src/main/java/org/jooq/tools/jdbc/MockRunnable.java
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
* This work is dual-licensed
|
||||
* - under the Apache Software License 2.0 (the "ASL")
|
||||
* - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
|
||||
* =============================================================================
|
||||
* You may choose which license applies to you:
|
||||
*
|
||||
* - If you're using this work with Open Source databases, you may choose
|
||||
* either ASL or jOOQ License.
|
||||
* - If you're using this work with at least one commercial database, you must
|
||||
* choose jOOQ License
|
||||
*
|
||||
* For more information, please visit http://www.jooq.org/licenses
|
||||
*
|
||||
* Apache Software License 2.0:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* jOOQ License and Maintenance Agreement:
|
||||
* -----------------------------------------------------------------------------
|
||||
* Data Geekery grants the Customer the non-exclusive, timely limited and
|
||||
* non-transferable license to install and use the Software under the terms of
|
||||
* the jOOQ License and Maintenance Agreement.
|
||||
*
|
||||
* This library is distributed with a LIMITED WARRANTY. See the jOOQ License
|
||||
* and Maintenance Agreement for more details: http://www.jooq.org/licensing
|
||||
*/
|
||||
package org.jooq.tools.jdbc;
|
||||
|
||||
import org.jooq.Configuration;
|
||||
|
||||
/**
|
||||
* An <code>FunctionalInterface</code> that wraps mockable code.
|
||||
*
|
||||
* @author Lukas Eder
|
||||
*/
|
||||
public interface MockRunnable {
|
||||
|
||||
/**
|
||||
* Run the mockable code.
|
||||
* @param configuration The {@link MockConfiguration} that executes all
|
||||
* queries using a {@link MockConnection} and the supplied
|
||||
* {@link MockDataProvider}.
|
||||
* @throws Exception Any exception.
|
||||
*/
|
||||
void run(Configuration configuration) throws Exception;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user