Skip to content

Commit 9e19339

Browse files
Tian Baoqiangpivovarit
authored andcommitted
BAEL-1026 (eugenp#2322)
1 parent 0a631ac commit 9e19339

8 files changed

Lines changed: 215 additions & 45 deletions

File tree

‎ratpack/pom.xml‎

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
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/maven-v4_0_0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.baeldung</groupId>
5-
<artifactId>ratpack</artifactId>
6-
<packaging>jar</packaging>
7-
<version>1.0-SNAPSHOT</version>
8-
<name>ratpack</name>
9-
<url>http://maven.apache.org</url>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>ratpack</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>ratpack</name>
9+
<url>http://maven.apache.org</url>
1010

11-
<properties>
12-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<maven.compiler.source>1.8</maven.compiler.source>
14-
<maven.compiler.target>1.8</maven.compiler.target>
15-
</properties>
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
<ratpack.version>1.4.6</ratpack.version>
16+
</properties>
1617

1718
<parent>
1819
<groupId>com.baeldung</groupId>
1920
<artifactId>parent-modules</artifactId>
2021
<version>1.0.0-SNAPSHOT</version>
21-
</parent>
22-
23-
<dependencies>
24-
<dependency>
25-
<groupId>io.ratpack</groupId>
26-
<artifactId>ratpack-core</artifactId>
27-
<version>1.4.5</version>
28-
</dependency>
29-
<dependency>
30-
<groupId>io.ratpack</groupId>
31-
<artifactId>ratpack-hikari</artifactId>
32-
<version>1.4.5</version>
33-
</dependency>
34-
<dependency>
35-
<groupId>io.ratpack</groupId>
36-
<artifactId>ratpack-test</artifactId>
37-
<version>1.4.5</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>com.h2database</groupId>
41-
<artifactId>h2</artifactId>
42-
<version>1.4.193</version>
43-
</dependency>
44-
</dependencies>
22+
</parent>
4523

46-
<build>
47-
<finalName>${project.artifactId}</finalName>
48-
<resources>
49-
<resource>
50-
<directory>src/main/resources</directory>
51-
</resource>
52-
</resources>
53-
</build>
24+
<dependencies>
25+
26+
<dependency>
27+
<groupId>io.ratpack</groupId>
28+
<artifactId>ratpack-spring-boot-starter</artifactId>
29+
<version>${ratpack.version}</version>
30+
<type>pom</type>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>io.ratpack</groupId>
35+
<artifactId>ratpack-core</artifactId>
36+
<version>${ratpack.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.ratpack</groupId>
40+
<artifactId>ratpack-hikari</artifactId>
41+
<version>${ratpack.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.ratpack</groupId>
45+
<artifactId>ratpack-test</artifactId>
46+
<version>${ratpack.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.h2database</groupId>
50+
<artifactId>h2</artifactId>
51+
<version>1.4.193</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<finalName>${project.artifactId}</finalName>
57+
<resources>
58+
<resource>
59+
<directory>src/main/resources</directory>
60+
</resource>
61+
</resources>
62+
</build>
5463

5564
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.spring;
2+
3+
import java.util.List;
4+
5+
/**
6+
* @author aiet
7+
*/
8+
public interface ArticleList {
9+
10+
List<String> articles();
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import java.util.Arrays;
7+
8+
/**
9+
* @author aiet
10+
*/
11+
@Configuration
12+
public class Config {
13+
14+
@Bean
15+
public Content content() {
16+
return () -> "hello baeldung!";
17+
}
18+
19+
@Bean
20+
public ArticleList articles() {
21+
return () -> Arrays.asList("Introduction to Ratpack", "Ratpack Google Guice Integration", "Ratpack Spring Boot Integration");
22+
}
23+
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring;
2+
3+
/**
4+
* @author aiet
5+
*/
6+
public interface Content {
7+
8+
String body();
9+
10+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.spring;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import ratpack.func.Action;
8+
import ratpack.handling.Chain;
9+
import ratpack.server.ServerConfig;
10+
import ratpack.spring.config.EnableRatpack;
11+
12+
/**
13+
* @author aiet
14+
*/
15+
@SpringBootApplication
16+
@EnableRatpack
17+
public class EmbedRatpackApp {
18+
19+
@Autowired private Content content;
20+
@Autowired private ArticleList list;
21+
22+
@Bean
23+
public Action<Chain> hello() {
24+
return chain -> chain.get("hello", ctx -> ctx.render(content.body()));
25+
}
26+
27+
@Bean
28+
public Action<Chain> list() {
29+
return chain -> chain.get("list", ctx -> ctx.render(list
30+
.articles()
31+
.toString()));
32+
}
33+
34+
@Bean
35+
public ServerConfig ratpackServerConfig() {
36+
return ServerConfig
37+
.builder()
38+
.findBaseDir("public")
39+
.build();
40+
}
41+
42+
public static void main(String[] args) {
43+
SpringApplication.run(EmbedRatpackApp.class, args);
44+
}
45+
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring;
2+
3+
import ratpack.server.RatpackServer;
4+
5+
import static ratpack.spring.Spring.spring;
6+
7+
public class EmbedSpringBootApp {
8+
9+
public static void main(String[] args) throws Exception {
10+
RatpackServer.start(server -> server
11+
.registry(spring(Config.class))
12+
.handlers(chain -> chain.get(ctx -> ctx.render(ctx
13+
.get(Content.class)
14+
.body()))));
15+
}
16+
17+
}
18+
19+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Special Static Resource</title>
6+
</head>
7+
<body>
8+
This page is static.
9+
</body>
10+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.spring;
2+
3+
import org.junit.Test;
4+
import ratpack.test.MainClassApplicationUnderTest;
5+
6+
import java.io.IOException;
7+
8+
import static org.hamcrest.CoreMatchers.containsString;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertThat;
11+
12+
/**
13+
* @author aiet
14+
*/
15+
public class EmbedRatpackAppIntegrationTest {
16+
17+
MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(EmbedRatpackApp.class);
18+
19+
@Test
20+
public void whenSayHello_thenGotWelcomeMessage() {
21+
assertEquals("hello baeldung!", appUnderTest
22+
.getHttpClient()
23+
.getText("/hello"));
24+
}
25+
26+
@Test
27+
public void whenRequestList_thenGotArticles() throws IOException {
28+
assertEquals(3, appUnderTest
29+
.getHttpClient()
30+
.getText("/list")
31+
.split(",").length);
32+
}
33+
34+
@Test
35+
public void whenRequestStaticResource_thenGotStaticContent() {
36+
assertThat(appUnderTest
37+
.getHttpClient()
38+
.getText("/"), containsString("page is static"));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)