Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.io.Closeable;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.CloseableService;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* You need at least the following two dependencies to run the test
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
* <version>2.30</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.assertj</groupId>
* <artifactId>assertj-core</artifactId>
* <version>3.15.0</version>
* <scope>test</scope>
* </dependency>
*/
public class ClosableServiceTest extends JerseyTest {
private static final String CONNECTION_NAME = "Connect-Name";
@Override
public ResourceConfig configure() {
return new ResourceConfig()
.register(TestResource.class)
.register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME),
Level.INFO, LoggingFeature.Verbosity.PAYLOAD_ANY, 10000))
.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ConnectionFactory.class)
.to(Connection.class)
.in(RequestScoped.class);
}
});
}
@After
@Before
public void resetConnectionHolder() {
ConnectionTestHolder.resetConnection();
}
@Test
public void testConnectionClosed() {
Response res = target("test").request().get();
assertThat(res.getStatus()).isEqualTo(200);
assertThat(res.readEntity(String.class)).isEqualTo(CONNECTION_NAME);
Connection connection = ConnectionTestHolder.getConnection();
verify(connection, times(1)).close();
}
@Path("test")
public static class TestResource {
@Inject
private Connection connection;
@GET
@Produces("text/plain")
public String getConnection() {
return connection.getConnectionName();
}
}
public interface Connection extends Closeable {
String getConnectionName();
void close();
}
public static class ConnectionFactory implements Supplier<Connection> {
private final CloseableService closeables;
@Inject
public ConnectionFactory(CloseableService closeables) {
this.closeables = closeables;
}
@Override
public Connection get() {
Connection connection = mock(Connection.class);
when(connection.getConnectionName()).thenReturn(CONNECTION_NAME);
closeables.add(connection);
ConnectionTestHolder.setConnection(connection);
return connection;
}
}
private static class ConnectionTestHolder {
private static Connection _connection;
private ConnectionTestHolder() {}
static void setConnection(Connection connection) {
_connection = connection;
}
static Connection getConnection() {
if (_connection == null) {
throw new IllegalStateException("No Connection set yet.");
}
return _connection;
}
static ConnectionTestHolder resetConnection() {
setConnection(null);
return new ConnectionTestHolder();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.