From 5d7ade7a1af191e95488166e62fc9ce273eeaa78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 27 Jun 2023 16:26:16 +0200 Subject: [PATCH] Introduce Jetty sample --- framework/webmvc-jetty/README.adoc | 1 + framework/webmvc-jetty/build.gradle | 31 +++++++++++ .../webmvc/WebMvcApplicationTests.java | 51 +++++++++++++++++++ .../com/example/webmvc/WebMvcApplication.java | 13 +++++ .../com/example/webmvc/WebMvcController.java | 14 +++++ .../src/main/resources/static/foo.html | 1 + .../com/example/webmvc/RandomPortTests.java | 40 +++++++++++++++ 7 files changed, 151 insertions(+) create mode 100644 framework/webmvc-jetty/README.adoc create mode 100644 framework/webmvc-jetty/build.gradle create mode 100644 framework/webmvc-jetty/src/appTest/java/com/example/webmvc/WebMvcApplicationTests.java create mode 100644 framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcApplication.java create mode 100644 framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcController.java create mode 100644 framework/webmvc-jetty/src/main/resources/static/foo.html create mode 100644 framework/webmvc-jetty/src/test/java/com/example/webmvc/RandomPortTests.java diff --git a/framework/webmvc-jetty/README.adoc b/framework/webmvc-jetty/README.adoc new file mode 100644 index 0000000..2453c51 --- /dev/null +++ b/framework/webmvc-jetty/README.adoc @@ -0,0 +1 @@ +Tests if WebMVC with Jetty is working. diff --git a/framework/webmvc-jetty/build.gradle b/framework/webmvc-jetty/build.gradle new file mode 100644 index 0000000..a123691 --- /dev/null +++ b/framework/webmvc-jetty/build.gradle @@ -0,0 +1,31 @@ +plugins { + id "java" + id "org.springframework.boot" + id "org.springframework.cr.smoke-test" +} + +dependencies { + implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)) + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.boot:spring-boot-starter-jetty") + modules { + module("org.springframework.boot:spring-boot-starter-tomcat") { + replacedBy("org.springframework.boot:spring-boot-starter-jetty", "Use Jetty instead of Tomcat") + } + } + // Downgrade for Jetty, see https://github.com/spring-projects/spring-boot/issues/33044 + implementation("jakarta.servlet:jakarta.servlet-api") { + version { + strictly "5.0.0" + } + } + implementation("org.crac:crac:0.1.3") + + testImplementation("org.springframework.boot:spring-boot-starter-test") + + appTestImplementation(project(":cr-smoke-test-support")) +} + +crSmokeTest { + webApplication = true +} diff --git a/framework/webmvc-jetty/src/appTest/java/com/example/webmvc/WebMvcApplicationTests.java b/framework/webmvc-jetty/src/appTest/java/com/example/webmvc/WebMvcApplicationTests.java new file mode 100644 index 0000000..8b55331 --- /dev/null +++ b/framework/webmvc-jetty/src/appTest/java/com/example/webmvc/WebMvcApplicationTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.webmvc; + +import org.junit.jupiter.api.Test; + +import org.springframework.cr.smoketest.support.junit.ApplicationTest; +import org.springframework.test.web.reactive.server.WebTestClient; + +import static org.assertj.core.api.Assertions.assertThat; + +@ApplicationTest +class WebMvcApplicationTests { + + @Test + void stringResponseBody(WebTestClient client) { + client.get() + .exchange() + .expectStatus() + .isOk() + .expectBody() + .consumeWith((result) -> assertThat(new String(result.getResponseBodyContent())) + .isEqualTo("Hello from Spring MVC and Jetty")); + } + + @Test + void resourceInStatic(WebTestClient client) { + client.get() + .uri("foo.html") + .exchange() + .expectStatus() + .isOk() + .expectBody() + .consumeWith((result) -> assertThat(new String(result.getResponseBodyContent())).isEqualTo("Foo")); + } + +} diff --git a/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcApplication.java b/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcApplication.java new file mode 100644 index 0000000..d1ca57e --- /dev/null +++ b/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcApplication.java @@ -0,0 +1,13 @@ +package com.example.webmvc; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class WebMvcApplication { + + public static void main(String[] args) { + SpringApplication.run(WebMvcApplication.class, args); + } + +} diff --git a/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcController.java b/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcController.java new file mode 100644 index 0000000..3da48e8 --- /dev/null +++ b/framework/webmvc-jetty/src/main/java/com/example/webmvc/WebMvcController.java @@ -0,0 +1,14 @@ +package com.example.webmvc; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class WebMvcController { + + @GetMapping("/") + public String hello() { + return "Hello from Spring MVC and Jetty"; + } + +} diff --git a/framework/webmvc-jetty/src/main/resources/static/foo.html b/framework/webmvc-jetty/src/main/resources/static/foo.html new file mode 100644 index 0000000..9f26b63 --- /dev/null +++ b/framework/webmvc-jetty/src/main/resources/static/foo.html @@ -0,0 +1 @@ +Foo \ No newline at end of file diff --git a/framework/webmvc-jetty/src/test/java/com/example/webmvc/RandomPortTests.java b/framework/webmvc-jetty/src/test/java/com/example/webmvc/RandomPortTests.java new file mode 100644 index 0000000..6cf73d0 --- /dev/null +++ b/framework/webmvc-jetty/src/test/java/com/example/webmvc/RandomPortTests.java @@ -0,0 +1,40 @@ +package com.example.webmvc; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +@SpringBootTest(webEnvironment = RANDOM_PORT) +class RandomPortTests { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate template; + + @Test + void check() { + assertThat(this.port).isNotZero(); + } + + @Test + void stringResponseBody() { + ResponseEntity response = this.template.getForEntity("/", String.class); + assertThat(response.getBody()).isEqualTo("Hello from Spring MVC and Jetty"); + } + + @Test + void resourceInStatic() { + ResponseEntity response = this.template.getForEntity("/foo.html", String.class); + assertThat(response.getBody()).isEqualTo("Foo"); + } + +}