Skip to content

Commit 2c8e3c9

Browse files
Tian Baoqiangpivovarit
authored andcommitted
#BAEL-100 tests and update spring boot to 1.5.1.RELEASE (eugenp#1135)
* add tests for #BAEL-100 and upgrade spring boot to 1.5.1.RELEASE * uncomment integration test configurations
1 parent 7aa7743 commit 2c8e3c9

20 files changed

Lines changed: 444 additions & 18 deletions

‎spring-boot/pom.xml‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.baeldung</groupId>
55
<artifactId>spring-boot</artifactId>
@@ -12,8 +12,8 @@
1212
<parent>
1313
<groupId>org.springframework.boot</groupId>
1414
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>1.4.2.RELEASE</version>
16-
<relativePath /> <!-- lookup parent from repository -->
15+
<version>1.5.1.RELEASE</version>
16+
<relativePath/> <!-- lookup parent from repository -->
1717
</parent>
1818

1919
<dependencies>
@@ -24,6 +24,26 @@
2424
<dependency>
2525
<groupId>org.springframework.boot</groupId>
2626
<artifactId>spring-boot-starter-web</artifactId>
27+
<exclusions>
28+
<exclusion>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-tomcat</artifactId>
31+
</exclusion>
32+
</exclusions>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.apache.geronimo.specs</groupId>
37+
<artifactId>geronimo-osgi-locator</artifactId>
38+
<version>1.1</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.apache.geronimo.components</groupId>
44+
<artifactId>geronimo-jaspi</artifactId>
45+
<version>2.0.0</version>
46+
<scope>test</scope>
2747
</dependency>
2848

2949
<dependency>
@@ -87,6 +107,20 @@
87107
<artifactId>jquery</artifactId>
88108
<version>${jquery.version}</version>
89109
</dependency>
110+
111+
<dependency>
112+
<groupId>org.apache.tomee</groupId>
113+
<artifactId>arquillian-tomee-embedded</artifactId>
114+
<version>${arquillian-tomee-embedded.version}</version>
115+
<scope>test</scope>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>org.apache.tomee</groupId>
120+
<artifactId>javaee-api</artifactId>
121+
<version>${tomee-javaee-api.version}</version>
122+
<scope>provided</scope>
123+
</dependency>
90124
</dependencies>
91125

92126
<build>
@@ -166,7 +200,7 @@
166200
</executions>
167201
<configuration>
168202
<systemPropertyVariables>
169-
<test.mime>json</test.mime>
203+
<test.mime>json</test.mime>
170204
</systemPropertyVariables>
171205
</configuration>
172206
</plugin>
@@ -186,6 +220,8 @@
186220
<jquery.version>3.1.1</jquery.version>
187221
<bootstrap.version>3.3.7-1</bootstrap.version>
188222
<subethasmtp.version>3.1.7</subethasmtp.version>
223+
<arquillian-tomee-embedded.version>7.0.2</arquillian-tomee-embedded.version>
224+
<tomee-javaee-api.version>7.0-1</tomee-javaee-api.version>
189225
</properties>
190226

191227
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.annotation.servletcomponentscan;
2+
3+
import javax.enterprise.context.ApplicationScoped;
4+
import javax.enterprise.context.Initialized;
5+
import javax.enterprise.event.Observes;
6+
import javax.servlet.ServletContext;
7+
8+
@ApplicationScoped
9+
public class JavaEEApp {
10+
11+
private ServletContext context;
12+
13+
/**
14+
* act as a servletContext provider
15+
*/
16+
private void setContext(@Observes @Initialized(ApplicationScoped.class) final ServletContext context) {
17+
if (this.context != null) {
18+
throw new IllegalStateException("app context started twice");
19+
}
20+
21+
this.context = context;
22+
}
23+
24+
public ServletContext getContext() {
25+
return context;
26+
}
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.annotation.servletcomponentscan;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.ServletComponentScan;
6+
7+
/**
8+
* using the following annotations are equivalent:
9+
* <ul><li>
10+
* <code>@ServletComponentScan</code>
11+
* </li><li>
12+
* <code>@ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")</code>
13+
* </li><li>
14+
* <code>@ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class})</code>
15+
* </li></ul>
16+
*/
17+
@SpringBootApplication
18+
@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.javaee")
19+
public class SpringBootAnnotatedApp {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(SpringBootAnnotatedApp.class, args);
23+
}
24+
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.annotation.servletcomponentscan;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.context.annotation.ComponentScan;
5+
6+
@SpringBootApplication
7+
@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee")
8+
public class SpringBootPlainApp {
9+
10+
public static void main(String[] args) {
11+
}
12+
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.annotation.servletcomponentscan.javaee;
2+
3+
import javax.servlet.ServletContextEvent;
4+
import javax.servlet.ServletContextListener;
5+
import javax.servlet.annotation.WebListener;
6+
7+
@WebListener
8+
public class AttrListener implements ServletContextListener {
9+
10+
@Override
11+
public void contextInitialized(ServletContextEvent servletContextEvent) {
12+
servletContextEvent
13+
.getServletContext()
14+
.setAttribute("servlet-context-attr", "test");
15+
System.out.println("context init");
16+
}
17+
18+
@Override
19+
public void contextDestroyed(ServletContextEvent servletContextEvent) {
20+
System.out.println("context destroy");
21+
}
22+
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.annotation.servletcomponentscan.javaee;
2+
3+
import javax.servlet.annotation.WebServlet;
4+
import javax.servlet.http.HttpServlet;
5+
import javax.servlet.http.HttpServletRequest;
6+
import javax.servlet.http.HttpServletResponse;
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.nio.file.CopyOption;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.StandardCopyOption;
13+
14+
@WebServlet(name = "echo servlet", urlPatterns = "/echo")
15+
public class EchoServlet extends HttpServlet {
16+
17+
@Override
18+
public void doPost(HttpServletRequest request, HttpServletResponse response) {
19+
try {
20+
Path path = File
21+
.createTempFile("echo", "tmp")
22+
.toPath();
23+
Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);
24+
Files.copy(path, response.getOutputStream());
25+
Files.delete(path);
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.annotation.servletcomponentscan.javaee;
2+
3+
import javax.servlet.*;
4+
import javax.servlet.annotation.WebFilter;
5+
import javax.servlet.annotation.WebInitParam;
6+
import java.io.IOException;
7+
8+
@WebFilter(urlPatterns = "/hello", description = "a filter for hello servlet", initParams = { @WebInitParam(name = "msg", value = "filtering ") }, filterName = "hello filter", servletNames = { "echo servlet" })
9+
public class HelloFilter implements Filter {
10+
11+
private FilterConfig filterConfig;
12+
13+
@Override
14+
public void init(FilterConfig filterConfig) throws ServletException {
15+
System.out.println("filter init");
16+
this.filterConfig = filterConfig;
17+
}
18+
19+
@Override
20+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
21+
servletResponse
22+
.getOutputStream()
23+
.print(filterConfig.getInitParameter("msg"));
24+
filterChain.doFilter(servletRequest, servletResponse);
25+
}
26+
27+
@Override
28+
public void destroy() {
29+
System.out.println("filter destroy");
30+
}
31+
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.annotation.servletcomponentscan.javaee;
2+
3+
import javax.servlet.ServletConfig;
4+
import javax.servlet.ServletException;
5+
import javax.servlet.annotation.WebInitParam;
6+
import javax.servlet.annotation.WebServlet;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello")})
13+
public class HelloServlet extends HttpServlet {
14+
15+
private ServletConfig servletConfig;
16+
17+
@Override
18+
public void init(ServletConfig servletConfig){
19+
this.servletConfig = servletConfig;
20+
}
21+
22+
@Override
23+
public void doGet(HttpServletRequest request, HttpServletResponse response) {
24+
try {
25+
response
26+
.getOutputStream()
27+
.write(servletConfig.getInitParameter("msg").getBytes());
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
33+
}

spring-boot/src/main/java/com/baeldung/TestController.java renamed to spring-boot/src/main/java/com/baeldung/webjar/TestController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.webjar;
22

33
import org.springframework.stereotype.Controller;
44
import org.springframework.ui.Model;

spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java renamed to spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package com.baeldung;
1+
package com.baeldung.webjar;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ComponentScan;
56

67
@SpringBootApplication
78
public class WebjarsdemoApplication {

0 commit comments

Comments
 (0)