Skip to content

Commit

Permalink
Reintroduce Weld SE + Servlet cooperation testing using Tomcat embedd…
Browse files Browse the repository at this point in the history
…ed servlet.
  • Loading branch information
manovotn committed Jun 14, 2022
1 parent dda3604 commit 62f4893
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ jobs:
run: tar -xzf maven-repo.tgz -C ~
- name: Build with Maven
run: |
mvn clean verify -Dincontainer=weld-se-coop -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -f environments/servlet/tests/jetty/pom.xml
mvn clean verify -Dincontainer=weld-se-coop -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 -f environments/servlet/tests/tomcat/pom.xml
- name: Prepare failure archive (if maven failed)
if: failure()
shell: bash
Expand Down
90 changes: 75 additions & 15 deletions environments/servlet/tests/tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>junit</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
Expand Down Expand Up @@ -78,7 +83,6 @@
<scope>test</scope>
</dependency>

<!-- TODO why do we need this dep now?! -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
Expand Down Expand Up @@ -154,6 +158,13 @@
<name>incontainer</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-embedded-10</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand All @@ -169,39 +180,88 @@
</plugins>
</build>
</profile>

<profile>
<id>tomcat10</id>
<!-- This profile tests weld-se and weld-servlet cooperation -->
<!-- weld-se bootstraps the container and weld-servlet is reusing the existing BeanManager -->
<!-- See also WELD-1665 -->
<id>weld-se-coop</id>

<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>incontainer</name>
<value>weld-se-coop</value>
</property>
</activation>

<properties>
<test.src.dir>src/test/weld-se-coop</test.src.dir>
</properties>

<dependencies>

<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-tomcat-embedded-10</artifactId>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</profile>
<profile>
<id>jdk9</id>
<activation>
<jdk>[9,)</jdk>
</activation>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/test/weld-se-coop-resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${test.src.dir}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<!-- https://issues.apache.org/bugzilla/show_bug.cgi?id=57314 -->
<exclude>**/ListenerSymmetryTest.java</exclude>
</excludes>
<includes>
<include>org.jboss.weld.environment.servlet.test.se.coop.BootstrapNotNeededTest</include>
<include>org.jboss.weld.environment.servlet.test.se.coop.builder.WeldSeBuilderTest</include>
</includes>
</configuration>
</plugin>
</plugins>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.servlet.test.se.coop;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.List;
import java.util.UUID;

import jakarta.enterprise.inject.spi.CDI;

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.environment.se.events.ContainerInitialized;
import org.jboss.weld.environment.servlet.Listener;
import org.jboss.weld.environment.servlet.WeldServletLifecycle;
import org.junit.Test;

import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;

public class BootstrapNotNeededTest {

@Test
public void testBootstrapNotNeeded() throws Exception {

String id = UUID.randomUUID().toString();

// First boostrap Weld SE
try (WeldContainer container = new Weld(id).initialize()) {

TestBean testBean = container.select(TestBean.class).get();
assertNotNull(testBean);

// @Initialized(ApplicationScoped.class) ContainerInitialized
List<Object> initEvents = testBean.getInitEvents();
assertEquals(1, initEvents.size());
Object event = initEvents.get(0);
assertTrue(event instanceof ContainerInitialized);
assertEquals(id, ((ContainerInitialized)event).getContainerId());

// Test CDIProvider
CDI<Object> cdi = CDI.current();
assertTrue(cdi instanceof WeldContainer);

// servlet setup
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir(new File(".").getAbsolutePath() + "/target");
tomcat.setHostname("localhost");
tomcat.setPort(8080);
// we need to call get connector to trigger creation of default one
tomcat.getConnector();

String contextPath = "";
String docBase = new File(".").getAbsolutePath() + "/target";
Context context = tomcat.addContext(contextPath, docBase);

String servletName = TestServlet.class.getSimpleName();
String urlPattern = "/test";
tomcat.addServlet(contextPath, servletName, TestServlet.class.getName());
context.addServletMappingDecoded(urlPattern, servletName);

// add Weld information
context.addApplicationListener(Listener.class.getName());
context.getServletContext().setAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME, container.getBeanManager());

try {
tomcat.start();

// @Initialized(ApplicationScoped.class) ServletContext not fired
assertEquals(1, initEvents.size());

WebClient webClient = new WebClient();
webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
Page page = webClient.getPage("http://localhost:8080/test");
assertEquals(testBean.getId(), page.getWebResponse().getContentAsString().trim());
} finally {
tomcat.stop();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2022, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.environment.servlet.test.se.coop;

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

import java.io.IOException;
import java.io.PrintWriter;

public class HelloWorldServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();

writer.println("Hello World!");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.servlet.test.se.coop;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;

@ApplicationScoped
public class TestBean {

private String id;

private final List<Object> initEvents = new CopyOnWriteArrayList<>();

@PostConstruct
public void init() {
this.id = UUID.randomUUID().toString();
}

public String getId() {
return id;
}

List<Object> getInitEvents() {
return initEvents;
}

public void onContainerInitialized(@Observes @Initialized(ApplicationScoped.class) Object event) {
initEvents.add(event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.servlet.test.se.coop;

import java.io.IOException;

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

@SuppressWarnings("serial")
public class TestServlet extends HttpServlet {

@Inject
TestBean bean;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/text");
resp.getWriter().write(bean.getId());
}

}
Loading

0 comments on commit 62f4893

Please sign in to comment.