Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* code of original test rules was changed to support executing them as JUnit 5 extensions, * maven-surefire-plugin version downgraded to 2.19.1 to work with JUnit 5, * code of original tests copied and modified to use JUnit 5.
- Loading branch information
Showing
46 changed files
with
1,948 additions
and
395 deletions.
There are no files selected for viewing
This file contains 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
174 changes: 174 additions & 0 deletions
174
dropwizard-testing/src/main/java/io/dropwizard/testing/common/DAOTest.java
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,174 @@ | |||
package io.dropwizard.testing.common; | |||
|
|||
import com.google.common.base.Throwables; | |||
import io.dropwizard.logging.BootstrapLogging; | |||
import io.dropwizard.testing.junit.DAOTestRule; | |||
import org.hibernate.Session; | |||
import org.hibernate.SessionFactory; | |||
import org.hibernate.Transaction; | |||
import org.hibernate.cfg.AvailableSettings; | |||
import org.hibernate.cfg.Configuration; | |||
import org.hibernate.context.internal.ManagedSessionContext; | |||
|
|||
import java.util.HashMap; | |||
import java.util.LinkedHashSet; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import java.util.UUID; | |||
import java.util.concurrent.Callable; | |||
|
|||
public class DAOTest { | |||
static { | |||
BootstrapLogging.bootstrap(); | |||
} | |||
|
|||
public static abstract class Builder<B extends Builder> { | |||
private String url = "jdbc:h2:mem:" + UUID.randomUUID(); | |||
private String username = "sa"; | |||
private String password = ""; | |||
private String driver = "org.h2.Driver"; | |||
private String hbm2ddlAuto = "create"; | |||
private boolean showSql = false; | |||
private boolean useSqlComments = false; | |||
private Set<Class<?>> entityClasses = new LinkedHashSet<>(); | |||
private Map<String, String> properties = new HashMap<>(); | |||
|
|||
public B setUrl(String url) { | |||
this.url = url; | |||
return (B) this; | |||
} | |||
|
|||
public B setUsername(String username) { | |||
this.username = username; | |||
return (B) this; | |||
} | |||
|
|||
public B setDriver(Class<? extends java.sql.Driver> driver) { | |||
this.driver = driver.getName(); | |||
return (B) this; | |||
} | |||
|
|||
public B setHbm2DdlAuto(String hbm2ddlAuto) { | |||
this.hbm2ddlAuto = hbm2ddlAuto; | |||
return (B) this; | |||
} | |||
|
|||
public B setShowSql(boolean showSql) { | |||
this.showSql = showSql; | |||
return (B) this; | |||
} | |||
|
|||
public B useSqlComments(boolean useSqlComments) { | |||
this.useSqlComments = useSqlComments; | |||
return (B) this; | |||
} | |||
|
|||
public B addEntityClass(Class<?> entityClass) { | |||
this.entityClasses.add(entityClass); | |||
return (B) this; | |||
} | |||
|
|||
public B setProperty(String key, String value) { | |||
this.properties.put(key, value); | |||
return (B) this; | |||
} | |||
|
|||
protected DAOTest buildDAOTest() { | |||
final Configuration config = new Configuration(); | |||
config.setProperty(AvailableSettings.URL, url); | |||
config.setProperty(AvailableSettings.USER, username); | |||
config.setProperty(AvailableSettings.PASS, password); | |||
config.setProperty(AvailableSettings.DRIVER, driver); | |||
config.setProperty(AvailableSettings.HBM2DDL_AUTO, hbm2ddlAuto); | |||
config.setProperty(AvailableSettings.SHOW_SQL, String.valueOf(showSql)); | |||
config.setProperty(AvailableSettings.USE_SQL_COMMENTS, String.valueOf(useSqlComments)); | |||
// Use the same configuration as in the Hibernate bundle to reduce differences between | |||
// testing and production environments. | |||
config.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed"); | |||
config.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true"); | |||
config.setProperty(AvailableSettings.GENERATE_STATISTICS, "true"); | |||
config.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true"); | |||
config.setProperty(AvailableSettings.ORDER_UPDATES, "true"); | |||
config.setProperty(AvailableSettings.ORDER_INSERTS, "true"); | |||
config.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true"); | |||
config.setProperty("jadira.usertype.autoRegisterUserTypes", "true"); | |||
|
|||
entityClasses.forEach(config::addAnnotatedClass); | |||
properties.forEach(config::setProperty); | |||
|
|||
return new DAOTest(config.buildSessionFactory()); | |||
} | |||
} | |||
|
|||
private final SessionFactory sessionFactory; | |||
|
|||
/** | |||
* Use {@link DAOTestRule#newBuilder()} | |||
*/ | |||
private DAOTest(SessionFactory sessionFactory) { | |||
this.sessionFactory = sessionFactory; | |||
} | |||
|
|||
public void before() throws Throwable { | |||
if (ManagedSessionContext.hasBind(sessionFactory)) { | |||
return; | |||
} | |||
|
|||
final Session session = sessionFactory.openSession(); | |||
ManagedSessionContext.bind(session); | |||
} | |||
|
|||
public void after() { | |||
if (!ManagedSessionContext.hasBind(sessionFactory)) { | |||
return; | |||
} | |||
|
|||
final Session currentSession = sessionFactory.getCurrentSession(); | |||
if (currentSession.isOpen()) { | |||
currentSession.close(); | |||
} | |||
ManagedSessionContext.unbind(sessionFactory); | |||
} | |||
|
|||
/** | |||
* Returns the current active session factory for injecting to DAOs. | |||
*` | |||
* @return {@link SessionFactory} with an open session. | |||
*/ | |||
public SessionFactory getSessionFactory() { | |||
return sessionFactory; | |||
} | |||
|
|||
/** | |||
* Performs a call in a transaction | |||
* | |||
* @param call the call | |||
* @param <T> the type of the returned result | |||
* @return the result of the call | |||
*/ | |||
public <T> T inTransaction(Callable<T> call) { | |||
final Session session = sessionFactory.getCurrentSession(); | |||
final Transaction transaction = session.beginTransaction(); | |||
try { | |||
final T result = call.call(); | |||
transaction.commit(); | |||
return result; | |||
} catch (final Exception e) { | |||
transaction.rollback(); | |||
Throwables.throwIfUnchecked(e); | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
|
|||
/** | |||
* Performs an action in a transaction | |||
* | |||
* @param action the action | |||
*/ | |||
public void inTransaction(Runnable action) { | |||
inTransaction(() -> { | |||
action.run(); | |||
return true; | |||
}); | |||
} | |||
} |
74 changes: 74 additions & 0 deletions
74
dropwizard-testing/src/main/java/io/dropwizard/testing/common/DropwizardClient.java
This file contains 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,74 @@ | |||
package io.dropwizard.testing.common; | |||
|
|||
import com.codahale.metrics.health.HealthCheck; | |||
import com.fasterxml.jackson.databind.ObjectMapper; | |||
import io.dropwizard.Application; | |||
import io.dropwizard.Configuration; | |||
import io.dropwizard.jetty.HttpConnectorFactory; | |||
import io.dropwizard.server.SimpleServerFactory; | |||
import io.dropwizard.setup.Environment; | |||
import io.dropwizard.testing.DropwizardTestSupport; | |||
|
|||
import java.net.URI; | |||
|
|||
public class DropwizardClient { | |||
private final Object[] resources; | |||
private final DropwizardTestSupport<Configuration> testSupport; | |||
|
|||
public DropwizardClient(Object... resources) { | |||
testSupport = new DropwizardTestSupport<Configuration>(FakeApplication.class, "") { | |||
@Override | |||
public Application<Configuration> newApplication() { | |||
return new FakeApplication(); | |||
} | |||
}; | |||
this.resources = resources; | |||
} | |||
|
|||
public URI baseUri() { | |||
return URI.create("http://localhost:" + testSupport.getLocalPort() + "/application"); | |||
} | |||
|
|||
public ObjectMapper getObjectMapper() { | |||
return testSupport.getObjectMapper(); | |||
} | |||
|
|||
public Environment getEnvironment() { | |||
return testSupport.getEnvironment(); | |||
} | |||
|
|||
public void before() throws Throwable { | |||
testSupport.before(); | |||
} | |||
|
|||
public void after() { | |||
testSupport.after(); | |||
} | |||
|
|||
private static class DummyHealthCheck extends HealthCheck { | |||
@Override | |||
protected Result check() { | |||
return Result.healthy(); | |||
} | |||
} | |||
|
|||
private class FakeApplication extends Application<Configuration> { | |||
@Override | |||
public void run(Configuration configuration, Environment environment) { | |||
final SimpleServerFactory serverConfig = new SimpleServerFactory(); | |||
configuration.setServerFactory(serverConfig); | |||
final HttpConnectorFactory connectorConfig = (HttpConnectorFactory) serverConfig.getConnector(); | |||
connectorConfig.setPort(0); | |||
|
|||
environment.healthChecks().register("dummy", new DummyHealthCheck()); | |||
|
|||
for (Object resource : resources) { | |||
if (resource instanceof Class<?>) { | |||
environment.jersey().register((Class<?>) resource); | |||
} else { | |||
environment.jersey().register(resource); | |||
} | |||
} | |||
} | |||
} | |||
} |
2 changes: 1 addition & 1 deletion
2
...g/junit/DropwizardTestResourceConfig.java → .../common/DropwizardTestResourceConfig.java
This file contains 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
Oops, something went wrong.