Issue #3731 - Adding CDI2 tests to test-distribution

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-06-05 08:07:32 -05:00
parent 85566d0377
commit 715bbbb6a2
5 changed files with 175 additions and 16 deletions

View File

@ -75,6 +75,13 @@
<type>war</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>test-cdi2-webapp</artifactId>
<version>${project.version}</version>
<type>war</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>

View File

@ -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;
}
/**

View File

@ -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.
*
* <p>
* This means the WAR does NOT have the weld libs in its
* WEB-INF/lib directory.
* </p>
*
* <p>
* The expectation is that CDI2 is provided by weld
* and the javax.el support comes from JSP
* </p>
*
* @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<Path> 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));
}
}
}
}

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/demo</Set>
<Set name="war"><Property name="jetty.webapps"/>/demo/</Set>
<Get name="serverClasspathPattern">
<Call name="add">
<Arg>-org.eclipse.jetty.util.Decorator</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.util.DecoratedObjectFactory</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.server.handler.ContextHandler.</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.server.handler.ContextHandler</Arg>
</Call>
<Call name="add">
<Arg>-org.eclipse.jetty.servlet.ServletContextHandler</Arg>
</Call>
</Get>
</Configure>

View File

@ -29,31 +29,19 @@
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<!-- <dependency>
<groupId>org.mortbay.jasper</groupId>
<artifactId>apache-el</artifactId>
<version>9.0.19</version>
<classifier>provided</classifier>
</dependency>
</dependency>-->
<!-- included in webapp -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>${weld.version}</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>