From 715bbbb6a27cb88d5f8f5510a6afdbddac265fd9 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 5 Jun 2019 08:07:32 -0500 Subject: [PATCH] Issue #3731 - Adding CDI2 tests to test-distribution Signed-off-by: Joakim Erdfelt --- tests/test-distribution/pom.xml | 7 + .../distribution/DistributionTester.java | 4 +- .../jetty/tests/distribution/CDITests.java | 138 ++++++++++++++++++ .../src/test/resources/cdi/demo_context.xml | 24 +++ tests/test-webapps/test-cdi2-webapp/pom.xml | 18 +-- 5 files changed, 175 insertions(+), 16 deletions(-) create mode 100644 tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/CDITests.java create mode 100644 tests/test-distribution/src/test/resources/cdi/demo_context.xml diff --git a/tests/test-distribution/pom.xml b/tests/test-distribution/pom.xml index c26af378a94..9850dfba808 100644 --- a/tests/test-distribution/pom.xml +++ b/tests/test-distribution/pom.xml @@ -75,6 +75,13 @@ war test + + org.eclipse.jetty.tests + test-cdi2-webapp + ${project.version} + war + test + org.eclipse.jetty.toolchain jetty-test-helper diff --git a/tests/test-distribution/src/main/java/org/eclipse/jetty/tests/distribution/DistributionTester.java b/tests/test-distribution/src/main/java/org/eclipse/jetty/tests/distribution/DistributionTester.java index 27724f8b436..e0ee4502676 100644 --- a/tests/test-distribution/src/main/java/org/eclipse/jetty/tests/distribution/DistributionTester.java +++ b/tests/test-distribution/src/main/java/org/eclipse/jetty/tests/distribution/DistributionTester.java @@ -191,15 +191,17 @@ public class DistributionTester * * @param warFile the war file to install * @param context the context path + * @return the path to the installed webapp exploded directory * @throws IOException if the installation fails */ - public void installWarFile(File warFile, String context) throws IOException + public Path installWarFile(File warFile, String context) throws IOException { //webapps Path webapps = config.jettyBase.resolve("webapps").resolve(context); if (!Files.exists(webapps)) Files.createDirectories(webapps); unzip(warFile, webapps.toFile()); + return webapps; } /** diff --git a/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/CDITests.java b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/CDITests.java new file mode 100644 index 00000000000..c614957b08e --- /dev/null +++ b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/CDITests.java @@ -0,0 +1,138 @@ +package org.eclipse.jetty.tests.distribution; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.http.HttpStatus; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class CDITests extends AbstractDistributionTest +{ + /** + * Tests a WAR file that is CDI complete as it includes the weld + * library in its WEB-INF/lib directory. + * + * @throws Exception + */ + @Test + public void testCDI2_IncludedInWebapp() throws Exception + { + String jettyVersion = System.getProperty("jettyVersion"); + DistributionTester distribution = DistributionTester.Builder.newInstance() + .jettyVersion(jettyVersion) + .mavenLocalRepository(System.getProperty("mavenRepoPath")) + .build(); + + String[] args1 = { + "--create-startd", + "--approve-all-licenses", + "--add-to-start=http,deploy,annotations,jsp" + }; + try (DistributionTester.Run run1 = distribution.start(args1)) + { + assertTrue(run1.awaitFor(5, TimeUnit.SECONDS)); + assertEquals(0, run1.getExitValue()); + + File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-cdi2-webapp:war:" + jettyVersion); + distribution.installWarFile(war, "demo"); + + distribution.installBaseResource("cdi/demo_context.xml", "webapps/demo.xml"); + + int port = distribution.freePort(); + try (DistributionTester.Run run2 = distribution.start("jetty.http.port=" + port)) + { + assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS)); + + startHttpClient(); + ContentResponse response = client.GET("http://localhost:" + port + "/demo/greetings"); + assertEquals(HttpStatus.OK_200, response.getStatus()); + // Confirm Servlet based CDI + assertThat(response.getContentAsString(), containsString("Hello GreetingsServlet")); + // Confirm Listener based CDI (this has been a problem in the past, keep this for regression testing!) + assertThat(response.getHeaders().get("Server"), containsString("CDI-Demo-org.eclipse.jetty.test")); + + run2.stop(); + assertTrue(run2.awaitFor(5, TimeUnit.SECONDS)); + } + } + } + + /** + * Tests a WAR file that is expects CDI to be provided by the server. + * + *

+ * This means the WAR does NOT have the weld libs in its + * WEB-INF/lib directory. + *

+ * + *

+ * The expectation is that CDI2 is provided by weld + * and the javax.el support comes from JSP + *

+ * + * @throws Exception + */ + @Test + public void testCDI2_ProvidedByServer() throws Exception + { + String jettyVersion = System.getProperty("jettyVersion"); + DistributionTester distribution = DistributionTester.Builder.newInstance() + .jettyVersion(jettyVersion) + .mavenLocalRepository(System.getProperty("mavenRepoPath")) + .build(); + + String[] args1 = { + "--create-startd", + "--approve-all-licenses", + // standard entries + "--add-to-start=http,deploy,annotations", + // cdi2 specific entry (should transitively pull in what it needs, the user should not be expected to know the transitive entries) + "--add-to-start=cdi2" + }; + try (DistributionTester.Run run1 = distribution.start(args1)) + { + assertTrue(run1.awaitFor(5, TimeUnit.SECONDS)); + assertEquals(0, run1.getExitValue()); + + File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-cdi2-webapp:war:" + jettyVersion); + Path demoDir = distribution.installWarFile(war, "demo"); + // Remove weld libs + Path libDir = demoDir.resolve("WEB-INF/lib"); + List weldLibs = Files.list(libDir).filter((path) -> path.getFileName().toString().contains("weld")) + .collect(Collectors.toList()); + for (Path weldLib : weldLibs) + { + assertTrue(Files.deleteIfExists(weldLib)); + } + + distribution.installBaseResource("cdi/demo_context.xml", "webapps/demo.xml"); + + int port = distribution.freePort(); + try (DistributionTester.Run run2 = distribution.start("jetty.http.port=" + port)) + { + assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS)); + + startHttpClient(); + ContentResponse response = client.GET("http://localhost:" + port + "/demo/greetings"); + assertEquals(HttpStatus.OK_200, response.getStatus()); + // Confirm Servlet based CDI + assertThat(response.getContentAsString(), containsString("Hello GreetingsServlet")); + // Confirm Listener based CDI (this has been a problem in the past, keep this for regression testing!) + assertThat(response.getHeaders().get("Server"), containsString("CDI-Demo-org.eclipse.jetty.test")); + + run2.stop(); + assertTrue(run2.awaitFor(5, TimeUnit.SECONDS)); + } + } + } +} diff --git a/tests/test-distribution/src/test/resources/cdi/demo_context.xml b/tests/test-distribution/src/test/resources/cdi/demo_context.xml new file mode 100644 index 00000000000..ed6a36f114e --- /dev/null +++ b/tests/test-distribution/src/test/resources/cdi/demo_context.xml @@ -0,0 +1,24 @@ + + + + + /demo + /demo/ + + + -org.eclipse.jetty.util.Decorator + + + -org.eclipse.jetty.util.DecoratedObjectFactory + + + -org.eclipse.jetty.server.handler.ContextHandler. + + + -org.eclipse.jetty.server.handler.ContextHandler + + + -org.eclipse.jetty.servlet.ServletContextHandler + + + diff --git a/tests/test-webapps/test-cdi2-webapp/pom.xml b/tests/test-webapps/test-cdi2-webapp/pom.xml index b5c53caa3c6..dec9d245562 100644 --- a/tests/test-webapps/test-cdi2-webapp/pom.xml +++ b/tests/test-webapps/test-cdi2-webapp/pom.xml @@ -29,31 +29,19 @@ javax.el javax.el-api + 3.0.0 - + org.jboss.weld.servlet weld-servlet ${weld.version} - - - org.eclipse.jetty - jetty-annotations - ${project.version} - test - - - org.eclipse.jetty - jetty-client - ${project.version} - test -