Skip to content

Commit

Permalink
Overridable TestContainerFactory and DeploymentContext (#10)
Browse files Browse the repository at this point in the history
Make it possible to override TestContainerFactory
and DeploymentContext. For tests where an Application
and/or the regular TestContainerFactory isn't enough.
  • Loading branch information
snago authored and hanleyt committed Nov 17, 2019
1 parent 5b4c67f commit 43b8ddb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/main/java/com/github/hanleyt/JerseyExtension.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.hanleyt;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -22,30 +25,37 @@ public class JerseyExtension implements BeforeEachCallback, AfterEachCallback, P

private static final Collection<Class<?>> INJECTABLE_PARAMETER_TYPES = Arrays.asList(Client.class, WebTarget.class, URI.class);

private final Function<ExtensionContext, Application> applicationProvider;
private final Function<ExtensionContext, TestContainerFactory> testContainerFactoryProvider;
private final Function<ExtensionContext, DeploymentContext> deploymentContextProvider;
private final BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider;

private JerseyExtension() {
throw new IllegalStateException("JerseyExtension must be registered programmatically");
}

public JerseyExtension(Supplier<Application> applicationSupplier) {
this.applicationProvider = (unused) -> applicationSupplier.get();
this.configProvider = null;
this((unused) -> applicationSupplier.get(), null);
}

public JerseyExtension(Supplier<Application> applicationSupplier, BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider) {
this.applicationProvider = (unused) -> applicationSupplier.get();
this.configProvider = configProvider;
public JerseyExtension(Supplier<Application> applicationSupplier,
BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider) {
this((unused) -> applicationSupplier.get(), configProvider);
}

public JerseyExtension(Function<ExtensionContext, Application> applicationProvider) {
this.applicationProvider = applicationProvider;
this.configProvider = null;
this(applicationProvider, null);
}

public JerseyExtension(Function<ExtensionContext, Application> applicationProvider,
BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider) {
this(null, (context) -> DeploymentContext.builder(applicationProvider.apply(context)).build(), configProvider);
}

public JerseyExtension(Function<ExtensionContext, Application> applicationProvider, BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider) {
this.applicationProvider = applicationProvider;
public JerseyExtension(Function<ExtensionContext, TestContainerFactory> testContainerFactoryProvider,
Function<ExtensionContext, DeploymentContext> deploymentContextProvider,
BiFunction<ExtensionContext, ClientConfig, ClientConfig> configProvider) {
this.testContainerFactoryProvider = testContainerFactoryProvider;
this.deploymentContextProvider = deploymentContextProvider;
this.configProvider = configProvider;
}

Expand All @@ -60,8 +70,16 @@ public void beforeEach(ExtensionContext context) throws Exception {
private JerseyTest initJerseyTest(ExtensionContext context) throws Exception {
JerseyTest jerseyTest = new JerseyTest() {
@Override
protected Application configure() {
return applicationProvider.apply(context);
protected DeploymentContext configureDeployment() {
return deploymentContextProvider.apply(context);
}

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
if (testContainerFactoryProvider != null) {
return testContainerFactoryProvider.apply(context);
}
return super.getTestContainerFactory();
}

@Override
Expand Down Expand Up @@ -100,4 +118,4 @@ public static ExtensionContext.Store getStore(ExtensionContext context) {
return context.getStore(ExtensionContext.Namespace.GLOBAL);
}

}
}
37 changes: 37 additions & 0 deletions src/test/java/com/github/hanleyt/JerseyExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -184,6 +189,38 @@ void client_is_injected(Client client, URI baseUri) {
}
}

@Nested
@DisplayName("when registered and configured with a TestContainerFactory and DeploymentContext.")
class DeploymentContextApp {

private final TestContainerFactory testContainerFactory = new GrizzlyWebTestContainerFactory();
private final ResourceConfig resourceConfig = new ResourceConfig().register(new DummyResource());
private final DeploymentContext deploymentContext = ServletDeploymentContext.builder(resourceConfig)
.servlet(new ServletContainer(resourceConfig)).servletPath("foo").build();

@RegisterExtension
JerseyExtension jerseyExtension = new JerseyExtension(this::getTestContainerFactory, this::configureDeploymentContext, null);

TestContainerFactory getTestContainerFactory(ExtensionContext extensionContext) {
assertNotNull(extensionContext);
return testContainerFactory;
}

DeploymentContext configureDeploymentContext(ExtensionContext extensionContext) {
assertNotNull(extensionContext);
return deploymentContext;
}

@Test
@DisplayName("access the resource using the injected Client and URI")
void client_is_injected(Client client, URI baseUri) {
assertNotNull(client);
String values = client.target(baseUri).path("values").request().get(String.class);
assertEquals(DummyResource.DEFAULT_VALUES, values);
}

}


private static class ExtensionNeededToConfigureJersey implements BeforeEachCallback, AfterEachCallback {

Expand Down

0 comments on commit 43b8ddb

Please sign in to comment.