refined #BAEL-100 and remove Java EE app for simplicity (#1168)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
491d36e745
commit
cf87412c42
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.context.Initialized;
|
||||
import javax.enterprise.event.Observes;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
@ApplicationScoped
|
||||
public class JavaEEApp {
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
/**
|
||||
* act as a servletContext provider
|
||||
*/
|
||||
private void setContext(@Observes @Initialized(ApplicationScoped.class) final ServletContext context) {
|
||||
if (this.context != null) {
|
||||
throw new IllegalStateException("app context started twice");
|
||||
}
|
||||
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public ServletContext getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,13 +9,13 @@ import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
* <ul><li>
|
||||
* <code>@ServletComponentScan</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")</code>
|
||||
* <code>@ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")</code>
|
||||
* </li><li>
|
||||
* <code>@ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class})</code>
|
||||
* </li></ul>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.javaee")
|
||||
@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.components")
|
||||
public class SpringBootAnnotatedApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")
|
||||
@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.components")
|
||||
public class SpringBootPlainApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
@@ -6,7 +6,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.CopyOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.annotation.WebFilter;
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.baeldung.annotation.servletcomponentscan.javaee;
|
||||
package com.baeldung.annotation.servletcomponentscan.components;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebInitParam;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.baeldung.annotation.servletcomponentscan;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.FilterRegistration;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.container.test.api.RunAsClient;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.arquillian.test.api.ArquillianResource;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.spec.WebArchive;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.AttrListener;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.EchoServlet;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.HelloFilter;
|
||||
import com.baeldung.annotation.servletcomponentscan.javaee.HelloServlet;
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class JavaEEAppIntegrationTest {
|
||||
|
||||
@Deployment
|
||||
public static WebArchive createDeployment() {
|
||||
return ShrinkWrap.create(WebArchive.class).addClass(JavaEEApp.class).addClasses(AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class);
|
||||
}
|
||||
|
||||
@Inject
|
||||
private ServletContext servletContext;
|
||||
|
||||
@Test
|
||||
public void givenServletContextListener_whenAccessSpecialAttrs_thenFound() throws MalformedURLException {
|
||||
assertNotNull(servletContext);
|
||||
assertNotNull(servletContext.getAttribute("servlet-context-attr"));
|
||||
assertEquals("test", servletContext.getAttribute("servlet-context-attr"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() throws MalformedURLException {
|
||||
assertNotNull(servletContext);
|
||||
FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter");
|
||||
|
||||
assertNotNull(filterRegistration);
|
||||
assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet"));
|
||||
}
|
||||
|
||||
@ArquillianResource
|
||||
private URL base;
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenFilterAndServlet_whenGetHello_thenRespondFilteringHello() throws MalformedURLException {
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(URI.create(new URL(base, "hello").toExternalForm()));
|
||||
Response response = target.request().get();
|
||||
|
||||
assertEquals("filtering hello", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RunAsClient
|
||||
public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() throws MalformedURLException {
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(URI.create(new URL(base, "echo").toExternalForm()));
|
||||
Response response = target.request().post(Entity.entity("echo", MediaType.TEXT_PLAIN_TYPE));
|
||||
|
||||
assertEquals("filtering echo", response.readEntity(String.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,7 +19,7 @@ import static org.junit.Assert.*;
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" })
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||
public class SpringBootWithServletComponentIntegrationTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
|
||||
@@ -19,7 +19,7 @@ import static org.junit.Assert.*;
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" })
|
||||
@TestPropertySource(properties = { "security.basic.enabled=false" })
|
||||
public class SpringBootWithoutServletComponentIntegrationTest {
|
||||
|
||||
@Autowired private ServletContext servletContext;
|
||||
|
||||
Reference in New Issue
Block a user