Skip to content

Commit

Permalink
Fix excluded tests for Weld injection in Jetty 12 ee9/10. Add asserti…
Browse files Browse the repository at this point in the history
…on that servlets can be injected into (#198)
  • Loading branch information
manovotn authored Apr 26, 2024
1 parent a86f597 commit d611907
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 36 deletions.
9 changes: 6 additions & 3 deletions jetty-embedded-12-ee10/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<description>Jetty Embedded 12.0.x container integration for the Arquillian project</description>

<properties>
<!-- when upgrading check if this fix JettyEmbeddedInContainerTestCase#shouldBeAbleToInjectMembersIntoTestClass -->
<version.weld>5.1.0.Final</version.weld>
<version.weld>5.1.2.Final</version.weld>
<jakarta.servlet.api.version>6.0.0</jakarta.servlet.api.version>

<!-- Jetty 12 need Java 17 -->
Expand Down Expand Up @@ -147,6 +146,11 @@
<artifactId>alpn-api</artifactId>
<version>1.1.3.v20160715</version>
</dependency>
<!-- Needed for Jetty be able to perform injection into servlets/filters etc -->
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-cdi</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
Expand Down Expand Up @@ -237,7 +241,6 @@
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppLifeCycle;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.ee10.cdi.CdiDecoratingListener;
import org.eclipse.jetty.ee10.cdi.CdiServletContainerInitializer;
import org.eclipse.jetty.http.CookieCompliance;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
Expand Down Expand Up @@ -253,6 +255,15 @@ public ProtocolMetaData deploy(final Archive<?> archive) throws DeploymentExcept
deployer.removeApp(app);
WebAppContext webAppContext = getWebAppContext(app);

// Jetty setup telling Jetty's CdiDecoratingListener how to operate.
webAppContext.setInitParameter(CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE, CdiDecoratingListener.MODE);
// jetty setup for layer between Weld and Jetty.
webAppContext.addServletContainerInitializer(new CdiServletContainerInitializer());
// Weld's org.jboss.weld.environment.servlet.EnhancedListener can be discovered automatically
// However, it won't happen if jetty-ee10-annotations JAR isn't present in runtime, hence we add it explicitly
// The listener will start up Weld container so long as there is an archive with any beans in it
webAppContext.addServletContainerInitializer(new org.jboss.weld.environment.servlet.EnhancedListener());

if (containerConfig.areMimeTypesSet()) {
containerConfig.getMimeTypes().forEach((s, s2) -> webAppContext.getMimeTypes().addMimeMapping(s, s2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -59,15 +62,17 @@ public class JettyEmbeddedClientTestCase {
@Deployment(testable = false)
public static WebArchive getTestArchive() {
return ShrinkWrap.create(WebArchive.class, "client-http.war")
.addClass(MyServlet.class)
.addClass(MyOtherServlet.class)
.addClass(MyOtherBean.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.setWebXML(new StringAsset(Descriptors.create(WebAppDescriptor.class)
.version("4.0")
.createServlet()
.servletClass(MyServlet.class.getName())
.servletName("MyServlet").up()
.servletClass(MyOtherServlet.class.getName())
.servletName("MyOtherServlet").up()
.createServletMapping()
.servletName("MyServlet")
.urlPattern(MyServlet.URL_PATTERN).up()
.servletName("MyOtherServlet")
.urlPattern(MyOtherServlet.URL_PATTERN).up()
.exportAsString()));
}

Expand Down Expand Up @@ -119,12 +124,12 @@ public void shutdown() throws Exception {
@Test
public void shouldBeAbleToInvokeServletInDeployedWebApp() throws Exception {

String body = httpClient.GET(new URL(url, MyServlet.URL_PATTERN).toURI()).getContentAsString();
String body = httpClient.GET(new URL(url, MyOtherServlet.URL_PATTERN).toURI()).getContentAsString();

assertThat(
"Verify that the servlet was deployed and returns expected result",
body,
Matchers.is(MyServlet.MESSAGE));
Matchers.is(MyOtherServlet.MESSAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ public void shouldBeAbleToInjectMembersIntoTestClass() throws Exception {
try (Connection c = ds.getConnection()) {
assertThat(c.getMetaData().getDatabaseProductName(), is("H2"));
}
// FIXME not working anymore with weld 5.1.0.Final
// assertThat(testBean, notNullValue());
// assertThat(testBean.getName(), is("Jetty"));
assertThat(testBean, notNullValue());
assertThat(testBean.getName(), is("Jetty"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee10;

import jakarta.annotation.Resource;
import jakarta.enterprise.context.Dependent;

@Dependent
public class MyBean {
@Resource(name = "name")
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee10;

import jakarta.enterprise.context.Dependent;

@Dependent
public class MyOtherBean {

public String ping() {
return MyOtherBean.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee10;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import jakarta.inject.Inject;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class MyOtherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public static final String URL_PATTERN = "Test2";

public static final String MESSAGE = "hey there";

@Inject
MyOtherBean bean;

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
assertThat(bean.ping(), is(MyOtherBean.class.getSimpleName()));
response.getWriter().append(MESSAGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
<env-entry-value>Remote</env-entry-value>
</env-entry>

<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

</web-app>
11 changes: 6 additions & 5 deletions jetty-embedded-12-ee9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
<description>Jetty Embedded 12.0.x container integration for the Arquillian project</description>

<properties>
<!-- when upgrading check if this fix JettyEmbeddedInContainerTestCase#shouldBeAbleToInjectMembersIntoTestClass -->
<version.weld>5.1.0.Final</version.weld>
<!-- <jakarta.servlet.api.version>6.0.0</jakarta.servlet.api.version>-->
<version.weld>5.1.2.Final</version.weld>

<!-- Jetty 12 need Java 17 -->
<maven.compiler.target>17</maven.compiler.target>
Expand All @@ -31,7 +29,6 @@
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>${version.weld}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
Expand Down Expand Up @@ -143,6 +140,11 @@
<artifactId>alpn-api</artifactId>
<version>1.1.3.v20160715</version>
</dependency>
<!-- Needed for Jetty be able to perform injection into servlets/filters etc -->
<dependency>
<groupId>org.eclipse.jetty.ee9</groupId>
<artifactId>jetty-ee9-cdi</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
Expand Down Expand Up @@ -225,7 +227,6 @@
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.AppLifeCycle;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.eclipse.jetty.ee9.cdi.CdiDecoratingListener;
import org.eclipse.jetty.ee9.cdi.CdiServletContainerInitializer;
import org.eclipse.jetty.http.CookieCompliance;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory;
Expand Down Expand Up @@ -253,6 +255,15 @@ public ProtocolMetaData deploy(final Archive<?> archive) throws DeploymentExcept
appProvider.createContextHandler(app);
WebAppContext webAppContext = getWebAppContext(app);

// Jetty setup telling Jetty's CdiDecoratingListener how to operate.
webAppContext.setInitParameter(CdiServletContainerInitializer.CDI_INTEGRATION_ATTRIBUTE, CdiDecoratingListener.MODE);
// jetty setup for layer between Weld and Jetty.
webAppContext.addServletContainerInitializer(new CdiServletContainerInitializer());
// Weld's org.jboss.weld.environment.servlet.EnhancedListener can be discovered automatically
// However, it won't happen if jetty-ee9-annotations JAR isn't present in runtime, hence we add it explicitly
// The listener will start up Weld container so long as there is an archive with any beans in it
webAppContext.addServletContainerInitializer(new org.jboss.weld.environment.servlet.EnhancedListener());

if (containerConfig.areMimeTypesSet()) {
containerConfig.getMimeTypes().forEach((s, s2) -> webAppContext.getMimeTypes().addMimeMapping(s, s2));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
Expand Down Expand Up @@ -59,15 +60,17 @@ public class JettyEmbeddedClientTestCase {
@Deployment(testable = false)
public static WebArchive getTestArchive() {
return ShrinkWrap.create(WebArchive.class, "client-http.war")
.addClass(MyServlet.class)
.addClass(MyOtherServlet.class)
.addClass(MyOtherBean.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.setWebXML(new StringAsset(Descriptors.create(WebAppDescriptor.class)
.version("4.0")
.createServlet()
.servletClass(MyServlet.class.getName())
.servletName("MyServlet").up()
.servletClass(MyOtherServlet.class.getName())
.servletName("MyOtherServlet").up()
.createServletMapping()
.servletName("MyServlet")
.urlPattern(MyServlet.URL_PATTERN).up()
.servletName("MyOtherServlet")
.urlPattern(MyOtherServlet.URL_PATTERN).up()
.exportAsString()));
}

Expand Down Expand Up @@ -119,12 +122,12 @@ public void shutdown() throws Exception {
@Test
public void shouldBeAbleToInvokeServletInDeployedWebApp() throws Exception {

String body = httpClient.GET(new URL(url, MyServlet.URL_PATTERN).toURI()).getContentAsString();
String body = httpClient.GET(new URL(url, MyOtherServlet.URL_PATTERN).toURI()).getContentAsString();

assertThat(
"Verify that the servlet was deployed and returns expected result",
body,
Matchers.is(MyServlet.MESSAGE));
Matchers.is(MyOtherServlet.MESSAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public void shouldBeAbleToInjectMembersIntoTestClass() throws Exception {
try (Connection c = ds.getConnection()) {
assertThat(c.getMetaData().getDatabaseProductName(), is("H2"));
}
// FIXME not working anymore with weld 5.1.0.Final
// assertThat(testBean, notNullValue());
// assertThat(testBean.getName(), is("Jetty"));
assertThat(testBean, notNullValue());
assertThat(testBean.getName(), is("Jetty"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee9;

import jakarta.annotation.Resource;
import jakarta.enterprise.context.Dependent;

@Dependent
public class MyBean {
@Resource(name = "name")
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee9;

import jakarta.enterprise.context.Dependent;

@Dependent
public class MyOtherBean {

public String ping() {
return MyOtherBean.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jboss.arquillian.container.jetty.embedded_12_ee9;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import jakarta.inject.Inject;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class MyOtherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public static final String URL_PATTERN = "Test2";

public static final String MESSAGE = "hey there";

@Inject
MyOtherBean bean;

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
assertThat(bean.ping(), is(MyOtherBean.class.getSimpleName()));
response.getWriter().append(MESSAGE);
}
}
4 changes: 0 additions & 4 deletions jetty-embedded-12-ee9/src/test/resources/in-container-web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
<env-entry-value>Remote</env-entry-value>
</env-entry>

<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

</web-app>

0 comments on commit d611907

Please sign in to comment.