Skip to content

Commit

Permalink
Add support for JUnit 5. (#2166)
Browse files Browse the repository at this point in the history
* 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
AnDyXX authored and arteam committed Nov 21, 2017
1 parent 7f7b855 commit 450e23e
Show file tree
Hide file tree
Showing 46 changed files with 1,948 additions and 395 deletions.
29 changes: 28 additions & 1 deletion dropwizard-testing/pom.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>


<parent> <parent>
Expand Down Expand Up @@ -33,6 +34,13 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
Expand Down Expand Up @@ -68,4 +76,23 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project> </project>
@@ -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;
});
}
}
@@ -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);
}
}
}
}
}
@@ -1,4 +1,4 @@
package io.dropwizard.testing.junit; package io.dropwizard.testing.common;


import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.MetricRegistry;
import io.dropwizard.jersey.DropwizardResourceConfig; import io.dropwizard.jersey.DropwizardResourceConfig;
Expand Down

0 comments on commit 450e23e

Please sign in to comment.