Renamed and repackaged the example for the blog-article
authorKai Moritz <kai@juplo.de>
Sat, 19 Sep 2020 08:11:41 +0000 (10:11 +0200)
committerKai Moritz <kai@juplo.de>
Sat, 19 Sep 2020 11:29:02 +0000 (13:29 +0200)
pom.xml
src/main/java/de/juplo/TestController.java [deleted file]
src/main/java/de/juplo/ThymeleafTestApplication.java [deleted file]
src/main/java/de/juplo/demo/ExceptionHandlingApplication.java [new file with mode: 0644]
src/main/java/de/juplo/demo/ThymeleafController.java [new file with mode: 0644]
src/test/java/de/juplo/ThymeleafTestApplicationTests.java [deleted file]
src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 36cdb2f..e0b19e8 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -8,11 +8,11 @@
                <version>2.3.4.RELEASE</version>
                <relativePath/> <!-- lookup parent from repository -->
        </parent>
-       <groupId>de.juplo</groupId>
-       <artifactId>thymeleafe-test</artifactId>
+       <groupId>de.juplo.demo</groupId>
+       <artifactId>exception-handling</artifactId>
        <version>0.0.1-SNAPSHOT</version>
-       <name>Thymeleaf-Test</name>
-       <description>Standalone version of the ThymeleafTest in http-resources</description>
+       <name>Testing Exception-Handling</name>
+       <description>Example-project, that showcases, how to test exception-handling in the springframework</description>
 
        <properties>
                <java.version>11</java.version>
diff --git a/src/main/java/de/juplo/TestController.java b/src/main/java/de/juplo/TestController.java
deleted file mode 100644 (file)
index 5a4d148..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-package de.juplo;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.thymeleaf.exceptions.TemplateInputException;
-
-@Controller
-public class TestController
-{
-    private final static Logger LOG =
-            LoggerFactory.getLogger(TestController.class);
-
-
-    @RequestMapping("/controller.html")
-    public String controller(
-            @RequestParam String template,
-            Model model
-    )
-    {
-        model.addAttribute("template", template);
-        return template;
-    }
-
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    @ExceptionHandler(TemplateInputException.class)
-    public void templateInputException(TemplateInputException e)
-    {
-        LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
-    }
-}
-
diff --git a/src/main/java/de/juplo/ThymeleafTestApplication.java b/src/main/java/de/juplo/ThymeleafTestApplication.java
deleted file mode 100644 (file)
index aee3821..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-package de.juplo;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class ThymeleafTestApplication {
-
-       public static void main(String[] args) {
-               SpringApplication.run(ThymeleafTestApplication.class, args);
-       }
-
-}
diff --git a/src/main/java/de/juplo/demo/ExceptionHandlingApplication.java b/src/main/java/de/juplo/demo/ExceptionHandlingApplication.java
new file mode 100644 (file)
index 0000000..584f702
--- /dev/null
@@ -0,0 +1,13 @@
+package de.juplo.demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class ExceptionHandlingApplication {
+
+       public static void main(String[] args) {
+               SpringApplication.run(ExceptionHandlingApplication.class, args);
+       }
+
+}
diff --git a/src/main/java/de/juplo/demo/ThymeleafController.java b/src/main/java/de/juplo/demo/ThymeleafController.java
new file mode 100644 (file)
index 0000000..f7b12ff
--- /dev/null
@@ -0,0 +1,38 @@
+package de.juplo.demo;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.thymeleaf.exceptions.TemplateInputException;
+
+@Controller
+public class ThymeleafController
+{
+    private final static Logger LOG =
+            LoggerFactory.getLogger(ThymeleafController.class);
+
+
+    @RequestMapping("/controller.html")
+    public String controller(
+            @RequestParam String template,
+            Model model
+    )
+    {
+        model.addAttribute("template", template);
+        return template;
+    }
+
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    @ExceptionHandler(TemplateInputException.class)
+    public void templateInputException(TemplateInputException e)
+    {
+        LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
+    }
+}
+
diff --git a/src/test/java/de/juplo/ThymeleafTestApplicationTests.java b/src/test/java/de/juplo/ThymeleafTestApplicationTests.java
deleted file mode 100644 (file)
index adb1e24..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-package de.juplo;
-
-import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
-import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.web.servlet.MockMvc;
-import org.springframework.test.web.servlet.MvcResult;
-
-import java.net.URI;
-
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-
-@SpringBootTest
-@AutoConfigureWebMvc
-@AutoConfigureMockMvc
-class ThymeleafTestApplicationTests {
-       private final static Logger LOG =
-                       LoggerFactory.getLogger(ThymeleafTestApplicationTests.class);
-
-       @Autowired
-       MockMvc mvc;
-
-
-       @Test
-       void contextLoads() throws Exception {
-       }
-
-       @Test
-       void requests() throws Exception {
-               mvc
-                               .perform(get(URI.create("http://test/controller.html?template=remote")))
-                               .andExpect(status().isOk());
-               mvc
-                               .perform(get(URI.create("http://test/controller.html?template=local")))
-                               .andExpect(status().isOk());
-               // The expected behaviour of the following test is, that the
-               // TemplateInputException, that is thrown by Thymeleaf because of the non-existent
-               // template-resource, is catched and reported as 503 Internal Server Error, as it
-               // happens in the real Spring-MVC environment, when you start the web-server and
-               // trigger the error manually.
-               // Instead, the exception bubbles up, becomes wrapped in a NestedServletException
-               // and is thrown in the call to perform()!
-               mvc
-                               .perform(get(URI.create("http://test/controller.html?template=foo")))
-                               .andExpect(status().isInternalServerError());
-       }
-
-}
diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java
new file mode 100644 (file)
index 0000000..71a415a
--- /dev/null
@@ -0,0 +1,52 @@
+package de.juplo.demo;
+
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.web.servlet.MockMvc;
+
+import java.net.URI;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+@SpringBootTest
+@AutoConfigureWebMvc
+@AutoConfigureMockMvc
+class ExceptionHandlingApplicationTests {
+       private final static Logger LOG =
+                       LoggerFactory.getLogger(ExceptionHandlingApplicationTests.class);
+
+       @Autowired
+       MockMvc mvc;
+
+
+       @Test
+       void contextLoads() throws Exception {
+       }
+
+       @Test
+       void requests() throws Exception {
+               mvc
+                               .perform(get(URI.create("http://test/controller.html?template=remote")))
+                               .andExpect(status().isOk());
+               mvc
+                               .perform(get(URI.create("http://test/controller.html?template=local")))
+                               .andExpect(status().isOk());
+               // The expected behaviour of the following test is, that the
+               // TemplateInputException, that is thrown by Thymeleaf because of the non-existent
+               // template-resource, is catched and reported as 503 Internal Server Error, as it
+               // happens in the real Spring-MVC environment, when you start the web-server and
+               // trigger the error manually.
+               // Instead, the exception bubbles up, becomes wrapped in a NestedServletException
+               // and is thrown in the call to perform()!
+               mvc
+                               .perform(get(URI.create("http://test/controller.html?template=foo")))
+                               .andExpect(status().isInternalServerError());
+       }
+
+}