Skip to content

Commit 4c7ef02

Browse files
andrea-ligiosmaibin
authored andcommitted
BAEL-2294 (eugenp#6191)
* BAEL-2294 * BAEL-2294 * live tests... * formatting * Live Tests! But not parent pom. Yet.
1 parent effde80 commit 4c7ef02

35 files changed

Lines changed: 1004 additions & 0 deletions

blade/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Relevant Articles:
2+
3+
- [Blade - A Complete GuideBook](http://www.baeldung.com/blade)
4+
5+
Run Integration Tests with `mvn integration-test`

blade/pom.xml

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>blade</artifactId>
7+
<name>blade</name>
8+
9+
<!-- WITH THIS mvn integration-test DOES WORK -->
10+
<groupId>com.baeldung</groupId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
13+
<!-- WITH THIS mvn integration-test DOESN'T WORK -->
14+
<!-- <parent> -->
15+
<!-- <groupId>com.baeldung</groupId> -->
16+
<!-- <artifactId>parent-modules</artifactId> -->
17+
<!-- <version>1.0.0-SNAPSHOT</version> -->
18+
<!-- </parent> -->
19+
20+
<properties>
21+
<maven.compiler.source>1.8</maven.compiler.source>
22+
<maven.compiler.target>1.8</maven.compiler.target>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>com.bladejava</groupId>
28+
<artifactId>blade-mvc</artifactId>
29+
<version>2.0.14.RELEASE</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.webjars</groupId>
34+
<artifactId>bootstrap</artifactId>
35+
<version>4.2.1</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.apache.commons</groupId>
40+
<artifactId>commons-lang3</artifactId>
41+
<version>3.8.1</version>
42+
</dependency>
43+
44+
<!-- PROVIDED -->
45+
<dependency>
46+
<groupId>org.projectlombok</groupId>
47+
<artifactId>lombok</artifactId>
48+
<version>1.18.4</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
<!-- TEST -->
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>4.12</version>
57+
<scope>test</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.assertj</groupId>
61+
<artifactId>assertj-core</artifactId>
62+
<version>3.11.1</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.apache.httpcomponents</groupId>
67+
<artifactId>httpclient</artifactId>
68+
<version>4.5.6</version>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.apache.httpcomponents</groupId>
73+
<artifactId>httpmime</artifactId>
74+
<version>4.5.6</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.apache.httpcomponents</groupId>
79+
<artifactId>httpcore</artifactId>
80+
<version>4.4.10</version>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
<build>
85+
<finalName>sample-blade-app</finalName>
86+
<plugins>
87+
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-surefire-plugin</artifactId>
91+
<configuration>
92+
<forkCount>3</forkCount>
93+
<reuseForks>true</reuseForks>
94+
<excludes>
95+
<exclude>**/*LiveTest.java</exclude>
96+
</excludes>
97+
</configuration>
98+
</plugin>
99+
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-failsafe-plugin</artifactId>
103+
<version>3.0.0-M3</version>
104+
<configuration>
105+
<includes>
106+
<include>**/*LiveTest.java</include>
107+
</includes>
108+
</configuration>
109+
<executions>
110+
<execution>
111+
<goals>
112+
<goal>integration-test</goal>
113+
<goal>verify</goal>
114+
</goals>
115+
</execution>
116+
</executions>
117+
</plugin>
118+
119+
<plugin>
120+
<groupId>com.bazaarvoice.maven.plugins</groupId>
121+
<artifactId>process-exec-maven-plugin</artifactId>
122+
<version>0.7</version>
123+
<executions>
124+
<!--Start Blade -->
125+
<execution>
126+
<id>blade-process</id>
127+
<phase>pre-integration-test</phase>
128+
<goals>
129+
<goal>start</goal>
130+
</goals>
131+
<configuration>
132+
<name>Blade</name>
133+
<waitForInterrupt>false</waitForInterrupt>
134+
<arguments>
135+
<argument>java</argument>
136+
<argument>-jar</argument>
137+
<argument>sample-blade-app.jar</argument>
138+
</arguments>
139+
</configuration>
140+
</execution>
141+
142+
<!--Stop all processes in reverse order -->
143+
<execution>
144+
<id>stop-all</id>
145+
<phase>post-integration-test</phase>
146+
<goals>
147+
<goal>stop-all</goal>
148+
</goals>
149+
</execution>
150+
</executions>
151+
</plugin>
152+
153+
154+
<plugin>
155+
<artifactId>maven-assembly-plugin</artifactId>
156+
<version>3.1.0</version>
157+
<configuration>
158+
<finalName>${project.build.finalName}</finalName>
159+
<appendAssemblyId>false</appendAssemblyId>
160+
<archive>
161+
<manifest>
162+
<mainClass>com.baeldung.blade.sample.App</mainClass>
163+
</manifest>
164+
</archive>
165+
<descriptorRefs>
166+
<descriptorRef>jar-with-dependencies</descriptorRef>
167+
</descriptorRefs>
168+
</configuration>
169+
<executions>
170+
<execution>
171+
<id>make-assembly</id>
172+
<phase>package</phase>
173+
<goals>
174+
<goal>single</goal>
175+
</goals>
176+
</execution>
177+
</executions>
178+
</plugin>
179+
<plugin>
180+
<artifactId>maven-compiler-plugin</artifactId>
181+
<configuration>
182+
<source>1.8</source>
183+
<target>1.8</target>
184+
<encoding>UTF-8</encoding>
185+
</configuration>
186+
</plugin>
187+
</plugins>
188+
</build>
189+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.blade.sample;
2+
3+
import com.baeldung.blade.sample.interceptors.BaeldungMiddleware;
4+
import com.blade.Blade;
5+
import com.blade.event.EventType;
6+
import com.blade.mvc.WebContext;
7+
import com.blade.mvc.http.Session;
8+
9+
public class App {
10+
11+
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(App.class);
12+
13+
public static void main(String[] args) {
14+
15+
Blade.of()
16+
.get("/", ctx -> ctx.render("index.html"))
17+
.get("/basic-route-example", ctx -> ctx.text("GET called"))
18+
.post("/basic-route-example", ctx -> ctx.text("POST called"))
19+
.put("/basic-route-example", ctx -> ctx.text("PUT called"))
20+
.delete("/basic-route-example", ctx -> ctx.text("DELETE called"))
21+
.addStatics("/custom-static")
22+
// .showFileList(true)
23+
.enableCors(true)
24+
.before("/user/*", ctx -> log.info("[NarrowedHook] Before '/user/*', URL called: " + ctx.uri()))
25+
.on(EventType.SERVER_STARTED, e -> {
26+
String version = WebContext.blade()
27+
.env("app.version")
28+
.orElse("N/D");
29+
log.info("[Event::serverStarted] Loading 'app.version' from configuration, value: " + version);
30+
})
31+
.on(EventType.SESSION_CREATED, e -> {
32+
Session session = (Session) e.attribute("session");
33+
session.attribute("mySessionValue", "Baeldung");
34+
})
35+
.use(new BaeldungMiddleware())
36+
.start(App.class, args);
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.blade.sample;
2+
3+
import com.blade.mvc.annotation.GetRoute;
4+
import com.blade.mvc.annotation.Path;
5+
import com.blade.mvc.http.Request;
6+
import com.blade.mvc.http.Response;
7+
import com.blade.mvc.http.Session;
8+
9+
@Path
10+
public class AttributesExampleController {
11+
12+
public final static String REQUEST_VALUE = "Some Request value";
13+
public final static String SESSION_VALUE = "1337";
14+
public final static String HEADER = "Some Header";
15+
16+
@GetRoute("/request-attribute-example")
17+
public void getRequestAttribute(Request request, Response response) {
18+
request.attribute("request-val", REQUEST_VALUE);
19+
String requestVal = request.attribute("request-val");
20+
response.text(requestVal);
21+
}
22+
23+
@GetRoute("/session-attribute-example")
24+
public void getSessionAttribute(Request request, Response response) {
25+
Session session = request.session();
26+
session.attribute("session-val", SESSION_VALUE);
27+
String sessionVal = session.attribute("session-val");
28+
response.text(sessionVal);
29+
}
30+
31+
@GetRoute("/header-example")
32+
public void getHeader(Request request, Response response) {
33+
String headerVal = request.header("a-header", HEADER);
34+
response.header("a-header", headerVal);
35+
}
36+
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.blade.sample;
2+
3+
import com.blade.mvc.annotation.Path;
4+
import com.blade.mvc.annotation.Route;
5+
import com.blade.mvc.http.Response;
6+
7+
@Path
8+
public class LogExampleController {
9+
10+
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleController.class);
11+
12+
@Route(value = "/test-logs")
13+
public void testLogs(Response response) {
14+
log.trace("This is a TRACE Message");
15+
log.debug("This is a DEBUG Message");
16+
log.info("This is an INFO Message");
17+
log.warn("This is a WARN Message");
18+
log.error("This is an ERROR Message");
19+
response.text("Check in ./logs");
20+
}
21+
22+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.blade.sample;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.StandardOpenOption;
5+
6+
import com.baeldung.blade.sample.vo.User;
7+
import com.blade.mvc.annotation.CookieParam;
8+
import com.blade.mvc.annotation.GetRoute;
9+
import com.blade.mvc.annotation.HeaderParam;
10+
import com.blade.mvc.annotation.JSON;
11+
import com.blade.mvc.annotation.MultipartParam;
12+
import com.blade.mvc.annotation.Param;
13+
import com.blade.mvc.annotation.Path;
14+
import com.blade.mvc.annotation.PathParam;
15+
import com.blade.mvc.annotation.PostRoute;
16+
import com.blade.mvc.http.Response;
17+
import com.blade.mvc.multipart.FileItem;
18+
import com.blade.mvc.ui.RestResponse;
19+
20+
@Path
21+
public class ParameterInjectionExampleController {
22+
23+
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ParameterInjectionExampleController.class);
24+
25+
@GetRoute("/params/form")
26+
public void formParam(@Param String name, Response response) {
27+
log.info("name: " + name);
28+
response.text(name);
29+
}
30+
31+
@GetRoute("/params/path/:uid")
32+
public void restfulParam(@PathParam Integer uid, Response response) {
33+
log.info("uid: " + uid);
34+
response.text(String.valueOf(uid));
35+
}
36+
37+
@PostRoute("/params-file") // DO NOT USE A SLASH WITHIN THE ROUTE OR IT WILL BREAK (?)
38+
@JSON
39+
public RestResponse<?> fileParam(@MultipartParam FileItem fileItem) throws Exception {
40+
try {
41+
byte[] fileContent = fileItem.getData();
42+
43+
log.debug("Saving the uploaded file");
44+
java.nio.file.Path tempFile = Files.createTempFile("baeldung_tempfiles", ".tmp");
45+
Files.write(tempFile, fileContent, StandardOpenOption.WRITE);
46+
47+
return RestResponse.ok();
48+
} catch (Exception e) {
49+
log.error(e.getMessage(), e);
50+
return RestResponse.fail(e.getMessage());
51+
}
52+
}
53+
54+
@GetRoute("/params/header")
55+
public void headerParam(@HeaderParam String customheader, Response response) {
56+
log.info("Custom header: " + customheader);
57+
response.text(customheader);
58+
}
59+
60+
@GetRoute("/params/cookie")
61+
public void cookieParam(@CookieParam(defaultValue = "default value") String myCookie, Response response) {
62+
log.info("myCookie: " + myCookie);
63+
response.text(myCookie);
64+
}
65+
66+
@PostRoute("/params/vo")
67+
public void voParam(@Param User user, Response response) {
68+
log.info("user as voParam: " + user.toString());
69+
response.html(user.toString() + "<br/><br/><a href='/'>Back</a>");
70+
}
71+
}

0 commit comments

Comments
 (0)