From 727b34ea22d05dca6ed9ab4fb22764d17278e881 Mon Sep 17 00:00:00 2001 From: LUCA Date: Mon, 13 Dec 2021 10:38:28 +0100 Subject: [PATCH 01/17] fix: use env to get properties --- .../main/java/com/baeldung/prefix/PrefixController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java index 00b728c7ae..dc6b0cd513 100644 --- a/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java +++ b/spring-boot-modules/spring-boot-environment/src/main/java/com/baeldung/prefix/PrefixController.java @@ -1,6 +1,7 @@ package com.baeldung.prefix; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -8,12 +9,12 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller public class PrefixController { - @Value(value = "${server.port}") - private int serverPort; + @Autowired + private Environment environment; @GetMapping("/prefix") public String getServerPortInfo(final Model model) { - model.addAttribute("serverPort", serverPort); + model.addAttribute("serverPort", environment.getProperty("server.port")); return "prefix"; } } From 3d6a16866e79f4c3cf848082e2a88db41fe94452 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Mon, 13 Dec 2021 20:26:29 +0000 Subject: [PATCH 02/17] [JAVA-8295] Split spring-mvc-xml module --- spring-web-modules/pom.xml | 3 +- .../spring-mvc-xml-2/.gitignore | 13 +++ spring-web-modules/spring-mvc-xml-2/README.md | 14 +++ spring-web-modules/spring-mvc-xml-2/pom.xml | 101 ++++++++++++++++++ .../com/baeldung/spring/ClientWebConfig.java | 17 +++ .../baeldung/spring/ClientWebConfigJava.java | 3 +- .../ConstraintViolationExceptionHandler.java | 0 .../spring/nomapping/GreetingController.java | 16 +++ ...stAndPathVariableValidationController.java | 17 +-- .../baeldung/spring/taglibrary}/Person.java | 2 +- .../spring/taglibrary}/PersonController.java | 17 ++- .../spring/taglibrary}/PersonValidator.java | 3 +- .../src/main/resources/logback.xml | 19 ++++ .../src/main/resources/messages.properties | 3 + .../src/main/resources/webMvcConfig.xml | 40 +++++++ .../src/main/webapp/WEB-INF/mvc-servlet.xml | 12 +++ .../src/main/webapp/WEB-INF/view/greeting.jsp | 9 ++ .../main/webapp/WEB-INF/view/personForm.jsp | 0 .../main/webapp/WEB-INF/view/personView.jsp | 0 .../src/main/webapp/WEB-INF/view/sample.jsp | 7 ++ .../src/main/webapp/WEB-INF/web.xml | 47 ++++++++ .../src/main/webapp/index.jsp | 15 +++ .../java/com/baeldung/SpringContextTest.java | 19 ++++ ...leValidationControllerIntegrationTest.java | 14 ++- spring-web-modules/spring-mvc-xml/README.md | 4 +- spring-web-modules/spring-mvc-xml/pom.xml | 5 - 26 files changed, 363 insertions(+), 37 deletions(-) create mode 100644 spring-web-modules/spring-mvc-xml-2/.gitignore create mode 100644 spring-web-modules/spring-mvc-xml-2/README.md create mode 100644 spring-web-modules/spring-mvc-xml-2/pom.xml create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfig.java rename spring-web-modules/{spring-mvc-xml => spring-mvc-xml-2}/src/main/java/com/baeldung/spring/ClientWebConfigJava.java (96%) rename spring-web-modules/{spring-mvc-xml => spring-mvc-xml-2}/src/main/java/com/baeldung/spring/controller/ConstraintViolationExceptionHandler.java (100%) create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/nomapping/GreetingController.java rename spring-web-modules/{spring-mvc-xml/src/main/java/com/baeldung/spring/controller => spring-mvc-xml-2/src/main/java/com/baeldung/spring/paramsvalidation}/RequestAndPathVariableValidationController.java (79%) rename spring-web-modules/{spring-mvc-xml/src/main/java/com/baeldung/spring/form => spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary}/Person.java (98%) rename spring-web-modules/{spring-mvc-xml/src/main/java/com/baeldung/spring/controller => spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary}/PersonController.java (95%) rename spring-web-modules/{spring-mvc-xml/src/main/java/com/baeldung/spring/validator => spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary}/PersonValidator.java (87%) create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/resources/logback.xml create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/resources/messages.properties create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/resources/webMvcConfig.xml create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/mvc-servlet.xml create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/greeting.jsp rename spring-web-modules/{spring-mvc-xml => spring-mvc-xml-2}/src/main/webapp/WEB-INF/view/personForm.jsp (100%) rename spring-web-modules/{spring-mvc-xml => spring-mvc-xml-2}/src/main/webapp/WEB-INF/view/personView.jsp (100%) create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/sample.jsp create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-web-modules/spring-mvc-xml-2/src/main/webapp/index.jsp create mode 100644 spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/SpringContextTest.java rename spring-web-modules/{spring-mvc-xml/src/test/java/com/baeldung/spring/controller => spring-mvc-xml-2/src/test/java/com/baeldung/spring/paramsvalidation}/RequestAndPathVariableValidationControllerIntegrationTest.java (90%) diff --git a/spring-web-modules/pom.xml b/spring-web-modules/pom.xml index 96aeb514a0..b84ea5a13a 100644 --- a/spring-web-modules/pom.xml +++ b/spring-web-modules/pom.xml @@ -30,6 +30,7 @@ spring-mvc-views spring-mvc-webflow spring-mvc-xml + spring-mvc-xml-2 spring-rest-angular spring-rest-http spring-rest-http-2 @@ -47,4 +48,4 @@ spring-web-url - \ No newline at end of file + diff --git a/spring-web-modules/spring-mvc-xml-2/.gitignore b/spring-web-modules/spring-mvc-xml-2/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml-2/README.md b/spring-web-modules/spring-mvc-xml-2/README.md new file mode 100644 index 0000000000..05a6172fa1 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/README.md @@ -0,0 +1,14 @@ +## Spring MVC XML + +This module contains articles about Spring MVC with XML configuration + +### The Course + +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: + +- [Exploring SpringMVC’s Form Tag Library](https://www.baeldung.com/spring-mvc-form-tags) +- [Validating RequestParams and PathVariables in Spring](https://www.baeldung.com/spring-validate-requestparam-pathvariable) +- [Debugging the Spring MVC 404 “No mapping found for HTTP request” Error](https://www.baeldung.com/spring-mvc-404-error) +- More articles: [[<-- prev]](../spring-mvc-xml) diff --git a/spring-web-modules/spring-mvc-xml-2/pom.xml b/spring-web-modules/spring-mvc-xml-2/pom.xml new file mode 100644 index 0000000000..e65259d512 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + spring-mvc-xml-2 + 0.1-SNAPSHOT + spring-mvc-xml-2 + war + + + com.baeldung + spring-web-modules + 0.0.1-SNAPSHOT + + + + + + org.springframework + spring-web + ${org.springframework.version} + + + org.springframework + spring-webmvc + ${org.springframework.version} + + + + javax.servlet + javax.servlet-api + ${javax.servlet-api.version} + provided + + + javax.servlet + jstl + ${jstl.version} + runtime + + + org.hibernate.validator + hibernate-validator + ${hibernate-validator.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + commons-io + commons-io + ${commons-io.version} + + + org.glassfish + javax.el + ${javax.el.version} + + + org.springframework.boot + spring-boot-starter-test + ${spring-boot.version} + test + + + + + spring-mvc-xml + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + + + + + 5.0.2.RELEASE + 1.5.10.RELEASE + 5.1.40 + 4.4.5 + 4.5.2 + 6.0.10.Final + 3.0.1-b08 + 19.0 + 1.6.1 + + + diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfig.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfig.java new file mode 100644 index 0000000000..a464717f5d --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.spring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@ImportResource("classpath:webMvcConfig.xml") +@Configuration +@ComponentScan +public class ClientWebConfig implements WebMvcConfigurer { + + public ClientWebConfig() { + super(); + } + +} \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfigJava.java similarity index 96% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfigJava.java index 09c34ccc80..dc8db43e41 100644 --- a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/ClientWebConfigJava.java @@ -6,6 +6,7 @@ import java.util.ResourceBundle; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; import org.springframework.context.support.MessageSourceResourceBundle; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; @@ -54,7 +55,7 @@ public class ClientWebConfigJava implements WebMvcConfigurer { return bean; } - + @Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ConstraintViolationExceptionHandler.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/controller/ConstraintViolationExceptionHandler.java similarity index 100% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ConstraintViolationExceptionHandler.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/controller/ConstraintViolationExceptionHandler.java diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/nomapping/GreetingController.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/nomapping/GreetingController.java new file mode 100644 index 0000000000..5016113d30 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/nomapping/GreetingController.java @@ -0,0 +1,16 @@ +package com.baeldung.spring.nomapping; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +public class GreetingController { + + @RequestMapping(value = "/greeting", method = RequestMethod.GET) + public String get(ModelMap model) { + model.addAttribute("message", "Hello, World!"); + return "greeting"; + } +} \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/RequestAndPathVariableValidationController.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationController.java similarity index 79% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/RequestAndPathVariableValidationController.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationController.java index b77598c113..4768237871 100644 --- a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/RequestAndPathVariableValidationController.java +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationController.java @@ -1,12 +1,17 @@ -package com.baeldung.spring.controller; +package com.baeldung.spring.paramsvalidation; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.GetMapping; import org.springframework.validation.annotation.Validated; -import javax.validation.constraints.*; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; @Controller @RequestMapping("/public/api/1") diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/Person.java similarity index 98% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/Person.java index 307a36b10f..ed2fa903ef 100644 --- a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.form; +package com.baeldung.spring.taglibrary; import java.util.List; diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonController.java similarity index 95% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonController.java index 71d9ad7845..0fcb66f2dd 100644 --- a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonController.java @@ -1,14 +1,5 @@ -package com.baeldung.spring.controller; +package com.baeldung.spring.taglibrary; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.validation.Valid; - -import com.baeldung.spring.form.Person; -import com.baeldung.spring.validator.PersonValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -19,6 +10,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; +import javax.validation.Valid; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + @Controller public class PersonController { diff --git a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonValidator.java similarity index 87% rename from spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java rename to spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonValidator.java index cda756cdfc..9f437429ae 100644 --- a/spring-web-modules/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java +++ b/spring-web-modules/spring-mvc-xml-2/src/main/java/com/baeldung/spring/taglibrary/PersonValidator.java @@ -1,6 +1,5 @@ -package com.baeldung.spring.validator; +package com.baeldung.spring.taglibrary; -import com.baeldung.spring.form.Person; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/resources/logback.xml b/spring-web-modules/spring-mvc-xml-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..56af2d397e --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/resources/messages.properties b/spring-web-modules/spring-mvc-xml-2/src/main/resources/messages.properties new file mode 100644 index 0000000000..a58f51db3e --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/resources/messages.properties @@ -0,0 +1,3 @@ +required.name = Name is required! +NotEmpty.person.password = Password is required! + diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/resources/webMvcConfig.xml b/spring-web-modules/spring-mvc-xml-2/src/main/resources/webMvcConfig.xml new file mode 100644 index 0000000000..5dcdef6ad4 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/resources/webMvcConfig.xml @@ -0,0 +1,40 @@ + + + + + + + + + image/jpeg + image/png + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/mvc-servlet.xml new file mode 100644 index 0000000000..e27665da95 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/greeting.jsp b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/greeting.jsp new file mode 100644 index 0000000000..ac17c75ab7 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/greeting.jsp @@ -0,0 +1,9 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Greeting + + +

${message}

+ + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/personForm.jsp similarity index 100% rename from spring-web-modules/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp rename to spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/personForm.jsp diff --git a/spring-web-modules/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/personView.jsp similarity index 100% rename from spring-web-modules/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp rename to spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/personView.jsp diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/sample.jsp b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/sample.jsp new file mode 100644 index 0000000000..7cc14b5dcd --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/view/sample.jsp @@ -0,0 +1,7 @@ + + + + +

This is the body of the sample view

+ + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/web.xml b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..e704d2eba3 --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,47 @@ + + + Spring MVC XML Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + com.baeldung.spring + + + + org.springframework.web.context.ContextLoaderListener + + + + + mvc + org.springframework.web.servlet.DispatcherServlet + 1 + + + mvc + / + + + + + 10 + + + index.jsp + + + + /errors + + diff --git a/spring-web-modules/spring-mvc-xml-2/src/main/webapp/index.jsp b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/index.jsp new file mode 100644 index 0000000000..14872cf3ba --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/main/webapp/index.jsp @@ -0,0 +1,15 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Spring MVC Examples + + + +

Spring MVC Examples

+ + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/SpringContextTest.java b/spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/SpringContextTest.java new file mode 100644 index 0000000000..62e34859ee --- /dev/null +++ b/spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/SpringContextTest.java @@ -0,0 +1,19 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import com.baeldung.spring.ClientWebConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = ClientWebConfig.class) +@WebAppConfiguration +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} diff --git a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/spring/controller/RequestAndPathVariableValidationControllerIntegrationTest.java b/spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationControllerIntegrationTest.java similarity index 90% rename from spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/spring/controller/RequestAndPathVariableValidationControllerIntegrationTest.java rename to spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationControllerIntegrationTest.java index c4332dd879..a71a024e62 100644 --- a/spring-web-modules/spring-mvc-xml/src/test/java/com/baeldung/spring/controller/RequestAndPathVariableValidationControllerIntegrationTest.java +++ b/spring-web-modules/spring-mvc-xml-2/src/test/java/com/baeldung/spring/paramsvalidation/RequestAndPathVariableValidationControllerIntegrationTest.java @@ -1,5 +1,6 @@ -package com.baeldung.spring.controller; +package com.baeldung.spring.paramsvalidation; +import com.baeldung.spring.ClientWebConfigJava; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -11,9 +12,6 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.baeldung.spring.ClientWebConfig; -import com.baeldung.spring.ClientWebConfigJava; - import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -35,13 +33,13 @@ public class RequestAndPathVariableValidationControllerIntegrationTest { @Test public void getNameOfDayByNumberRequestParam_whenGetWithProperRequestParam_thenReturn200() throws Exception { mockMvc.perform(get("/public/api/1/name-for-day").param("dayOfWeek", Integer.toString(5))) - .andExpect(status().isOk()); + .andExpect(status().isOk()); } @Test public void getNameOfDayByNumberRequestParam_whenGetWithRequestParamOutOfRange_thenReturn400() throws Exception { mockMvc.perform(get("/public/api/1/name-for-day").param("dayOfWeek", Integer.toString(15))) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()); } @Test @@ -52,7 +50,7 @@ public class RequestAndPathVariableValidationControllerIntegrationTest { @Test public void getNameOfDayByPathVariable_whenGetWithRequestParamOutOfRange_thenReturn400() throws Exception { mockMvc.perform(get("/public/api/1/name-for-day/{dayOfWeek}", Integer.toString(15))) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()); } @Test @@ -63,7 +61,7 @@ public class RequestAndPathVariableValidationControllerIntegrationTest { @Test public void validStringRequestParam_whenGetWithTooLongRequestParam_thenReturn400() throws Exception { mockMvc.perform(get("/public/api/1/valid-name").param("name", "asdfghjklqw")) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()); } @Test diff --git a/spring-web-modules/spring-mvc-xml/README.md b/spring-web-modules/spring-mvc-xml/README.md index 3fbea3626b..3260d91d94 100644 --- a/spring-web-modules/spring-mvc-xml/README.md +++ b/spring-web-modules/spring-mvc-xml/README.md @@ -12,12 +12,10 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Returning Image/Media Data with Spring MVC](https://www.baeldung.com/spring-mvc-image-media-data) - [Geolocation by IP in Java](https://www.baeldung.com/geolocation-by-ip-with-maxmind) - [Guide to JavaServer Pages (JSP)](https://www.baeldung.com/jsp) -- [Exploring SpringMVC’s Form Tag Library](https://www.baeldung.com/spring-mvc-form-tags) - [web.xml vs Initializer with Spring](https://www.baeldung.com/spring-xml-vs-java-config) - [A Java Web Application Without a web.xml](https://www.baeldung.com/java-web-app-without-web-xml) -- [Validating RequestParams and PathVariables in Spring](https://www.baeldung.com/spring-validate-requestparam-pathvariable) -- [Debugging the Spring MVC 404 “No mapping found for HTTP request” Error](https://www.baeldung.com/spring-mvc-404-error) - [Introduction to Servlets and Servlet Containers](https://www.baeldung.com/java-servlets-containers-intro) +- More articles: [[more -->]](../spring-mvc-xml-2) ## Spring MVC with XML Configuration Example Project diff --git a/spring-web-modules/spring-mvc-xml/pom.xml b/spring-web-modules/spring-mvc-xml/pom.xml index e67052e0cd..c814787ba7 100644 --- a/spring-web-modules/spring-mvc-xml/pom.xml +++ b/spring-web-modules/spring-mvc-xml/pom.xml @@ -39,11 +39,6 @@ ${jstl.version} runtime - - org.hibernate.validator - hibernate-validator - ${hibernate-validator.version} - com.fasterxml.jackson.core From 9f418cd99643d3ef988adbd3748b6eda193df5cc Mon Sep 17 00:00:00 2001 From: jason-m-s <45552030+jason-m-s@users.noreply.github.com> Date: Tue, 14 Dec 2021 07:34:51 +0530 Subject: [PATCH 03/17] BAEL-5138: Implementation code for Lombok @With Methods (#11494) * BAEL-5138: Implementation code for Lombok @With Methods * BAEL-5138: Implementation code for Lombok @With Methods * Fixed a type issue in HolderUnitTest * Removed a constructor from User to avoid confusion with article --- .../java/com/baeldung/lombok/with/Device.java | 13 ++++++++++ .../java/com/baeldung/lombok/with/Holder.java | 16 +++++++++++++ .../baeldung/lombok/with/ImprovedUser.java | 14 +++++++++++ .../com/baeldung/lombok/with/KioskDevice.java | 13 ++++++++++ .../java/com/baeldung/lombok/with/Stock.java | 23 ++++++++++++++++++ .../java/com/baeldung/lombok/with/User.java | 14 +++++++++++ .../baeldung/lombok/with/HolderUnitTest.java | 24 +++++++++++++++++++ .../lombok/with/ImprovedUserUnitTest.java | 22 +++++++++++++++++ .../lombok/with/KioskDeviceWithUnitTest.java | 21 ++++++++++++++++ .../lombok/with/StockWithUnitTest.java | 22 +++++++++++++++++ .../lombok/with/UserWithUnitTest.java | 21 ++++++++++++++++ 11 files changed, 203 insertions(+) create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/Device.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/Holder.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/ImprovedUser.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/KioskDevice.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/Stock.java create mode 100644 lombok/src/main/java/com/baeldung/lombok/with/User.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/with/HolderUnitTest.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/with/ImprovedUserUnitTest.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/with/KioskDeviceWithUnitTest.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/with/StockWithUnitTest.java create mode 100644 lombok/src/test/java/com/baeldung/lombok/with/UserWithUnitTest.java diff --git a/lombok/src/main/java/com/baeldung/lombok/with/Device.java b/lombok/src/main/java/com/baeldung/lombok/with/Device.java new file mode 100644 index 0000000000..d2c6c33feb --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/Device.java @@ -0,0 +1,13 @@ +package com.baeldung.lombok.with; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.With; + +@Getter +@AllArgsConstructor +public abstract class Device { + private final String serial; + @With + private final boolean isInspected; +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/with/Holder.java b/lombok/src/main/java/com/baeldung/lombok/with/Holder.java new file mode 100644 index 0000000000..f379eae0c1 --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/Holder.java @@ -0,0 +1,16 @@ +package com.baeldung.lombok.with; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.With; + +@Getter +@AllArgsConstructor +public class Holder { + @With + private String variableA; + @With + private String _variableB; + @With + private String $variableC; +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/with/ImprovedUser.java b/lombok/src/main/java/com/baeldung/lombok/with/ImprovedUser.java new file mode 100644 index 0000000000..5fafa08eda --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/ImprovedUser.java @@ -0,0 +1,14 @@ +package com.baeldung.lombok.with; + +import lombok.AllArgsConstructor; +import lombok.NonNull; +import lombok.With; + +@With +@AllArgsConstructor +public class ImprovedUser { + @NonNull + private final String username; + @NonNull + private final String emailAddress; +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/with/KioskDevice.java b/lombok/src/main/java/com/baeldung/lombok/with/KioskDevice.java new file mode 100644 index 0000000000..de015b69af --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/KioskDevice.java @@ -0,0 +1,13 @@ +package com.baeldung.lombok.with; + +public class KioskDevice extends Device { + + public KioskDevice(String serial, boolean isInspected) { + super(serial, isInspected); + } + + @Override + public Device withInspected(boolean isInspected) { + return new KioskDevice(getSerial(), isInspected); + } +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/with/Stock.java b/lombok/src/main/java/com/baeldung/lombok/with/Stock.java new file mode 100644 index 0000000000..33b29737cf --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/Stock.java @@ -0,0 +1,23 @@ +package com.baeldung.lombok.with; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.With; + +@Getter +@AllArgsConstructor +public class Stock { + @With + private String sku; + private int stockCount; + + public Stock withSku(String sku) { + return new Stock("mod-" + sku, stockCount); + } + + public Stock withSKU(String... sku) { + return sku == null || sku.length == 0 ? + new Stock("unknown", stockCount) : + new Stock("mod-" + sku[0], stockCount); + } +} \ No newline at end of file diff --git a/lombok/src/main/java/com/baeldung/lombok/with/User.java b/lombok/src/main/java/com/baeldung/lombok/with/User.java new file mode 100644 index 0000000000..3aa03d692b --- /dev/null +++ b/lombok/src/main/java/com/baeldung/lombok/with/User.java @@ -0,0 +1,14 @@ +package com.baeldung.lombok.with; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.With; + +@Getter +@AllArgsConstructor +public class User { + private final String username; + private final String emailAddress; + @With + private final boolean isAuthenticated; +} \ No newline at end of file diff --git a/lombok/src/test/java/com/baeldung/lombok/with/HolderUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/with/HolderUnitTest.java new file mode 100644 index 0000000000..c0a3306a76 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/with/HolderUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.lombok.with; + + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +public class HolderUnitTest { + + @Test + public void whenWithMethodsGenerated_thenUsable() { + Holder value = new Holder("a", "b"); + + Holder valueModifiedA = value.withVariableA("mod-a"); + Holder valueModifiedB = value.with_variableB("mod-b"); + // Holder valueModifiedC = value.with$VariableC("mod-c"); not possible + + assertNotSame(valueModifiedA, value); + assertNotSame(valueModifiedB, value); + assertEquals("mod-a", valueModifiedA.getVariableA()); + assertEquals("mod-b", valueModifiedB.get_variableB()); + } +} diff --git a/lombok/src/test/java/com/baeldung/lombok/with/ImprovedUserUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/with/ImprovedUserUnitTest.java new file mode 100644 index 0000000000..b8d466ac4f --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/with/ImprovedUserUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.lombok.with; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ImprovedUserUnitTest { + + @Test + public void whenUsernameNull_thenException() { + ImprovedUser user = new ImprovedUser("testuser", "test@mail.com"); + + assertThrows(NullPointerException.class, () -> user.withUsername(null)); + } + + @Test + public void whenEmailNull_thenException() { + ImprovedUser user = new ImprovedUser("testuser", "test@mail.com"); + + assertThrows(NullPointerException.class, () -> user.withEmailAddress(null)); + } +} \ No newline at end of file diff --git a/lombok/src/test/java/com/baeldung/lombok/with/KioskDeviceWithUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/with/KioskDeviceWithUnitTest.java new file mode 100644 index 0000000000..40bd5de8a1 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/with/KioskDeviceWithUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.lombok.with; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class KioskDeviceWithUnitTest { + + @Test + public void whenDeviceInspected_thenClonedAndUpdated() { + KioskDevice device = new KioskDevice("S-001", false); + + Device inspectedDevice = device.withInspected(true); + + assertNotSame(inspectedDevice, device); + assertFalse(device.isInspected()); + assertTrue(inspectedDevice.isInspected()); + } +} diff --git a/lombok/src/test/java/com/baeldung/lombok/with/StockWithUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/with/StockWithUnitTest.java new file mode 100644 index 0000000000..8d757e86ef --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/with/StockWithUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.lombok.with; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class StockWithUnitTest { + + @Test + public void givenWithManuallyProvided_whenSkuChanged_thenManualMethodUsed() { + Stock stock = new Stock("sku-001", 5); + + Stock modifiedStock = stock.withSku("sku-002"); + Stock anotherModifiedStock = stock.withSKU("sku-003", "sku-004"); + + assertNotSame(modifiedStock, stock); + assertNotSame(anotherModifiedStock, stock); + assertTrue(modifiedStock.getSku().startsWith("mod")); + assertTrue(anotherModifiedStock.getSku().startsWith("mod")); + } +} \ No newline at end of file diff --git a/lombok/src/test/java/com/baeldung/lombok/with/UserWithUnitTest.java b/lombok/src/test/java/com/baeldung/lombok/with/UserWithUnitTest.java new file mode 100644 index 0000000000..ce32ab9ee3 --- /dev/null +++ b/lombok/src/test/java/com/baeldung/lombok/with/UserWithUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.lombok.with; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class UserWithUnitTest { + + @Test + public void whenUserAuthenticated_thenClonedAndUpdated() { + User immutableUser = new User("testuser", "test@mail.com", false); + + User authenticatedUser = immutableUser.withAuthenticated(true); + + assertNotSame(immutableUser, authenticatedUser); + assertFalse(immutableUser.isAuthenticated()); + assertTrue(authenticatedUser.isAuthenticated()); + } +} \ No newline at end of file From 53b9df34a2db9f78b7052235c7a1f123287c766f Mon Sep 17 00:00:00 2001 From: sachin <56427366+sachin071287@users.noreply.github.com> Date: Tue, 14 Dec 2021 07:48:00 +0530 Subject: [PATCH 04/17] Bael-5125 Java implicit super constructor is undefined error (#11566) Co-authored-by: Sachin kumar --- .../Employee.java | 26 ++++++++++++++ .../Person.java | 35 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Employee.java create mode 100644 core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Person.java diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Employee.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Employee.java new file mode 100644 index 0000000000..708e2f33f8 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Employee.java @@ -0,0 +1,26 @@ +package com.baeldung.implicitsuperconstructorundefined; + +public class Employee extends Person { + + private Double salary; + + public Employee(String name, Integer age, Double salary) { + // comment this super call to see the error. + super(name, age); + this.salary = salary; + } + + public Employee(Double salary) { + super(); + this.salary = salary; + } + + public Double getSalary() { + return salary; + } + + public void setSalary(Double salary) { + this.salary = salary; + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Person.java b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Person.java new file mode 100644 index 0000000000..ff4cde5db7 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-constructors/src/main/java/com/baeldung/implicitsuperconstructorundefined/Person.java @@ -0,0 +1,35 @@ +package com.baeldung.implicitsuperconstructorundefined; + +public class Person { + + private String name; + + private Integer age; + + public Person(String name, Integer age) { + this.name = name; + this.age = age; + } + + // comment this constructor to see the error. + public Person() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + +} From 1bf2293e0ba36afa4cddd87260bb57d852983735 Mon Sep 17 00:00:00 2001 From: Amy DeGregorio Date: Tue, 14 Dec 2021 13:15:43 -0500 Subject: [PATCH 05/17] BAEL-4811 (#11552) * BAEL-4811 * BAEL-4811 Add unit tests --- micronaut/pom.xml | 3 +- .../vs/springboot/CompareApplication.java | 9 +++ .../springboot/client/ArithmeticClient.java | 23 +++++++ .../client/ArithmeticClientImpl.java | 41 +++++++++++ .../controller/ArithmeticController.java | 58 ++++++++++++++++ .../springboot/service/ArithmeticService.java | 25 +++++++ .../springboot/ArithmeticClientUnitTest.java | 61 +++++++++++++++++ .../vs/springboot/CompareApplication.java | 13 ++++ .../controller/ArithmeticController.java | 60 ++++++++++++++++ .../springboot/service/ArithmeticService.java | 25 +++++++ .../ArithmeticControllerUnitTest.java | 68 +++++++++++++++++++ 11 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java create mode 100644 micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClient.java create mode 100644 micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClientImpl.java create mode 100644 micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java create mode 100644 micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java create mode 100644 micronaut/src/test/java/com/baeldung/micronaut/vs/springboot/ArithmeticClientUnitTest.java create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java create mode 100644 spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticControllerUnitTest.java diff --git a/micronaut/pom.xml b/micronaut/pom.xml index 019bd6ab29..a04fc5c99c 100644 --- a/micronaut/pom.xml +++ b/micronaut/pom.xml @@ -135,7 +135,8 @@ - com.baeldung.micronaut.helloworld.server.ServerApplication + + com.baeldung.micronaut.vs.springboot.CompareApplication 1.0.0.RC2 1.8 1.3.2 diff --git a/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java new file mode 100644 index 0000000000..3375c302c2 --- /dev/null +++ b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java @@ -0,0 +1,9 @@ +package com.baeldung.micronaut.vs.springboot; + +import io.micronaut.runtime.Micronaut; + +public class CompareApplication { + public static void main(String[] args) { + Micronaut.run(CompareApplication.class); + } +} diff --git a/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClient.java b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClient.java new file mode 100644 index 0000000000..eb314d8a1d --- /dev/null +++ b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClient.java @@ -0,0 +1,23 @@ +package com.baeldung.micronaut.vs.springboot.client; + +import io.micronaut.http.annotation.Get; +import io.micronaut.http.client.annotation.Client; + +@Client("/math") +public interface ArithmeticClient { + + @Get("/sum/{number1}/{number2}") + String sum(float number1, float number2); + + @Get("/subtract/{number1}/{number2}") + String subtract(float number1, float number2); + + @Get("/multiply/{number1}/{number2}") + String multiply(float number1, float number2); + + @Get("/divide/{number1}/{number2}") + String divide(float number1, float number2); + + @Get("/memory") + String memory(); +} diff --git a/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClientImpl.java b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClientImpl.java new file mode 100644 index 0000000000..1a9cad7981 --- /dev/null +++ b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/client/ArithmeticClientImpl.java @@ -0,0 +1,41 @@ +package com.baeldung.micronaut.vs.springboot.client; + +import javax.inject.Singleton; + +import io.micronaut.http.HttpRequest; +import io.micronaut.http.client.RxHttpClient; +import io.micronaut.http.client.annotation.Client; + +@Singleton +public class ArithmeticClientImpl { + private RxHttpClient httpClient; + + public ArithmeticClientImpl(@Client("/") RxHttpClient httpClient) { + this.httpClient = httpClient; + } + + public String sum(float number1, float number2) { + HttpRequest req = HttpRequest.GET("/math/sum/" + number1 + "/" + number2); + return httpClient.retrieve(req).blockingFirst(); + } + + public String subtract(float number1, float number2) { + HttpRequest req = HttpRequest.GET("/math/subtract/" + number1 + "/" + number2); + return httpClient.retrieve(req).blockingFirst(); + } + + public String multiply(float number1, float number2) { + HttpRequest req = HttpRequest.GET("/math/multiply/" + number1 + "/" + number2); + return httpClient.retrieve(req).blockingFirst(); + } + + public String divide(float number1, float number2) { + HttpRequest req = HttpRequest.GET("/math/divide/" + number1 + "/" + number2); + return httpClient.retrieve(req).blockingFirst(); + } + + public String memory() { + HttpRequest req = HttpRequest.GET("/math/memory"); + return httpClient.retrieve(req).blockingFirst(); + } +} diff --git a/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java new file mode 100644 index 0000000000..930bdba029 --- /dev/null +++ b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java @@ -0,0 +1,58 @@ +package com.baeldung.micronaut.vs.springboot.controller; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; + +import javax.inject.Inject; + +import com.baeldung.micronaut.vs.springboot.service.ArithmeticService; + +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; + +@Controller("/math") +public class ArithmeticController { + @Inject + private ArithmeticService arithmeticService; + + @Get("/sum/{number1}/{number2}") + public float getSum(float number1, float number2) { + return arithmeticService.add(number1, number2); + } + + @Get("/subtract/{number1}/{number2}") + public float getDifference(float number1, float number2) { + return arithmeticService.subtract(number1, number2); + } + + @Get("/multiply/{number1}/{number2}") + public float getMultiplication(float number1, float number2) { + return arithmeticService.multiply(number1, number2); + } + + @Get("/divide/{number1}/{number2}") + public float getDivision(float number1, float number2) { + return arithmeticService.divide(number1, number2); + } + + @Get("/memory") + public String getMemoryStatus() { + MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); + String memoryStats = ""; + + String init = String.format("Initial: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getInit() /1073741824); + String usedHeap = String.format("Used: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getUsed() /1073741824); + String maxHeap = String.format("Max: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getMax() /1073741824); + String committed = String.format("Committed: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getCommitted() /1073741824); + memoryStats += init; + memoryStats += usedHeap; + memoryStats += maxHeap; + memoryStats += committed; + + return memoryStats; + } +} diff --git a/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java new file mode 100644 index 0000000000..663f96b3b5 --- /dev/null +++ b/micronaut/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java @@ -0,0 +1,25 @@ +package com.baeldung.micronaut.vs.springboot.service; + +import javax.inject.Singleton; + +@Singleton +public class ArithmeticService { + public float add(float number1, float number2) { + return number1 + number2; + } + + public float subtract(float number1, float number2) { + return number1 - number2; + } + + public float multiply(float number1, float number2) { + return number1 * number2; + } + + public float divide(float number1, float number2) { + if (number2 == 0) { + throw new IllegalArgumentException("'number2' cannot be zero"); + } + return number1 / number2; + } +} diff --git a/micronaut/src/test/java/com/baeldung/micronaut/vs/springboot/ArithmeticClientUnitTest.java b/micronaut/src/test/java/com/baeldung/micronaut/vs/springboot/ArithmeticClientUnitTest.java new file mode 100644 index 0000000000..abf10d6f5e --- /dev/null +++ b/micronaut/src/test/java/com/baeldung/micronaut/vs/springboot/ArithmeticClientUnitTest.java @@ -0,0 +1,61 @@ +package com.baeldung.micronaut.vs.springboot; + +import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; + +import io.micronaut.context.ApplicationContext; +import io.micronaut.runtime.server.EmbeddedServer; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.micronaut.vs.springboot.client.ArithmeticClientImpl; + + +public class ArithmeticClientUnitTest { + private EmbeddedServer server; + private ArithmeticClientImpl client; + + @Before + public void setup() { + server = ApplicationContext.run(EmbeddedServer.class); + client = server.getApplicationContext().getBean(ArithmeticClientImpl.class); + } + + @After + public void cleanup() { + server.stop(); + } + + @Test + public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() { + String expected = Float.valueOf(10 + 20).toString(); + assertEquals(expected, client.sum(10, 20)); + } + + @Test + public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() { + String expected = Float.valueOf(20 - 10).toString(); + assertEquals(expected, client.subtract(20, 10)); + } + + @Test + public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() { + String expected = Float.valueOf(10 * 20).toString(); + assertEquals(expected, client.multiply(10, 20)); + } + + @Test + public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() { + String expected = Float.valueOf(30 / 10).toString(); + assertEquals(expected, client.divide(30, 10)); + } + + @Test + public void whenMemory_thenCorrectAnswerReturned() { + String expected = "Initial:"; + assertThat(client.memory(), containsString(expected)); + } +} diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java new file mode 100644 index 0000000000..5bf1e0a57c --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/CompareApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.micronaut.vs.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung.micronaut.vs.springboot") +public class CompareApplication { + public static void main(final String[] args) { + SpringApplication.run(CompareApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java new file mode 100644 index 0000000000..2bb8046f66 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticController.java @@ -0,0 +1,60 @@ +package com.baeldung.micronaut.vs.springboot.controller; + +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.micronaut.vs.springboot.service.ArithmeticService; + +@RestController +@RequestMapping("/math") +public class ArithmeticController { + @Autowired + private ArithmeticService arithmeticService; + + @GetMapping("/sum/{number1}/{number2}") + public float getSum(@PathVariable("number1") float number1, @PathVariable("number2") float number2) { + return arithmeticService.add(number1, number2); + } + + @GetMapping("/subtract/{number1}/{number2}") + public float getDifference(@PathVariable("number1") float number1, @PathVariable("number2") float number2) { + return arithmeticService.subtract(number1, number2); + } + + @GetMapping("/multiply/{number1}/{number2}") + public float getMultiplication(@PathVariable("number1") float number1, @PathVariable("number2") float number2) { + return arithmeticService.multiply(number1, number2); + } + + @GetMapping("/divide/{number1}/{number2}") + public float getDivision(@PathVariable("number1") float number1, @PathVariable("number2") float number2) { + return arithmeticService.divide(number1, number2); + } + + @GetMapping("/memory") + public String getMemoryStatus() { + MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); + String memoryStats = ""; + + String init = String.format("Initial: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getInit() /1073741824); + String usedHeap = String.format("Used: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getUsed() /1073741824); + String maxHeap = String.format("Max: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getMax() /1073741824); + String committed = String.format("Committed: %.2f GB \n", + (double)memoryBean.getHeapMemoryUsage().getCommitted() /1073741824); + memoryStats += init; + memoryStats += usedHeap; + memoryStats += maxHeap; + memoryStats += committed; + + return memoryStats; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java new file mode 100644 index 0000000000..485cfd4d8c --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/main/java/com/baeldung/micronaut/vs/springboot/service/ArithmeticService.java @@ -0,0 +1,25 @@ +package com.baeldung.micronaut.vs.springboot.service; + +import org.springframework.stereotype.Service; + +@Service +public class ArithmeticService { + public float add(float number1, float number2) { + return number1 + number2; + } + + public float subtract(float number1, float number2) { + return number1 - number2; + } + + public float multiply(float number1, float number2) { + return number1 * number2; + } + + public float divide(float number1, float number2) { + if (number2 == 0) { + throw new IllegalArgumentException("'number2' cannot be zero"); + } + return number1 / number2; + } +} diff --git a/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticControllerUnitTest.java b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticControllerUnitTest.java new file mode 100644 index 0000000000..8734faef54 --- /dev/null +++ b/spring-boot-modules/spring-boot-mvc-3/src/test/java/com/baeldung/micronaut/vs/springboot/controller/ArithmeticControllerUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.micronaut.vs.springboot.controller; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.hamcrest.Matchers.containsString; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import com.baeldung.micronaut.vs.springboot.CompareApplication; + +@SpringBootTest(classes = CompareApplication.class) +@AutoConfigureMockMvc +public class ArithmeticControllerUnitTest { + @Autowired + private MockMvc mockMvc; + + @Test + public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() throws Exception { + Float expected = Float.valueOf(10 + 20); + this.mockMvc.perform(MockMvcRequestBuilders.get("/math/sum/10/20") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(expected.toString())); + } + + @Test + public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() throws Exception { + Float expected = Float.valueOf(20 - 10); + this.mockMvc.perform(MockMvcRequestBuilders.get("/math/subtract/20/10") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(expected.toString())); + } + + @Test + public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() throws Exception { + Float expected = Float.valueOf(20 * 10); + this.mockMvc.perform(MockMvcRequestBuilders.get("/math/multiply/20/10") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(expected.toString())); + } + + @Test + public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() throws Exception { + Float expected = Float.valueOf(20 / 10); + this.mockMvc.perform(MockMvcRequestBuilders.get("/math/divide/20/10") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(expected.toString())); + } + + @Test + public void whenMemory_thenMemoryStringReturned() throws Exception { + this.mockMvc.perform(MockMvcRequestBuilders.get("/math/memory") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Initial:"))); + } +} From 1a59d999912626947c0c20f3747cae87beec6653 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Tue, 14 Dec 2021 23:05:19 +0000 Subject: [PATCH 06/17] [JAVA-8377] Split spring-boot-testing module --- spring-boot-modules/pom.xml | 1 + .../spring-boot-testing-2/.gitignore | 5 + .../.mvn/wrapper/maven-wrapper.properties | 1 + .../spring-boot-testing-2/README.md | 12 + .../spring-boot-testing-2/mvnw | 227 ++++++++++++++++++ .../spring-boot-testing-2/mvnw.cmd | 145 +++++++++++ .../spring-boot-testing-2/pom.xml | 90 +++++++ .../java/com/baeldung/boot/Application.java | 12 + .../baeldung/component/OtherComponent.java | 0 .../testloglevel/TestLogLevelApplication.java | 0 .../testloglevel/TestLogLevelController.java | 0 .../src/main/resources/application.properties | 1 + .../com/baeldung/boot/SpringContextTest.java | 15 ++ ...ltiProfileTestLogLevelIntegrationTest.java | 0 .../LogbackTestLogLevelIntegrationTest.java | 0 ...estLogLevelWithProfileIntegrationTest.java | 0 .../application-logback-test.properties | 0 .../application-logback-test2.properties | 0 .../application-logging-test.properties | 0 .../src/test/resources/application.properties | 3 + .../test/resources/logback-multiprofile.xml | 0 .../src/test/resources/logback-test.xml | 13 + .../spring-boot-testing/README.md | 2 +- .../src/test/resources/logback-test.xml | 1 - 24 files changed, 526 insertions(+), 2 deletions(-) create mode 100644 spring-boot-modules/spring-boot-testing-2/.gitignore create mode 100644 spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties create mode 100644 spring-boot-modules/spring-boot-testing-2/README.md create mode 100755 spring-boot-modules/spring-boot-testing-2/mvnw create mode 100644 spring-boot-modules/spring-boot-testing-2/mvnw.cmd create mode 100644 spring-boot-modules/spring-boot-testing-2/pom.xml create mode 100644 spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/boot/Application.java rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/main/java/com/baeldung/component/OtherComponent.java (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java (100%) create mode 100644 spring-boot-modules/spring-boot-testing-2/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/boot/SpringContextTest.java rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/resources/application-logback-test.properties (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/resources/application-logback-test2.properties (100%) rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/resources/application-logging-test.properties (100%) create mode 100644 spring-boot-modules/spring-boot-testing-2/src/test/resources/application.properties rename spring-boot-modules/{spring-boot-testing => spring-boot-testing-2}/src/test/resources/logback-multiprofile.xml (100%) create mode 100644 spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-test.xml diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index b0a9f41acc..ed8237771c 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -70,6 +70,7 @@ spring-boot-swagger spring-boot-swagger-jwt spring-boot-testing + spring-boot-testing-2 spring-boot-vue spring-boot-actuator spring-boot-data-2 diff --git a/spring-boot-modules/spring-boot-testing-2/.gitignore b/spring-boot-modules/spring-boot-testing-2/.gitignore new file mode 100644 index 0000000000..da7c2c5c0a --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/.gitignore @@ -0,0 +1,5 @@ +/target/ +.settings/ +.classpath +.project + diff --git a/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties b/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 0000000000..a447c9fa81 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1 @@ +distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing-2/README.md b/spring-boot-modules/spring-boot-testing-2/README.md new file mode 100644 index 0000000000..33664a4448 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/README.md @@ -0,0 +1,12 @@ +## Spring Boot Testing + +This module contains articles about Spring Boot testing + +### The Course + +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: + +- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level) +- More articles: [[<-- prev]](../spring-boot-testing) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing-2/mvnw b/spring-boot-modules/spring-boot-testing-2/mvnw new file mode 100755 index 0000000000..e96ccd5fbb --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/mvnw @@ -0,0 +1,227 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Maven2 Start Up Batch script +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# M2_HOME - location of maven2's installed home dir +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "`uname`" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + export JAVA_HOME="`/usr/libexec/java_home`" + else + export JAVA_HOME="/Library/Java/Home" + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=`java-config --jre-home` + fi +fi + +if [ -z "$M2_HOME" ] ; then + ## resolve links - $0 may be a link to maven's home + PRG="$0" + + # need this for relative symlinks + while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG="`dirname "$PRG"`/$link" + fi + done + + saveddir=`pwd` + + M2_HOME=`dirname "$PRG"`/.. + + # make it fully qualified + M2_HOME=`cd "$M2_HOME" && pwd` + + cd "$saveddir" + # echo Using m2 at $M2_HOME +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --unix "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --unix "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$M2_HOME" ] && + M2_HOME="`(cd "$M2_HOME"; pwd)`" + [ -n "$JAVA_HOME" ] && + JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" + # TODO classpath? +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="`which javac`" + if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=`which readlink` + if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then + if $darwin ; then + javaHome="`dirname \"$javaExecutable\"`" + javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + else + javaExecutable="`readlink -f \"$javaExecutable\"`" + fi + javaHome="`dirname \"$javaExecutable\"`" + javaHome=`expr "$javaHome" : '\(.*\)/bin'` + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="`which java`" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=`cd "$wdir/.."; pwd` + fi + # end of workaround + done + echo "${basedir}" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + echo "$(tr -s '\n' ' ' < "$1")" + fi +} + +BASE_DIR=`find_maven_basedir "$(pwd)"` +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +if [ "$MVNW_VERBOSE" = true ]; then + echo $MAVEN_PROJECTBASEDIR +fi +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$M2_HOME" ] && + M2_HOME=`cygpath --path --windows "$M2_HOME"` + [ -n "$JAVA_HOME" ] && + JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` + [ -n "$CLASSPATH" ] && + CLASSPATH=`cygpath --path --windows "$CLASSPATH"` + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` +fi + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +exec "$JAVACMD" \ + $MAVEN_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring-boot-modules/spring-boot-testing-2/mvnw.cmd b/spring-boot-modules/spring-boot-testing-2/mvnw.cmd new file mode 100644 index 0000000000..6a6eec39ba --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/mvnw.cmd @@ -0,0 +1,145 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Maven2 Start Up Batch script +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM M2_HOME - location of maven2's installed home dir +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" +if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" + +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" +if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%" == "on" pause + +if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% + +exit /B %ERROR_CODE% diff --git a/spring-boot-modules/spring-boot-testing-2/pom.xml b/spring-boot-modules/spring-boot-testing-2/pom.xml new file mode 100644 index 0000000000..3ac8f9b697 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + spring-boot-testing-2 + spring-boot-testing-2 + war + This is simple boot application for demonstrating testing features. + + + com.baeldung.spring-boot-modules + spring-boot-modules + 1.0.0-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + spring-boot-testing + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-war-plugin + + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + get-the-git-infos + + revision + + initialize + + + validate-the-git-infos + + validateRevision + + package + + + + true + ${project.build.outputDirectory}/git.properties + + + + org.codehaus.gmavenplus + gmavenplus-plugin + ${gmavenplus-plugin.version} + + + + compileTests + + + + + + + + + + com.baeldung.boot.Application + 2.2.4 + 1.6 + 2.5.0 + + + diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/boot/Application.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/boot/Application.java new file mode 100644 index 0000000000..a78bb0410b --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/boot/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.boot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/component/OtherComponent.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java rename to spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/component/OtherComponent.java diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java rename to spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java b/spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java rename to spring-boot-modules/spring-boot-testing-2/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java diff --git a/spring-boot-modules/spring-boot-testing-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-testing-2/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/boot/SpringContextTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/boot/SpringContextTest.java new file mode 100644 index 0000000000..f3c8b9a954 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/boot/SpringContextTest.java @@ -0,0 +1,15 @@ +package com.baeldung.boot; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java rename to spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java rename to spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java rename to spring-boot-modules/spring-boot-testing-2/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-testing/src/test/resources/application-logback-test.properties b/spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logback-test.properties similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/resources/application-logback-test.properties rename to spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logback-test.properties diff --git a/spring-boot-modules/spring-boot-testing/src/test/resources/application-logback-test2.properties b/spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logback-test2.properties similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/resources/application-logback-test2.properties rename to spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logback-test2.properties diff --git a/spring-boot-modules/spring-boot-testing/src/test/resources/application-logging-test.properties b/spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logging-test.properties similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/resources/application-logging-test.properties rename to spring-boot-modules/spring-boot-testing-2/src/test/resources/application-logging-test.properties diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/resources/application.properties b/spring-boot-modules/spring-boot-testing-2/src/test/resources/application.properties new file mode 100644 index 0000000000..2b406d1c6e --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/test/resources/application.properties @@ -0,0 +1,3 @@ +# logging.level.com.baeldung.testloglevel=DEBUG + +# logging.level.root=INFO diff --git a/spring-boot-modules/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-multiprofile.xml similarity index 100% rename from spring-boot-modules/spring-boot-testing/src/test/resources/logback-multiprofile.xml rename to spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-multiprofile.xml diff --git a/spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-test.xml b/spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..0528aa88f3 --- /dev/null +++ b/spring-boot-modules/spring-boot-testing-2/src/test/resources/logback-test.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + diff --git a/spring-boot-modules/spring-boot-testing/README.md b/spring-boot-modules/spring-boot-testing/README.md index 058c78c9bb..4d23c32422 100644 --- a/spring-boot-modules/spring-boot-testing/README.md +++ b/spring-boot-modules/spring-boot-testing/README.md @@ -10,9 +10,9 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Testing with Spring and Spock](https://www.baeldung.com/spring-spock-testing) - [Exclude Auto-Configuration Classes in Spring Boot Tests](https://www.baeldung.com/spring-boot-exclude-auto-configuration-test) -- [Setting the Log Level in Spring Boot when Testing](https://www.baeldung.com/spring-boot-testing-log-level) - [Embedded Redis Server with Spring Boot Test](https://www.baeldung.com/spring-embedded-redis) - [Testing Spring Boot @ConfigurationProperties](https://www.baeldung.com/spring-boot-testing-configurationproperties) - [Prevent ApplicationRunner or CommandLineRunner Beans From Executing During Junit Testing](https://www.baeldung.com/spring-junit-prevent-runner-beans-testing-execution) - [Testing in Spring Boot](https://www.baeldung.com/spring-boot-testing) - [Fixing the NoSuchMethodError JUnit Error](https://www.baeldung.com/junit-nosuchmethoderror) +- More articles: [[more -->]](../spring-boot-testing-2) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing/src/test/resources/logback-test.xml b/spring-boot-modules/spring-boot-testing/src/test/resources/logback-test.xml index 0528aa88f3..9553dcad41 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/resources/logback-test.xml +++ b/spring-boot-modules/spring-boot-testing/src/test/resources/logback-test.xml @@ -9,5 +9,4 @@ - From 9f382cd21cb89ddd11e455368cdb8436b932551e Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 15 Dec 2021 10:50:22 +0000 Subject: [PATCH 07/17] [JAVA-8377] Code clean up --- .../.mvn/wrapper/maven-wrapper.properties | 1 - .../spring-boot-testing-2/mvnw | 227 ------------------ .../spring-boot-testing-2/mvnw.cmd | 145 ----------- .../spring-boot-testing-2/pom.xml | 59 +---- 4 files changed, 1 insertion(+), 431 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties delete mode 100755 spring-boot-modules/spring-boot-testing-2/mvnw delete mode 100644 spring-boot-modules/spring-boot-testing-2/mvnw.cmd diff --git a/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties b/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties deleted file mode 100644 index a447c9fa81..0000000000 --- a/spring-boot-modules/spring-boot-testing-2/.mvn/wrapper/maven-wrapper.properties +++ /dev/null @@ -1 +0,0 @@ -distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-testing-2/mvnw b/spring-boot-modules/spring-boot-testing-2/mvnw deleted file mode 100755 index e96ccd5fbb..0000000000 --- a/spring-boot-modules/spring-boot-testing-2/mvnw +++ /dev/null @@ -1,227 +0,0 @@ -#!/bin/sh -# ---------------------------------------------------------------------------- -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you 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 -# -# http://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. -# ---------------------------------------------------------------------------- - -# ---------------------------------------------------------------------------- -# Maven2 Start Up Batch script -# -# Required ENV vars: -# ------------------ -# JAVA_HOME - location of a JDK home dir -# -# Optional ENV vars -# ----------------- -# M2_HOME - location of maven2's installed home dir -# MAVEN_OPTS - parameters passed to the Java VM when running Maven -# e.g. to debug Maven itself, use -# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -# MAVEN_SKIP_RC - flag to disable loading of mavenrc files -# ---------------------------------------------------------------------------- - -if [ -z "$MAVEN_SKIP_RC" ] ; then - - if [ -f /etc/mavenrc ] ; then - . /etc/mavenrc - fi - - if [ -f "$HOME/.mavenrc" ] ; then - . "$HOME/.mavenrc" - fi - -fi - -# OS specific support. $var _must_ be set to either true or false. -cygwin=false; -darwin=false; -mingw=false -case "`uname`" in - CYGWIN*) cygwin=true ;; - MINGW*) mingw=true;; - Darwin*) darwin=true - # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home - # See https://developer.apple.com/library/mac/qa/qa1170/_index.html - if [ -z "$JAVA_HOME" ]; then - if [ -x "/usr/libexec/java_home" ]; then - export JAVA_HOME="`/usr/libexec/java_home`" - else - export JAVA_HOME="/Library/Java/Home" - fi - fi - ;; -esac - -if [ -z "$JAVA_HOME" ] ; then - if [ -r /etc/gentoo-release ] ; then - JAVA_HOME=`java-config --jre-home` - fi -fi - -if [ -z "$M2_HOME" ] ; then - ## resolve links - $0 may be a link to maven's home - PRG="$0" - - # need this for relative symlinks - while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG="`dirname "$PRG"`/$link" - fi - done - - saveddir=`pwd` - - M2_HOME=`dirname "$PRG"`/.. - - # make it fully qualified - M2_HOME=`cd "$M2_HOME" && pwd` - - cd "$saveddir" - # echo Using m2 at $M2_HOME -fi - -# For Cygwin, ensure paths are in UNIX format before anything is touched -if $cygwin ; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --unix "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --unix "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --unix "$CLASSPATH"` -fi - -# For Mingw, ensure paths are in UNIX format before anything is touched -if $mingw ; then - [ -n "$M2_HOME" ] && - M2_HOME="`(cd "$M2_HOME"; pwd)`" - [ -n "$JAVA_HOME" ] && - JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" - # TODO classpath? -fi - -if [ -z "$JAVA_HOME" ]; then - javaExecutable="`which javac`" - if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then - # readlink(1) is not available as standard on Solaris 10. - readLink=`which readlink` - if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then - if $darwin ; then - javaHome="`dirname \"$javaExecutable\"`" - javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" - else - javaExecutable="`readlink -f \"$javaExecutable\"`" - fi - javaHome="`dirname \"$javaExecutable\"`" - javaHome=`expr "$javaHome" : '\(.*\)/bin'` - JAVA_HOME="$javaHome" - export JAVA_HOME - fi - fi -fi - -if [ -z "$JAVACMD" ] ; then - if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" - else - JAVACMD="$JAVA_HOME/bin/java" - fi - else - JAVACMD="`which java`" - fi -fi - -if [ ! -x "$JAVACMD" ] ; then - echo "Error: JAVA_HOME is not defined correctly." >&2 - echo " We cannot execute $JAVACMD" >&2 - exit 1 -fi - -if [ -z "$JAVA_HOME" ] ; then - echo "Warning: JAVA_HOME environment variable is not set." -fi - -CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher - -# traverses directory structure from process work directory to filesystem root -# first directory with .mvn subdirectory is considered project base directory -find_maven_basedir() { - - if [ -z "$1" ] - then - echo "Path not specified to find_maven_basedir" - return 1 - fi - - basedir="$1" - wdir="$1" - while [ "$wdir" != '/' ] ; do - if [ -d "$wdir"/.mvn ] ; then - basedir=$wdir - break - fi - # workaround for JBEAP-8937 (on Solaris 10/Sparc) - if [ -d "${wdir}" ]; then - wdir=`cd "$wdir/.."; pwd` - fi - # end of workaround - done - echo "${basedir}" -} - -# concatenates all lines of a file -concat_lines() { - if [ -f "$1" ]; then - echo "$(tr -s '\n' ' ' < "$1")" - fi -} - -BASE_DIR=`find_maven_basedir "$(pwd)"` -if [ -z "$BASE_DIR" ]; then - exit 1; -fi - -export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} -if [ "$MVNW_VERBOSE" = true ]; then - echo $MAVEN_PROJECTBASEDIR -fi -MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" - -# For Cygwin, switch paths to Windows format before running java -if $cygwin; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --path --windows "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --windows "$CLASSPATH"` - [ -n "$MAVEN_PROJECTBASEDIR" ] && - MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` -fi - -WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -exec "$JAVACMD" \ - $MAVEN_OPTS \ - -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ - "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ - ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/spring-boot-modules/spring-boot-testing-2/mvnw.cmd b/spring-boot-modules/spring-boot-testing-2/mvnw.cmd deleted file mode 100644 index 6a6eec39ba..0000000000 --- a/spring-boot-modules/spring-boot-testing-2/mvnw.cmd +++ /dev/null @@ -1,145 +0,0 @@ -@REM ---------------------------------------------------------------------------- -@REM Licensed to the Apache Software Foundation (ASF) under one -@REM or more contributor license agreements. See the NOTICE file -@REM distributed with this work for additional information -@REM regarding copyright ownership. The ASF licenses this file -@REM to you under the Apache License, Version 2.0 (the -@REM "License"); you may not use this file except in compliance -@REM with the License. You may obtain a copy of the License at -@REM -@REM http://www.apache.org/licenses/LICENSE-2.0 -@REM -@REM Unless required by applicable law or agreed to in writing, -@REM software distributed under the License is distributed on an -@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -@REM KIND, either express or implied. See the License for the -@REM specific language governing permissions and limitations -@REM under the License. -@REM ---------------------------------------------------------------------------- - -@REM ---------------------------------------------------------------------------- -@REM Maven2 Start Up Batch script -@REM -@REM Required ENV vars: -@REM JAVA_HOME - location of a JDK home dir -@REM -@REM Optional ENV vars -@REM M2_HOME - location of maven2's installed home dir -@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands -@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending -@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven -@REM e.g. to debug Maven itself, use -@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files -@REM ---------------------------------------------------------------------------- - -@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' -@echo off -@REM set title of command window -title %0 -@REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' -@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% - -@REM set %HOME% to equivalent of $HOME -if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") - -@REM Execute a user defined script before this one -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre -@REM check for pre script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" -if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" -:skipRcPre - -@setlocal - -set ERROR_CODE=0 - -@REM To isolate internal variables from possible post scripts, we use another setlocal -@setlocal - -@REM ==== START VALIDATION ==== -if not "%JAVA_HOME%" == "" goto OkJHome - -echo. -echo Error: JAVA_HOME not found in your environment. >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -:OkJHome -if exist "%JAVA_HOME%\bin\java.exe" goto init - -echo. -echo Error: JAVA_HOME is set to an invalid directory. >&2 -echo JAVA_HOME = "%JAVA_HOME%" >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -@REM ==== END VALIDATION ==== - -:init - -@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". -@REM Fallback to current working directory if not found. - -set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% -IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir - -set EXEC_DIR=%CD% -set WDIR=%EXEC_DIR% -:findBaseDir -IF EXIST "%WDIR%"\.mvn goto baseDirFound -cd .. -IF "%WDIR%"=="%CD%" goto baseDirNotFound -set WDIR=%CD% -goto findBaseDir - -:baseDirFound -set MAVEN_PROJECTBASEDIR=%WDIR% -cd "%EXEC_DIR%" -goto endDetectBaseDir - -:baseDirNotFound -set MAVEN_PROJECTBASEDIR=%EXEC_DIR% -cd "%EXEC_DIR%" - -:endDetectBaseDir - -IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig - -@setlocal EnableExtensions EnableDelayedExpansion -for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a -@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% - -:endReadAdditionalConfig - -SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" - -set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" -set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end -@endlocal & set ERROR_CODE=%ERROR_CODE% - -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost -@REM check for post script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" -if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" -:skipRcPost - -@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' -if "%MAVEN_BATCH_PAUSE%" == "on" pause - -if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% - -exit /B %ERROR_CODE% diff --git a/spring-boot-modules/spring-boot-testing-2/pom.xml b/spring-boot-modules/spring-boot-testing-2/pom.xml index 3ac8f9b697..d1000156c6 100644 --- a/spring-boot-modules/spring-boot-testing-2/pom.xml +++ b/spring-boot-modules/spring-boot-testing-2/pom.xml @@ -5,7 +5,7 @@ 4.0.0 spring-boot-testing-2 spring-boot-testing-2 - war + jar This is simple boot application for demonstrating testing features. @@ -26,65 +26,8 @@ - - spring-boot-testing - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-war-plugin - - - pl.project13.maven - git-commit-id-plugin - ${git-commit-id-plugin.version} - - - get-the-git-infos - - revision - - initialize - - - validate-the-git-infos - - validateRevision - - package - - - - true - ${project.build.outputDirectory}/git.properties - - - - org.codehaus.gmavenplus - gmavenplus-plugin - ${gmavenplus-plugin.version} - - - - compileTests - - - - - - - - com.baeldung.boot.Application - 2.2.4 - 1.6 - 2.5.0 From 89ca1a60abda0439791161534b82b6effb06e561 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Wed, 15 Dec 2021 17:31:42 +0200 Subject: [PATCH 08/17] Update README.md --- core-java-modules/core-java-lang-oop-types-2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-oop-types-2/README.md b/core-java-modules/core-java-lang-oop-types-2/README.md index e1ce057035..c5e2a75f25 100644 --- a/core-java-modules/core-java-lang-oop-types-2/README.md +++ b/core-java-modules/core-java-lang-oop-types-2/README.md @@ -5,4 +5,4 @@ This module contains articles about types in Java ### Relevant Articles: - [Convert an Array of Primitives to an Array of Objects](https://www.baeldung.com/java-primitive-array-to-object-array) -- [Check if an Enum Value Exists in Java](https://www.baeldung.com/java-find-enum-by-criteria) +- [Check if an Enum Value Exists in Java](https://www.baeldung.com/java-search-enum-values) From 8e175b72e292f78a0eca5d79963f0e2c54a1fb1d Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Wed, 15 Dec 2021 22:35:42 +0530 Subject: [PATCH 09/17] JAVA-8373: Split or move spring-boot-bootstrap module --- .../spring-boot-annotations-2/README.md | 1 + .../springbootconfiguration/Application.java | 0 .../springbootconfiguration/PersonService.java | 0 .../PersonServiceImpl.java | 0 .../src/main/resources/application.properties | 1 + .../SpringContextTest.java | 15 +++++++++++++++ .../spring-boot-bootstrap/README.md | 1 - 7 files changed, 17 insertions(+), 1 deletion(-) rename spring-boot-modules/{spring-boot-bootstrap => spring-boot-annotations-2}/src/main/java/com/baeldung/springbootconfiguration/Application.java (100%) rename spring-boot-modules/{spring-boot-bootstrap => spring-boot-annotations-2}/src/main/java/com/baeldung/springbootconfiguration/PersonService.java (100%) rename spring-boot-modules/{spring-boot-bootstrap => spring-boot-annotations-2}/src/main/java/com/baeldung/springbootconfiguration/PersonServiceImpl.java (100%) create mode 100644 spring-boot-modules/spring-boot-annotations-2/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-annotations-2/src/test/java/com/baeldung/springbootconfiguration/SpringContextTest.java diff --git a/spring-boot-modules/spring-boot-annotations-2/README.md b/spring-boot-modules/spring-boot-annotations-2/README.md index 991c771986..ec72620b37 100644 --- a/spring-boot-modules/spring-boot-annotations-2/README.md +++ b/spring-boot-modules/spring-boot-annotations-2/README.md @@ -5,4 +5,5 @@ This module contains articles about Spring Boot annotations ### Relevant Articles: - [Spring Conditional Annotations](https://www.baeldung.com/spring-conditional-annotations) +- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation) - More articles: [[<-- prev]](/spring-boot-modules/spring-boot-annotations) diff --git a/spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/Application.java b/spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/Application.java similarity index 100% rename from spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/Application.java rename to spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/Application.java diff --git a/spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/PersonService.java b/spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/PersonService.java similarity index 100% rename from spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/PersonService.java rename to spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/PersonService.java diff --git a/spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/PersonServiceImpl.java b/spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/PersonServiceImpl.java similarity index 100% rename from spring-boot-modules/spring-boot-bootstrap/src/main/java/com/baeldung/springbootconfiguration/PersonServiceImpl.java rename to spring-boot-modules/spring-boot-annotations-2/src/main/java/com/baeldung/springbootconfiguration/PersonServiceImpl.java diff --git a/spring-boot-modules/spring-boot-annotations-2/src/main/resources/application.properties b/spring-boot-modules/spring-boot-annotations-2/src/main/resources/application.properties new file mode 100644 index 0000000000..ba17f62cd0 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations-2/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.main.web-application-type=none \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-annotations-2/src/test/java/com/baeldung/springbootconfiguration/SpringContextTest.java b/spring-boot-modules/spring-boot-annotations-2/src/test/java/com/baeldung/springbootconfiguration/SpringContextTest.java new file mode 100644 index 0000000000..11bbad8bb2 --- /dev/null +++ b/spring-boot-modules/spring-boot-annotations-2/src/test/java/com/baeldung/springbootconfiguration/SpringContextTest.java @@ -0,0 +1,15 @@ +package com.baeldung.springbootconfiguration; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringContextTest { + + @Test + public void contextLoads() { + } +} diff --git a/spring-boot-modules/spring-boot-bootstrap/README.md b/spring-boot-modules/spring-boot-bootstrap/README.md index 02ec52f755..5787f3262d 100644 --- a/spring-boot-modules/spring-boot-bootstrap/README.md +++ b/spring-boot-modules/spring-boot-bootstrap/README.md @@ -9,5 +9,4 @@ This module contains articles about bootstrapping Spring Boot applications. - [Deploy a Spring Boot Application to Google App Engine](https://www.baeldung.com/spring-boot-google-app-engine) - [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift) - [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk) -- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation) - [Implement Health Checks in OpenShift](https://www.baeldung.com/ops/openshift-health-checks) From b8e52354460c75c9bd909f2f156517ecf81fb509 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 15 Dec 2021 17:53:39 +0000 Subject: [PATCH 10/17] [JAVA-8679] Fix test for JDK 17 --- .../java9/modules/ModuleAPIUnitTest.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/core-java-modules/core-java-9-jigsaw/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java b/core-java-modules/core-java-9-jigsaw/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java index f6ea266676..ed2dcaf695 100644 --- a/core-java-modules/core-java-9-jigsaw/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java +++ b/core-java-modules/core-java-9-jigsaw/src/test/java/com/baeldung/java9/modules/ModuleAPIUnitTest.java @@ -2,12 +2,21 @@ package com.baeldung.java9.modules; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.Matchers.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.collection.IsEmptyCollection.empty; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import java.lang.module.ModuleDescriptor; -import java.lang.module.ModuleDescriptor.*; + +import java.lang.module.ModuleDescriptor.Builder; +import java.lang.module.ModuleDescriptor.Exports; +import java.lang.module.ModuleDescriptor.Opens; +import java.lang.module.ModuleDescriptor.Provides; +import java.lang.module.ModuleDescriptor.Requires; import java.sql.Date; import java.sql.Driver; import java.util.HashMap; @@ -16,7 +25,7 @@ import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; -import org.junit.Ignore; + public class ModuleAPIUnitTest { @@ -28,14 +37,9 @@ public class ModuleAPIUnitTest { @Before public void setUp() { - Class hashMapClass = HashMap.class; - javaBaseModule = hashMapClass.getModule(); - - Class dateClass = Date.class; - javaSqlModule = dateClass.getModule(); - - Class personClass = Person.class; - module = personClass.getModule(); + javaBaseModule = HashMap.class.getModule(); + javaSqlModule = Date.class.getModule(); + module = Person.class.getModule(); } @Test @@ -111,7 +115,6 @@ public class ModuleAPIUnitTest { } @Test - @Ignore // fixing in http://team.baeldung.com/browse/JAVA-8679 public void givenModules_whenAccessingModuleDescriptorProvides_thenProvidesAreReturned() { Set javaBaseProvides = javaBaseModule.getDescriptor().provides(); Set javaSqlProvides = javaSqlModule.getDescriptor().provides(); @@ -120,7 +123,7 @@ public class ModuleAPIUnitTest { .map(Provides::service) .collect(Collectors.toSet()); - assertThat(javaBaseProvidesService, contains("java.nio.file.spi.FileSystemProvider")); + assertThat(javaBaseProvidesService, hasItem("java.nio.file.spi.FileSystemProvider")); assertThat(javaSqlProvides, empty()); } @@ -132,15 +135,14 @@ public class ModuleAPIUnitTest { .map(Exports::source) .collect(Collectors.toSet()); - assertThat(javaSqlExportsSource, hasItems("java.sql", "javax.sql")); + assertThat(javaSqlExportsSource, hasItems("java.sql", "javax.sql")); } @Test public void givenModules_whenAccessingModuleDescriptorUses_thenUsesAreReturned() { - Set javaBaseUses = javaBaseModule.getDescriptor().uses(); Set javaSqlUses = javaSqlModule.getDescriptor().uses(); - assertThat(javaSqlUses, contains("java.sql.Driver")); + assertThat(javaSqlUses, hasItem("java.sql.Driver")); } @Test From 06096168d86f999c105fef42b5053fb82d91f3aa Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Dec 2021 19:27:06 +0100 Subject: [PATCH 11/17] JAVA-8794: Set spring.mongodb.embedded.version=3.5.5 in spring-boot-persistence-mongodb --- .../src/test/resources/application.properties | 1 + 1 file changed, 1 insertion(+) create mode 100644 persistence-modules/spring-boot-persistence-mongodb/src/test/resources/application.properties diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb/src/test/resources/application.properties new file mode 100644 index 0000000000..ac8c1cf2d2 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.mongodb.embedded.version=3.5.5 \ No newline at end of file From de6bc0de3e75e13b32b01baf3f5099f4a112753c Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Dec 2021 19:27:32 +0100 Subject: [PATCH 12/17] JAVA-8794: Fix spring-data-jpa-query-2 dependencies --- persistence-modules/spring-data-jpa-query-2/pom.xml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/persistence-modules/spring-data-jpa-query-2/pom.xml b/persistence-modules/spring-data-jpa-query-2/pom.xml index 96585d9325..ebdbc2d2d9 100644 --- a/persistence-modules/spring-data-jpa-query-2/pom.xml +++ b/persistence-modules/spring-data-jpa-query-2/pom.xml @@ -30,10 +30,6 @@ com.fasterxml.jackson.core jackson-databind - - org.hibernate - hibernate-envers - javax.transaction jta @@ -56,12 +52,10 @@ org.hibernate hibernate-core - ${hibernate.version} org.hibernate hibernate-envers - ${hibernate.version} org.springframework @@ -84,8 +78,6 @@ 9.0.0.M26 1.1 21.0 - - 5.2.10.Final \ No newline at end of file From d72347f195637cfe1942558bc1db5af4fa65a490 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Dec 2021 19:53:30 +0100 Subject: [PATCH 13/17] JAVA-8794: Fix circular dependency issue in spring-security-web-thymeleaf --- .../PasswordEncoderConfiguration.java | 15 +++++++++++++++ .../SecurityConfiguration.java | 13 +++++-------- .../ViewControllerIntegrationTest.java | 2 ++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/PasswordEncoderConfiguration.java diff --git a/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/PasswordEncoderConfiguration.java b/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/PasswordEncoderConfiguration.java new file mode 100644 index 0000000000..5ffe2c35f9 --- /dev/null +++ b/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/PasswordEncoderConfiguration.java @@ -0,0 +1,15 @@ +package com.baeldung.springsecuritythymeleaf; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordEncoderConfiguration { + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java b/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java index f7f8cfb708..8e9047f629 100644 --- a/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java +++ b/spring-security-modules/spring-security-web-thymeleaf/src/main/java/com/baeldung/springsecuritythymeleaf/SecurityConfiguration.java @@ -8,6 +8,7 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.util.matcher.AntPathRequestMatcher; @Configuration @@ -32,19 +33,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { } @Autowired - public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + public void configureGlobal(AuthenticationManagerBuilder auth, PasswordEncoder passwordEncoder) throws Exception { auth.inMemoryAuthentication() .withUser("user") - .password(passwordEncoder().encode("password")) + .password(passwordEncoder.encode("password")) .roles("USER") .and() .withUser("admin") - .password(passwordEncoder().encode("admin")) + .password(passwordEncoder.encode("admin")) .roles("ADMIN"); } - - @Bean - public BCryptPasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } + } diff --git a/spring-security-modules/spring-security-web-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java b/spring-security-modules/spring-security-web-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java index c87d1bacc3..606a33dcf5 100644 --- a/spring-security-modules/spring-security-web-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java +++ b/spring-security-modules/spring-security-web-thymeleaf/src/test/java/com/baeldung/springsecuritythymeleaf/ViewControllerIntegrationTest.java @@ -9,11 +9,13 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.Import; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; @RunWith(SpringRunner.class) @WebMvcTest +@Import(PasswordEncoderConfiguration.class) public class ViewControllerIntegrationTest { @Autowired From 6ac9d1eb876502a70a8e1948aba70cfa9bf45f8c Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Dec 2021 20:01:08 +0100 Subject: [PATCH 14/17] JAVA-8794: Fix circular dependency issue in spring-session-redis --- .../spring/session/PasswordEncoderConfig.java | 15 +++++++++++++++ .../baeldung/spring/session/SecurityConfig.java | 8 ++------ 2 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/PasswordEncoderConfig.java diff --git a/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/PasswordEncoderConfig.java b/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/PasswordEncoderConfig.java new file mode 100644 index 0000000000..d9d0725204 --- /dev/null +++ b/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/PasswordEncoderConfig.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.session; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordEncoderConfig { + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java b/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java index 678c98e7eb..d9476d7704 100644 --- a/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java +++ b/spring-security-modules/spring-session/spring-session-redis/src/main/java/com/baeldung/spring/session/SecurityConfig.java @@ -15,8 +15,8 @@ import org.springframework.security.crypto.password.PasswordEncoder; public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired - public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder().encode("password")).roles("ADMIN"); + public void configureGlobal(AuthenticationManagerBuilder auth, PasswordEncoder passwordEncoder) throws Exception { + auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("password")).roles("ADMIN"); } @Override @@ -24,8 +24,4 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.httpBasic().and().authorizeRequests().antMatchers("/").hasRole("ADMIN").anyRequest().authenticated(); } - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } } From 077898aaa59dc4c612e660fe110beb5fd79b96a1 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Wed, 15 Dec 2021 20:08:11 +0100 Subject: [PATCH 15/17] JAVA-8794: Fix circular dependency issue in spring-boot-libraries-2 --- .../com/baeldung/JobRunrSpringBootApp.java | 11 ----------- .../baeldung/config/StorageProviderConfig.java | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/config/StorageProviderConfig.java diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/JobRunrSpringBootApp.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/JobRunrSpringBootApp.java index 061dba15bf..3821332132 100644 --- a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/JobRunrSpringBootApp.java +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/JobRunrSpringBootApp.java @@ -1,15 +1,11 @@ package com.baeldung; import com.baeldung.jobrunr.service.SampleJobService; -import org.jobrunr.jobs.mappers.JobMapper; import org.jobrunr.scheduling.JobScheduler; import org.jobrunr.scheduling.cron.Cron; -import org.jobrunr.storage.InMemoryStorageProvider; -import org.jobrunr.storage.StorageProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; import javax.annotation.PostConstruct; @@ -23,13 +19,6 @@ public class JobRunrSpringBootApp { SpringApplication.run(JobRunrSpringBootApp.class, args); } - @Bean - public StorageProvider storageProvider(JobMapper jobMapper) { - InMemoryStorageProvider storageProvider = new InMemoryStorageProvider(); - storageProvider.setJobMapper(jobMapper); - return storageProvider; - } - @PostConstruct public void scheduleRecurrently() { jobScheduler.scheduleRecurrently(Cron.every5minutes(), x -> x.executeSampleJob("a recurring job")); diff --git a/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/config/StorageProviderConfig.java b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/config/StorageProviderConfig.java new file mode 100644 index 0000000000..e4b8cf12d7 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries-2/src/main/java/com/baeldung/config/StorageProviderConfig.java @@ -0,0 +1,18 @@ +package com.baeldung.config; + +import org.jobrunr.jobs.mappers.JobMapper; +import org.jobrunr.storage.InMemoryStorageProvider; +import org.jobrunr.storage.StorageProvider; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class StorageProviderConfig { + + @Bean + public StorageProvider storageProvider(JobMapper jobMapper) { + InMemoryStorageProvider storageProvider = new InMemoryStorageProvider(); + storageProvider.setJobMapper(jobMapper); + return storageProvider; + } +} From 106603023d15179cf1b96a1a327c392d52bd6830 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 16 Dec 2021 10:49:19 +0100 Subject: [PATCH 16/17] JAVA-8794: Update spring-cloud-starter version --- spring-boot-modules/spring-boot-runtime/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-runtime/pom.xml b/spring-boot-modules/spring-boot-runtime/pom.xml index d42ae303a7..2f25c28b06 100644 --- a/spring-boot-modules/spring-boot-runtime/pom.xml +++ b/spring-boot-modules/spring-boot-runtime/pom.xml @@ -95,7 +95,7 @@ 2.2 18.0 3.1.7 - 3.0.2 + 3.1.0 4.5.8 From 15eeebb140d5631d672f6378201b367fcb0b50eb Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Thu, 16 Dec 2021 12:21:02 +0100 Subject: [PATCH 17/17] JAVA-8794: Leave spring-zuul-rate-limiting on 2.4.7 --- .../spring-cloud-zuul/spring-zuul-rate-limiting/pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spring-cloud/spring-cloud-zuul/spring-zuul-rate-limiting/pom.xml b/spring-cloud/spring-cloud-zuul/spring-zuul-rate-limiting/pom.xml index e09fcd3711..2969b5eed9 100644 --- a/spring-cloud/spring-cloud-zuul/spring-zuul-rate-limiting/pom.xml +++ b/spring-cloud/spring-cloud-zuul/spring-zuul-rate-limiting/pom.xml @@ -42,6 +42,8 @@ 2.2.0.RELEASE + 2.4.7 + 2020.0.4 \ No newline at end of file