From 43b8ddbafedd21ca22a47e26aa5a56c111d393f1 Mon Sep 17 00:00:00 2001 From: Robin Karlsson Date: Sun, 17 Nov 2019 19:41:04 +0100 Subject: [PATCH] Overridable TestContainerFactory and DeploymentContext (#10) Make it possible to override TestContainerFactory and DeploymentContext. For tests where an Application and/or the regular TestContainerFactory isn't enough. --- .../com/github/hanleyt/JerseyExtension.java | 44 +++++++++++++------ .../github/hanleyt/JerseyExtensionTest.java | 37 ++++++++++++++++ 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/hanleyt/JerseyExtension.java b/src/main/java/com/github/hanleyt/JerseyExtension.java index 381d15b..b6617be 100644 --- a/src/main/java/com/github/hanleyt/JerseyExtension.java +++ b/src/main/java/com/github/hanleyt/JerseyExtension.java @@ -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; @@ -22,7 +25,8 @@ public class JerseyExtension implements BeforeEachCallback, AfterEachCallback, P private static final Collection> INJECTABLE_PARAMETER_TYPES = Arrays.asList(Client.class, WebTarget.class, URI.class); - private final Function applicationProvider; + private final Function testContainerFactoryProvider; + private final Function deploymentContextProvider; private final BiFunction configProvider; private JerseyExtension() { @@ -30,22 +34,28 @@ private JerseyExtension() { } public JerseyExtension(Supplier applicationSupplier) { - this.applicationProvider = (unused) -> applicationSupplier.get(); - this.configProvider = null; + this((unused) -> applicationSupplier.get(), null); } - public JerseyExtension(Supplier applicationSupplier, BiFunction configProvider) { - this.applicationProvider = (unused) -> applicationSupplier.get(); - this.configProvider = configProvider; + public JerseyExtension(Supplier applicationSupplier, + BiFunction configProvider) { + this((unused) -> applicationSupplier.get(), configProvider); } public JerseyExtension(Function applicationProvider) { - this.applicationProvider = applicationProvider; - this.configProvider = null; + this(applicationProvider, null); + } + + public JerseyExtension(Function applicationProvider, + BiFunction configProvider) { + this(null, (context) -> DeploymentContext.builder(applicationProvider.apply(context)).build(), configProvider); } - public JerseyExtension(Function applicationProvider, BiFunction configProvider) { - this.applicationProvider = applicationProvider; + public JerseyExtension(Function testContainerFactoryProvider, + Function deploymentContextProvider, + BiFunction configProvider) { + this.testContainerFactoryProvider = testContainerFactoryProvider; + this.deploymentContextProvider = deploymentContextProvider; this.configProvider = configProvider; } @@ -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 @@ -100,4 +118,4 @@ public static ExtensionContext.Store getStore(ExtensionContext context) { return context.getStore(ExtensionContext.Namespace.GLOBAL); } -} \ No newline at end of file +} diff --git a/src/test/java/com/github/hanleyt/JerseyExtensionTest.java b/src/test/java/com/github/hanleyt/JerseyExtensionTest.java index e035865..aced74c 100644 --- a/src/test/java/com/github/hanleyt/JerseyExtensionTest.java +++ b/src/test/java/com/github/hanleyt/JerseyExtensionTest.java @@ -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; @@ -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 {