diff --git a/spring-thymeleaf-2/pom.xml b/spring-thymeleaf-2/pom.xml index 1b95cac43c..ddf71e2ea1 100644 --- a/spring-thymeleaf-2/pom.xml +++ b/spring-thymeleaf-2/pom.xml @@ -5,11 +5,17 @@ spring-thymeleaf-2 war + + + + + + - com.baeldung - parent-boot-2 - 0.0.1-SNAPSHOT - ../parent-boot-2 + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + @@ -21,6 +27,12 @@ org.springframework.boot spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/lists/ListsController.java b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/lists/ListsController.java new file mode 100644 index 0000000000..55a7f7c38e --- /dev/null +++ b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/lists/ListsController.java @@ -0,0 +1,64 @@ +package com.baeldung.thymeleaf.lists; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/lists") +public class ListsController { + + @GetMapping("/toList") + public String usingToList(Model model) { + List colors = getColors(); + String[] colorsArray = colors.toArray(new String[0]); + model.addAttribute("myArray", colorsArray); + return "lists/toList"; + } + + @GetMapping("/contains") + public String usingContains(Model model) { + model.addAttribute("myList", getColors()); + model.addAttribute("others", getOtherColors()); + return "lists/contains"; + } + + @GetMapping("/size") + public String usingSize(Model model) { + model.addAttribute("myList", getColors()); + return "lists/size"; + } + + @GetMapping("/isEmpty") + public String usingIsEmpty(Model model) { + model.addAttribute("myList", getColors()); + return "lists/isEmpty"; + } + + @GetMapping("/sort") + public String usingSort(Model model) { + model.addAttribute("myList", getColors()); + model.addAttribute("reverse", Comparator.reverseOrder()); + return "lists/sort"; + } + + private List getColors() { + List colors = new ArrayList<>(); + colors.add("green"); + colors.add("yellow"); + colors.add("red"); + colors.add("blue"); + return colors; + } + + private List getOtherColors() { + List colors = new ArrayList<>(); + colors.add("green"); + colors.add("blue"); + return colors; + } +} diff --git a/spring-thymeleaf-2/src/main/resources/templates/lists/contains.html b/spring-thymeleaf-2/src/main/resources/templates/lists/contains.html new file mode 100644 index 0000000000..bfa7874010 --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/lists/contains.html @@ -0,0 +1,12 @@ + + + + + Lists Utility Class in Thymeleaf + + +myList contains red: + +myList contains red and green: + + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/main/resources/templates/lists/isEmpty.html b/spring-thymeleaf-2/src/main/resources/templates/lists/isEmpty.html new file mode 100644 index 0000000000..06c66153c7 --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/lists/isEmpty.html @@ -0,0 +1,14 @@ + + + + + Lists Utility Class in Thymeleaf + + + +isEmpty Check : + +List is not empty + + + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/main/resources/templates/lists/size.html b/spring-thymeleaf-2/src/main/resources/templates/lists/size.html new file mode 100644 index 0000000000..594ff08467 --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/lists/size.html @@ -0,0 +1,12 @@ + + + + + Lists Utility Class in Thymeleaf + + + +size: + + + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/main/resources/templates/lists/sort.html b/spring-thymeleaf-2/src/main/resources/templates/lists/sort.html new file mode 100644 index 0000000000..e23c7bccbb --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/lists/sort.html @@ -0,0 +1,12 @@ + + + + + Lists Utility Class in Thymeleaf + + +sort: + +sort with Comparator: + + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/main/resources/templates/lists/toList.html b/spring-thymeleaf-2/src/main/resources/templates/lists/toList.html new file mode 100644 index 0000000000..680c13160e --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/lists/toList.html @@ -0,0 +1,14 @@ + + + + + Lists Utility Class in Thymeleaf + + + + + converted list size: + + + + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/test/java/com/baeldung/thymeleaf/lists/ListsControllerIntegrationTest.java b/spring-thymeleaf-2/src/test/java/com/baeldung/thymeleaf/lists/ListsControllerIntegrationTest.java new file mode 100644 index 0000000000..252ec3bef3 --- /dev/null +++ b/spring-thymeleaf-2/src/test/java/com/baeldung/thymeleaf/lists/ListsControllerIntegrationTest.java @@ -0,0 +1,60 @@ +package com.baeldung.thymeleaf.lists; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +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.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureMockMvc(printOnlyOnFailure = false) +public class ListsControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void whenCalledToList_ThenConvertsToList() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/lists/toList")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("converted list size: 4"))); + } + + @Test + public void whenCalledContains_ThenChecksMembership() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/lists/contains")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("myList contains red: true"))) + .andExpect(content().string(containsString("myList contains red and green: true"))); + } + + @Test + public void whenCalledSize_ThenReturnsSize() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/lists/size")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("size: 4"))); + } + + @Test + public void whenCalledSort_ThenSortsItems() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/lists/sort")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("sort: [blue, green, red, yellow]"))) + .andExpect(content().string(containsString("sort with Comparator: [yellow, red, green, blue]"))); + } + + @Test + public void whenCalledIsEmpty_ThenChecksAnyMembers() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/lists/isEmpty")) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("isEmpty Check : false"))); + } +} \ No newline at end of file