-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reintroduce Weld SE + Servlet cooperation testing using Tomcat embedd…
…ed servlet.
- Loading branch information
Showing
12 changed files
with
513 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
102 changes: 102 additions & 0 deletions
102
.../weld-se-coop/org/jboss/weld/environment/servlet/test/se/coop/BootstrapNotNeededTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
.../test/weld-se-coop/org/jboss/weld/environment/servlet/test/se/coop/HelloWorldServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
|
||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...omcat/src/test/weld-se-coop/org/jboss/weld/environment/servlet/test/se/coop/TestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...at/src/test/weld-se-coop/org/jboss/weld/environment/servlet/test/se/coop/TestServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
Oops, something went wrong.