refined #BAEL-100 and remove Java EE app for simplicity (#1168)

This commit is contained in:
Tian Baoqiang 2017-02-15 00:34:44 +08:00 committed by Grzegorz Piwowarek
parent 491d36e745
commit cf87412c42
11 changed files with 14 additions and 156 deletions

View File

@ -24,26 +24,6 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-osgi-locator</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.components</groupId>
<artifactId>geronimo-jaspi</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
@ -109,18 +89,12 @@
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${arquillian-tomee-embedded.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomee</groupId>
<artifactId>javaee-api</artifactId>
<version>${tomee-javaee-api.version}</version>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>${tomee-servlet-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
@ -220,8 +194,7 @@
<jquery.version>3.1.1</jquery.version>
<bootstrap.version>3.3.7-1</bootstrap.version>
<subethasmtp.version>3.1.7</subethasmtp.version>
<arquillian-tomee-embedded.version>7.0.2</arquillian-tomee-embedded.version>
<tomee-javaee-api.version>7.0-1</tomee-javaee-api.version>
<tomee-servlet-api.version>8.5.11</tomee-servlet-api.version>
</properties>
</project>

View File

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

View File

@ -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) {

View File

@ -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) {

View File

@ -1,4 +1,4 @@
package com.baeldung.annotation.servletcomponentscan.javaee;
package com.baeldung.annotation.servletcomponentscan.components;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

View File

@ -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;

View File

@ -1,4 +1,4 @@
package com.baeldung.annotation.servletcomponentscan.javaee;
package com.baeldung.annotation.servletcomponentscan.components;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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;

View File

@ -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;